commit cb667a50cb18cc38d49e520642743de727bed3ca
parent a17d3f9dc6856d0cb60bf176e66844dc2752782d
Author: Oliver Lowe <o@olowe.co>
Date: Sat, 12 Feb 2022 11:11:20 +1100
Store last Host check result and time
Includes tests and simplified unmarshal test.
Diffstat:
4 files changed, 20 insertions(+), 19 deletions(-)
diff --git a/host.go b/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
}
diff --git a/host_test.go b/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)
}
}
diff --git a/object.go b/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
diff --git a/testdata/objects/hosts/VuS9jZ8u.example.org b/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": "",