forked from rday/zabbix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zabbix.go
562 lines (479 loc) · 17.4 KB
/
zabbix.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
package zabbix
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"reflect"
"strconv"
"strings"
)
/**
Zabbix and Go's RPC implementations don't play with each other.. at all.
So I've re-created the wheel at bit.
*/
type JsonRPCResponse struct {
Jsonrpc string `json:"jsonrpc"`
Error ZabbixError `json:"error"`
Result interface{} `json:"result"`
Id int `json:"id"`
}
type JsonRPCRequest struct {
Jsonrpc string `json:"jsonrpc"`
Method string `json:"method"`
Params interface{} `json:"params"`
// Zabbix 2.0:
// The "user.login" method must be called without the "auth" parameter
Auth string `json:"auth,omitempty"`
Id int `json:"id"`
}
type ZabbixError struct {
Code int `json:"code"`
Message string `json:"message"`
Data string `json:"data"`
}
func (z *ZabbixError) Error() string {
return z.Data
}
type ZabbixHost struct {
HostID string `json:"hostid"`
Host string `json:"host"`
Available ZabbixJSONInt `json:"available,omitempty"`
Description string `json:"description,omitempty"`
DisableUntil ZabbixJSONInt64 `json:"disable_until,omitempty"`
Error string `json:"error,omitempty"`
ErrorsFrom ZabbixJSONInt64 `json:"errors_from,omitempty"`
Flags ZabbixJSONInt `json:"flags,omitempty"`
InventoryMode ZabbixJSONInt `json:"inventory_mode,omitempty"`
IPMIAuthType ZabbixJSONInt `json:"ipmi_authtype,omitempty"`
IPMIAvailable ZabbixJSONInt `json:"ipmi_available,omitempty"`
IPMIDisableUntil ZabbixJSONInt64 `json:"ipmi_disable_until,omitempty"`
IPMIError string `json:"ipmi_error,omitempty"`
IPMIErrorsFrom ZabbixJSONInt64 `json:"ipmi_errors_from,omitempty"`
IPMIPassword string `json:"ipmi_password,omitempty"`
IPMIPrivilege ZabbixJSONInt `json:"ipmi_privilege,omitempty"`
IPMIUsername string `json:"ipmi_username,omitempty"`
JmxAvailable ZabbixJSONInt `json:"jmx_available,omitempty"`
JmxDisableUntil ZabbixJSONInt64 `json:"jmx_disable_until,omitempty"`
JmxError string `json:"jmx_error,omitempty"`
JmxErrorsFrom ZabbixJSONInt64 `json:"jmx_errors_from,omitempty"`
MaintenanceFrom ZabbixJSONInt64 `json:"maintenance_from,omitempty"`
MaintenanceStatus ZabbixJSONInt `json:"maintenance_status,omitempty"`
MaintenanceType ZabbixJSONInt `json:"maintenance_type,omitempty"`
MaintenanceID string `json:"maintenanceid,omitempty"`
Name string `json:"name,omitempty"`
ProxyHostID string `json:"proxy_hostid,omitempty"`
SNMPAvailable ZabbixJSONInt `json:"snmp_available"`
SNMPDisableUntil ZabbixJSONInt64 `json:"snmp_disable_until,omitempty"`
SNMPError string `json:"snmp_error,omitempty"`
SNMPErrorsFrom ZabbixJSONInt64 `json:"snmp_errors_from,omitempty"`
Status ZabbixJSONInt `json:"status,omitempty"`
TLSIssuer string `json:"tls_issuer,omitempty"`
TLSSubject string `json:"tls_subject,omitempty"`
TLSPskIdentity string `json:"tls_psk_identity,omitempty"`
TLSPsk string `json:"tls_psk,omitempty"`
TLSConnect string `json:"tls_connect,omitempty"`
TLSAccept string `json:"tls_accept,omitempty"`
LastAccess ZabbixJSONInt64 `json:"lastaccess,omitempty"`
TemplateID string `json:"templateid,omitempty"`
Groups []ZabbixGroup `json:"groups,omitempty"`
Applications []ZabbixApplication `json:"applications,omitempty"`
Discoveries []ZabbixDiscovery `json:"discoveries,omitempty"`
DiscoveryRule ZabbixDiscoveryRule `json:"discoveryRule,omitempty"`
Graphs []ZabbixGraph `json:"graphs,omitempty"`
HostDiscovery []ZabbixHostDiscovery `json:"hostDiscovery,omitempty"`
HTTPTests []ZabbixHTTPTest `json:"httpTests,omitempty"`
Interfaces []ZabbixInterface `json:"interfaces,omitempty"`
Inventory ZabbixInventory `json:"inventory,omitempty"`
Items []ZabbixItem `json:"items,omitempty"`
Macros []ZabbixMacros `json:"macros,omitempty"`
ParentTemplates []ZabbixParentTemplate `json:"parentTemplates,omitempty"`
Screens []ZabbixScreen `json:"screens,omitempty"`
Triggers []ZabbixTrigger `json:"triggers,omitempty"`
}
type ZabbixGraph map[string]interface{}
type ZabbixGraphItem map[string]interface{}
type ZabbixGroup struct {
Groupid string `json:"groupid"`
Name string `json:"name"`
Flags ZabbixJSONInt `json:"flags,omitempty"`
Internal string `json:"internal"`
}
type ZabbixDiscovery map[string]interface{}
type ZabbixHostDiscovery map[string]interface{}
type ZabbixHTTPTest map[string]interface{}
type ZabbixInventory map[string]interface{}
type ZabbixMacros map[string]interface{}
type ZabbixParentTemplate map[string]interface{}
type ZabbixScreen map[string]interface{}
type ZabbixJSONInt int
type ZabbixJSONInt64 int64
type ZabbixJSONFloat32 float32
func (fi *ZabbixJSONInt) UnmarshalJSON(b []byte) error {
if b[0] != '"' {
return json.Unmarshal(b, (*int)(fi))
}
var s string
if err := json.Unmarshal(b, &s); err != nil {
return err
}
i, err := strconv.Atoi(s)
if err != nil {
return err
}
*fi = ZabbixJSONInt(i)
return nil
}
func (fi *ZabbixJSONInt64) UnmarshalJSON(b []byte) error {
if b[0] != '"' {
return json.Unmarshal(b, (*int64)(fi))
}
var s string
if err := json.Unmarshal(b, &s); err != nil {
return err
}
i, err := strconv.Atoi(s)
if err != nil {
return err
}
*fi = ZabbixJSONInt64(i)
return nil
}
func (fi *ZabbixJSONFloat32) UnmarshalJSON(b []byte) error {
if b[0] != '"' {
return json.Unmarshal(b, (*float32)(fi))
}
var s string
if err := json.Unmarshal(b, &s); err != nil {
return err
}
if s == "" {
return nil
}
i, err := strconv.ParseFloat(s, 32)
if err != nil {
return err
}
*fi = ZabbixJSONFloat32(i)
return nil
}
type ZabbixInterface struct {
InterfaceID string `json:"interfaceid"`
DNS string `json:"dns"`
HostID string `json:"hostid"`
IP string `json:"ip"`
Main ZabbixJSONInt `json:"main"`
Port string `json:"port"`
Type ZabbixJSONInt `json:"type"`
UseIP ZabbixJSONInt `json:"useip"`
Bulk ZabbixJSONInt `json:"bulk,omitempty"`
}
type ZabbixTrigger struct {
TriggerID string `json:"triggerid"`
Description string `json:"description"`
Expression string `json:"expression"`
Comments string `json:"comments,omitempty"`
Flags ZabbixJSONInt `json:"flags,omitempty"`
LastChange ZabbixJSONInt64 `json:"lastchange,omitempty"`
Priority ZabbixJSONInt `json:"priority,omitempty"`
State ZabbixJSONInt `json:"state,omitempty"`
Status ZabbixJSONInt `json:"status,omitempty"`
TemplateID string `json:"templateid,omitempty"`
Type ZabbixJSONInt `json:"type,omitempty"`
URL string `json:"url,omitempty"`
Value ZabbixJSONInt `json:"value,omitempty"`
RecoveryMode ZabbixJSONInt `json:"recovery_mode,omitempty"`
RecoveryExpression string `json:"recovery_expression,omitempty"`
CorrelationMode ZabbixJSONInt `json:"correlation_mode,omitempty"`
CorrelationTag string `json:"correlation_tag,omitempty"`
ManualClose ZabbixJSONInt `json:"manual_close,omitempty"`
Error string `json:"error,omitempty"`
}
type ZabbixApplication map[string]interface{}
type ZabbixDiscoveryRule map[string]interface{}
type ZabbixItemDiscovery map[string]interface{}
type ZabbixPreprocessing map[string]interface{}
type ZabbixHistoryItem struct {
Clock string `json:"clock"`
Value string `json:"value"`
Itemid string `json:"itemid"`
}
type ZabbixItem struct {
ItemID string `json:"itemid"`
Delay string `json:"delay"`
HostID string `json:"hostid"`
InterfaceID string `json:"interfaceid"`
Key string `json:"key_"`
Name string `json:"name"`
Type ZabbixJSONInt `json:"type"`
ValueType ZabbixJSONInt `json:"value_type"`
AuthType ZabbixJSONInt `json:"authtype,omitempty"`
Description string `json:"description,omitempty"`
Error string `json:"error,omitempty"`
Flags ZabbixJSONInt `json:"flags,omitempty"`
History string `json:"history,omitempty"`
InventoryLink ZabbixJSONInt `json:"inventory_link,omitempty"`
IPMISensor string `json:"ipmi_sensor,omitempty"`
LastClock ZabbixJSONInt64 `json:"lastclock,omitempty"`
LastNs ZabbixJSONInt `json:"lastns,omitempty"`
LastValue string `json:"lastvalue,omitempty"`
LogTimeFmt string `json:"logtimefmt,omitempty"`
MTime ZabbixJSONInt64 `json:"mtime,omitempty"`
Params string `json:"params,omitempty"`
Password string `json:"password,omitempty"`
Port string `json:"port,omitempty"`
PrevValue string `json:"prevvalue,omitempty"`
PrivateKey string `json:"privatekey,omitempty"`
PublicKey string `json:"publickey,omitempty"`
SNMPCommunity string `json:"snmp_community,omitempty"`
SNMPOID string `json:"snmp_oid,omitempty"`
SNMPv3AuthPassPhrase string `json:"snmpv3_authpassphrase,omitempty"`
SNMPv3AuthProtocol ZabbixJSONInt `json:"snmpv3_authprotocol,omitempty"`
SNMPv3ContextName string `json:"snmpv3_contextname,omitempty"`
SNMPv3PrivPassPhrase string `json:"snmpv3_privpassphrase,omitempty"`
SNMPv3PrivProtocol ZabbixJSONInt `json:"snmpv3_privprotocol,omitempty"`
SNMPv3SecurityLevel ZabbixJSONInt `json:"snmpv3_securitylevel,omitempty"`
SNMPv3SecurityName string `json:"snmpv3_securityname,omitempty"`
State ZabbixJSONInt `json:"state,omitempty"`
Status ZabbixJSONInt `json:"status,omitempty"`
TemplateID string `json:"templateid,omitempty"`
TrapperHosts string `json:"trapper_hosts"`
Trends string `json:"trends,omitempty"`
Units string `json:"units,omitempty"`
Username string `json:"username,omitempty"`
ValueMapID string `json:"valuemapid,omitempty"`
JmxEndpoint string `json:"jmx_endpoint"`
MasterItemID ZabbixJSONInt `json:"master_itemid,omitempty"`
DataType ZabbixJSONInt `json:"data_type,omitempty"`
DelayFlex string `json:"delay_flex,omitempty"`
Delta string `json:"delta,omitempty"`
Formula ZabbixJSONFloat32 `json:"formula,omitempty"`
Multiplier ZabbixJSONInt `json:"multiplier,omitempty"`
LastLogSize string `json:"lastlogsize,omitempty"`
LifeTime string `json:"lifetime,omitempty"`
EvalType string `json:"evaltype,omitempty"`
Hosts []ZabbixHost `json:"hosts,omitempty"`
Interfaces []ZabbixInterface `json:"interfaces,omitempty"`
Triggers []ZabbixTrigger `json:"triggers,omitempty"`
Applications []ZabbixApplication `json:"applications,omitempty"`
DiscoveryRule ZabbixDiscoveryRule `json:"discoveryRule,omitempty"`
ItemDiscovery []ZabbixItemDiscovery `json:"itemDiscovery,omitempty"`
Processing []ZabbixPreprocessing `json:"preprocessing,omitempty"`
}
type API struct {
url string
user string
passwd string
id int
auth string
Client *http.Client
}
func GetFields(i interface{}) []string {
fields := []string{}
t := reflect.TypeOf(i)
if t.Kind() == reflect.Struct {
// Iterate over all available fields and read the tag value
for i := 0; i < t.NumField(); i++ {
field := t.Field(i)
switch field.Type.Kind() {
case reflect.Bool, reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Float32, reflect.Float64, reflect.String:
tag := field.Tag.Get("json")
parts := strings.Split(strings.TrimSpace(tag), ",")
fields = append(fields, parts[0])
}
}
}
return fields
}
func NewAPI(server, user, passwd string) (*API, error) {
return &API{server, user, passwd, 0, "", &http.Client{}}, nil
}
func (api *API) GetAuth() string {
return api.auth
}
/**
Each request establishes its own connection to the server. This makes it easy
to keep request/responses in order without doing any concurrency
*/
func (api *API) ZabbixRequest(method string, data interface{}) (JsonRPCResponse, error) {
// Setup our JSONRPC Request data
id := api.id
api.id = api.id + 1
jsonobj := JsonRPCRequest{"2.0", method, data, api.auth, id}
encoded, err := json.Marshal(jsonobj)
if err != nil {
return JsonRPCResponse{}, err
}
// Setup our HTTP request
request, err := http.NewRequest("POST", api.url, bytes.NewBuffer(encoded))
if err != nil {
return JsonRPCResponse{}, err
}
request.Header.Add("Content-Type", "application/json-rpc")
if api.auth != "" {
// XXX Not required in practice, check spec
//request.SetBasicAuth(api.user, api.passwd)
//request.Header.Add("Authorization", api.auth)
}
// Execute the request
response, err := api.Client.Do(request)
if err != nil {
return JsonRPCResponse{}, err
}
/**
We can't rely on response.ContentLength because it will
be set at -1 for large responses that are chunked. So
we treat each API response as streamed data.
*/
var result JsonRPCResponse
var buf bytes.Buffer
_, err = io.Copy(&buf, response.Body)
if err != nil {
return JsonRPCResponse{}, err
}
json.Unmarshal(buf.Bytes(), &result)
response.Body.Close()
return result, nil
}
func (api *API) Login() (bool, error) {
params := make(map[string]string, 0)
params["user"] = api.user
params["password"] = api.passwd
response, err := api.ZabbixRequest("user.login", params)
if err != nil {
fmt.Printf("Error: %s\n", err)
return false, err
}
if response.Error.Code != 0 {
return false, &response.Error
}
api.auth = response.Result.(string)
return true, nil
}
func (api *API) Logout() (bool, error) {
emptyparams := make(map[string]string, 0)
response, err := api.ZabbixRequest("user.logout", emptyparams)
if err != nil {
return false, err
}
if response.Error.Code != 0 {
return false, &response.Error
}
return true, nil
}
func (api *API) Version() (string, error) {
response, err := api.ZabbixRequest("APIInfo.version", make(map[string]string, 0))
if err != nil {
return "", err
}
if response.Error.Code != 0 {
return "", &response.Error
}
return response.Result.(string), nil
}
/**
Interface to the user.* calls
*/
func (api *API) User(method string, data interface{}) ([]interface{}, error) {
response, err := api.ZabbixRequest("user."+method, data)
if err != nil {
return nil, err
}
if response.Error.Code != 0 {
return nil, &response.Error
}
return response.Result.([]interface{}), nil
}
/**
Interface to the host.* calls
*/
func (api *API) Host(method string, data interface{}) ([]ZabbixHost, error) {
response, err := api.ZabbixRequest("host."+method, data)
if err != nil {
return nil, err
}
if response.Error.Code != 0 {
return nil, &response.Error
}
//fmt.Print(data)
// XXX uhg... there has got to be a better way to convert the response
// to the type I want to return
res, err := json.Marshal(response.Result)
var ret []ZabbixHost
err = json.Unmarshal(res, &ret)
//fmt.Print(err)
return ret, nil
}
/**
Interface to the graph.* calls
*/
func (api *API) Graph(method string, data interface{}) ([]ZabbixGraph, error) {
response, err := api.ZabbixRequest("graph."+method, data)
if err != nil {
return nil, err
}
if response.Error.Code != 0 {
return nil, &response.Error
}
// XXX uhg... there has got to be a better way to convert the response
// to the type I want to return
res, err := json.Marshal(response.Result)
var ret []ZabbixGraph
err = json.Unmarshal(res, &ret)
return ret, nil
}
/**
Interface to the history.* calls
*/
func (api *API) History(method string, data interface{}) ([]ZabbixHistoryItem, error) {
response, err := api.ZabbixRequest("history."+method, data)
if err != nil {
return nil, err
}
if response.Error.Code != 0 {
return nil, &response.Error
}
// XXX uhg... there has got to be a better way to convert the response
// to the type I want to return
res, err := json.Marshal(response.Result)
var ret []ZabbixHistoryItem
err = json.Unmarshal(res, &ret)
return ret, nil
}
/**
Interface to the item.* calls
*/
func (api *API) Item(method string, data interface{}) ([]ZabbixItem, error) {
response, err := api.ZabbixRequest("item."+method, data)
if err != nil {
return nil, err
}
if response.Error.Code != 0 {
return nil, &response.Error
}
// XXX uhg... there has got to be a better way to convert the response
// to the type I want to return
res, err := json.Marshal(response.Result)
var ret []ZabbixItem
err = json.Unmarshal(res, &ret)
return ret, nil
}
/**
Interface to the hostgroup.* calls
*/
func (api *API) HostGroup(method string, data interface{}) ([]ZabbixGroup, error) {
response, err := api.ZabbixRequest("hostgroup."+method, data)
if err != nil {
return nil, err
}
if response.Error.Code != 0 {
return nil, &response.Error
}
// XXX uhg... there has got to be a better way to convert the response
// to the type I want to return
res, err := json.Marshal(response.Result)
var ret []ZabbixGroup
err = json.Unmarshal(res, &ret)
return ret, nil
}