Blob


1 package icinga
3 import (
4 "encoding/json"
5 "fmt"
6 )
8 // Host represents a Host object.
9 type Host struct {
10 Name string `json:"name"`
11 Address string `json:"address"`
12 Address6 string `json:"address6"`
13 Groups []string `json:"groups"`
14 State HostState `json:"state"`
15 CheckCommand string `json:"check_command"`
16 DisplayName string `json:"display_name"`
17 }
19 type HostState int
21 const (
22 HostUp HostState = 0 + iota
23 HostDown
24 HostUnreachable
25 )
27 func (s HostState) String() string {
28 switch s {
29 case HostUp:
30 return "HostUp"
31 case HostDown:
32 return "HostDown"
33 case HostUnreachable:
34 return "HostUnreachable"
35 }
36 return "unhandled host state"
37 }
39 func (h Host) name() string {
40 return h.Name
41 }
43 func (h Host) path() string {
44 return "/objects/hosts/" + h.Name
45 }
47 func (h Host) attrs() map[string]interface{} {
48 m := make(map[string]interface{})
49 m["display_name"] = h.DisplayName
50 return m
51 }
53 func (h Host) MarshalJSON() ([]byte, error) {
54 type Attrs struct {
55 Address string `json:"address"`
56 CheckCommand string `json:"check_command"`
57 DisplayName string `json:"display_name"`
58 }
59 type host struct {
60 Attrs Attrs `json:"attrs"`
61 }
62 jhost := &host{
63 Attrs: Attrs{
64 Address: h.Address,
65 CheckCommand: h.CheckCommand,
66 DisplayName: h.DisplayName,
67 },
68 }
69 return json.Marshal(jhost)
70 }
72 // Hosts returns all Hosts in the Icinga2 configuration.
73 func (c *Client) Hosts() ([]Host, error) {
74 objects, err := c.filterObjects("/objects/hosts", "")
75 if err != nil {
76 return nil, fmt.Errorf("get all hosts: %w", err)
77 }
78 var hosts []Host
79 for _, o := range objects {
80 v, ok := o.(Host)
81 if !ok {
82 return nil, fmt.Errorf("get all hosts: %T in response", v)
83 }
84 hosts = append(hosts, v)
85 }
86 return hosts, nil
87 }
89 // FilterHosts returns any matching hosts after applying the filter
90 // expression expr. If no hosts match, an empty slice and an error wrapping
91 // ErrNoMatch is returned.
92 func (c *Client) FilterHosts(expr string) ([]Host, error) {
93 objects, err := c.filterObjects("/objects/hosts", expr)
94 if err != nil {
95 return nil, fmt.Errorf("filter hosts %q: %w", expr, err)
96 }
97 var hosts []Host
98 for _, o := range objects {
99 v, ok := o.(Host)
100 if !ok {
101 return nil, fmt.Errorf("filter hosts %q: %T in response", expr, v)
103 hosts = append(hosts, v)
105 return hosts, nil
108 // LookupHost returns the Host identified by name. If no Host is found,
109 // error wraps ErrNotExist.
110 func (c *Client) LookupHost(name string) (Host, error) {
111 obj, err := c.lookupObject("/objects/hosts/" + name)
112 if err != nil {
113 return Host{}, fmt.Errorf("lookup %s: %w", name, err)
115 v, ok := obj.(Host)
116 if !ok {
117 return Host{}, fmt.Errorf("lookup %s: result type %T is not host", name, obj)
119 return v, nil
122 // CreateHost creates the Host host.
123 // The Name and CheckCommand fields of host must be non-zero.
124 func (c *Client) CreateHost(host Host) error {
125 if err := c.createObject(host); err != nil {
126 return fmt.Errorf("create host %s: %w", host.Name, err)
128 return nil
131 // DeleteHost deletes the Host identified by name.
132 // If no Host is found, error wraps ErrNotExist.
133 func (c *Client) DeleteHost(name string) error {
134 if err := c.deleteObject("/objects/hosts/" + name); err != nil {
135 return fmt.Errorf("delete host %s: %w", name, err)
137 return nil