forked from claranet/go-zabbix-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
trigger.go
176 lines (154 loc) · 4.98 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
package zabbix
type (
// SeverityType of a trigger
// Zabbix severity see : https://www.zabbix.com/documentation/3.2/manual/api/reference/trigger/object
SeverityType int
)
const (
// Different severity see : https://www.zabbix.com/documentation/3.2/manual/config/triggers/severity
// NotClassified is Not classified severity
NotClassified SeverityType = 0
// Information is Information severity
Information SeverityType = 1
// Warning is Warning severity
Warning SeverityType = 2
// Average is Average severity
Average SeverityType = 3
// High is high severity
High SeverityType = 4
// Critical is critical severity
Critical SeverityType = 5
)
const (
// Enabled trigger status enabled
Enabled StatusType = 0
// Disabled trigger status disabled
Disabled StatusType = 1
)
const (
// Trigger value see : https://www.zabbix.com/documentation/3.2/manual/config/triggers
// OK trigger value ok
OK ValueType = 0
// Problem trigger value probleme
Problem ValueType = 1
)
// TriggerFunction The function objects represents the functions used in the trigger expression
type TriggerFunction struct {
FunctionID string `json:"functionid"`
ItemID string `json:"itemid"`
Function string `json:"function"`
Parameter string `json:"parameter"`
}
// TriggerFunctions is an array of TriggerFunction
type TriggerFunctions []TriggerFunction
// Trigger represent Zabbix trigger object
// https://www.zabbix.com/documentation/3.2/manual/api/reference/trigger/object
type Trigger struct {
TriggerID string `json:"triggerid,omitempty"`
Description string `json:"description"`
Expression string `json:"expression"`
Comments string `json:"comments"`
//TemplateId string `json:"templateid"`
//Value ValueType `json:""`
Priority SeverityType `json:"priority,string"`
Status StatusType `json:"status,string"`
Dependencies Triggers `json:"dependencies,omitempty"`
Functions TriggerFunctions `json:"functions,omitempty"`
// Items contained by the trigger in the items property.
ContainedItems Items `json:"items,omitempty"`
// Hosts that the trigger belongs to in the hosts property.
ParentHosts Hosts `json:"hosts,omitempty"`
}
// Triggers is an array of Trigger
type Triggers []Trigger
// TriggersGet Wrapper for trigger.get
// https://www.zabbix.com/documentation/3.2/manual/api/reference/trigger/get
func (api *API) TriggersGet(params Params) (res Triggers, err error) {
if _, present := params["output"]; !present {
params["output"] = "extend"
}
err = api.CallWithErrorParse("trigger.get", params, &res)
return
}
// TriggerGetByID Gets trigger by Id only if there is exactly 1 matching host.
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 {
e := ExpectedOneResult(len(triggers))
err = &e
return
}
res = &triggers[0]
return
}
// TriggersCreate Wrapper for trigger.create
// https://www.zabbix.com/documentation/3.2/manual/api/reference/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
}
// TriggersUpdate Wrapper for trigger.update
// https://www.zabbix.com/documentation/3.2/manual/api/reference/trigger/update
func (api *API) TriggersUpdate(triggers Triggers) (err error) {
_, err = api.CallWithError("trigger.update", triggers)
return
}
// TriggersDelete Wrapper for trigger.delete
// Cleans ItemId in all triggers elements if call succeed.
// https://www.zabbix.com/documentation/3.2/manual/api/reference/trigger/delete
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
}
// TriggersDeleteByIds Wrapper for trigger.delete
// https://www.zabbix.com/documentation/3.2/manual/api/reference/trigger/delete
func (api *API) TriggersDeleteByIds(ids []string) (err error) {
deleteIds, err := api.TriggersDeleteIDs(ids)
if err != nil {
return
}
l := len(deleteIds)
if len(ids) != l {
err = &ExpectedMore{len(ids), l}
}
return
}
// TriggersDeleteIDs Wrapper for trigger.delete
// return the id of the deleted trigger
func (api *API) TriggersDeleteIDs(ids []string) (triggerids []interface{}, err error) {
response, err := api.CallWithError("trigger.delete", ids)
if err != nil {
return
}
result := response.Result.(map[string]interface{})
triggerids1, ok := result["triggerids"].([]interface{})
if !ok {
triggerids2 := result["triggerids"].(map[string]interface{})
for _, id := range triggerids2 {
triggerids = append(triggerids, id)
}
} else {
triggerids = triggerids1
}
return
}