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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
|
/*
** Zabbix
** Copyright (C) 2001-2021 Zabbix SIA
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**/
#include "common.h"
#include "db.h"
#include "zbxtasks.h"
#include "log.h"
#include "zbxserver.h"
#include "postinit.h"
//#include "valuecache.h"
#define ZBX_HIST_MACRO_NONE (-1)
#define ZBX_HIST_MACRO_ITEM_VALUE 0
#define ZBX_HIST_MACRO_ITEM_LASTVALUE 1
/******************************************************************************
* *
* Function: get_trigger_count *
* *
* Purpose: gets the total number of triggers on system *
* *
* Parameters: *
* *
* Return value: The total number of triggers on system. *
* *
******************************************************************************/
static int get_trigger_count(void)
{
DB_RESULT result;
DB_ROW row;
int triggers_num;
result = DBselect("select count(*) from triggers");
if (NULL != (row = DBfetch(result)))
triggers_num = atoi(row[0]);
else
triggers_num = 0;
DBfree_result(result);
return triggers_num;
}
/******************************************************************************
* *
* Function: is_historical_macro *
* *
* Purpose: checks if this is historical macro that cannot be expanded for *
* bulk event name update *
* *
* Parameters: macro - [IN] the macro name *
* *
* Return value: ZBX_HIST_MACRO_* defines *
* *
******************************************************************************/
static int is_historical_macro(const char *macro)
{
if (0 == strncmp(macro, "ITEM.VALUE", ZBX_CONST_STRLEN("ITEM.VALUE")))
return ZBX_HIST_MACRO_ITEM_VALUE;
if (0 == strncmp(macro, "ITEM.LASTVALUE", ZBX_CONST_STRLEN("ITEM.LASTVALUE")))
return ZBX_HIST_MACRO_ITEM_LASTVALUE;
return ZBX_HIST_MACRO_NONE;
}
/******************************************************************************
* *
* Function: convert_historical_macro *
* *
* Purpose: translates historical macro to lld macro format *
* *
* Parameters: macro - [IN] the macro type (see ZBX_HIST_MACRO_* defines) *
* *
* Return value: the macro *
* *
* Comments: Some of the macros can be converted to different name. *
* *
******************************************************************************/
static const char *convert_historical_macro(int macro)
{
/* When expanding macros for old events ITEM.LASTVALUE macro would */
/* always expand to one (latest) value. Expanding it as ITEM.VALUE */
/* makes more sense in this case. */
const char *macros[] = {"#ITEM.VALUE", "#ITEM.VALUE"};
return macros[macro];
}
/******************************************************************************
* *
* Function: preprocess_trigger_name *
* *
* Purpose: pre-process trigger name(description) by expanding non historical *
* macros *
* *
* Parameters: trigger - [IN] the trigger *
* historical - [OUT] 1 - trigger name contains historical macros *
* 0 - otherwise *
* *
* Comments: Some historical macros might be replaced with other macros to *
* better match the trigger name at event creation time. *
* *
******************************************************************************/
static void preprocess_trigger_name(DB_TRIGGER *trigger, int *historical)
{
int pos = 0, macro_len, macro_type;
zbx_token_t token;
size_t name_alloc, name_len, replace_alloc = 64, replace_offset, r, l;
char *replace;
const char *macro;
DB_EVENT event;
*historical = FAIL;
replace = (char *)zbx_malloc(NULL, replace_alloc);
name_alloc = name_len = strlen(trigger->description) + 1;
while (SUCCEED == zbx_token_find(trigger->description, pos, &token, ZBX_TOKEN_SEARCH_BASIC))
{
if (ZBX_TOKEN_MACRO == token.type || ZBX_TOKEN_FUNC_MACRO == token.type)
{
/* the macro excluding the opening and closing brackets {}, for example: ITEM.VALUE */
if (ZBX_TOKEN_MACRO == token.type)
{
l = token.data.macro.name.l;
r = token.data.macro.name.r;
}
else
{
l = token.data.func_macro.macro.l + 1;
r = token.data.func_macro.macro.r - 1;
}
macro = trigger->description + l;
if (ZBX_HIST_MACRO_NONE != (macro_type = is_historical_macro(macro)))
{
if (0 != isdigit(*(trigger->description + r)))
macro_len = r - l;
else
macro_len = r - l + 1;
macro = convert_historical_macro(macro_type);
token.loc.r += zbx_replace_mem_dyn(&trigger->description, &name_alloc, &name_len, l,
macro_len, macro, strlen(macro));
*historical = SUCCEED;
}
}
pos = token.loc.r;
}
memset(&event, 0, sizeof(DB_EVENT));
event.object = EVENT_OBJECT_TRIGGER;
event.objectid = trigger->triggerid;
event.trigger = *trigger;
substitute_simple_macros(NULL, &event, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
&trigger->description, MACRO_TYPE_TRIGGER_DESCRIPTION, NULL, 0);
if (SUCCEED == *historical)
{
pos = 0;
name_alloc = name_len = strlen(trigger->description) + 1;
while (SUCCEED == zbx_token_find(trigger->description, pos, &token, ZBX_TOKEN_SEARCH_BASIC))
{
if (ZBX_TOKEN_LLD_MACRO == token.type || ZBX_TOKEN_LLD_FUNC_MACRO == token.type)
{
if (ZBX_TOKEN_LLD_MACRO == token.type)
{
l = token.data.lld_macro.name.l;
r = token.data.lld_macro.name.r;
}
else
{
l = token.data.lld_func_macro.macro.l + 2;
r = token.data.lld_func_macro.macro.r - 1;
}
macro = trigger->description + l;
if (ZBX_HIST_MACRO_NONE != is_historical_macro(macro))
{
macro_len = r - l + 1;
replace_offset = 0;
zbx_strncpy_alloc(&replace, &replace_alloc, &replace_offset, macro, macro_len);
token.loc.r += zbx_replace_mem_dyn(&trigger->description, &name_alloc,
&name_len, l - 1, macro_len + 1, replace, replace_offset);
}
}
pos = token.loc.r;
}
}
zbx_free(replace);
}
/******************************************************************************
* *
* Function: process_event_bulk_update *
* *
* Purpose: update event/problem names for a trigger with bulk request *
* *
* Parameters: trigger - [IN] the trigger *
* sql - [IN/OUT] the sql query *
* sql_alloc - [IN/OUT] the sql query size *
* sql_offset - [IN/OUT] the sql query length *
* *
* Return value: SUCCEED - the update was successful *
* FAIL - otherwise *
* *
* Comments: Event names for triggers without historical macros will be the *
* same and can be updated with a single sql query. *
* *
******************************************************************************/
static int process_event_bulk_update(const DB_TRIGGER *trigger, char **sql, size_t *sql_alloc, size_t *sql_offset)
{
char *name_esc;
int ret;
name_esc = DBdyn_escape_string_len(trigger->description, EVENT_NAME_LEN);
zbx_snprintf_alloc(sql, sql_alloc, sql_offset,
"update events"
" set name='%s'"
" where source=%d"
" and object=%d"
" and objectid=" ZBX_FS_UI64 ";\n",
name_esc, EVENT_SOURCE_TRIGGERS, EVENT_OBJECT_TRIGGER, trigger->triggerid);
if (SUCCEED == (ret = DBexecute_overflowed_sql(sql, sql_alloc, sql_offset)))
{
zbx_snprintf_alloc(sql, sql_alloc, sql_offset,
"update problem"
" set name='%s'"
" where source=%d"
" and object=%d"
" and objectid=" ZBX_FS_UI64 ";\n",
name_esc, EVENT_SOURCE_TRIGGERS, EVENT_OBJECT_TRIGGER, trigger->triggerid);
ret = DBexecute_overflowed_sql(sql, sql_alloc, sql_offset);
}
zbx_free(name_esc);
return ret;
}
/******************************************************************************
* *
* Function: process_event_update *
* *
* Purpose: update event/problem names for a trigger with separate requests *
* for each event *
* *
* Parameters: trigger - [IN] the trigger *
* sql - [IN/OUT] the sql query *
* sql_alloc - [IN/OUT] the sql query size *
* sql_offset - [IN/OUT] the sql query length *
* *
* Return value: SUCCEED - the update was successful *
* FAIL - otherwise *
* *
* Comments: Event names for triggers with historical macros might differ and *
* historical macros in trigger name must be expanded for each *
* event. *
* *
******************************************************************************/
static int process_event_update(const DB_TRIGGER *trigger, char **sql, size_t *sql_alloc, size_t *sql_offset)
{
DB_RESULT result;
DB_ROW row;
DB_EVENT event;
char *name, *name_esc;
int ret = SUCCEED;
memset(&event, 0, sizeof(DB_EVENT));
result = DBselect("select eventid,source,object,objectid,clock,value,acknowledged,ns,name"
" from events"
" where source=%d"
" and object=%d"
" and objectid=" ZBX_FS_UI64
" order by eventid",
EVENT_SOURCE_TRIGGERS, EVENT_OBJECT_TRIGGER, trigger->triggerid);
while (SUCCEED == ret && NULL != (row = DBfetch(result)))
{
ZBX_STR2UINT64(event.eventid, row[0]);
event.source = atoi(row[1]);
event.object = atoi(row[2]);
ZBX_STR2UINT64(event.objectid, row[3]);
event.clock = atoi(row[4]);
event.value = atoi(row[5]);
event.acknowledged = atoi(row[6]);
event.ns = atoi(row[7]);
event.name = row[8];
event.trigger = *trigger;
name = zbx_strdup(NULL, trigger->description);
substitute_simple_macros(NULL, &event, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
&name, MACRO_TYPE_TRIGGER_DESCRIPTION, NULL, 0);
name_esc = DBdyn_escape_string_len(name, EVENT_NAME_LEN);
zbx_snprintf_alloc(sql, sql_alloc, sql_offset,
"update events"
" set name='%s'"
" where eventid=" ZBX_FS_UI64 ";\n",
name_esc, event.eventid);
if (SUCCEED == (ret = DBexecute_overflowed_sql(sql, sql_alloc, sql_offset)))
{
zbx_snprintf_alloc(sql, sql_alloc, sql_offset,
"update problem"
" set name='%s'"
" where eventid=" ZBX_FS_UI64 ";\n",
name_esc, event.eventid);
ret = DBexecute_overflowed_sql(sql, sql_alloc, sql_offset);
}
zbx_free(name_esc);
zbx_free(name);
}
DBfree_result(result);
return ret;
}
/******************************************************************************
* *
* Function: update_event_names *
* *
* Purpose: update event names in events and problem tables *
* *
* Return value: SUCCEED - the update was successful *
* FAIL - otherwise *
* *
******************************************************************************/
static int update_event_names(void)
{
DB_RESULT result;
DB_ROW row;
DB_TRIGGER trigger;
int ret = SUCCEED, historical, triggers_num, processed_num = 0, completed, last_completed = 0;
char *sql;
size_t sql_alloc = 4096, sql_offset = 0;
zabbix_log(LOG_LEVEL_WARNING, "starting event name update forced by database upgrade");
if (0 == (triggers_num = get_trigger_count()))
goto out;
memset(&trigger, 0, sizeof(DB_TRIGGER));
sql = (char *)zbx_malloc(NULL, sql_alloc);
DBbegin_multiple_update(&sql, &sql_alloc, &sql_offset);
result = DBselect(
"select triggerid,description,expression,priority,comments,url,recovery_expression,"
"recovery_mode,value"
" from triggers"
" order by triggerid");
while (SUCCEED == ret && NULL != (row = DBfetch(result)))
{
ZBX_STR2UINT64(trigger.triggerid, row[0]);
trigger.description = zbx_strdup(NULL, row[1]);
trigger.expression = zbx_strdup(NULL, row[2]);
ZBX_STR2UCHAR(trigger.priority, row[3]);
trigger.comments = zbx_strdup(NULL, row[4]);
trigger.url = zbx_strdup(NULL, row[5]);
trigger.recovery_expression = zbx_strdup(NULL, row[6]);
ZBX_STR2UCHAR(trigger.recovery_mode, row[7]);
ZBX_STR2UCHAR(trigger.value, row[8]);
preprocess_trigger_name(&trigger, &historical);
if (FAIL == historical)
ret = process_event_bulk_update(&trigger, &sql, &sql_alloc, &sql_offset);
else
ret = process_event_update(&trigger, &sql, &sql_alloc, &sql_offset);
zbx_db_trigger_clean(&trigger);
processed_num++;
if (last_completed != (completed = 100.0 * processed_num / triggers_num))
{
zabbix_log(LOG_LEVEL_WARNING, "completed %d%% of event name update", completed);
last_completed = completed;
}
}
DBend_multiple_update(&sql, &sql_alloc, &sql_offset);
if (SUCCEED == ret && 16 < sql_offset) /* in ORACLE always present begin..end; */
{
if (ZBX_DB_OK > DBexecute("%s", sql))
ret = FAIL;
}
DBfree_result(result);
zbx_free(sql);
out:
if (SUCCEED == ret)
zabbix_log(LOG_LEVEL_WARNING, "event name update completed");
else
zabbix_log(LOG_LEVEL_WARNING, "event name update failed");
return ret;
}
/******************************************************************************
* *
* Function: zbx_check_postinit_tasks *
* *
* Purpose: process post initialization tasks *
* *
* Return value: SUCCEED - the update was successful *
* FAIL - otherwise *
* *
******************************************************************************/
int zbx_check_postinit_tasks(char **error)
{
DB_RESULT result;
DB_ROW row;
int ret = SUCCEED;
result = DBselect("select taskid from task where type=%d and status=%d", ZBX_TM_TASK_UPDATE_EVENTNAMES,
ZBX_TM_STATUS_NEW);
if (NULL != (row = DBfetch(result)))
{
DBbegin();
if (SUCCEED == (ret = update_event_names()))
{
DBexecute("delete from task where taskid=%s", row[0]);
DBcommit();
}
else
DBrollback();
}
DBfree_result(result);
if (SUCCEED != ret)
*error = zbx_strdup(*error, "cannot update event names");
return ret;
}
|