forked from claranet/go-zabbix-api
-
Notifications
You must be signed in to change notification settings - Fork 1
/
action.go
394 lines (338 loc) · 15.7 KB
/
action.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
package zabbix
import (
"strconv"
)
type (
// Whether to pause escalation during maintenance periods or not.
// "pause_suppressed" in https://www.zabbix.com/documentation/4.0/manual/api/reference/action/object#action
PauseType int
// Filter condition evaluation method.
// "evaltype" in https://www.zabbix.com/documentation/4.0/manual/api/reference/action/object#action_filter
ActionEvaluationType int
// Type of condition.
// "conditiontype" in https://www.zabbix.com/documentation/4.0/manual/api/reference/action/object#action_filter_condition
// "conditiontype" in https://www.zabbix.com/documentation/4.0/manual/api/reference/action/object#action_operation_condition
ActionConditionType int
// Condition operator.
// "operator" in https://www.zabbix.com/documentation/4.0/manual/api/reference/action/object#action_filter_condition
ActionFilterConditionOperator int
// Type of operation.
// "operationtype" in https://www.zabbix.com/documentation/4.0/manual/api/reference/action/object#action_operation
ActionOperationType int
// Type of operation command.
// "type" in https://www.zabbix.com/documentation/4.0/manual/api/reference/action/object#action_operation_command
ActionOperationCommandType int
// Authentication method used for SSH commands.
// "authtype" in https://www.zabbix.com/documentation/4.0/manual/api/reference/action/object#action_operation_command
ActionOperationCommandAuthType int
// Target on which the custom script operation command will be executed.
// "execute_on" in https://www.zabbix.com/documentation/4.0/manual/api/reference/action/object#action_operation_command
ActionOperationCommandExecutorType int
)
const (
DontPause PauseType = 0
Pause PauseType = 1
)
const (
AndOr ActionEvaluationType = 0
And ActionEvaluationType = 1
Or ActionEvaluationType = 2
Custom ActionEvaluationType = 3
)
const (
HostGroupCondition ActionConditionType = 0
HostCondition ActionConditionType = 1
TriggerCondition ActionConditionType = 2
TriggerNameCondition ActionConditionType = 3
TriggerSeverityCondition ActionConditionType = 4
TimePeriodCondition ActionConditionType = 6
HostIpCondition ActionConditionType = 7
DiscoveredServiceTypeCondition ActionConditionType = 8
DiscoveredServicePortCondition ActionConditionType = 9
DiscoveryStatusCondition ActionConditionType = 10
UptimeOrDowntimeDurationCondition ActionConditionType = 11
ReceivedValueCondition ActionConditionType = 12
HostTemplateCondition ActionConditionType = 13
EventAcknowledgedCondition ActionConditionType = 14
ApplicationCondition ActionConditionType = 15
ProblemIsSuppressedCondition ActionConditionType = 16
DiscoveryRuleCondition ActionConditionType = 18
DiscoveryCheckCondition ActionConditionType = 19
ProxyCondition ActionConditionType = 20
DiscoveryObjectCondition ActionConditionType = 21
HostNameCondition ActionConditionType = 22
EventTypeCondition ActionConditionType = 23
HostMetadataCondition ActionConditionType = 24
EventTagCondition ActionConditionType = 25
EventTagValueCondition ActionConditionType = 26
)
const (
Equals ActionFilterConditionOperator = 0
DoesNotEqual ActionFilterConditionOperator = 1
Contains ActionFilterConditionOperator = 2
DoesNotContains ActionFilterConditionOperator = 3
In ActionFilterConditionOperator = 4
IsGreaterThanOrEquals ActionFilterConditionOperator = 5
IsLessThanOrEquals ActionFilterConditionOperator = 6
NotIn ActionFilterConditionOperator = 7
Matches ActionFilterConditionOperator = 8
DoesNotMatch ActionFilterConditionOperator = 9
Yes ActionFilterConditionOperator = 10
No ActionFilterConditionOperator = 11
)
const (
SendMessage ActionOperationType = 0
RemoteCommand ActionOperationType = 1
AddHost ActionOperationType = 2
RemoveHost ActionOperationType = 3
AddToHostGroup ActionOperationType = 4
RemoveFromHostGroup ActionOperationType = 5
LinkToTemplate ActionOperationType = 6
UnlinkFromTemplate ActionOperationType = 7
EnableHost ActionOperationType = 8
DisableHost ActionOperationType = 9
SetHostInventoryMode ActionOperationType = 10
NotifyRecoveryAllInvolved ActionOperationType = 11
NotifyUpdateAllInvolved ActionOperationType = 12
)
const (
CustomScript ActionOperationCommandType = 0
IpmiCommand ActionOperationCommandType = 1
SshCommand ActionOperationCommandType = 2
TelnetCommand ActionOperationCommandType = 3
GlobalScript ActionOperationCommandType = 4
)
const (
Password ActionOperationCommandAuthType = 0
PublicKey ActionOperationCommandAuthType = 1
)
const (
AgentExecutor ActionOperationCommandExecutorType = 0
ServerExecutor ActionOperationCommandExecutorType = 1
ProxyExecutor ActionOperationCommandExecutorType = 2
)
// Action represent Zabbix Action type returned from Zabbix API
// https://www.zabbix.com/documentation/4.0/manual/api/reference/action/object
// https://www.zabbix.com/documentation/5.0/manual/api/reference/action/object
type Action struct {
ActionID string `json:"actionid,omitempty"`
//Period string `json:"esc_period"`
EventSource EventType `json:"eventsource,omitempty"` // NOTE: Can not update
Name string `json:"name"`
//DefaultMessage string `json:"def_longdata"` // NOTE: no support on Zabbix 5.0 onward
//DefaultSubject string `json:"def_shortdata"` // NOTE: no support on Zabbix 5.0 onward
//RecoveryMessage string `json:"r_longdata"` // NOTE: no support on Zabbix 5.0 onward
//RecoverySubject string `json:"r_shortdata"` // NOTE: no support on Zabbix 5.0 onward
//AckMessage string `json:"ack_longdata"` // NOTE: no support on Zabbix 5.0 onward
//AckSubject string `json:"ack_shortdata"` // NOTE: no support on Zabbix 5.0 onward
Status StatusType `json:"status,omitempty,string"`
PauseSuppressed *PauseType `json:"pause_suppressed,omitempty,string"`
Filter ActionFilter `json:"filter,omitempty"`
Operations ActionOperations `json:"operations,omitempty"`
RecoveryOperations ActionRecoveryOperations `json:"recoveryOperations,omitempty"`
UpdateOperations ActionUpdateOperations `json:"acknowledgeOperations,omitempty"`
}
type Actions []Action
type ActionFilter struct {
Conditions ActionFilterConditions `json:"conditions"`
EvaluationType ActionEvaluationType `json:"evaltype,string"`
Formula string `json:"formula,omitempty"`
}
type ActionFilterCondition struct {
ConditionID string `json:"conditionid,omitempty"`
ConditionType ActionConditionType `json:"conditiontype,string"`
Value string `json:"value"`
//Value2 string `json:"value2,omitempty"`
FormulaID string `json:"formulaid,omitempty"`
Operator ActionFilterConditionOperator `json:"operator,string"`
}
type ActionFilterConditions []ActionFilterCondition
type ActionOperation struct {
OperationID string `json:"operationid,omitempty"`
OperationType ActionOperationType `json:"operationtype,string"`
ActionID string `json:"actionid,omitempty"`
EvaluationType ActionEvaluationType `json:"evaltype,omitempty,string"`
Command *ActionOperationCommand `json:"opcommand,omitempty"`
CommandHostGroups ActionOperationCommandHostGroups `json:"opcommand_grp,omitempty"`
CommandHosts ActionOperationCommandHosts `json:"opcommand_hst,omitempty"`
Conditions ActionOperationConditions `json:"opconditions,omitempty"`
HostGroups ActionOperationHostGroups `json:"opgroup,omitempty"`
Message *ActionOperationMessage `json:"opmessage,omitempty"`
MessageUserGroups ActionOperationMessageUserGroups `json:"opmessage_grp,omitempty"`
MessageUsers ActionOperationMessageUsers `json:"opmessage_usr,omitempty"`
Templates ActionOperationTemplates `json:"optemplate,omitempty"`
Inventory *ActionOperationInventory `json:"opinventory,omitempty"`
}
type ActionOperations []ActionOperation
type ActionOperationCommand struct {
OperationID string `json:"operationid,omitempty"`
Type ActionOperationCommandType `json:"type,string"`
Command string `json:"command,omitempty"`
AuthType *ActionOperationCommandAuthType `json:"authtype,omitempty,string"`
ExecuteOn *ActionOperationCommandExecutorType `json:"execute_on,omitempty,string"`
Username string `json:"username,omitempty"`
Password string `json:"password,omitempty"`
Port string `json:"port,omitempty"`
PrivateKey string `json:"privatekey,omitempty"`
PublicKey string `json:"publickey,omitempty"`
ScriptID string `json:"scriptid,omitempty"`
}
type ActionOperationCommandHostGroup struct {
CommandHostGroupID string `json:"opcommand_grpid,omitempty"`
OperationID string `json:"operationid,omitempty"`
GroupID string `json:"groupid"`
}
type ActionOperationCommandHostGroups []ActionOperationCommandHostGroup
type ActionOperationCommandHost struct {
CommandHostID string `json:"opcommand_hstid,omitempty"`
OperationID string `json:"operationid,omitempty"`
HostID string `json:"hostid"`
}
type ActionOperationCommandHosts []ActionOperationCommandHost
type ActionOperationCondition struct {
OperationID string `json:"operationid,omitempty"`
ConditionID string `json:"opconditionid,omitempty"`
Condition ActionConditionType `json:"conditiontype,string"`
Value string `json:"value"`
Operator ActionFilterConditionOperator `json:"operator,string"`
}
type ActionOperationConditions []ActionOperationCondition
type ActionOperationHostGroup struct {
//OperationID string `json:"operationid,omitempty"`
GroupID string `json:"groupid"`
}
type ActionOperationHostGroups []ActionOperationHostGroup
type ActionOperationMessage struct {
OperationID string `json:"operationid,omitempty"`
DefaultMessage string `json:"default_msg"`
MediaTypeID string `json:"mediatypeid"`
Message string `json:"message"`
Subject string `json:"subject"`
}
type ActionOperationMessageUserGroup struct {
OperationID string `json:"operationid,omitempty"`
UserGroupID string `json:"usrgrpid"`
}
type ActionOperationMessageUserGroups []ActionOperationMessageUserGroup
type ActionOperationMessageUser struct {
OperationID string `json:"operationid,omitempty"`
UserID string `json:"userid"`
}
type ActionOperationMessageUsers []ActionOperationMessageUser
type ActionOperationTemplate struct {
//OperationID string `json:"operationid"`
TemplateID string `json:"templateid"`
}
type ActionOperationTemplates []ActionOperationTemplate
type ActionOperationInventory struct {
OperationID string `json:"operationid,omitempty"`
InventoryMode string `json:"inventory_mode"`
}
type ActionRecoveryOperation struct {
OperationID string `json:"operationid,omitempty"`
OperationType ActionOperationType `json:"operationtype,string"`
ActionID string `json:"actionid,omitempty"`
Command *ActionOperationCommand `json:"opcommand,omitempty"`
CommandHostGroups ActionOperationCommandHostGroups `json:"opcommand_grp,omitempty"`
CommandHosts ActionOperationCommandHosts `json:"opcommand_hst,omitempty"`
Message *ActionOperationMessage `json:"opmessage,omitempty"`
MessageUserGroups ActionOperationMessageUserGroups `json:"opmessage_grp,omitempty"`
MessageUsers ActionOperationMessageUsers `json:"opmessage_usr,omitempty"`
}
type ActionRecoveryOperations []ActionRecoveryOperation
type ActionUpdateOperation struct {
//OperationID string `json:"operationid,omitempty"`
OperationType ActionOperationType `json:"operationtype,string"`
Command *ActionOperationCommand `json:"opcommand,omitempty"`
CommandHostGroups ActionOperationCommandHostGroups `json:"opcommand_grp,omitempty"`
CommandHosts ActionOperationCommandHosts `json:"opcommand_hst,omitempty"`
Message *ActionOperationMessage `json:"opmessage,omitempty"`
MessageUserGroups ActionOperationMessageUserGroups `json:"opmessage_grp,omitempty"`
MessageUsers ActionOperationMessageUsers `json:"opmessage_usr,omitempty"`
}
type ActionUpdateOperations []ActionUpdateOperation
// ActionsGet Wrapper for action.get
// https://www.zabbix.com/documentation/4.0/manual/api/reference/action/get
func (api *API) ActionsGet(params Params) (res Actions, err error) {
if _, present := params["output"]; !present {
params["output"] = "extend"
}
err = api.CallWithErrorParse("action.get", params, &res)
return
}
// ActionGetByID Gets action by Id only if there is exactly 1 matching action.
func (api *API) ActionGetByID(id string) (res *Action, err error) {
params := Params{
"actionids": id,
"selectFilter": "extend",
"selectOperations": "extend",
"selectRecoveryOperations": "extend",
"selectAcknowledgeOperations": "extend",
}
actions, err := api.ActionsGet(params)
if err != nil {
return
}
if len(actions) == 1 {
res = &actions[0]
} else {
e := ExpectedOneResult(len(actions))
err = &e
}
return
}
// ActionsCreate Wrapper for action.create
// https://www.zabbix.com/documentation/4.0/manual/api/reference/action/create
func (api *API) ActionsCreate(actions Actions) (err error) {
response, err := api.CallWithError("action.create", actions)
if err != nil {
return
}
result := response.Result.(map[string]interface{})
actionids := result["actionids"].([]interface{})
for i, id := range actionids {
switch id.(type) {
case string:
actions[i].ActionID = id.(string)
case float64:
actions[i].ActionID = strconv.Itoa(int(id.(float64)))
}
}
return
}
// ActionsUpdate Wrapper for action.update
// https://www.zabbix.com/documentation/4.0/manual/api/reference/action/update
func (api *API) ActionsUpdate(actions Actions) (err error) {
_, err = api.CallWithError("action.update", actions)
return
}
// ActionsDelete Wrapper for action.delete
// Cleans ActionId in all actions elements if call succeed.
// https://www.zabbix.com/documentation/4.0/manual/api/reference/action/delete
func (api *API) ActionsDelete(actions Actions) (err error) {
ids := make([]string, len(actions))
for i, action := range actions {
ids[i] = action.ActionID
}
err = api.ActionsDeleteByIds(ids)
if err == nil {
for i := range actions {
actions[i].ActionID = ""
}
}
return
}
// ActionsDeleteByIds Wrapper for action.delete
// https://www.zabbix.com/documentation/4.0/manual/api/reference/action/delete
func (api *API) ActionsDeleteByIds(ids []string) (err error) {
response, err := api.CallWithError("action.delete", ids)
if err != nil {
return
}
result := response.Result.(map[string]interface{})
actionids := result["actionids"].([]interface{})
if len(ids) != len(actionids) {
err = &ExpectedMore{len(ids), len(actionids)}
}
return
}