commit 8f08828e8134e1160b4be74544d3b6020059366e from: Oliver Lowe date: Fri Feb 11 04:39:22 2022 UTC store a service's last check result commit - f4705a64149eb5336dd9f69e1a32ebf7d886a132 commit + 8f08828e8134e1160b4be74544d3b6020059366e blob - 06a63ce3ee17549ea8e373106a3ad58600e9e86f blob + 27ad6d6ed237df31a19942fa9698a7817eb54162 --- service.go +++ service.go @@ -1,6 +1,10 @@ package icinga -import "encoding/json" +import ( + "encoding/json" + "strings" + "time" +) func (s Service) name() string { return s.Name @@ -18,12 +22,15 @@ type Service struct { 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"` } type CheckResult struct { - Output string + CheckSource string `json:"check_source"` + Command interface{} + Output string } type ServiceState int @@ -52,6 +59,7 @@ func (s *Service) UnmarshalJSON(data []byte) error { type alias Service aux := &struct { Acknowledgement int + LastCheck float64 `json:"last_check"` *alias }{ alias: (*alias)(s), @@ -62,5 +70,26 @@ func (s *Service) UnmarshalJSON(data []byte) error { if aux.Acknowledgement != 0 { s.Acknowledgement = true } + s.LastCheck = time.Unix(int64(aux.LastCheck), 0) return nil } + +func (s Service) Host() string { + return strings.SplitN(s.Name, "!", 2)[0] +} + +func (cr CheckResult) RawCommand() string { + switch v := cr.Command.(type) { + case string: + return v + case []interface{}: + var cmd []string + for i := range v { + if arg, ok := v[i].(string); ok { + cmd = append(cmd, arg) + } + } + return strings.Join(cmd, " ") + } + return "no command" +}