9 e0c24850 2022-01-14 o // Host represents a Host object. To create a Host, the Name and CheckCommand
10 e0c24850 2022-01-14 o // fields must be set.
11 6e415953 2022-01-06 o type Host struct {
12 f21c51c7 2022-11-01 o Name string `json:"-"`
13 f21c51c7 2022-11-01 o Address string `json:"address"`
14 f21c51c7 2022-11-01 o Address6 string `json:"address6"`
15 f21c51c7 2022-11-01 o Groups []string `json:"groups,omitempty"`
16 f21c51c7 2022-11-01 o State HostState `json:"state,omitempty"`
17 f21c51c7 2022-11-01 o StateType StateType `json:"state_type,omitempty"`
18 f21c51c7 2022-11-01 o CheckCommand string `json:"check_command"`
19 f21c51c7 2022-11-01 o DisplayName string `json:"display_name,omitempty"`
20 f21c51c7 2022-11-01 o LastCheck time.Time `json:",omitempty"`
21 cb667a50 2022-02-12 o LastCheckResult CheckResult `json:"last_check_result,omitempty"`
22 f21c51c7 2022-11-01 o Acknowledgement bool `json:",omitempty"`
23 001c466e 2023-03-11 o Notes string `json:"notes,omitempty"`
24 001c466e 2023-03-11 o NotesURL string `json:"notes_url,omitempty"`
27 e0c24850 2022-01-14 o type HostGroup struct {
28 46ca0f68 2022-02-01 o Name string `json:"-"`
29 e0c24850 2022-01-14 o DisplayName string `json:"display_name"`
32 76669f9b 2022-01-11 o type HostState int
35 76669f9b 2022-01-11 o HostUp HostState = 0 + iota
37 76669f9b 2022-01-11 o HostUnreachable
40 dca9442c 2022-02-02 o func (state HostState) String() string {
43 76669f9b 2022-01-11 o return "HostUp"
45 76669f9b 2022-01-11 o return "HostDown"
47 dca9442c 2022-02-02 o return "HostUnreachable"
50 76669f9b 2022-01-11 o func (h Host) name() string {
54 76669f9b 2022-01-11 o func (h Host) path() string {
55 76669f9b 2022-01-11 o return "/objects/hosts/" + h.Name
58 e0c24850 2022-01-14 o func (hg HostGroup) name() string {
62 e0c24850 2022-01-14 o func (hg HostGroup) path() string {
63 e0c24850 2022-01-14 o return "/objects/hostgroups/" + hg.Name
66 562d5c1e 2022-01-19 o // UnmarhsalJSON unmarshals host attributes into more meaningful Host field types.
67 562d5c1e 2022-01-19 o func (h *Host) UnmarshalJSON(data []byte) error {
68 562d5c1e 2022-01-19 o type alias Host
69 562d5c1e 2022-01-19 o aux := &struct {
70 f21c51c7 2022-11-01 o Acknowledgement interface{} `json:"acknowledgement"`
71 f21c51c7 2022-11-01 o State interface{} `json:"state"`
72 f21c51c7 2022-11-01 o StateType interface{} `json:"state_type"`
73 f21c51c7 2022-11-01 o LastCheck float64 `json:"last_check"`
76 562d5c1e 2022-01-19 o alias: (*alias)(h),
78 562d5c1e 2022-01-19 o if err := json.Unmarshal(data, &aux); err != nil {
79 f21c51c7 2022-11-01 o fmt.Println("uh oh!")
82 f21c51c7 2022-11-01 o switch v := aux.Acknowledgement.(type) {
85 f21c51c7 2022-11-01 o h.Acknowledgement = true
88 f21c51c7 2022-11-01 o if int(v) != 0 {
89 f21c51c7 2022-11-01 o h.Acknowledgement = true
92 f21c51c7 2022-11-01 o switch v := aux.State.(type) {
94 f21c51c7 2022-11-01 o h.State = HostState(v)
96 f21c51c7 2022-11-01 o h.State = HostState(v)
98 f21c51c7 2022-11-01 o switch v := aux.StateType.(type) {
100 f21c51c7 2022-11-01 o h.StateType = StateType(v)
102 f21c51c7 2022-11-01 o h.StateType = StateType(v)
104 cb667a50 2022-02-12 o h.LastCheck = time.Unix(int64(aux.LastCheck), 0)