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
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
|
/*
** 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 "db.h"
#include "../../libs/zbxaudit/audit_trigger.h"
#include "trigger_dep_linking.h"
typedef struct
{
zbx_uint64_t triggerid;
int flags;
}
resolve_dependencies_triggers_flags_t;
static zbx_hash_t triggers_flags_hash_func(const void *data)
{
const resolve_dependencies_triggers_flags_t *trigger_entry =
(const resolve_dependencies_triggers_flags_t *)data;
return ZBX_DEFAULT_UINT64_HASH_ALGO(&(trigger_entry->triggerid), sizeof(trigger_entry->triggerid),
ZBX_DEFAULT_HASH_SEED);
}
static int triggers_flags_compare_func(const void *d1, const void *d2)
{
const resolve_dependencies_triggers_flags_t *trigger_entry_1 =
(const resolve_dependencies_triggers_flags_t *)d1;
const resolve_dependencies_triggers_flags_t *trigger_entry_2 =
(const resolve_dependencies_triggers_flags_t *)d2;
ZBX_RETURN_IF_NOT_EQUAL(trigger_entry_1->triggerid, trigger_entry_2->triggerid);
return 0;
}
typedef struct
{
zbx_uint64_t trigger_dep_id;
zbx_uint64_t trigger_down_id;
zbx_uint64_t trigger_up_id;
int status;
int flags;
}
zbx_trigger_dep_vec_entry_t;
ZBX_PTR_VECTOR_DECL(trigger_up_entries, zbx_trigger_dep_vec_entry_t *)
ZBX_PTR_VECTOR_IMPL(trigger_up_entries, zbx_trigger_dep_vec_entry_t *)
typedef struct
{
zbx_uint64_t trigger_down_id;
zbx_vector_trigger_up_entries_t v;
}
zbx_trigger_dep_entry_t;
static zbx_hash_t zbx_trigger_dep_entries_hash_func(const void *data)
{
const zbx_trigger_dep_entry_t *trigger_dep_entry = (const zbx_trigger_dep_entry_t *)data;
return ZBX_DEFAULT_UINT64_HASH_ALGO(&((trigger_dep_entry)->trigger_down_id),
sizeof(trigger_dep_entry->trigger_down_id), ZBX_DEFAULT_HASH_SEED);
}
static int zbx_trigger_dep_entries_compare_func(const void *d1, const void *d2)
{
const zbx_trigger_dep_entry_t *trigger_dep_entry_1 = (const zbx_trigger_dep_entry_t *)d1;
const zbx_trigger_dep_entry_t *trigger_dep_entry_2 = (const zbx_trigger_dep_entry_t *)d2;
ZBX_RETURN_IF_NOT_EQUAL(trigger_dep_entry_1->trigger_down_id, trigger_dep_entry_2->trigger_down_id);
return 0;
}
static void zbx_trigger_dep_vec_entry_clean(zbx_trigger_dep_vec_entry_t *trigger_dep_entry)
{
zbx_free(trigger_dep_entry);
}
static void zbx_triggers_dep_entries_clean(zbx_hashset_t *h)
{
zbx_hashset_iter_t iter;
zbx_trigger_dep_entry_t *trigger_dep_entry;
zbx_hashset_iter_reset(h, &iter);
while (NULL != (trigger_dep_entry = (zbx_trigger_dep_entry_t *)zbx_hashset_iter_next(&iter)))
{
zbx_vector_trigger_up_entries_clear_ext(&(trigger_dep_entry->v), zbx_trigger_dep_vec_entry_clean);
zbx_vector_trigger_up_entries_destroy(&(trigger_dep_entry->v));
}
zbx_hashset_destroy(h);
}
/*********************************************************************************
* *
* Function: DBresolve_template_trigger_dependencies *
* *
* Purpose: resolves trigger dependencies for the specified triggers based on *
* host and linked templates *
* *
* Parameters: hostid - [IN] host identifier from database *
* trids - [IN] vector of trigger identifiers from database *
* links - [OUT] pairs of trigger dependencies (down,up) *
* trigger_flags - [OUT] map that lets audit to know if trigger is *
* a prototype or just trigger *
* *
* Return value: upon successful completion return SUCCEED, or FAIL on DB error *
* *
*********************************************************************************/
static int DBresolve_template_trigger_dependencies(zbx_uint64_t hostid, const zbx_vector_uint64_t *trids,
zbx_vector_uint64_pair_t *links, zbx_hashset_t *triggers_flags)
{
char *sql = NULL;
int i, res = SUCCEED;
size_t sql_alloc = 512, sql_offset;
DB_RESULT result;
DB_ROW row;
zbx_vector_uint64_pair_t dep_list_ids, map_ids;
zbx_vector_uint64_t all_templ_ids;
zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __func__);
zbx_vector_uint64_create(&all_templ_ids);
zbx_vector_uint64_pair_create(&dep_list_ids);
zbx_vector_uint64_pair_create(&map_ids);
sql = (char *)zbx_malloc(sql, sql_alloc);
sql_offset = 0;
/* get triggerids on which the 'parent template trigger of the new trigger' depends on */
zbx_strcpy_alloc(&sql, &sql_alloc, &sql_offset,
"select distinct td.triggerid_down,td.triggerid_up"
" from triggers t,trigger_depends td"
" where t.templateid in (td.triggerid_up,td.triggerid_down) and");
DBadd_condition_alloc(&sql, &sql_alloc, &sql_offset, "t.triggerid", trids->values, trids->values_num);
if (NULL == (result = DBselect("%s", sql)))
{
res = FAIL;
goto clean;
}
while (NULL != (row = DBfetch(result)))
{
zbx_uint64_pair_t dep_list_id;
ZBX_STR2UINT64(dep_list_id.first, row[0]);
ZBX_STR2UINT64(dep_list_id.second, row[1]);
zbx_vector_uint64_pair_append(&dep_list_ids, dep_list_id);
zbx_vector_uint64_append(&all_templ_ids, dep_list_id.first);
zbx_vector_uint64_append(&all_templ_ids, dep_list_id.second);
}
DBfree_result(result);
if (0 == dep_list_ids.values_num) /* not all trigger templates have a dependency trigger */
goto clean;
zbx_vector_uint64_sort(&all_templ_ids, ZBX_DEFAULT_UINT64_COMPARE_FUNC);
zbx_vector_uint64_uniq(&all_templ_ids, ZBX_DEFAULT_UINT64_COMPARE_FUNC);
sql_offset = 0;
/* find triggers which have a parent template trigger that we have dependency on, */
/* those are the dependency triggers of interest on our host */
zbx_snprintf_alloc(&sql, &sql_alloc, &sql_offset,
"select t.triggerid,t.templateid,t.flags"
" from triggers t,functions f,items i"
" where t.triggerid=f.triggerid"
" and f.itemid=i.itemid"
" and i.hostid=" ZBX_FS_UI64
" and",
hostid);
DBadd_condition_alloc(&sql, &sql_alloc, &sql_offset, "t.templateid", all_templ_ids.values,
all_templ_ids.values_num);
zbx_strcpy_alloc(&sql, &sql_alloc, &sql_offset, " and");
DBadd_condition_alloc(&sql, &sql_alloc, &sql_offset, "t.triggerid", trids->values,
trids->values_num);
if (NULL == (result = DBselect("%s", sql)))
{
res = FAIL;
goto clean;
}
while (NULL != (row = DBfetch(result)))
{
zbx_uint64_pair_t map_id;
resolve_dependencies_triggers_flags_t *found, temp_t;
ZBX_STR2UINT64(temp_t.triggerid, row[0]);
if (NULL == (found = (resolve_dependencies_triggers_flags_t *)zbx_hashset_search(triggers_flags,
&temp_t)))
{
resolve_dependencies_triggers_flags_t local_temp_t;
ZBX_STR2UINT64(local_temp_t.triggerid, row[0]);
local_temp_t.flags = atoi(row[2]);
zbx_hashset_insert(triggers_flags, &local_temp_t, sizeof(local_temp_t));
}
ZBX_STR2UINT64(map_id.first, row[0]);
ZBX_DBROW2UINT64(map_id.second, row[1]);
zbx_vector_uint64_pair_append(&map_ids, map_id);
}
DBfree_result(result);
for (i = 0; i < dep_list_ids.values_num; i++)
{
zbx_uint64_t templateid_down = dep_list_ids.values[i].first;
zbx_uint64_t templateid_up = dep_list_ids.values[i].second;
/* Convert template ids to corresponding trigger ids. */
/* If template trigger depends on host trigger rather than */
/* template trigger then up id conversion will fail and the */
/* original value (host trigger id) will be used as intended. */
zbx_uint64_t triggerid_down = 0;
zbx_uint64_t triggerid_up = templateid_up;
int j;
for (j = 0; j < map_ids.values_num; j++)
{
zbx_uint64_t hst_triggerid = map_ids.values[j].first;
zbx_uint64_t tpl_triggerid = map_ids.values[j].second;
if (tpl_triggerid == templateid_down)
triggerid_down = hst_triggerid;
if (tpl_triggerid == templateid_up)
triggerid_up = hst_triggerid;
}
if (0 != triggerid_down)
{
zbx_uint64_pair_t link = {triggerid_down, triggerid_up};
zbx_vector_uint64_pair_append(links, link);
}
}
clean:
zbx_free(sql);
zbx_vector_uint64_destroy(&all_templ_ids);
zbx_vector_uint64_pair_destroy(&map_ids);
zbx_vector_uint64_pair_destroy(&dep_list_ids);
zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __func__, zbx_result_string(res));
return res;
}
/**********************************************************************************************************
* *
* Function: prepare_trigger_dependencies_updates_and_deletes *
* *
* Purpose: takes a list of pending trigger dependencies (links) and excludes entries that are *
* already present on the target host to generate a new list (links_processed). Also, prepare *
* the list of the trigger dependencies (trigger_dep_ids_del) that need to be deleted on the *
* target host, since they are not present on the template trigger. *
* *
* Parameters: trids - [IN] vector of trigger identifiers from database *
* links - [OUT] pairs of trigger dependencies, list of links_up and links_down *
* links that we want to be present on the target host *
* links_processed - [OUT] processed links with entries that are already present excluded *
* trigger_dep_ids_del - [OUT] list of triggers dependencies that need to be deleted *
* *
* Return value: upon successful completion return SUCCEED, or FAIL on DB error *
* *
*********************************************************************************************************/
static int prepare_trigger_dependencies_updates_and_deletes(const zbx_vector_uint64_t *trids,
zbx_vector_uint64_pair_t *links, zbx_vector_uint64_pair_t *links_processed,
zbx_vector_uint64_t *trigger_dep_ids_del)
{
char *sql = NULL;
int i, res = SUCCEED;
size_t sql_alloc = 256, sql_offset = 0;
DB_RESULT result;
DB_ROW row;
zbx_hashset_t h;
zbx_hashset_iter_t iter;
zbx_trigger_dep_entry_t *found;
zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __func__);
#define TRIGGER_FUNCS_HASHSET_DEF_SIZE 100
zbx_hashset_create(&h, TRIGGER_FUNCS_HASHSET_DEF_SIZE, zbx_trigger_dep_entries_hash_func,
zbx_trigger_dep_entries_compare_func);
#undef TRIGGER_FUNCS_HASHSET_DEF_SIZE
zbx_snprintf_alloc(&sql, &sql_alloc, &sql_offset,
"select td.triggerdepid,td.triggerid_down,td.triggerid_up,t.flags"
" from trigger_depends td,triggers t "
" where t.triggerid=td.triggerid_down"
" and");
DBadd_condition_alloc(&sql, &sql_alloc, &sql_offset, "td.triggerid_down", trids->values, trids->values_num);
if (NULL == (result = DBselect("%s", sql)))
{
res = FAIL;
goto clean;
}
while (NULL != (row = DBfetch(result)))
{
int flags;
zbx_uint64_t trigger_dep_id, trigger_id_down, trigger_id_up;
zbx_trigger_dep_entry_t temp_t;
zbx_trigger_dep_vec_entry_t *s;
ZBX_STR2UINT64(trigger_dep_id, row[0]);
ZBX_STR2UINT64(trigger_id_down, row[1]);
ZBX_STR2UINT64(trigger_id_up, row[2]);
flags = atoi(row[3]);
temp_t.trigger_down_id = trigger_id_down;
s = (zbx_trigger_dep_vec_entry_t *)zbx_malloc(NULL, sizeof(zbx_trigger_dep_vec_entry_t));
s->trigger_dep_id = trigger_dep_id;
s->trigger_down_id = trigger_id_down;
s->trigger_up_id = trigger_id_up;
s->status = 0;
s->flags = flags;
if (NULL != (found = (zbx_trigger_dep_entry_t *)zbx_hashset_search(&h, &temp_t)))
{
zbx_vector_trigger_up_entries_append(&(found->v), s);
}
else
{
zbx_trigger_dep_entry_t local_temp_t;
zbx_vector_trigger_up_entries_create(&(local_temp_t.v));
zbx_vector_trigger_up_entries_append(&(local_temp_t.v), s);
local_temp_t.trigger_down_id = trigger_id_down;
zbx_hashset_insert(&h, &local_temp_t, sizeof(local_temp_t));
}
}
DBfree_result(result);
for (i = 0; i < links->values_num; i++)
{
zbx_trigger_dep_entry_t temp_t;
temp_t.trigger_down_id = links->values[i].first;
if (NULL != (found = (zbx_trigger_dep_entry_t *) zbx_hashset_search(&h, &temp_t)))
{
int j;
int found_trigger_up = 0;
for (j = 0; j < found->v.values_num; j++)
{
/* trigger_up are equal */
if (links->values[i].second == found->v.values[j]->trigger_up_id)
{
found_trigger_up = 1;
/* mark it as 'need to preserve' */
found->v.values[j]->status = 1;
break;
}
}
if (0 == found_trigger_up)
{
zbx_uint64_pair_t x;
x.first = links->values[i].first;
x.second = links->values[i].second;
zbx_vector_uint64_pair_append(links_processed, x);
}
}
else
{
zbx_uint64_pair_t x;
x.first = links->values[i].first;
x.second = links->values[i].second;
zbx_vector_uint64_pair_append(links_processed, x);
}
}
zbx_hashset_iter_reset(&h, &iter);
while (NULL != (found = (zbx_trigger_dep_entry_t *)zbx_hashset_iter_next(&iter)))
{
for (i = 0; i < found->v.values_num; i++)
{
if (0 == found->v.values[i]->status)
{
zbx_vector_uint64_append(trigger_dep_ids_del,
found->v.values[i]->trigger_dep_id);
zbx_audit_trigger_update_json_remove_dependency(found->v.values[i]->flags,
found->v.values[i]->trigger_dep_id,
found->v.values[i]->trigger_down_id);
}
}
}
clean:
zbx_triggers_dep_entries_clean(&h);
zbx_free(sql);
zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __func__, zbx_result_string(res));
return res;
}
static int DBadd_trigger_dependencies(zbx_vector_uint64_pair_t *links, zbx_hashset_t *triggers_flags)
{
int res = SUCCEED;
zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __func__);
if (0 < links->values_num)
{
int i;
zbx_uint64_t triggerdepid;
zbx_db_insert_t db_insert;
triggerdepid = DBget_maxid_num("trigger_depends", links->values_num);
zbx_db_insert_prepare(&db_insert, "trigger_depends", "triggerdepid", "triggerid_down", "triggerid_up",
NULL);
for (i = 0; i < links->values_num; i++)
{
resolve_dependencies_triggers_flags_t *found, temp_t;
zbx_db_insert_add_values(&db_insert, triggerdepid, links->values[i].first,
links->values[i].second);
temp_t.triggerid = links->values[i].first;
if (NULL != (found = (resolve_dependencies_triggers_flags_t *)zbx_hashset_search(
triggers_flags, &temp_t)))
{
zbx_audit_trigger_update_json_add_dependency(found->flags, triggerdepid,
links->values[i].first, links->values[i].second);
}
else
{
THIS_SHOULD_NEVER_HAPPEN;
res = FAIL;
break;
}
triggerdepid++;
}
if (SUCCEED == res)
zbx_db_insert_execute(&db_insert);
zbx_db_insert_clean(&db_insert);
}
zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __func__, zbx_result_string(res));
return res;
}
static int DBadd_and_remove_trigger_dependencies(zbx_vector_uint64_pair_t *links,
const zbx_vector_uint64_t *trids, zbx_hashset_t *triggers_flags)
{
int res;
char *sql = NULL;
size_t sql_alloc = 0, sql_offset = 0;
zbx_vector_uint64_pair_t links_processed;
zbx_vector_uint64_t trigger_dep_ids_del;
zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __func__);
zbx_vector_uint64_create(&trigger_dep_ids_del);
zbx_vector_uint64_pair_create(&links_processed);
if (FAIL == (res = prepare_trigger_dependencies_updates_and_deletes(trids, links, &links_processed,
&trigger_dep_ids_del)))
{
goto clean;
}
if (0 < trigger_dep_ids_del.values_num)
{
zbx_strcpy_alloc(&sql, &sql_alloc, &sql_offset, "delete from trigger_depends where");
DBadd_condition_alloc(&sql, &sql_alloc, &sql_offset, "triggerdepid",
trigger_dep_ids_del.values, trigger_dep_ids_del.values_num);
if (ZBX_DB_OK > DBexecute("%s", sql))
{
res = FAIL;
goto clean;
}
}
res = DBadd_trigger_dependencies(&links_processed, triggers_flags);
clean:
zbx_free(sql);
zbx_vector_uint64_destroy(&trigger_dep_ids_del);
zbx_vector_uint64_pair_destroy(&links_processed);
zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __func__, zbx_result_string(res));
return res;
}
/********************************************************************************
* *
* Function: DBsync_template_dependencies_for_triggers *
* *
* Purpose: update trigger dependencies for specified host *
* *
* Parameters: hostid - [IN] host identifier from database *
* trids - [IN] vector of trigger identifiers from database *
* is_update - [IN] flag. Values: *
* TRIGGER_DEP_SYNC_INSERT_OP - 'trids' contains *
* identifiers of new triggers, *
* TRIGGER_DEP_SYNC_UPDATE_OP - 'trids' contains *
* identifiers of already present triggers which *
* need to be updated *
* *
* Return value: upon successful completion return SUCCEED, or FAIL on DB error *
* *
* Comments: !!! Don't forget to sync the code with PHP !!! *
* *
********************************************************************************/
int DBsync_template_dependencies_for_triggers(zbx_uint64_t hostid, const zbx_vector_uint64_t *trids, int is_update)
{
int res = SUCCEED;
zbx_vector_uint64_pair_t links;
zbx_hashset_t triggers_flags;
zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __func__);
if (0 == trids->values_num)
goto out;
zbx_vector_uint64_pair_create(&links);
#define TRIGGER_FUNCS_HASHSET_DEF_SIZE 100
zbx_hashset_create(&triggers_flags, TRIGGER_FUNCS_HASHSET_DEF_SIZE, triggers_flags_hash_func,
triggers_flags_compare_func);
#undef TRIGGER_FUNCS_HASHSET_DEF_SIZE
if (FAIL == (res = DBresolve_template_trigger_dependencies(hostid, trids, &links, &triggers_flags)))
goto clean;
if (TRIGGER_DEP_SYNC_INSERT_OP == is_update)
{
if (FAIL == (res = DBadd_trigger_dependencies(&links, &triggers_flags)))
goto clean;
}
else if (TRIGGER_DEP_SYNC_UPDATE_OP == is_update)
{
res = DBadd_and_remove_trigger_dependencies(&links, trids, &triggers_flags);
}
clean:
zbx_vector_uint64_pair_destroy(&links);
zbx_hashset_destroy(&triggers_flags);
out:
zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __func__, zbx_result_string(res));
return res;
}
|