forked from zabbix-tools/go-zabbix
-
Notifications
You must be signed in to change notification settings - Fork 2
/
alert.go
150 lines (118 loc) · 4.17 KB
/
alert.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
package zabbix
import (
"fmt"
"time"
)
const (
// AlertTypeMessage indicates that an Alert is a notification message.
AlertTypeMessage = iota
// AlertTypeRemoteCommand indicates that an Alert is a remote command call.
AlertTypeRemoteCommand
)
const (
// AlertMessageStatusNotSent indicates that an Alert of type
// AlertTypeMessage has not been sent yet.
AlertMessageStatusNotSent = iota
// AlertMessageStatusSent indicates that an Alert of type AlertTypeMessage
// has been sent successfully.
AlertMessageStatusSent
// AlertMessageStatusFailed indicates that an Alert of type AlertTypeMessage
// failed to send.
AlertMessageStatusFailed
)
const (
// AlertCommandStatusRun indicates that an Alert of type
// AlertTypeRemoteCommand has been run.
AlertCommandStatusRun = 1 + iota
// AlertCommandStatusAgentUnavailable indicates that an Alert of type
// AlertTypeRemoteCommand failed to run as the Zabbix Agent was unavailable.
AlertCommandStatusAgentUnavailable
)
// Alert represents a Zabbix Alert returned from the Zabbix API.
//
// See: https://www.zabbix.com/documentation/2.2/manual/config/notifications
type Alert struct {
// AlertID is the unique ID of the Alert.
AlertID string
// ActionID is the unique ID of the Action that generated this Alert.
ActionID string
// AlertType is the type of the Alert.
// AlertType must be one of the AlertType constants.
AlertType int
// Timestamp is the UTC timestamp at which the Alert was generated.
Timestamp time.Time
// ErrorText is the error message if there was a problem sending a message
// or running a remote command.
ErrorText string
// EscalationStep is the escalation step during which the Alert was
// generated.
EscalationStep int
// EventID is the unique ID of the Event that triggered this Action that
// generated this Alert.
EventID string
// MediaTypeID is the unique ID of the Media Type that was used to send this
// Alert if the AlertType is AlertTypeMessage.
MediaTypeID string
// Message is the Alert message body if AlertType is AlertTypeMessage.
Message string
// RetryCount is the number of times Zabbix tried to send a message.
RetryCount int
// Recipient is the end point address of a message if AlertType is
// AlertTypeMessage.
Recipient string
// Status indicates the outcome of executing the Alert.
//
// If AlertType is AlertTypeMessage, Status must be one of the
// AlertMessageStatus constants.
//
// If AlertType is AlertTypeRemoteCommand, Status must be one of the
// AlertCommandStatus constants.
Status int
// Subject is the Alert message subject if AlertType is AlertTypeMessage.
Subject string
// UserID is the unique ID of the User the Alert message was sent to.
UserID string
// Hosts is an array of Hosts that triggered this Alert.
//
// Hosts is only populated if AlertGetParams.SelectHosts is given in the
// query parameters that returned this Alert.
Hosts []Host
}
// AlertGetParams is query params for alert.get call
type AlertGetParams struct {
GetParameters
// SelectHosts causes all Hosts which triggered the Alert to be attached in
// the search results.
SelectHosts SelectQuery `json:"selectHosts,omitempty"`
// SelectMediaTypes causes the Media Types used for the Alert to be attached
// in the search results.
SelectMediaTypes SelectQuery `json:"selectMediatypes,omitempty"`
// SelectUsers causes all Users to which the Alert was addressed to be
// attached in the search results.
SelectUsers SelectQuery `json:"selectUsers,omitempty"`
}
// GetAlerts queries the Zabbix API for Alerts matching the given search
// parameters.
//
// ErrNotFound is returned if the search result set is empty.
// An error is returned if a transport, parsing or API error occurs.
func (c *Session) GetAlerts(params AlertGetParams) ([]Alert, error) {
alerts := make([]jAlert, 0)
err := c.Get("alert.get", params, &alerts)
if err != nil {
return nil, err
}
if len(alerts) == 0 {
return nil, ErrNotFound
}
// map JSON Alerts to Go Alerts
out := make([]Alert, len(alerts))
for i, jalert := range alerts {
alert, err := jalert.Alert()
if err != nil {
return nil, fmt.Errorf("Error mapping Alert %d in response: %v", i, err)
}
out[i] = *alert
}
return out, nil
}