forked from zabbix-tools/go-zabbix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
alert.go
138 lines (108 loc) · 4.24 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
package zabbix
import (
"github.com/fabiang/go-zabbix/types"
)
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 `json:"alertid"`
// ActionID is the unique ID of the Action that generated this Alert.
ActionID string `json:"actionid"`
// AlertType is the type of the Alert.
// AlertType must be one of the AlertType constants.
AlertType int `json:"alerttype,string"`
// Timestamp is the UTC timestamp at which the Alert was generated.
Timestamp types.ZBXUnixTimestamp `json:"clock,string"`
// ErrorText is the error message if there was a problem sending a message
// or running a remote command.
ErrorText string `json:"error"`
// EscalationStep is the escalation step during which the Alert was
// generated.
EscalationStep int `json:"esc_step,string"`
// EventID is the unique ID of the Event that triggered this Action that
// generated this Alert.
EventID string `json:"eventid"`
// MediaTypeID is the unique ID of the Media Type that was used to send this
// Alert if the AlertType is AlertTypeMessage.
MediaTypeID string `json:"mediatypeid"`
// Message is the Alert message body if AlertType is AlertTypeMessage.
Message string `json:"message"`
// RetryCount is the number of times Zabbix tried to send a message.
RetryCount int `json:"retries,string"`
// Recipient is the end point address of a message if AlertType is
// AlertTypeMessage.
Recipient string `json:"sendto"`
// 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 `json:"status,string"`
// Subject is the Alert message subject if AlertType is AlertTypeMessage.
Subject string `json:"subject"`
// UserID is the unique ID of the User the Alert message was sent to.
UserID string `json:"userid"`
// 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 `json:"hosts"`
}
// 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([]Alert, 0)
err := c.Get("alert.get", params, &alerts)
if err != nil {
return nil, err
}
if len(alerts) == 0 {
return nil, ErrNotFound
}
return alerts, nil
}