commit f21c51c745099616c3451e39569df1b46feae6af from: Oliver Lowe date: Tue Nov 01 23:49:15 2022 UTC Support icinga 2.12 acknowledgement, state, statetype field On Debian 11 these fields are floats, not ints (or boolean) for some reason. This patch allows unmarshalling either floats or integers depending on what the icinga API sends us. commit - fb8a219e2a3363c6ac9f5e819b45cca7ca4a1086 commit + f21c51c745099616c3451e39569df1b46feae6af blob - 9f383893d447961fbb9659a79fa71f6a4086fd04 blob + ce54c7f99a16173ac5b64adfbca9c1329e5c5a4b --- host.go +++ host.go @@ -2,23 +2,24 @@ package icinga import ( "encoding/json" + "fmt" "time" ) // Host represents a Host object. To create a Host, the Name and CheckCommand // fields must be set. type Host struct { - Name string `json:"-"` - Address string `json:"address"` - Address6 string `json:"address6"` - Groups []string `json:"groups,omitempty"` - State HostState `json:"state,omitempty"` - StateType StateType `json:"state_type,omitempty"` - CheckCommand string `json:"check_command"` - DisplayName string `json:"display_name,omitempty"` - LastCheck time.Time `json:",omitempty"` + Name string `json:"-"` + Address string `json:"address"` + Address6 string `json:"address6"` + Groups []string `json:"groups,omitempty"` + State HostState `json:"state,omitempty"` + StateType StateType `json:"state_type,omitempty"` + CheckCommand string `json:"check_command"` + DisplayName string `json:"display_name,omitempty"` + LastCheck time.Time `json:",omitempty"` LastCheckResult CheckResult `json:"last_check_result,omitempty"` - Acknowledgement bool `json:",omitempty"` + Acknowledgement bool `json:",omitempty"` } type HostGroup struct { @@ -64,18 +65,40 @@ func (hg HostGroup) path() string { func (h *Host) UnmarshalJSON(data []byte) error { type alias Host aux := &struct { - Acknowledgement int - LastCheck float64 `json:"last_check"` + Acknowledgement interface{} `json:"acknowledgement"` + State interface{} `json:"state"` + StateType interface{} `json:"state_type"` + LastCheck float64 `json:"last_check"` *alias }{ alias: (*alias)(h), } if err := json.Unmarshal(data, &aux); err != nil { + fmt.Println("uh oh!") return err } - if aux.Acknowledgement != 0 { - h.Acknowledgement = true + switch v := aux.Acknowledgement.(type) { + case int: + if v != 0 { + h.Acknowledgement = true + } + case float64: + if int(v) != 0 { + h.Acknowledgement = true + } } + switch v := aux.State.(type) { + case int: + h.State = HostState(v) + case float64: + h.State = HostState(v) + } + switch v := aux.StateType.(type) { + case int: + h.StateType = StateType(v) + case float64: + h.StateType = StateType(v) + } h.LastCheck = time.Unix(int64(aux.LastCheck), 0) return nil } blob - 2efeac2254523381f0045edf0dbe2e1c796cc489 blob + 030bf88aed340fe9a3ce219c7e1af405f844326e --- service.go +++ service.go @@ -58,8 +58,10 @@ func (state ServiceState) String() string { func (s *Service) UnmarshalJSON(data []byte) error { type alias Service aux := &struct { - Acknowledgement int - LastCheck float64 `json:"last_check"` + Acknowledgement interface{} `json:"acknowledgement"` + State interface{} `json:"state"` + StateType interface{} `json:"state_type"` + LastCheck float64 `json:"last_check"` *alias }{ alias: (*alias)(s), @@ -67,9 +69,28 @@ func (s *Service) UnmarshalJSON(data []byte) error { if err := json.Unmarshal(data, &aux); err != nil { return err } - if aux.Acknowledgement != 0 { - s.Acknowledgement = true + switch v := aux.Acknowledgement.(type) { + case int: + if v != 0 { + s.Acknowledgement = true + } + case float64: + if int(v) != 0 { + s.Acknowledgement = true + } } + switch v := aux.State.(type) { + case int: + s.State = ServiceState(v) + case float64: + s.State = ServiceState(v) + } + switch v := aux.StateType.(type) { + case int: + s.StateType = StateType(v) + case float64: + s.StateType = StateType(v) + } s.LastCheck = time.Unix(int64(aux.LastCheck), 0) return nil }