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
|
/*
** Zabbix
** Copyright (C) 2001-2020 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 "zbxmocktest.h"
#include "zbxmockdata.h"
#include "zbxmockassert.h"
#include "zbxmockutil.h"
#include "zbxserver.h"
#include "common.h"
#include "zbxalgo.h"
#include "dbcache.h"
#include "mutexs.h"
#define ZBX_DBCONFIG_IMPL
#include "dbconfig.h"
#include "configcache.h"
extern zbx_mock_config_t mock_config;
static int config_gmacro_context_compare(const void *d1, const void *d2)
{
const ZBX_DC_GMACRO *m1 = *(const ZBX_DC_GMACRO **)d1;
const ZBX_DC_GMACRO *m2 = *(const ZBX_DC_GMACRO **)d2;
/* macros without context have higher priority than macros with */
if (NULL == m1->context)
return NULL == m2->context ? 0 : -1;
if (NULL == m2->context)
return 1;
/* CONDITION_OPERATOR_EQUAL (0) has higher priority than CONDITION_OPERATOR_REGEXP (8) */
ZBX_RETURN_IF_NOT_EQUAL(m1->context_op, m2->context_op);
return strcmp(m1->context, m2->context);
}
static int config_hmacro_context_compare(const void *d1, const void *d2)
{
const ZBX_DC_HMACRO *m1 = *(const ZBX_DC_HMACRO **)d1;
const ZBX_DC_HMACRO *m2 = *(const ZBX_DC_HMACRO **)d2;
/* macros without context have higher priority than macros with */
if (NULL == m1->context)
return NULL == m2->context ? 0 : -1;
if (NULL == m2->context)
return 1;
/* CONDITION_OPERATOR_EQUAL (0) has higher priority than CONDITION_OPERATOR_REGEXP (8) */
ZBX_RETURN_IF_NOT_EQUAL(m1->context_op, m2->context_op);
return strcmp(m1->context, m2->context);
}
void mock_config_load_user_macros(const char *path)
{
zbx_mock_handle_t hmacros, handle;
zbx_mock_error_t err;
int i;
ZBX_DC_HMACRO_HM *hm;
ZBX_DC_GMACRO_M *gm;
zbx_vector_ptr_create(&mock_config.host_macros);
zbx_vector_ptr_create(&mock_config.global_macros);
hmacros = zbx_mock_get_parameter_handle(path);
while (ZBX_MOCK_END_OF_VECTOR != (err = (zbx_mock_vector_element(hmacros, &handle))))
{
char *name = NULL, *context = NULL;
const char *macro_name, *macro_value;
zbx_uint64_t macro_hostid;
zbx_mock_handle_t hhostid;
if (ZBX_MOCK_SUCCESS != err)
{
fail_msg("Cannot read 'macros' element #%d: %s", mock_config.host_macros.values_num,
zbx_mock_error_string(err));
}
if (ZBX_MOCK_SUCCESS == zbx_mock_object_member(handle, "hostid", &hhostid))
{
if (ZBX_MOCK_SUCCESS != zbx_mock_uint64(hhostid, ¯o_hostid))
fail_msg("Cannot parse macro hostid");
macro_hostid = zbx_mock_get_object_member_uint64(handle, "hostid");
}
else
macro_hostid = 0;
macro_name = zbx_mock_get_object_member_string(handle, "name");
macro_value = zbx_mock_get_object_member_string(handle, "value");
if (SUCCEED != zbx_user_macro_parse_dyn(macro_name, &name, &context, NULL, NULL))
fail_msg("invalid user macro: %s", macro_name);
if (0 == macro_hostid)
{
ZBX_DC_GMACRO *macro;
for (i = 0; i < mock_config.global_macros.values_num; i++)
{
gm = (ZBX_DC_GMACRO_M *)mock_config.global_macros.values[i];
if (0 == strcmp(gm->macro, name))
break;
}
if (i == mock_config.global_macros.values_num)
{
gm = zbx_malloc(NULL, sizeof(ZBX_DC_GMACRO_M));
gm->macro = zbx_strdup(NULL, name);
zbx_vector_ptr_create(&gm->gmacros);
zbx_vector_ptr_append(&mock_config.global_macros, gm);
}
macro = (ZBX_DC_GMACRO *)zbx_malloc(0, sizeof(ZBX_DC_GMACRO));
memset(macro, 0, sizeof(ZBX_DC_GMACRO));
macro->macro = name;
macro->context = context;
macro->value = macro_value;
zbx_vector_ptr_append(&gm->gmacros, macro);
}
else
{
ZBX_DC_HMACRO *macro;
for (i = 0; i < mock_config.host_macros.values_num; i++)
{
hm = (ZBX_DC_HMACRO_HM *)mock_config.host_macros.values[i];
if (hm->hostid == macro_hostid && 0 == strcmp(hm->macro, name))
break;
}
if (i == mock_config.host_macros.values_num)
{
hm = zbx_malloc(NULL, sizeof(ZBX_DC_HMACRO_HM));
hm->hostid = macro_hostid;
hm->macro = zbx_strdup(NULL, name);
zbx_vector_ptr_create(&hm->hmacros);
zbx_vector_ptr_append(&mock_config.host_macros, hm);
}
macro = (ZBX_DC_HMACRO *)zbx_malloc(0, sizeof(ZBX_DC_HMACRO));
memset(macro, 0, sizeof(ZBX_DC_HMACRO));
macro->macro = name;
macro->context = context;
macro->value = macro_value;
zbx_vector_ptr_append(&hm->hmacros, macro);
}
}
for (i = 0; i < mock_config.global_macros.values_num; i++)
{
gm = (ZBX_DC_GMACRO_M *)mock_config.global_macros.values[i];
zbx_vector_ptr_sort(&gm->gmacros, config_gmacro_context_compare);
}
for (i = 0; i < mock_config.host_macros.values_num; i++)
{
hm = (ZBX_DC_HMACRO_HM *)mock_config.host_macros.values[i];
zbx_vector_ptr_sort(&hm->hmacros, config_hmacro_context_compare);
}
mock_config.initialized |= ZBX_MOCK_CONFIG_USERMACROS;
}
void mock_config_free_user_macros(void)
{
int i, j;
for (i = 0; i < mock_config.global_macros.values_num; i++)
{
ZBX_DC_GMACRO_M *gm;
ZBX_DC_GMACRO *macro;
gm = (ZBX_DC_GMACRO_M *)mock_config.global_macros.values[i];
for (j = 0; j < gm->gmacros.values_num; j++)
{
macro = (ZBX_DC_GMACRO *)gm->gmacros.values[j];
free_string(macro->macro);
free_string(macro->context);
zbx_free(macro);
}
zbx_vector_ptr_destroy(&gm->gmacros);
free_string(gm->macro);
zbx_free(gm);
}
zbx_vector_ptr_destroy(&mock_config.global_macros);
for (i = 0; i < mock_config.host_macros.values_num; i++)
{
ZBX_DC_HMACRO_HM *hm;
ZBX_DC_HMACRO *macro;
hm = (ZBX_DC_HMACRO_HM *)mock_config.host_macros.values[i];
for (j = 0; j < hm->hmacros.values_num; j++)
{
macro = (ZBX_DC_HMACRO *)hm->hmacros.values[j];
free_string(macro->macro);
free_string(macro->context);
zbx_free(macro);
}
zbx_vector_ptr_destroy(&hm->hmacros);
free_string(hm->macro);
zbx_free(hm);
}
zbx_vector_ptr_destroy(&mock_config.host_macros);
}
|