-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser_alerts.php
311 lines (252 loc) · 8.19 KB
/
user_alerts.php
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
<?php
/**
* @var $zabbixApiUrl string
* @var $zabbixAuth string
*/
include dirname(__FILE__).'/config.priv.php';
require_once dirname(__FILE__).'/lib_zabbixApi.php';
require_once dirname(__FILE__).'/lib_arrHelper.php';
$zabbix=new zabbixApi();
$zabbix->init($zabbixApiUrl,$zabbixAuth,[]);
$login='kotov.n';
$user=$zabbix->searchUserByLogin($login)[0]??[];
//var_dump($user);
if (!$user['userid']??false) die("User $login not found");
$username=$user['surname'];
if (!$user['surname']??false) die("User $login has empty surname!");
//mediatypeid:
// 0 - email
// 6 - sms скрипт
// 8 - telegram
$mail_message=new stdClass();
$mail_message->mediatypeid=1;
$sms_message=new stdClass();
$sms_message->mediatypeid=6;
function formOperation($step_len,$step,$message,$user) {
$addr_user=new stdClass();
$addr_user->userid=$user['userid'];
$operation=new stdClass();
$operation->operationtype=0; //0 - "send message".
$operation->esc_step_from=$step; //60m
$operation->esc_step_to=$step; //60m
$operation->opmessage=$message;
$operation->opmessage_usr=[$addr_user];
$operation->esc_period=$step_len;//Default operation step duration.
return $operation;
}
//https://www.zabbix.com/documentation/current/en/manual/api/reference/action/object#action-operation
$operation_mail_10m= formOperation('10m',2,$mail_message,$user);
$operation_mail_120m=formOperation('10m',13,$mail_message,$user);
$operation_sms_15m= formOperation('15m',2,$sms_message,$user);
$operation_sms_60m= formOperation('10m',7,$sms_message,$user);
//$operation_mail_60m
//https://www.zabbix.com/documentation/current/en/manual/api/reference/action/object#action-filter-condition
/* Type of condition.
Possible values if eventsource of Action object is set to "event created by a trigger":
0 - host group;
1 - host;
2 - trigger;
3 - trigger name;
4 - trigger severity;
6 - time period;
13 - host template;
16 - problem is suppressed;
25 - event tag;
26 - event tag value.
Possible values if eventsource of Action object is set to "event created by a discovery rule":
7 - host IP;
8 - discovered service type;
9 - discovered service port;
10 - discovery status;
11 - uptime or downtime duration;
12 - received value;
18 - discovery rule;
19 - discovery check;
20 - proxy;
21 - discovery object.
Possible values if eventsource of Action object is set to "event created by active agent autoregistration":
20 - proxy;
22 - host name;
24 - host metadata.
Possible values if eventsource of Action object is set to "internal event":
0 - host group;
1 - host;
13 - host template;
23 - event type;
25 - event tag;
26 - event tag value.
Possible values if eventsource of Action object is set to "event created on service status update":
25 - event tag;
26 - event tag value;
27 - service;
28 - service name.
*/
/* Severity of the trigger.
Possible values:
0 - (default) not classified;
1 - information;
2 - warning;
3 - average;
4 - high;
5 - disaster.
*/
/* Condition operator.
Possible values:
0 - (default) equals;
1 - does not equal;
2 - contains;
3 - does not contain;
4 - in;
5 - is greater than or equals;
6 - is less than or equals;
7 - not in;
8 - matches;
9 - does not match;
10 - Yes;
11 - No.
*/
//problem is not suppressed
$condition['A']=new stdClass();
$condition['A']->conditiontype=16; //16 - problem is suppressed;
$condition['A']->operator=11; //11 - No
$condition['A']->formulaid='A';
$notSuppressed='A';
//severity is disaster
$condition['B']=new stdClass();
$condition['B']->conditiontype=4; //4 - trigger severity;
$condition['B']->value=5; //5 - disaster.
$condition['B']->operator=0; //0 - equals
$condition['B']->formulaid='B';
$notDisaster='B';
//severity is NOT disaster
$condition['C']=new stdClass();
$condition['C']->conditiontype=4; //4 - trigger severity;
$condition['C']->value=5; //5 - disaster.
$condition['C']->operator=1; //1 - does not equal;
$condition['C']->formulaid='C';
$isDisaster='C';
//Tag does not equal supportteam
$condition['D']=new stdClass();
$condition['D']->conditiontype=25; //25 - event tag;
$condition['D']->value='supportteam';
$condition['D']->operator=1; //1 - does not equal;
$condition['D']->formulaid='D';
$noSupportTeam='D';
//Value of tag supportteam contains Акаев
$condition['E']=new stdClass();
$condition['E']->conditiontype=26; //26 - event tag value;
$condition['E']->value2='supportteam';
$condition['E']->value=$username;
$condition['E']->operator=2; //contains
$condition['E']->formulaid='E';
$isSupportTeam='E';
//Value of tag node-support contains Акаев
$condition['F']=new stdClass();
$condition['F']->conditiontype=26; //26 - event tag value;
$condition['F']->value2='node-support';
$condition['F']->value=$username;
$condition['F']->operator=2; //contains
$condition['F']->formulaid='F';
$isNodeSupport='(D and F)';
//Tag does not equal serviceman
$condition['G']=new stdClass();
$condition['G']->conditiontype=25; //25 - event tag;
$condition['G']->value='serviceman';
$condition['G']->operator=1; //1 - does not equal;
$condition['G']->formulaid='G';
$noServiceMan='G';
//Value of tag serviceman contains Акаев
$condition['H']=new stdClass();
$condition['H']->conditiontype=26; //26 - event tag value;
$condition['H']->value2='serviceman';
$condition['H']->value=$username;
$condition['H']->operator=2; //contains
$condition['H']->formulaid='H';
$isServiceMan='H';
//Value of tag node-service contains Акаев
$condition['I']=new stdClass();
$condition['I']->conditiontype=26; //26 - event tag value;
$condition['I']->value2='node-service';
$condition['I']->value=$username;
$condition['I']->operator=2; //contains
$condition['I']->formulaid='I';
$isNodeService='I';
//Value of tag alert contains Акаев
$condition['J']=new stdClass();
$condition['J']->conditiontype=26; //26 - event tag value;
$condition['J']->value2='alert';
$condition['J']->value=$username;
$condition['J']->operator=2; //contains
$condition['J']->formulaid='J';
$isAlert='J';
//Value of tag alert contains смсАкаев
$condition['K']=new stdClass();
$condition['K']->conditiontype=26; //26 - event tag value;
$condition['K']->value2='alert';
$condition['K']->value="смс$username";
$condition['K']->operator=2; //contains
$condition['K']->formulaid='K';
$isSmsAlert='K';
function fetchActionId($name) {
global $zabbix;
$exist=$zabbix->searchActionByName($name);
if (!count($exist)) {
return null;
//die ("$name not found");
}
return $exist[0]['actionid'];
}
function formCondition($formula) {
global $condition;
//https://www.zabbix.com/documentation/current/en/manual/api/reference/action/object#action-filter
$filter=new stdClass();
$filter->evaltype=3;//3 - custom expression.
$filter->formula=$formula;
$filter->conditions=[];
$letters=preg_replace('/[^A-Z]+/','',$formula);
foreach (str_split($letters) as $letter)
$filter->conditions[]=$condition[$letter];
return $filter;
}
function formAction($name,$operation,$formula) {
$action=new stdClass();
$action->name=$name;
$action->eventsource=0; //0 - event created by a trigger;
$action->notify_if_canceled=0;
$action->filter=formCondition($formula);
$action->operations=[$operation];
if ($actionid=fetchActionId($name))
$action->actionid=$actionid;
return $action;
}
$except_sms=formAction(
"$username - кроме смс",
$operation_mail_10m,
"$notSuppressed and ($isServiceMan or $isNodeService or $isAlert)"
);
$tp_except_sms=formAction(
"$username - ТП кроме смс",
$operation_mail_120m,
"$notSuppressed and ($isSupportTeam or $isNodeSupport)"
);
$sms=formAction(
"$username - смс",
$operation_sms_15m,
"($notSuppressed and $isDisaster) and ($isServiceMan or $isNodeService or $isSmsAlert)"
);
$tp_sms=formAction(
"$username - ТП смс",
$operation_sms_60m,
"($notSuppressed and $isDisaster) and ($isSupportTeam or $isNodeSupport)"
);
function pushAction($action) {
global $zabbix;
if ($action->actionid??false)
$zabbix->req('action.update',$action);
else
$zabbix->req('action.create',$action);
}
pushAction($except_sms);
pushAction($tp_except_sms);
pushAction($sms);
pushAction($tp_sms);