icinga

Icinga2 API client in Go
git clone https://git.olowe.co/icinga
Log | Files | Refs | README | LICENSE

commit f21c51c745099616c3451e39569df1b46feae6af
parent fb8a219e2a3363c6ac9f5e819b45cca7ca4a1086
Author: Oliver Lowe <o@olowe.co>
Date:   Wed,  2 Nov 2022 09:21:56 +1100

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.

Diffstat:
Mhost.go | 51+++++++++++++++++++++++++++++++++++++--------------
Mservice.go | 29+++++++++++++++++++++++++----
2 files changed, 62 insertions(+), 18 deletions(-)

diff --git a/host.go b/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,17 +65,39 @@ 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 diff --git a/service.go b/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,8 +69,27 @@ 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