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 CheckCommand string `json:"check_command"`
14 DisplayName string `json:"display_name"`
15 }
17 type HostGroup struct {
18 Name string `json:"name"`
19 DisplayName string `json:"display_name"`
20 }
22 type HostState int
24 const (
25 HostUp HostState = 0 + iota
26 HostDown
27 HostUnreachable
28 )
30 func (s HostState) String() string {
31 switch s {
32 case HostUp:
33 return "HostUp"
34 case HostDown:
35 return "HostDown"
36 case HostUnreachable:
37 return "HostUnreachable"
38 }
39 return "unhandled host state"
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 func (h Host) MarshalJSON() ([]byte, error) {
59 type Attrs struct {
60 Address string `json:"address"`
61 CheckCommand string `json:"check_command"`
62 DisplayName string `json:"display_name"`
63 }
64 type host struct {
65 Attrs Attrs `json:"attrs"`
66 }
67 jhost := &host{
68 Attrs: Attrs{
69 Address: h.Address,
70 CheckCommand: h.CheckCommand,
71 DisplayName: h.DisplayName,
72 },
73 }
74 return json.Marshal(jhost)
75 }
77 func (hg HostGroup) MarshalJSON() ([]byte, error) {
78 type attrs struct {
79 DisplayName string `json:"display_name"`
80 }
81 type group struct {
82 Attrs attrs `json:"attrs"`
83 }
84 return json.Marshal(&group{
85 Attrs: attrs{
86 DisplayName: hg.DisplayName,
87 },
88 })
89 }