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
|
/*
** 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 "dbcache.h"
#include "log.h"
#include "audit_trigger.h"
static int trigger_flag_to_resource_type(int flag)
{
if (ZBX_FLAG_DISCOVERY_NORMAL == flag || ZBX_FLAG_DISCOVERY_CREATED == flag)
{
return AUDIT_RESOURCE_TRIGGER;
}
else if (ZBX_FLAG_DISCOVERY_PROTOTYPE == flag)
{
return AUDIT_RESOURCE_TRIGGER_PROTOTYPE;
}
else
{
zabbix_log(LOG_LEVEL_CRIT, "unexpected audit trigger flag detected: ->%d<-", flag);
THIS_SHOULD_NEVER_HAPPEN;
exit(EXIT_FAILURE);
}
}
#define TR_OR_TRP(s) (AUDIT_RESOURCE_TRIGGER == resource_type) ? "trigger."#s : "triggerprototype."#s
void zbx_audit_trigger_create_entry(int audit_action, zbx_uint64_t triggerid, const char *name, int flags)
{
int resource_type;
zbx_audit_entry_t local_audit_trigger_entry, **found_audit_trigger_entry;
zbx_audit_entry_t *local_audit_trigger_entry_x = &local_audit_trigger_entry;
RETURN_IF_AUDIT_OFF();
resource_type = trigger_flag_to_resource_type(flags);
local_audit_trigger_entry.id = triggerid;
local_audit_trigger_entry.cuid = NULL;
found_audit_trigger_entry = (zbx_audit_entry_t**)zbx_hashset_search(zbx_get_audit_hashset(),
&(local_audit_trigger_entry_x));
if (NULL == found_audit_trigger_entry)
{
zbx_audit_entry_t *local_audit_trigger_entry_insert;
local_audit_trigger_entry_insert = zbx_audit_entry_init(triggerid, name, audit_action, resource_type);
zbx_hashset_insert(zbx_get_audit_hashset(), &local_audit_trigger_entry_insert,
sizeof(local_audit_trigger_entry_insert));
if (AUDIT_ACTION_ADD == audit_action)
{
zbx_audit_update_json_append_uint64(triggerid, AUDIT_DETAILS_ACTION_ADD,
TR_OR_TRP(triggerid), triggerid);
}
}
}
void zbx_audit_trigger_update_json_add_data(zbx_uint64_t triggerid, zbx_uint64_t templateid,
unsigned char recovery_mode, unsigned char status, unsigned char type, zbx_uint64_t value,
zbx_uint64_t state, unsigned char priority, const char *comments, const char *url, int flags,
unsigned char correlation_mode, const char *correlation_tag, unsigned char manual_close,
const char *opdata, unsigned char discover, const char *event_name)
{
char audit_key[AUDIT_DETAILS_KEY_LEN], audit_key_event_name[AUDIT_DETAILS_KEY_LEN],
audit_key_opdata[AUDIT_DETAILS_KEY_LEN], audit_key_comments[AUDIT_DETAILS_KEY_LEN],
audit_key_flags[AUDIT_DETAILS_KEY_LEN], audit_key_priority[AUDIT_DETAILS_KEY_LEN],
audit_key_state[AUDIT_DETAILS_KEY_LEN], audit_key_status[AUDIT_DETAILS_KEY_LEN],
audit_key_templateid[AUDIT_DETAILS_KEY_LEN], audit_key_type[AUDIT_DETAILS_KEY_LEN],
audit_key_url[AUDIT_DETAILS_KEY_LEN], audit_key_value[AUDIT_DETAILS_KEY_LEN],
audit_key_recovery_mode[AUDIT_DETAILS_KEY_LEN], audit_key_correlation_mode[AUDIT_DETAILS_KEY_LEN],
audit_key_correlation_tag[AUDIT_DETAILS_KEY_LEN], audit_key_manual_close[AUDIT_DETAILS_KEY_LEN],
audit_key_discover[AUDIT_DETAILS_KEY_LEN];
int resource_type;
RETURN_IF_AUDIT_OFF();
resource_type = trigger_flag_to_resource_type(flags);
zbx_snprintf(audit_key, sizeof(audit_key), (AUDIT_RESOURCE_TRIGGER == resource_type) ? "trigger" :
"triggerprototype");
#define AUDIT_KEY_SNPRINTF(r) zbx_snprintf(audit_key_##r, sizeof(audit_key_##r), TR_OR_TRP(r));
AUDIT_KEY_SNPRINTF(event_name)
AUDIT_KEY_SNPRINTF(opdata)
AUDIT_KEY_SNPRINTF(comments)
AUDIT_KEY_SNPRINTF(flags)
AUDIT_KEY_SNPRINTF(priority)
AUDIT_KEY_SNPRINTF(state)
AUDIT_KEY_SNPRINTF(status)
AUDIT_KEY_SNPRINTF(templateid)
AUDIT_KEY_SNPRINTF(type)
AUDIT_KEY_SNPRINTF(url)
AUDIT_KEY_SNPRINTF(value)
AUDIT_KEY_SNPRINTF(recovery_mode)
AUDIT_KEY_SNPRINTF(correlation_mode)
AUDIT_KEY_SNPRINTF(correlation_tag)
AUDIT_KEY_SNPRINTF(manual_close)
if (AUDIT_RESOURCE_TRIGGER_PROTOTYPE == resource_type)
AUDIT_KEY_SNPRINTF(discover)
#undef AUDIT_KEY_SNPRINTF
zbx_audit_update_json_append_no_value(triggerid, AUDIT_DETAILS_ACTION_ADD, audit_key);
#define ADD_STR(r) zbx_audit_update_json_append_string(triggerid, AUDIT_DETAILS_ACTION_ADD, audit_key_##r, r);
#define ADD_UINT64(r) zbx_audit_update_json_append_uint64(triggerid, AUDIT_DETAILS_ACTION_ADD, audit_key_##r, r);
#define ADD_INT(r) zbx_audit_update_json_append_int(triggerid, AUDIT_DETAILS_ACTION_ADD, audit_key_##r, r);
ADD_STR(event_name)
ADD_STR(opdata)
ADD_STR(comments)
ADD_INT(flags)
ADD_INT(priority)
ADD_UINT64(state)
ADD_INT(status)
ADD_UINT64(templateid)
ADD_INT(type)
ADD_STR(url)
ADD_UINT64(value)
ADD_INT(recovery_mode)
ADD_INT(correlation_mode)
ADD_STR(correlation_tag)
ADD_INT(manual_close)
if (AUDIT_RESOURCE_TRIGGER_PROTOTYPE == resource_type)
ADD_UINT64(discover)
#undef ADD_STR
#undef ADD_UINT64
#undef ADD_INT
}
void zbx_audit_trigger_update_json_add_expr(zbx_uint64_t triggerid, int flags, const char *expression)
{
char buf[AUDIT_DETAILS_KEY_LEN];
int resource_type;
RETURN_IF_AUDIT_OFF();
resource_type = trigger_flag_to_resource_type(flags);
zbx_snprintf(buf, sizeof(buf), TR_OR_TRP(expression));
zbx_audit_update_json_append_string(triggerid, AUDIT_DETAILS_ACTION_ADD, buf, expression);
}
void zbx_audit_trigger_update_json_add_rexpr(zbx_uint64_t triggerid, int flags, const char *recovery_expression)
{
char buf[AUDIT_DETAILS_KEY_LEN];
int resource_type;
RETURN_IF_AUDIT_OFF();
resource_type = trigger_flag_to_resource_type(flags);
zbx_snprintf(buf, sizeof(buf), TR_OR_TRP(recovery_expression));
zbx_audit_update_json_append_string(triggerid, AUDIT_DETAILS_ACTION_ADD, buf, recovery_expression);
}
#define PREPARE_AUDIT_TRIGGER_UPDATE(resource, type1, type2) \
void zbx_audit_trigger_update_json_update_##resource(zbx_uint64_t triggerid, int flags, \
type1 resource##_old, type1 resource##_new) \
{ \
char buf[AUDIT_DETAILS_KEY_LEN]; \
int resource_type; \
\
RETURN_IF_AUDIT_OFF(); \
\
resource_type = trigger_flag_to_resource_type(flags); \
\
zbx_snprintf(buf, sizeof(buf), TR_OR_TRP(resource)); \
\
zbx_audit_update_json_update_##type2(triggerid, buf, resource##_old, resource##_new); \
}
PREPARE_AUDIT_TRIGGER_UPDATE(recovery_mode, int, int)
PREPARE_AUDIT_TRIGGER_UPDATE(correlation_mode, int, int)
PREPARE_AUDIT_TRIGGER_UPDATE(correlation_tag, const char*, string)
PREPARE_AUDIT_TRIGGER_UPDATE(manual_close, int, int)
PREPARE_AUDIT_TRIGGER_UPDATE(opdata, const char*, string)
PREPARE_AUDIT_TRIGGER_UPDATE(discover, int, int)
PREPARE_AUDIT_TRIGGER_UPDATE(event_name, const char*, string)
PREPARE_AUDIT_TRIGGER_UPDATE(type, int, int)
PREPARE_AUDIT_TRIGGER_UPDATE(templateid, zbx_uint64_t, uint64)
#undef PREPARE_AUDIT_ITEM_UPDATE
#undef TR_OR_TRP
/******************************************************************************
* *
* Function: zbx_audit_DBselect_delete_for_trigger *
* *
* Parameters: sql - [IN] sql statement *
* ids - [OUT] sorted list of selected uint64 values *
* *
* Return value: SUCCEED - query SUCCEEDED *
* FAIL - otherwise *
* *
******************************************************************************/
void zbx_audit_DBselect_delete_for_trigger(const char *sql, zbx_vector_uint64_t *ids)
{
DB_RESULT result;
DB_ROW row;
result = DBselect("%s", sql);
while (NULL != (row = DBfetch(result)))
{
zbx_uint64_t id;
ZBX_STR2UINT64(id, row[0]);
zbx_vector_uint64_append(ids, id);
zbx_audit_trigger_create_entry(AUDIT_ACTION_DELETE, id, row[1], atoi(row[2]));
}
DBfree_result(result);
zbx_vector_uint64_sort(ids, ZBX_DEFAULT_UINT64_COMPARE_FUNC);
}
void zbx_audit_trigger_update_json_add_dependency(int flags, zbx_uint64_t triggerdepid,
zbx_uint64_t triggerid, zbx_uint64_t triggerid_up)
{
char audit_key[AUDIT_DETAILS_KEY_LEN], audit_key_triggerid_up[AUDIT_DETAILS_KEY_LEN];
int resource_type;
RETURN_IF_AUDIT_OFF();
resource_type = trigger_flag_to_resource_type(flags);
if (AUDIT_RESOURCE_TRIGGER == resource_type)
{
zbx_snprintf(audit_key, sizeof(audit_key), "trigger.dependencies[" ZBX_FS_UI64 "]", triggerdepid);
zbx_snprintf(audit_key_triggerid_up, sizeof(audit_key_triggerid_up), "trigger.dependencies["
ZBX_FS_UI64 "].dependsOnTriggerid", triggerdepid);
}
else
{
zbx_snprintf(audit_key, sizeof(audit_key), "triggerprototype.dependencies[" ZBX_FS_UI64 "]",
triggerdepid);
zbx_snprintf(audit_key_triggerid_up, sizeof(audit_key_triggerid_up), "triggerprototype.dependencies["
ZBX_FS_UI64 "].dependsOnTriggerid", triggerdepid);
}
zbx_audit_update_json_append_no_value(triggerid, AUDIT_DETAILS_ACTION_ADD, audit_key);
zbx_audit_update_json_append_uint64(triggerid, AUDIT_DETAILS_ACTION_ADD, audit_key_triggerid_up, triggerid_up);
}
void zbx_audit_trigger_update_json_add_tags_and_values(zbx_uint64_t triggerid, int flags, zbx_uint64_t triggertagid,
const char *tag, const char *value)
{
char audit_key[AUDIT_DETAILS_KEY_LEN], audit_key_tag[AUDIT_DETAILS_KEY_LEN],
audit_key_value[AUDIT_DETAILS_KEY_LEN];
int resource_type;
RETURN_IF_AUDIT_OFF();
resource_type = trigger_flag_to_resource_type(flags);
if (AUDIT_RESOURCE_TRIGGER == resource_type)
{
zbx_snprintf(audit_key, AUDIT_DETAILS_KEY_LEN, "trigger.tags[" ZBX_FS_UI64 "]", triggertagid);
zbx_snprintf(audit_key_tag, AUDIT_DETAILS_KEY_LEN, "trigger.tags[" ZBX_FS_UI64 "].tag", triggertagid);
zbx_snprintf(audit_key_value, AUDIT_DETAILS_KEY_LEN, "trigger.tags[" ZBX_FS_UI64 "].value",
triggertagid);
}
else if(AUDIT_RESOURCE_TRIGGER_PROTOTYPE == resource_type)
{
zbx_snprintf(audit_key, AUDIT_DETAILS_KEY_LEN, "triggerprototype.tags[" ZBX_FS_UI64 "]", triggertagid);
zbx_snprintf(audit_key_tag, AUDIT_DETAILS_KEY_LEN, "triggerprototype.tags[" ZBX_FS_UI64 "].tag",
triggertagid);
zbx_snprintf(audit_key_value, AUDIT_DETAILS_KEY_LEN, "triggerprototype.tags[" ZBX_FS_UI64 "].value",
triggertagid);
}
else
{
zabbix_log(LOG_LEVEL_CRIT, "unexpected audit trigger resource type detected: ->%d<-", resource_type);
THIS_SHOULD_NEVER_HAPPEN;
exit(EXIT_FAILURE);
}
zbx_audit_update_json_append_no_value(triggerid, AUDIT_DETAILS_ACTION_ADD, audit_key);
zbx_audit_update_json_append_string(triggerid, AUDIT_DETAILS_ACTION_ADD, audit_key_tag, tag);
zbx_audit_update_json_append_string(triggerid, AUDIT_DETAILS_ACTION_ADD, audit_key_value, value);
}
void zbx_audit_trigger_update_json_delete_tags(zbx_uint64_t triggerid, int flags, zbx_uint64_t triggertagid)
{
char audit_key[AUDIT_DETAILS_KEY_LEN];
int resource_type;
RETURN_IF_AUDIT_OFF();
resource_type = trigger_flag_to_resource_type(flags);
if (AUDIT_RESOURCE_TRIGGER == resource_type)
zbx_snprintf(audit_key, AUDIT_DETAILS_KEY_LEN, "trigger.tags[" ZBX_FS_UI64 "]", triggertagid);
else
zbx_snprintf(audit_key, AUDIT_DETAILS_KEY_LEN, "triggerprototype.tags[" ZBX_FS_UI64 "]", triggertagid);
zbx_audit_update_json_append_no_value(triggerid, AUDIT_DETAILS_ACTION_DELETE, audit_key);
}
|