-
Notifications
You must be signed in to change notification settings - Fork 0
/
action.go
206 lines (194 loc) · 5.47 KB
/
action.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package zabbix
import (
"encoding/json"
"fmt"
"net/http"
"strings"
)
var (
actionGetTemplate = `{
"jsonrpc":"2.0",
"method":"action.get",
"params":{
"output":"extend",
"selectOperations":"extend",
"selectRecoveryOperations":"extend",
"selectFilter":"extend",
"filter":{
"eventsource":0,
"name":"%v"
}
},
"auth":"%v",
"id":%v
}`
actionDeleteTemplate = `{
"jsonrpc": "2.0",
"method": "action.delete",
"params": [
%v
],
"auth": "%v",
"id": %v
}`
actionOperationTemplate = `{
"operationtype": "0",
"esc_period": "%v",
"esc_step_from": "1",
"esc_step_to": "%v",
"evaltype": "0",
"opconditions": [
{
"conditiontype":14,
"value":0
}
],
"opmessage": {
"default_msg": "1",
"subject": "Problem: {EVENT.NAME}",
"message": "{EVENT.DATE} {EVENT.TIME}#{EVENT.NAME}#{EVENT.ID}#{ITEM.VALUE}#{ITEM.NAME}",
"mediatypeid": "%v"
},
"opmessage_grp": [],
"opmessage_usr": [
%v
]
}`
actionRecoveryOperationsTemplate = `{
"operationtype": "0",
"evaltype": "0",
"opconditions": [],
"opmessage": {
"operationid": "14",
"default_msg": "1",
"subject": "Resolved: {EVENT.NAME}",
"message": "{EVENT.RECOVERY.DATE} {EVENT.RECOVERY.TIME}#{EVENT.NAME}#{EVENT.ID}#{ITEM.VALUE}#{ITEM.NAME}",
"mediatypeid": "%v"
},
"opmessage_grp": [],
"opmessage_usr": [
%v
]
}`
actionPostTemplate = `{
"jsonrpc": "2.0",
"method": "action.create",
"params": {
"name": "%v",
"eventsource": "0",
"status": "0",
"esc_period": "%v",
"def_shortdata": "Problem: {EVENT.NAME}",
"def_longdata": "告警触发: \r\nEventID: {EVENT.ID}\r\nStartAt: {EVENT.TIME} {EVENT.DATE}\r\n{TRIGGER.DESCRIPTION}\r\n{TRIGGER.NAME}: {ITEM.VALUE}",
"r_shortdata": "Resolved: {EVENT.NAME}",
"r_longdata": "告警恢复: \r\nEventID: {EVENT.ID}\r\nStartAt: {EVENT.TIME} {EVENT.DATE}\r\n{TRIGGER.DESCRIPTION}\r\n{TRIGGER.NAME}: {ITEM.VALUE}",
"pause_suppressed": "1",
"ack_shortdata": "Updated problem: {EVENT.NAME}",
"ack_longdata": "{USER.FULLNAME} {EVENT.UPDATE.ACTION} problem at {EVENT.UPDATE.DATE} {EVENT.UPDATE.TIME}.\r\n{EVENT.UPDATE.MESSAGE}\r\n\r\nCurrent problem status is {EVENT.STATUS}, acknowledged: {EVENT.ACK.STATUS}.",
"filter": {
"evaltype": "0",
"conditions": [{
"conditiontype": "3",
"operator": "2",
"value": "%v"
}]
},
"operations": [%v],
"recovery_operations": [%v]
},
"auth": "%v",
"id": %v
}`
)
// ActionCreate create zabbix action
// toWX key mediatypeid of weixin value is admin weixin user, toEmail mediatypeid of emal value of the user email
func (api *API) ActionCreate(name, interval, StepDuration string, alertNum int32, mediatypeidUserIDS map[string]string) error {
operations := []string{}
recoveryPperations := []string{}
for mediatypeid, userIDS := range mediatypeidUserIDS {
users := ""
ids := strings.Split(userIDS, ",")
for index, id := range ids {
if index == 0 {
users += fmt.Sprintf(`{"operationid":"14","userid":"%v"}`, id)
} else {
users += "," + fmt.Sprintf(`{"operationid":"14","userid":"%v"}`, id)
}
}
operations = append(operations, fmt.Sprintf(actionOperationTemplate, StepDuration, alertNum, mediatypeid, users))
recoveryPperations = append(recoveryPperations, fmt.Sprintf(actionRecoveryOperationsTemplate, mediatypeid, users))
}
payload := strings.NewReader(fmt.Sprintf(actionPostTemplate, name, interval, name, strings.Join(operations, ","), strings.Join(recoveryPperations, ","), api.Session, api.ID))
req, err := http.NewRequest("POST", api.URL, payload)
if err != nil {
return err
}
req.Header.Add("content-type", "application/json")
res, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer res.Body.Close()
if res.StatusCode != 200 {
return fmt.Errorf("zabbix api return response code %v", res.StatusCode)
}
result := map[string]interface{}{}
if err = json.NewDecoder(res.Body).Decode(&result); err != nil {
return err
}
if errmsg, ok := result["error"]; ok {
return fmt.Errorf("%v", errmsg)
}
return nil
}
// ActionDelete delete zabbix action
func (api *API) ActionDelete(ids []string) error {
names := ""
for _, id := range ids {
names += fmt.Sprintf(`"%v"`, id)
}
payload := strings.NewReader(fmt.Sprintf(actionDeleteTemplate, names, api.Session, api.ID))
req, err := http.NewRequest("POST", api.URL, payload)
if err != nil {
return err
}
req.Header.Add("content-type", "application/json")
res, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer res.Body.Close()
result := map[string]interface{}{}
if err = json.NewDecoder(res.Body).Decode(&result); err != nil {
return err
}
if errmsg, ok := result["error"]; ok {
return fmt.Errorf("%v", errmsg)
}
return nil
}
// ActionGet get zabbix action
func (api *API) ActionGet(name string) (map[string]interface{}, error) {
payload := strings.NewReader(fmt.Sprintf(actionGetTemplate, name, api.Session, api.ID))
req, err := http.NewRequest("POST", api.URL, payload)
if err != nil {
return nil, err
}
req.Header.Add("content-type", "application/json")
res, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
data := map[string]interface{}{}
if err = json.NewDecoder(res.Body).Decode(&data); err != nil {
return nil, err
}
if errmsg, ok := data["error"]; ok {
return nil, fmt.Errorf("%v", errmsg)
}
if len(data["result"].([]interface{})) != 0 {
return data["result"].([]interface{})[0].(map[string]interface{}), nil
}
return nil, nil
}