forked from AlekSi/zabbix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrigger.go
196 lines (168 loc) · 5.73 KB
/
trigger.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
// triggers
package zabbix
import (
"fmt"
"strconv"
"github.com/wOvAN/reflector"
)
const (
// Trigger Selectors
/*
selectGroups query Return the host groups that the trigger belongs to in the groups property.
selectHosts query Return the hosts that the trigger belongs to in the hosts property.
selectItems query Return items contained by the trigger in the items property.
selectFunctions query Return functions used in the trigger in the functions property.
The function objects represents the functions used in the trigger expression and has the following properties:
functionid - (string) ID of the function;
itemid - (string) ID of the item used in the function;
function - (string) name of the function;
parameter - (string) parameter passed to the function.
selectDependencies query Return triggers that the trigger depends on in the dependencies property.
selectDiscoveryRule query Return the low-level discovery rule that created the trigger.
selectLastEvent query Return the last significant trigger event in the lastEvent property.
selectTags query Return the trigger tags in tags property.
*/
// Trigget expandors
expandComment = "expandComment"
expandDescription = "expandDescription"
expandExpression = "expandExpression"
)
// https://www.zabbix.com/documentation/4.0/manual/api/reference/trigger/object
type (
TriggerPriority interface{}
TriggerStatus interface{}
Trigger struct {
//string `json:",omitempty"`
TriggerId string `json:"triggerid,omitempty"`
Description string `json:"description"`
Expression string `json:"expression"`
Comments string `json:"comments,omitempty"`
Priority TriggerPriority `json:"priority,omitempty"`
Status TriggerStatus `json:"status,omitempty"`
// Recovery_mode recovery_mode
Recovery_expression string `json:"recovery_expression,omitempty"`
// Correlation_mode correlation_mode
Correlation_tag string `json:"correlation_tag,omitempty"`
// Extended fields
}
Triggers []Trigger
TriggerId struct {
TriggerId string `json:"triggerid"`
}
TriggerIds []TriggerId
)
var (
// Priorities
TriggerPriorityDefault TriggerPriority = 0
TriggerPriorityInformation TriggerPriority = 1
TriggerPriorityWarning TriggerPriority = 2
TriggerPriorityAverage TriggerPriority = 3
TriggerPriorityHigh TriggerPriority = 4
TriggerPriorityDisaster TriggerPriority = 5
// Status
TriggerStatusEnabled TriggerStatus = 0
TriggerStatusDisabled TriggerStatus = 0
)
func TriggerPriorityToText(aTriggerPriority TriggerPriority) string {
v, e := strconv.Atoi(string(aTriggerPriority.(string)))
if e != nil {
return e.Error()
}
switch v {
case TriggerPriorityDefault:
return "Default"
case TriggerPriorityInformation:
return "Information"
case TriggerPriorityWarning:
return "Warning"
case TriggerPriorityAverage:
return "Average"
case TriggerPriorityHigh:
return "High"
case TriggerPriorityDisaster:
return "Disaster"
default:
return "Unknown (" + fmt.Sprintf("%s", aTriggerPriority) + ")"
}
}
func TriggerStatusToText(aTriggerStatus TriggerStatus) string {
v, e := strconv.Atoi(string(aTriggerStatus.(string)))
if e != nil {
return e.Error()
}
switch v {
case TriggerStatusEnabled:
return "Enabled"
case TriggerStatusDisabled:
return "Disabled"
default:
return "Unknown (" + fmt.Sprintf("%s", aTriggerStatus) + ")"
}
}
// Wrapper for trigger.get: https://www.zabbix.com/documentation/4.0/manual/api/reference/trigger/get
func (api *API) TriggersGet(params Params) (res Triggers, err error) {
if _, present := params["output"]; !present {
params["output"] = "extend"
}
response, err := api.CallWithError("trigger.get", params)
if err != nil {
return
}
reflector.MapsToStructs2(response.Result.([]interface{}), &res, reflector.Strconv, "json")
return
}
// Gets host trigger by Id only if there is exactly 1 matching host trigger.
func (api *API) TriggerGetById(id string) (res *Trigger, err error) {
triggers, err := api.TriggersGet(Params{"triggerids": id})
if err != nil {
return
}
if len(triggers) == 1 {
res = &triggers[0]
} else {
e := ExpectedOneResult(len(triggers))
err = &e
}
return
}
// Wrapper for trigger.create: https://www.zabbix.com/documentation/2.2/manual/appendix/api/trigger/create
func (api *API) TriggersCreate(triggers Triggers) (err error) {
response, err := api.CallWithError("trigger.create", triggers)
if err != nil {
return
}
result := response.Result.(map[string]interface{})
triggerids := result["triggerids"].([]interface{})
for i, id := range triggerids {
triggers[i].TriggerId = id.(string)
}
return
}
// Wrapper for trigger.delete: https://www.zabbix.com/documentation/2.2/manual/appendix/api/trigger/delete
// Cleans TriggerId in all triggers elements if call succeed.
func (api *API) TriggersDelete(triggers Triggers) (err error) {
ids := make([]string, len(triggers))
for i, trigger := range triggers {
ids[i] = trigger.TriggerId
}
err = api.TriggersDeleteByIds(ids)
if err == nil {
for i := range triggers {
triggers[i].TriggerId = ""
}
}
return
}
// Wrapper for trigger.delete: https://www.zabbix.com/documentation/2.2/manual/appendix/api/trigger/delete
func (api *API) TriggersDeleteByIds(ids []string) (err error) {
response, err := api.CallWithError("trigger.delete", ids)
if err != nil {
return
}
result := response.Result.(map[string]interface{})
triggerids := result["triggerids"].([]interface{})
if len(ids) != len(triggerids) {
err = &ExpectedMore{len(ids), len(triggerids)}
}
return
}