forked from dainis/zabbix
-
Notifications
You must be signed in to change notification settings - Fork 18
/
item.go
253 lines (226 loc) · 6.41 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
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
package zabbix
import (
"fmt"
)
type (
// ItemType type of the item
ItemType int
// ValueType type of information of the item
ValueType int
// DataType data type of the item
DataType int
// DeltaType value that will be stored
DeltaType int
)
const (
// Different item type, see :
// - "type" in https://www.zabbix.com/documentation/4.4/manual/api/reference/item/object
// - https://www.zabbix.com/documentation/4.4/manual/config/items/itemtypes
// ZabbixAgent type
ZabbixAgent ItemType = 0
// SNMPv1Agent type
SNMPv1Agent ItemType = 1
// ZabbixTrapper type
ZabbixTrapper ItemType = 2
// SimpleCheck type
SimpleCheck ItemType = 3
// SNMPv2Agent type
SNMPv2Agent ItemType = 4
// ZabbixInternal type
ZabbixInternal ItemType = 5
// SNMPv3Agent type
SNMPv3Agent ItemType = 6
// ZabbixAgentActive type
ZabbixAgentActive ItemType = 7
// ZabbixAggregate type
ZabbixAggregate ItemType = 8
// WebItem type
WebItem ItemType = 9
// ExternalCheck type
ExternalCheck ItemType = 10
// DatabaseMonitor type
DatabaseMonitor ItemType = 11
//IPMIAgent type
IPMIAgent ItemType = 12
// SSHAgent type
SSHAgent ItemType = 13
// TELNETAgent type
TELNETAgent ItemType = 14
// Calculated type
Calculated ItemType = 15
// JMXAgent type
JMXAgent ItemType = 16
// SNMPTrap type (new in 2.2)
SNMPTrap ItemType = 17
// DependentItem type (new in 3.4)
DependentItem ItemType = 18
//HTTPAgent type (new in 4.0)
HTTPAgent ItemType = 19
)
const (
// Type of information of the item
// see "value_type" in https://www.zabbix.com/documentation/3.2/manual/api/reference/item/object
// Float value
Float ValueType = 0
// Character value
Character ValueType = 1
// Log value
Log ValueType = 2
// Unsigned value
Unsigned ValueType = 3
// Text value
Text ValueType = 4
)
const (
// Data type of the item
// see "data_type" in https://www.zabbix.com/documentation/3.2/manual/api/reference/item/object
// Decimal data (default)
Decimal DataType = 0
// Octal data
Octal DataType = 1
// Hexadecimal data
Hexadecimal DataType = 2
// Boolean data
Boolean DataType = 3
)
const (
// Value that will be stored
// see "delta" in https://www.zabbix.com/documentation/3.2/manual/api/reference/item/object
// AsIs as is (default)
AsIs DeltaType = 0
// Speed speed per second
Speed DeltaType = 1
// Delta simple change
Delta DeltaType = 2
)
// Item represent Zabbix item object
// https://www.zabbix.com/documentation/3.2/manual/api/reference/item/object
type Item struct {
ItemID string `json:"itemid,omitempty"`
Delay string `json:"delay"`
HostID string `json:"hostid"`
InterfaceID string `json:"interfaceid,omitempty"`
Key string `json:"key_"`
Name string `json:"name"`
Type ItemType `json:"type,string"`
ValueType ValueType `json:"value_type,string"`
DataType DataType `json:"data_type,string"`
Delta DeltaType `json:"delta,string"`
Description string `json:"description"`
Error string `json:"error,omitempty"`
History string `json:"history,omitempty"`
Trends string `json:"trends,omitempty"`
TrapperHosts string `json:"trapper_hosts,omitempty"`
// Fields below used only when creating applications
ApplicationIds []string `json:"applications,omitempty"`
ItemParent Hosts `json:"hosts"`
}
// Items is an array of Item
type Items []Item
// ByKey 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
}
// ItemsGet Wrapper for item.get
// https://www.zabbix.com/documentation/3.2/manual/api/reference/item/get
func (api *API) ItemsGet(params Params) (res Items, err error) {
if _, present := params["output"]; !present {
params["output"] = "extend"
}
err = api.CallWithErrorParse("item.get", params, &res)
return
}
// ItemGetByID Gets item by Id only if there is exactly 1 matching host.
func (api *API) ItemGetByID(id string) (res *Item, err error) {
items, err := api.ItemsGet(Params{"itemids": id})
if err != nil {
return
}
if len(items) != 1 {
e := ExpectedOneResult(len(items))
err = &e
return
}
res = &items[0]
return
}
// ItemsGetByApplicationID Gets items by application Id.
func (api *API) ItemsGetByApplicationID(id string) (res Items, err error) {
return api.ItemsGet(Params{"applicationids": id})
}
// ItemsCreate Wrapper for item.create
// https://www.zabbix.com/documentation/3.2/manual/api/reference/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
}
// ItemsUpdate Wrapper for item.update
// https://www.zabbix.com/documentation/3.2/manual/api/reference/item/update
func (api *API) ItemsUpdate(items Items) (err error) {
_, err = api.CallWithError("item.update", items)
return
}
// ItemsDelete Wrapper for item.delete
// Cleans ItemId in all items elements if call succeed.
// https://www.zabbix.com/documentation/3.2/manual/api/reference/item/delete
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
}
// ItemsDeleteByIds Wrapper for item.delete
// https://www.zabbix.com/documentation/3.2/manual/api/reference/item/delete
func (api *API) ItemsDeleteByIds(ids []string) (err error) {
deleteIds, err := api.ItemsDeleteIDs(ids)
if err != nil {
return
}
l := len(deleteIds)
if len(ids) != l {
err = &ExpectedMore{len(ids), l}
}
return
}
// ItemsDeleteIDs Wrapper for item.delete
// Delete the item and return the id of the deleted item
func (api *API) ItemsDeleteIDs(ids []string) (itemids []interface{}, err error) {
response, err := api.CallWithError("item.delete", ids)
if err != nil {
return
}
result := response.Result.(map[string]interface{})
itemids1, ok := result["itemids"].([]interface{})
if !ok {
itemids2 := result["itemids"].(map[string]interface{})
for _, id := range itemids2 {
itemids = append(itemids, id)
}
} else {
itemids = itemids1
}
return
}