Blob


1 package icinga
3 import "encoding/json"
5 // Host represents a Host object. To create a Host, the Name and CheckCommand
6 // fields must be set.
7 type Host struct {
8 Name string `json:"-"`
9 Address string `json:"address"`
10 Address6 string `json:"address6"`
11 Groups []string `json:"groups,omitempty"`
12 State HostState `json:"state,omitempty"`
13 StateType StateType `json:"state_type,omitempty"`
14 CheckCommand string `json:"check_command"`
15 DisplayName string `json:"display_name,omitempty"`
16 Acknowledgement bool `json:",omitempty"`
17 }
19 type HostGroup struct {
20 Name string `json:"-"`
21 DisplayName string `json:"display_name"`
22 }
24 type HostState int
26 const (
27 HostUp HostState = 0 + iota
28 HostDown
29 HostUnreachable
30 )
32 func (state HostState) String() string {
33 switch state {
34 case HostUp:
35 return "HostUp"
36 case HostDown:
37 return "HostDown"
38 }
39 return "HostUnreachable"
40 }
42 func (h Host) name() string {
43 return h.Name
44 }
46 func (h Host) path() string {
47 return "/objects/hosts/" + h.Name
48 }
50 func (hg HostGroup) name() string {
51 return hg.Name
52 }
54 func (hg HostGroup) path() string {
55 return "/objects/hostgroups/" + hg.Name
56 }
58 // UnmarhsalJSON unmarshals host attributes into more meaningful Host field types.
59 func (h *Host) UnmarshalJSON(data []byte) error {
60 type alias Host
61 aux := &struct {
62 Acknowledgement int
63 *alias
64 }{
65 alias: (*alias)(h),
66 }
67 if err := json.Unmarshal(data, &aux); err != nil {
68 return err
69 }
70 if aux.Acknowledgement != 0 {
71 h.Acknowledgement = true
72 }
73 return nil
74 }