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 type Attrs struct {
61 Address string `json:"address"`
62 CheckCommand string `json:"check_command"`
63 DisplayName string `json:"display_name"`
64 }
65 type host struct {
66 Attrs Attrs `json:"attrs"`
67 }
68 jhost := &host{
69 Attrs: Attrs{
70 Address: h.Address,
71 CheckCommand: h.CheckCommand,
72 DisplayName: h.DisplayName,
73 },
74 }
75 return json.Marshal(jhost)
76 }
78 func (hg HostGroup) MarshalJSON() ([]byte, error) {
79 type attrs struct {
80 DisplayName string `json:"display_name"`
81 }
82 type group struct {
83 Attrs attrs `json:"attrs"`
84 }
85 return json.Marshal(&group{
86 Attrs: attrs{
87 DisplayName: hg.DisplayName,
88 },
89 })
90 }