forked from claranet/go-zabbix-api
-
Notifications
You must be signed in to change notification settings - Fork 2
/
host.go
173 lines (151 loc) · 4.81 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package zabbix
type (
// AvailableType (readonly) Availability of Zabbix agent
// see "available" in: https://www.zabbix.com/documentation/3.2/manual/api/reference/host/object
AvailableType int
// StatusType Status and function of the host.
// see "status" in: https://www.zabbix.com/documentation/3.2/manual/api/reference/host/object
StatusType int
)
const (
// Unknown (default)
Unknown AvailableType = 0
// Available host is available
Available AvailableType = 1
// Unavailable host is unavailable
Unavailable AvailableType = 2
)
const (
// Monitored monitored host(default)
Monitored StatusType = 0
// Unmonitored unmonitored host
Unmonitored StatusType = 1
)
// Host represent Zabbix host object
// https://www.zabbix.com/documentation/3.2/manual/api/reference/host/object
type Host struct {
HostID string `json:"hostid,omitempty"`
Host string `json:"host"`
Available AvailableType `json:"available,string"`
Error string `json:"error"`
Name string `json:"name"`
Status StatusType `json:"status,string"`
// Fields below used if specified selectInterfaces or selectMacros or selectParentTemplates parameters
Interfaces HostInterfaces `json:"interfaces,omitempty"`
UserMacros Macros `json:"macros,omitempty"`
Templates Templates `json:"parentTemplates,omitempty"`
// Fields below used only when creating hosts
GroupIds HostGroupIDs `json:"groups,omitempty"`
TemplateIDs TemplateIDs `json:"templates,omitempty"`
}
// Hosts is an array of Host
type Hosts []Host
// HostsGet Wrapper for host.get
// https://www.zabbix.com/documentation/3.2/manual/api/reference/host/get
func (api *API) HostsGet(params Params) (res Hosts, err error) {
if _, present := params["output"]; !present {
params["output"] = "extend"
}
err = api.CallWithErrorParse("host.get", params, &res)
return
}
// HostsGetByHostGroupIds Gets hosts by host group Ids.
func (api *API) HostsGetByHostGroupIds(ids []string) (res Hosts, err error) {
return api.HostsGet(Params{"groupids": ids})
}
// HostsGetByHostGroups 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)
}
// HostGetByID 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
}
// HostGetByHost 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
}
// HostsCreate Wrapper for host.create
// https://www.zabbix.com/documentation/3.2/manual/api/reference/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
}
// HostsUpdate Wrapper for host.update
// https://www.zabbix.com/documentation/3.2/manual/api/reference/host/update
func (api *API) HostsUpdate(hosts Hosts) (err error) {
_, err = api.CallWithError("host.update", hosts)
return
}
// HostsDelete Wrapper for host.delete
// Cleans HostId in all hosts elements if call succeed.
// https://www.zabbix.com/documentation/3.2/manual/api/reference/host/delete
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
}
// HostsDeleteByIds Wrapper for host.delete
// https://www.zabbix.com/documentation/3.2/manual/api/reference/host/delete
func (api *API) HostsDeleteByIds(ids []string) (err error) {
response, err := api.CallWithError("host.delete", ids)
if err != nil {
if e, ok := err.(*Error); ok && e.Code == -32500 {
// Zabbix 2.0 and older use old syntax only with hostid
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
}