commit cb667a50cb18cc38d49e520642743de727bed3ca from: Oliver Lowe date: Sat Feb 12 00:11:20 2022 UTC Store last Host check result and time Includes tests and simplified unmarshal test. commit - a17d3f9dc6856d0cb60bf176e66844dc2752782d commit + cb667a50cb18cc38d49e520642743de727bed3ca blob - acd4390c9d48fa0fd5d47a465bf3e067da19816c blob + 9f383893d447961fbb9659a79fa71f6a4086fd04 --- host.go +++ host.go @@ -1,6 +1,9 @@ package icinga -import "encoding/json" +import ( + "encoding/json" + "time" +) // Host represents a Host object. To create a Host, the Name and CheckCommand // fields must be set. @@ -13,6 +16,8 @@ type Host 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"` } @@ -60,6 +65,7 @@ func (h *Host) UnmarshalJSON(data []byte) error { type alias Host aux := &struct { Acknowledgement int + LastCheck float64 `json:"last_check"` *alias }{ alias: (*alias)(h), @@ -70,5 +76,6 @@ func (h *Host) UnmarshalJSON(data []byte) error { if aux.Acknowledgement != 0 { h.Acknowledgement = true } + h.LastCheck = time.Unix(int64(aux.LastCheck), 0) return nil } blob - 18943250b3644b6953b6f9f2ba10819721dcc176 blob + 5dbbc2be788f77314110f3e09e341be0a15b6d25 --- host_test.go +++ host_test.go @@ -2,21 +2,10 @@ package icinga import ( "os" - "reflect" "testing" ) func TestHostUnmarshal(t *testing.T) { - want := Host{ - Name: "VuS9jZ8u.example.org", - Address: "", - Groups: []string{}, - State: HostDown, - StateType: StateSoft, - CheckCommand: "hostalive", - DisplayName: "VuS9jZ8u.example.org", - Acknowledgement: false, - } f, err := os.Open("testdata/objects/hosts/VuS9jZ8u.example.org") if err != nil { t.Fatal(err) @@ -26,8 +15,14 @@ func TestHostUnmarshal(t *testing.T) { if err != nil { t.Fatal(err) } - got := resp.Results[0].(Host) - if !reflect.DeepEqual(want, got) { - t.Errorf("want %+v, got %+v", want, got) + host := resp.Results[0].(Host) + if host.LastCheck.IsZero() { + t.Error("zero time") } + if !host.Acknowledgement { + t.Error("should be acknowledged") + } + if t.Failed() { + t.Log(host) + } } blob - 8489d3be87abb80b5dfdf4791a962e05133740dd blob + 0085a777636dfc7ca7c1d96ba594c780da81f234 --- object.go +++ object.go @@ -27,9 +27,8 @@ func jsonForCreate(obj object) ([]byte, error) { m["attrs"] = v case Host: aux := &struct { - // fields not added to Host yet - // LastCheck struct{} - // LastCheckResult struct{} + LastCheck *struct{} `json:",omitempty"` + LastCheckResult *struct{} `json:"last_check_result,omitempty"` Host }{Host: v} m["attrs"] = aux blob - 7f7cfd4e0aebe84b39cce6a4b2852c1f6db1e453 blob + 04f7c04817e692311cd86d05ada51b03a41412a8 --- testdata/objects/hosts/VuS9jZ8u.example.org +++ testdata/objects/hosts/VuS9jZ8u.example.org @@ -3,7 +3,7 @@ { "attrs": { "__name": "VuS9jZ8u.example.org", - "acknowledgement": 0, + "acknowledgement": 1, "acknowledgement_expiry": 0, "acknowledgement_last_change": 0, "action_url": "",