forked from AlekSi/zabbix
-
Notifications
You must be signed in to change notification settings - Fork 4
/
host.go
152 lines (130 loc) · 4 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
package zabbix
import (
"github.com/AlekSi/reflector"
)
// https://www.zabbix.com/documentation/2.2/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"`
TlsConnect int `json:"tls_connect"`
TlsAccept int `json:"tls_accept"`
TlsPskIdentity string `json:"tls_psk_identity"`
TlsPsk string `json:"tls_psk"`
ProxyHostId string `json:"proxy_hostid"`
// Fields below used only when creating hosts
GroupIds HostGroupIds `json:"groups,omitempty"`
Interfaces HostInterfaces `json:"interfaces,omitempty"`
TemplateIds TemplateIds `json:"templates,omitempty"`
}
type Hosts []Host
// Wrapper for host.get: https://www.zabbix.com/documentation/2.2/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.2/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
}
func (api *API) HostsUpdate(hosts Hosts) (err error) {
_, err = api.CallWithError("host.update", hosts)
return
}
// Wrapper for host.delete: https://www.zabbix.com/documentation/2.2/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.zabbix.com/documentation/2.2/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 {
// Zabbix 2.4 uses new syntax only
if e, ok := err.(*Error); ok && e.Code == ZbxApiErrorInternal {
response, err = api.CallWithError("host.delete", ids)
}
}
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
}