forked from AlekSi/zabbix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevent.go
110 lines (95 loc) · 3.6 KB
/
event.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
package zabbix
import "github.com/AlekSi/reflector"
type (
ObjectType int
SourceType int
EventValueType int
)
const (
ObjectTrigger ObjectType = 0
ObjectDiscoveredHost ObjectType = 1
ObjectDiscoveredService ObjectType = 2
ObjectAutoRegisteredHost ObjectType = 3
ObjectItem ObjectType = 4
ObjectLLDRule ObjectType = 5
)
const (
SourceTrigger SourceType = 0
SourceDiscoveryRule SourceType = 1
SourceActiveAgent SourceType = 2
SourceInternal SourceType = 3
)
const (
SourceTriggerOK EventValueType = 0
SourceTriggerProblem EventValueType = 1
SourceDiscoveryHostUp EventValueType = 0
SourceDiscoveryHostDown EventValueType = 1
SourceDiscoveryHostDiscovered EventValueType = 2
SourceDiscoveryHostLost EventValueType = 3
SourceInternalNormal EventValueType = 0
SourceInternalUnknown EventValueType = 1
)
// Acknowledge struct from https://www.zabbix.com/documentation/2.4/manual/api/reference/event/get?s[]=acknowledgeid
type Acknowledge struct {
AcknowledgeId string `json:"acknowledgeid"`
Alias string `json:"alias,omitempty"`
Clock int64 `json:"clock"`
EventId string `json:"eventid"`
Message string `json:"message"`
Name string `json:"name,omitempty"`
SurName string `json:"surname,omitempty"`
UserId string `json:"userid,omitempty"`
}
type Acknowledges []Acknowledge
// Event struct from https://www.zabbix.com/documentation/2.4/manual/api/reference/event/object
type Event struct {
Acknowledged int `json:"acknowledged"`
Acknowledges Acknowledges `json:"acknowledges,omitempty"`
Clock int64 `json:"clock"`
EventId string `json:"eventid"`
Ns int64 `json:"ns"`
Object ObjectType `json:"object"`
ObjectId string `json:"objectid"`
Source SourceType `json:"source"`
Value ValueType `json:"value"`
Triggers Triggers `json:"triggers,omitempty"`
}
type Events []Event
// EventsGet gets all events https://www.zabbix.com/documentation/2.4/manual/api/reference/event/get
func (api *API) EventsGet(params Params) (res Events, err error) {
if _, present := params["output"]; !present {
params["output"] = "extend"
}
response, err := api.CallWithError("event.get", params)
if err != nil {
return
}
res = make(Events, 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 triggers, ok := h2["triggers"]; ok {
reflector.MapsToStructs2(triggers.([]interface{}), &res[i].Triggers, reflector.Strconv, "json")
}
if acknowledges, ok := h2["acknowledges"]; ok {
reflector.MapsToStructs2(acknowledges.([]interface{}), &res[i].Acknowledges, reflector.Strconv, "json")
}
}
return
}
// EventsGetByID gets an event by item ID
func (api *API) EventsGetByID(id string) (res Events, err error) {
return api.EventsGet(Params{"eventids": id, "select_acknowledges": "extend"})
}
// EventsGetByTriggerID gets an event by item ID, default source = 0 (triggers)
func (api *API) EventsGetByTriggerID(id string) (res Events, err error) {
return api.EventsGet(Params{"objectids": id})
}
// EventsAckByID acknowledges event using id and text message - https://www.zabbix.com/documentation/2.4/manual/api/reference/event/acknowledge
func (api *API) EventsAckByID(id string, message string) (err error) {
_, err = api.CallWithError("event.acknowledge", Params{"eventids": id, "message": message})
if err != nil {
return
}
return
}