forked from AlekSi/zabbix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
host.go
146 lines (124 loc) · 3.6 KB
/
host.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package zabbix
import (
"github.com/AlekSi/reflector"
)
type (
AvailableType int
StatusType int
)
const (
Available AvailableType = 1
Unavailable AvailableType = 2
Monitored StatusType = 0
Unmonitored StatusType = 1
)
// https://www.zabbix.com/documentation/2.0/manual/appendix/api/host/definitions
type Host struct {
HostId string `json:"hostid,omitempty"`
Host string `json:"host"`
Available AvailableType `json:"available"`
Error string `json:"error"`
Name string `json:"name"`
Status StatusType `json:"status"`
// Fields below used only when creating hosts
GroupIds HostGroupIds `json:"groups,omitempty"`
Interfaces HostInterfaces `json:"interfaces,omitempty"`
}
type Hosts []Host
// Wrapper for host.get: https://www.zabbix.com/documentation/2.0/manual/appendix/api/host/get
func (api *API) HostsGet(params Params) (res Hosts, err error) {
if _, present := params["output"]; !present {
params["output"] = "extend"
}
response, err := api.CallWithError("host.get", params)
if err != nil {
return
}
reflector.MapsToStructs2(response.Result.([]interface{}), &res, reflector.Strconv, "json")
return
}
// Gets hosts by host group Ids.
func (api *API) HostsGetByHostGroupIds(ids []string) (res Hosts, err error) {
return api.HostsGet(Params{"groupids": ids})
}
// Gets hosts by host groups.
func (api *API) HostsGetByHostGroups(hostGroups HostGroups) (res Hosts, err error) {
ids := make([]string, len(hostGroups))
for i, id := range hostGroups {
ids[i] = id.GroupId
}
return api.HostsGetByHostGroupIds(ids)
}
// Gets host by Id only if there is exactly 1 matching host.
func (api *API) HostGetById(id string) (res *Host, err error) {
hosts, err := api.HostsGet(Params{"hostids": id})
if err != nil {
return
}
if len(hosts) == 1 {
res = &hosts[0]
} else {
e := ExpectedOneResult(len(hosts))
err = &e
}
return
}
// Gets host by Host only if there is exactly 1 matching host.
func (api *API) HostGetByHost(host string) (res *Host, err error) {
hosts, err := api.HostsGet(Params{"filter": map[string]string{"host": host}})
if err != nil {
return
}
if len(hosts) == 1 {
res = &hosts[0]
} else {
e := ExpectedOneResult(len(hosts))
err = &e
}
return
}
// Wrapper for host.create: https://www.zabbix.com/documentation/2.0/manual/appendix/api/host/create
func (api *API) HostsCreate(hosts Hosts) (err error) {
response, err := api.CallWithError("host.create", hosts)
if err != nil {
return
}
result := response.Result.(map[string]interface{})
hostids := result["hostids"].([]interface{})
for i, id := range hostids {
hosts[i].HostId = id.(string)
}
return
}
// Wrapper for host.delete: https://www.zabbix.com/documentation/2.0/manual/appendix/api/host/delete
// Cleans HostId in all hosts elements if call succeed.
func (api *API) HostsDelete(hosts Hosts) (err error) {
ids := make([]string, len(hosts))
for i, host := range hosts {
ids[i] = host.HostId
}
err = api.HostsDeleteByIds(ids)
if err == nil {
for i := range hosts {
hosts[i].HostId = ""
}
}
return
}
// Wrapper for host.delete: https://www.zabbxix.com/documentation/2.0/manual/appendix/api/host/delete
func (api *API) HostsDeleteByIds(ids []string) (err error) {
hostIds := make([]map[string]string, len(ids))
for i, id := range ids {
hostIds[i] = map[string]string{"hostid": id}
}
response, err := api.CallWithError("host.delete", hostIds)
if err != nil {
return
}
result := response.Result.(map[string]interface{})
hostids := result["hostids"].([]interface{})
if len(ids) != len(hostids) {
err = &ExpectedMore{len(ids), len(hostids)}
}
return
}