forked from neogan74/go-zabbix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaction.go
237 lines (189 loc) · 6.66 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
package zabbix
import (
"fmt"
)
const (
// ActionEvalTypeAndOr indicated that an Action will evaluate its conditions
// using AND/OR bitwise logic.
ActionEvalTypeAndOr = iota
// ActionEvalTypeAnd indicated that an Action will evaluate its conditions
// using AND bitwise logic.
ActionEvalTypeAnd
// ActionEvalTypeOr indicated that an Action will evaluate its conditions
// using OR bitwise logic.
ActionEvalTypeOr
)
// Action represents a Zabbix Action returned from the Zabbix API.
//
// See: https://www.zabbix.com/documentation/2.2/manual/config/notifications/action
type Action struct {
// ActionID is the unique ID of the Action.
ActionID string
// StepDuration is the interval in seconds between each operation step.
StepDuration int
// EvaluationType determines the bitwise logic used to evaluate the Actions
// conditions.
//
// EvaluationType must be one of the ActionEvalType constants.
//
// EvaluationType is only supported up to Zabbix v2.2.
EvaluationType int
// EventType is the type of Events that this Action will handle.
//
// Source must be one of the EventSource constants.
EventType int
// Name is the name of the Action.
Name string
// ProblemMessageBody is the message body text to be submitted for this
// Action.
ProblemMessageBody string
// ProblemMessageSubject is the short summary text to be submitted for this
// Action.
ProblemMessageSubject string
// RecoveryMessageBody is the message body text to be submitted for this
// Action.
RecoveryMessageBody string
// RecoveryMessageSubject is the short summary text to be submitted for this
// Action.
RecoveryMessageSubject string
// RecoveryMessageEnabled determines whether recovery messages will be
// submitted for the Action when the source problem is resolved.
RecoveryMessageEnabled bool
// Enabled determines whether the Action is enabled or disabled.
Enabled bool
// Conditions are the conditions which must be met for this Action to
// execute.
Conditions []ActionCondition
// Operations are the operations which will be exectuted for this Action.
Operations []ActionOperation
}
// ActionCondition is action condition
type ActionCondition struct{}
// ActionOperation is action operation
type ActionOperation struct{}
// ActionGetParams is query params for action.get call
type ActionGetParams struct {
GetParameters
EventSource string `json:"eventsource"`
}
type ActionCreateResponse struct {
GetParameters
ActionIDs []string `json:"actionids"`
}
type ActionDeleteResponse struct {
GetParameters
ActionIDs []string `json:"actionids"`
}
type CreateActionGetParams struct {
Name string `json:"name"`
EventSource string `json:"eventsource"`
Status string `json:"status"`
EscPeriod string `json:"esc_period"`
// DefShortData string `json:"def_shortdata"`
// DefLongData string `json:"def_longdata"`
// RShortData string `json:"r_shortdata"`
// RLongData string `json:"r_longdata"`
Filter ActionFilter `json:"filter"`
Operations []Operations `json:"operations,omitempty"`
RecoveryOperations []RecoveryOperations `json:"recovery_operations,omitempty"`
UpdateOperations []UpdateOperations `json:"update_operations,omitempty"`
}
type Conditions struct {
Conditiontype int `json:"conditiontype"`
Operator int `json:"operator"`
Value string `json:"value"`
FormulaID string `json:"formulaid,omitempty"`
}
type ActionFilter struct {
Evaltype int `json:"evaltype"`
Conditions []Conditions `json:"conditions,omitempty"`
}
type OpmessageGrp struct {
Usrgrpid string `json:"usrgrpid,omitempty"`
}
type Opmessage struct {
DefaultMsg int `json:"default_msg"`
MediaTypeID string `json:"mediatypeid,omitempty"`
Message string `json:"message,omitempty"`
Subject string `json:"subject,omitempty"`
}
type Opconditions struct {
Conditiontype int `json:"conditiontype"`
Operator int `json:"operator"`
Value string `json:"value"`
}
type OpcommandGrp struct {
Groupid string `json:"groupid,omitempty"`
}
type Opcommand struct {
Type int `json:"type,omitempty"`
Scriptid string `json:"scriptid,omitempty"`
}
type Operations struct {
Operationtype int `json:"operationtype"`
EscPeriod string `json:"esc_period,omitempty"`
EscStepFrom int `json:"esc_step_from"`
EscStepTo int `json:"esc_step_to"`
Evaltype int `json:"evaltype"`
Opconditions []Opconditions `json:"opconditions,omitempty"`
OpcommandGrp []OpcommandGrp `json:"opcommand_grp,omitempty"`
OpmessageGrp []OpmessageGrp `json:"opmessage_grp,omitempty"`
Opmessage Opmessage `json:"opmessage,omitempty"`
}
type RecoveryOperations struct {
Operationtype int `json:"operationtype"`
Opmessage Opmessage `json:"opmessage,omitempty"`
}
type UpdateOperations struct {
Operationtype string `json:"operationtype,omitempty"`
Opmessage Opmessage `json:"opmessage,omitempty"`
}
// GetActions queries the Zabbix API for Actions 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.
func (c *Session) GetActions(params ActionGetParams) ([]Action, error) {
actions := make([]jAction, 0)
err := c.Get("action.get", params, &actions)
if err != nil {
return nil, err
}
if len(actions) == 0 {
return nil, ErrNotFound
}
// map JSON Actions to Go Actions
out := make([]Action, len(actions))
for i, jaction := range actions {
action, err := jaction.Action()
if err != nil {
return nil, fmt.Errorf("Error mapping Action %d in response: %v", i, err)
}
out[i] = *action
}
return out, nil
}
func (c *Session) CreateActions(params CreateActionGetParams) ([]string, error) {
var body ActionCreateResponse
var out []string
if err := c.Get("action.create", params, &body); err != nil {
return []string{""}, err
}
if (body.ActionIDs == nil) || (len(body.ActionIDs) == 0) {
return []string{""}, ErrNotFound
}
out = append(out, fmt.Sprint(body.ActionIDs[0]))
return out, nil
}
func (c *Session) DeleteAction(ActionIDs []string) ([]string, error) {
var body ActionDeleteResponse
var out []string
if err := c.Get("action.delete", ActionIDs, &body); err != nil {
return []string{""}, err
}
if (body.ActionIDs == nil) || (len(body.ActionIDs) == 0) {
return []string{""}, ErrNotFound
}
out = append(out, fmt.Sprint(body.ActionIDs[0]))
return out, nil
}