forked from AlekSi/zabbix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
item.go
156 lines (137 loc) · 4.04 KB
/
item.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
package zabbix
import (
"fmt"
"github.com/AlekSi/reflector"
)
type (
ItemType int
ValueType int
DataType int
DeltaType int
)
const (
ZabbixAgent ItemType = 0
SNMPv1Agent ItemType = 1
ZabbixTrapper ItemType = 2
SimpleCheck ItemType = 3
SNMPv2Agent ItemType = 4
ZabbixInternal ItemType = 5
SNMPv3Agent ItemType = 6
ZabbixAgentActive ItemType = 7
ZabbixAggregate ItemType = 8
WebItem ItemType = 9
ExternalCheck ItemType = 10
DatabaseMonitor ItemType = 11
IPMIAgent ItemType = 12
SSHAgent ItemType = 13
TELNETAgent ItemType = 14
Calculated ItemType = 15
JMXAgent ItemType = 16
Float ValueType = 0
Character ValueType = 1
Log ValueType = 2
Unsigned ValueType = 3
Text ValueType = 4
Decimal DataType = 0
Octal DataType = 1
Hexadecimal DataType = 2
Boolean DataType = 3
AsIs DeltaType = 0
Speed DeltaType = 1
Delta DeltaType = 2
)
// https://www.zabbix.com/documentation/2.2/manual/appendix/api/item/definitions
type Item struct {
ItemId string `json:"itemid,omitempty"`
Delay int `json:"delay"`
HostId string `json:"hostid"`
InterfaceId string `json:"interfaceid,omitempty"`
Key string `json:"key_"`
Name string `json:"name"`
Type ItemType `json:"type"`
ValueType ValueType `json:"value_type"`
DataType DataType `json:"data_type"`
Delta DeltaType `json:"delta"`
Description string `json:"description"`
Error string `json:"error"`
History int `json:"history,omitempty"`
Trends int `json:"trends,omitempty"`
// Fields below used only when creating applications
ApplicationIds []string `json:"applications,omitempty"`
}
type Items []Item
// Converts slice to map by key. Panics if there are duplicate keys.
func (items Items) ByKey() (res map[string]Item) {
res = make(map[string]Item, len(items))
for _, i := range items {
_, present := res[i.Key]
if present {
panic(fmt.Errorf("Duplicate key %s", i.Key))
}
res[i.Key] = i
}
return
}
// Wrapper for item.get https://www.zabbix.com/documentation/2.2/manual/appendix/api/item/get
func (api *API) ItemsGet(params Params) (res Items, err error) {
if _, present := params["output"]; !present {
params["output"] = "extend"
}
response, err := api.CallWithError("item.get", params)
if err != nil {
return
}
reflector.MapsToStructs2(response.Result.([]interface{}), &res, reflector.Strconv, "json")
return
}
// Gets items by application Id.
func (api *API) ItemsGetByApplicationId(id string) (res Items, err error) {
return api.ItemsGet(Params{"applicationids": id})
}
// Wrapper for item.create: https://www.zabbix.com/documentation/2.2/manual/appendix/api/item/create
func (api *API) ItemsCreate(items Items) (err error) {
response, err := api.CallWithError("item.create", items)
if err != nil {
return
}
result := response.Result.(map[string]interface{})
itemids := result["itemids"].([]interface{})
for i, id := range itemids {
items[i].ItemId = id.(string)
}
return
}
// Wrapper for item.delete: https://www.zabbix.com/documentation/2.2/manual/appendix/api/item/delete
// Cleans ItemId in all items elements if call succeed.
func (api *API) ItemsDelete(items Items) (err error) {
ids := make([]string, len(items))
for i, item := range items {
ids[i] = item.ItemId
}
err = api.ItemsDeleteByIds(ids)
if err == nil {
for i := range items {
items[i].ItemId = ""
}
}
return
}
// Wrapper for item.delete: https://www.zabbix.com/documentation/2.2/manual/appendix/api/item/delete
func (api *API) ItemsDeleteByIds(ids []string) (err error) {
response, err := api.CallWithError("item.delete", ids)
if err != nil {
return
}
result := response.Result.(map[string]interface{})
itemids1, ok := result["itemids"].([]interface{})
l := len(itemids1)
if !ok {
// some versions actually return map there
itemids2 := result["itemids"].(map[string]interface{})
l = len(itemids2)
}
if len(ids) != l {
err = &ExpectedMore{len(ids), l}
}
return
}