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:"name"`
9 Address string `json:"address"`
10 Address6 string `json:"address6"`
11 Groups []string `json:"groups"`
12 State HostState `json:"state"`
13 StateType StateType `json:"state_type"`
14 CheckCommand string `json:"check_command"`
15 DisplayName string `json:"display_name"`
16 }
18 type HostGroup struct {
19 Name string `json:"name"`
20 DisplayName string `json:"display_name"`
21 }
23 type HostState int
25 const (
26 HostUp HostState = 0 + iota
27 HostDown
28 HostUnreachable
29 )
31 func (s HostState) String() string {
32 switch s {
33 case HostUp:
34 return "HostUp"
35 case HostDown:
36 return "HostDown"
37 case HostUnreachable:
38 return "HostUnreachable"
39 }
40 return "unhandled host state"
41 }
43 func (h Host) name() string {
44 return h.Name
45 }
47 func (h Host) path() string {
48 return "/objects/hosts/" + h.Name
49 }
51 func (hg HostGroup) name() string {
52 return hg.Name
53 }
55 func (hg HostGroup) path() string {
56 return "/objects/hostgroups/" + hg.Name
57 }
59 func (h Host) MarshalJSON() ([]byte, error) {
60 attrs := make(map[string]interface{})
61 attrs["address"] = h.Address
62 attrs["address6"] = h.Address6
63 if len(h.Groups) > 0 {
64 attrs["groups"] = h.Groups
65 }
66 attrs["check_command"] = h.CheckCommand
67 attrs["display_name"] = h.DisplayName
68 m := make(map[string]interface{})
69 m["attrs"] = attrs
70 return json.Marshal(m)
71 }
73 func (hg HostGroup) MarshalJSON() ([]byte, error) {
74 type attrs struct {
75 DisplayName string `json:"display_name"`
76 }
77 type group struct {
78 Attrs attrs `json:"attrs"`
79 }
80 return json.Marshal(&group{
81 Attrs: attrs{
82 DisplayName: hg.DisplayName,
83 },
84 })
85 }