forked from neogan74/go-zabbix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhost.go
305 lines (236 loc) · 9.35 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
package zabbix
import (
"fmt"
)
const (
// HostSourceDefault indicates that a Host was created in the normal way.
HostSourceDefault = 0
// HostSourceDiscovery indicates that a Host was created by Host discovery.
HostSourceDiscovery = 4
)
const (
// HostStatusMonitored represents the numberic state of the enabled host
HostStatusMonitored = "0"
// HostStatusUnmonitored represents the numberic state of the disabled host
HostStatusUnmonitored = "1"
)
const (
// HostEncryptionDisabled represents the host flag when encryption disabled
// for the host
HostEncryptionDisabled = 1
// HostEncryptionPSK represents the state when PSK encryption is used
// for the host
HostEncryptionPSK = 2
// HostEncryptionCert represents the sate when cert encryption is used
// for the host
HostEncryptionCert = 4
)
// Host represents a Zabbix Host returned from the Zabbix API.
//
// See: https://www.zabbix.com/documentation/2.2/manual/config/hosts
type Host struct {
// HostID is the unique ID of the Host.
HostID string `json:"hostid,omitempty"`
// Hostname is the technical name of the Host.
Hostname string `json:"host,omitempty"`
// DisplayName is the visible name of the Host.
DisplayName string `json:"name,omitempty"`
// Source is the origin of the Host and must be one of the HostSource
// constants.
Source int `json:"flags,string,omitempty"`
// Macros contains all Host Macros assigned to the Host.
Macros HostMacros `json:"macros,omitempty"`
// Groups contains all Host Groups assigned to the Host.
Groups Hostgroups `json:"groups,omitempty"`
// Description of the host.
Description string `json:"description,omitempty"`
// Status and function of the host.
//
// Status must be one of the HostStatus constants.
Status string `json:"status,omitempty"`
// ProxyID is ID of the proxy that is used to monitor the host.
ProxyID string `json:"proxy_hostid,omitempty"`
// Connections to host.
//
// TLSConnect must be one of the HostEncryption constants.
TLSConnect int `json:"tls_connect,string,omitempty"`
// Connections from host.
//
// TLSAccept must be one of the HostEncryption constants.
TLSAccept int `json:"tls_accept,string,omitempty"`
// Certificate issuer.
TLSIssuer string `json:"tls_issuer,omitempty"`
// Certificate subject.
TLSSubject string `json:"tls_subject,omitempty"`
// PSK identity. Required if either TLSConnect or TLSAccept has PSK enabled.
TLSPSKIdentity string `json:"tls_psk_identity,omitempty"`
// The preshared key. Required if either TLSConnect or TLSAccept has PSK enabled.
TLSPSK string `json:"tls_psk,omitempty"`
}
// Hosts represents the set of the hosts returned from Zabbix API
type Hosts []Host
// HostGetParams represent the parameters for a `host.get` API call.
//
// See: https://www.zabbix.com/documentation/2.2/manual/api/reference/host/get#parameters
type HostGetParams struct {
GetParameters
// GroupIDs filters search results to hosts that are members of the given
// Group IDs.
GroupIDs []string `json:"groupids,omitempty"`
// ApplicationIDs filters search results to hosts that have items in the
// given Application IDs.
ApplicationIDs []string `json:"applicationids,omitempty"`
// DiscoveredServiceIDs filters search results to hosts that are related to
// the given discovered service IDs.
DiscoveredServiceIDs []string `json:"dserviceids,omitempty"`
// GraphIDs filters search results to hosts that have the given graph IDs.
GraphIDs []string `json:"graphids,omitempty"`
// HostIDs filters search results to hosts that matched the given Host IDs.
HostIDs []string `json:"hostids,omitempty"`
// WebCheckIDs filters search results to hosts with the given Web Check IDs.
WebCheckIDs []string `json:"httptestids,omitempty"`
// InterfaceIDs filters search results to hosts that use the given Interface
// IDs.
InterfaceIDs []string `json:"interfaceids,omitempty"`
// ItemIDs filters search results to hosts with the given Item IDs.
ItemIDs []string `json:"itemids,omitempty"`
// MaintenanceIDs filters search results to hosts that are affected by the
// given Maintenance IDs
MaintenanceIDs []string `json:"maintenanceids,omitempty"`
// MonitoredOnly filters search results to return only monitored hosts.
MonitoredOnly bool `json:"monitored_hosts,omitempty"`
// ProxyOnly filters search results to hosts which are Zabbix proxies.
ProxiesOnly bool `json:"proxy_host,omitempty"`
// ProxyIDs filters search results to hosts monitored by the given Proxy
// IDs.
ProxyIDs []string `json:"proxyids,omitempty"`
// IncludeTemplates extends search results to include Templates.
IncludeTemplates bool `json:"templated_hosts,omitempty"`
// SelectGroups causes the Host Groups that each Host belongs to to be
// attached in the search results.
SelectGroups SelectQuery `json:"selectGroups,omitempty"`
// SelectApplications causes the Applications from each Host to be attached
// in the search results.
SelectApplications SelectQuery `json:"selectApplications,omitempty"`
// SelectDiscoveries causes the Low-Level Discoveries from each Host to be
// attached in the search results.
SelectDiscoveries SelectQuery `json:"selectDiscoveries,omitempty"`
// SelectDiscoveryRule causes the Low-Level Discovery Rule that created each
// Host to be attached in the search results.
SelectDiscoveryRule SelectQuery `json:"selectDiscoveryRule,omitempty"`
// SelectGraphs causes the Graphs from each Host to be attached in the
// search results.
SelectGraphs SelectQuery `json:"selectGraphs,omitempty"`
SelectHostDiscovery SelectQuery `json:"selectHostDiscovery,omitempty"`
SelectWebScenarios SelectQuery `json:"selectHttpTests,omitempty"`
SelectInterfaces SelectQuery `json:"selectInterfaces,omitempty"`
SelectInventory SelectQuery `json:"selectInventory,omitempty"`
SelectItems SelectQuery `json:"selectItems,omitempty"`
SelectMacros SelectQuery `json:"selectMacros,omitempty"`
SelectParentTemplates SelectQuery `json:"selectParentTemplates,omitempty"`
SelectScreens SelectQuery `json:"selectScreens,omitempty"`
SelectTriggers SelectQuery `json:"selectTriggers,omitempty"`
}
// HostCreateParams struct represent the Zabbix paramaeters required for
// creaing the hosts
type HostCreateParams struct {
GetParameters
Hosts string `json:"host"`
Hostgroups []CreateHostgroup `json:"groups"`
Interfaces CreateHostInterface `json:"interfaces"`
Templates Templates `json:"templates,omitempty"`
// Inventory Inventory `json:"inventory,omitempty"`
}
type CreateHostInterface struct {
Type int `json:"type"`
Main int `json:"main"`
Useip int `json:"useip"`
IP string `json:"ip"`
DNS string `json:"dns"`
Port string `json:"port"`
}
type CreateHostgroup struct {
GroupID string `json:"groupid,omitempty"`
}
// HostUpdateParams struct represents the Zabbix basic parameters for
// updating the host by Zabbix API
type HostUpdateParams struct {
GetParameters
Interfaces HostInterfaces `json:"interfaces,omitempty"`
Templates Templates `json:"templates,omitempty"`
UnlinkTemplates Templates `json:"templates,omitempty"`
// Inventory Inventory `json:"inventory,omitempty"`
}
// HostResponse represent host action response body
type HostResponse struct {
HostIDs []string `json:"hostids"`
}
// GetHosts queries the Zabbix API for Hosts matching the given search
// parameters.
//
// ErrNotFound is returned if the search result set is empty.
// An error is returned if a transport, parsing or API error occurs.
//
// https://www.zabbix.com/documentation/3.4/manual/api/reference/host/get
func (c *Session) GetHosts(params HostGetParams) ([]Host, error) {
hosts := make([]jHost, 0)
err := c.Get("host.get", params, &hosts)
if err != nil {
return nil, err
}
if len(hosts) == 0 {
return nil, ErrNotFound
}
// map JSON Events to Go Events
out := make([]Host, len(hosts))
for i, jhost := range hosts {
host, err := jhost.Host()
if err != nil {
return nil, fmt.Errorf("Error mapping Host %d in response: %v", i, err)
}
out[i] = *host
}
return out, nil
}
// CreateHosts creates a single or multiple new hosts.
// Returns a list of ids of created hosts.
//
// https://www.zabbix.com/documentation/3.4/manual/api/reference/host/create
func (c *Session) CreateHosts(params ...HostCreateParams) (hostIds []string, err error) {
var body HostResponse
if err := c.Get("host.create", params, &body); err != nil {
return nil, err
}
if (body.HostIDs == nil) || (len(body.HostIDs) == 0) {
return nil, ErrNotFound
}
return body.HostIDs, nil
}
// DeleteHosts method allows to delete hosts.
// Returns a list of deleted hosts ids.
//
// https://www.zabbix.com/documentation/3.4/manual/api/reference/host/delete
func (c *Session) DeleteHosts(hostIDs ...string) (hostIds []string, err error) {
var body HostResponse
if err := c.Get("host.delete", hostIDs, &body); err != nil {
return nil, err
}
if (body.HostIDs == nil) || (len(body.HostIDs) == 0) {
return nil, ErrNotFound
}
return body.HostIDs, nil
}
// UpdateHosts method allows to update hosts.
// Returns a list of updated hosts ids.
//
// https://www.zabbix.com/documentation/3.4/manual/api/reference/host/update
func (c *Session) UpdateHosts(params ...HostUpdateParams) (hostIds []string, err error) {
var body HostResponse
if err := c.Get("host.update", params, &body); err != nil {
return nil, err
}
if (body.HostIDs == nil) || (len(body.HostIDs) == 0) {
return nil, ErrNotFound
}
return body.HostIDs, nil
}