forked from AlekSi/zabbix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
trigger.go
71 lines (60 loc) · 1.68 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
package zabbix
import "github.com/AlekSi/reflector"
type (
PriorityType int
)
const (
NotClassified PriorityType = 0
Information PriorityType = 1
Warning PriorityType = 2
Average PriorityType = 3
High PriorityType = 4
Disaster PriorityType = 5
)
const (
TriggerOk ValueType = 0
TriggerProblem ValueType = 1
)
type Trigger struct {
TriggerId string `json:"triggerid"`
Description string `json:"description"`
Expression string `json:"expression"`
Error string `json:"error"`
Hosts Hosts `json:"hosts,omitempty"`
Priority PriorityType `json:"priority"`
Value ValueType `json:"value"`
}
type Triggers []Trigger
// TriggersGet gets all triggers
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
}
res = make(Triggers, len(response.Result.([]interface{})))
for i, h := range response.Result.([]interface{}) {
h2 := h.(map[string]interface{})
reflector.MapToStruct(h2, &res[i], reflector.Strconv, "json")
if hosts, ok := h2["hosts"]; ok {
reflector.MapsToStructs2(hosts.([]interface{}), &res[i].Hosts, reflector.Strconv, "json")
}
}
return
}
// TriggerGetByID gets trigger by Id only if there is exactly 1 matching 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
}