-
Notifications
You must be signed in to change notification settings - Fork 71
/
trigger_json.go
96 lines (80 loc) · 2.23 KB
/
trigger_json.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
package zabbix
import "fmt"
// jTrigger is a private map for the Zabbix API Trigger object.
// See: https://www.zabbix.com/documentation/3.4/manual/api/reference/trigger/object
type jTrigger struct {
TriggerID string `json:"triggerid"`
AlarmState int `json:"value,string"`
Description string `json:"description"`
Enabled string `json:"status"`
Expression string `json:"expression"`
Groups jHostgroups `json:"groups"`
Hosts jHosts `json:"hosts"`
LastChange int `json:"lastchange,string"`
Severity int `json:"priority,string"`
State int `json:"state,string"`
Tags jTriggerTags `json:"tags"`
LastEvent *jEvent `json:"lastEvent"`
URL string `json:"url"`
}
type jTriggerTag struct {
Name string `json:"tag"`
Value string `json:"value"`
}
func (c *jTriggerTag) Tag() (*TriggerTag, error) {
tag := &TriggerTag{}
tag.Name = c.Name
tag.Value = c.Value
return tag, nil
}
type jTriggerTags []jTriggerTag
func (c jTriggerTags) Tags() ([]TriggerTag, error) {
if c != nil {
tags := make([]TriggerTag, len(c))
for i, jTriggerTag := range c {
tag, err := jTriggerTag.Tag()
if err != nil {
return nil, fmt.Errorf("Error unmarshalling Trigger Tag %d in JSON data: %v", i, err)
}
tags[i] = *tag
}
return tags, nil
}
return nil, nil
}
// Trigger returns a native Go Trigger struct mapped from the given JSON Trigger data.
func (c *jTrigger) Trigger() (*Trigger, error) {
var err error
trigger := &Trigger{}
trigger.TriggerID = c.TriggerID
trigger.AlarmState = c.AlarmState
trigger.Enabled = (c.Enabled == "1")
trigger.Description = c.Description
trigger.Expression = c.Expression
trigger.URL = c.URL
if c.LastEvent != nil {
trigger.LastEvent, err = c.LastEvent.Event()
if err != nil {
return nil, err
}
}
// map groups
trigger.Groups, err = c.Groups.Hostgroups()
if err != nil {
return nil, err
}
// map hosts
trigger.Hosts, err = c.Hosts.Hosts()
if err != nil {
return nil, err
}
trigger.LastChange = c.LastChange
trigger.Severity = c.Severity
trigger.State = c.State
// map tags
trigger.Tags, err = c.Tags.Tags()
if err != nil {
return nil, err
}
return trigger, nil
}