Welcome to mirror list, hosted at ThFree Co, Russian Federation.

query.c « zbxeval « libs « src - github.com/zabbix/zabbix.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 928f7b05de8b943568b88a3fac619d4efd3486ce (plain)
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
/*
** 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 "log.h"
#include "zbxalgo.h"
#include "zbxserver.h"
#include "zbxeval.h"
#include "eval.h"

/* The tag expression token is virtual token used during item query filter processing. */
#define ZBX_EVAL_TOKEN_TAG_EXPRESSION		(1000)

/******************************************************************************
 *                                                                            *
 * Function: zbx_eval_parse_query                                             *
 *                                                                            *
 * Purpose: parse item query /host/key?[filter] into host, key and filter     *
 *          components                                                        *
 *                                                                            *
 * Parameters: str   - [IN] the item query                                    *
 *             len   - [IN] the query length                                  *
 *             query - [IN] the parsed item query                             *
 *                                                                            *
 * Return value: The number of parsed characters.                             *
 *                                                                            *
 ******************************************************************************/
size_t	zbx_eval_parse_query(const char *str, size_t len, zbx_item_query_t *query)
{
	size_t		n;
	const char	*host, *key, *filter;

	if (0 == (n = eval_parse_query(str, &host, &key, &filter)) || n != len)
		return 0;

	query->host = (host != key - 1 ? zbx_substr(host, 0, key - host - 2) : NULL);

	if (NULL != filter)
	{
		query->key = zbx_substr(key, 0, (filter - key) - 3);
		query->filter = zbx_substr(filter, 0, (str + len - filter) - 2);
	}
	else
	{
		query->key = zbx_substr(key, 0, (str + len - key) - 1);
		query->filter = NULL;
	}

	return n;
}

/******************************************************************************
 *                                                                            *
 * Function: zbx_eval_clear_filter                                            *
 *                                                                            *
 * Purpose: frees resources allocated by item reference                       *
 *                                                                            *
 ******************************************************************************/
void	zbx_eval_clear_query(zbx_item_query_t *query)
{
	zbx_free(query->host);
	zbx_free(query->key);
	zbx_free(query->filter);
}

/******************************************************************************
 *                                                                            *
 * Function: zbx_eval_prepare_filter                                          *
 *                                                                            *
 * Purpose: prepare filter expression by converting property comparisons      *
 *          prop =/<> "value" to prop("value")/not prop("value") function     *
 *          calls.                                                            *
 *                                                                            *
 * Parameters: ctx - [IN] the evaluation context                              *
 *                                                                            *
 ******************************************************************************/
void	zbx_eval_prepare_filter(zbx_eval_context_t *ctx)
{
	int	i, j;

	for (i = 0; i < ctx->stack.values_num; i++)
	{
		zbx_eval_token_t	*prop = &ctx->stack.values[i];

		if (0 == (prop->type & ZBX_EVAL_CLASS_PROPERTY))
			continue;

		for (j = i + 1; j < ctx->stack.values_num; j++)
		{
			zbx_eval_token_t	*op = &ctx->stack.values[j];

			if (ZBX_EVAL_TOKEN_OP_EQ == op->type || ZBX_EVAL_TOKEN_OP_NE == op->type)
			{
				zbx_eval_token_t	*func;

				func = &ctx->stack.values[j - 1];

				if (i != j - 1)
				{
					zbx_strloc_t	loc;

					loc = prop->loc;
					*prop = *func;
					func->loc = loc;
				}

				func->opt = 1;
				func->type = ZBX_EVAL_TOKEN_FUNCTION;

				if (ZBX_EVAL_TOKEN_OP_NE == op->type)
					op->type = ZBX_EVAL_TOKEN_OP_NOT;
				else
					op->type = ZBX_EVAL_TOKEN_NOP;

				break;
			}
		}
	}
}

/******************************************************************************
 *                                                                            *
 * Function: eval_filter_apply_op2                                            *
 *                                                                            *
 * Purpose: apply binary operation to stack                                   *
 *                                                                            *
 * Parameters: token - [IN] the operation token                               *
 *             stack - [IN/OUT] the target stack                              *
 *             index - [IN/OUT] the stack index                               *
 *                                                                            *
 ******************************************************************************/
static void	eval_filter_apply_op2(zbx_eval_token_t *token, zbx_vector_eval_token_t *stack,
		zbx_vector_uint64_t *index)
{
	zbx_eval_token_t	*left, *right;
	int			li, ri;

	li = (int)index->values[index->values_num - 2];
	left = &stack->values[li];
	ri = (int)index->values[index->values_num - 1];
	right = &stack->values[ri];

	if (ZBX_EVAL_TOKEN_TAG_EXPRESSION == left->type || ZBX_EVAL_TOKEN_TAG_EXPRESSION == right->type)
	{
		switch (token->type)
		{
			case ZBX_EVAL_TOKEN_OP_AND:
				if (ZBX_EVAL_TOKEN_TAG_EXPRESSION == left->type)
				{
					memmove(left, right, (stack->values_num - ri) * sizeof(zbx_eval_token_t));
					stack->values_num -= (ri - li);
				}
				else
					stack->values_num--;
				break;
			default:
				left->type = ZBX_EVAL_TOKEN_TAG_EXPRESSION;
				stack->values_num = li + 1;
				break;
		}

		index->values_num--;
		return;
	}

	zbx_vector_eval_token_append_ptr(stack, token);
	index->values_num--;
}

/******************************************************************************
 *                                                                            *
 * Function: eval_filter_apply_op1                                            *
 *                                                                            *
 * Purpose: apply unary operation to stack                                    *
 *                                                                            *
 * Parameters: token - [IN] the operation token                               *
 *             stack - [IN/OUT] the target stack                              *
 *             index - [IN/OUT] the stack index                               *
 *                                                                            *
 ******************************************************************************/
static void	eval_filter_apply_op1(zbx_eval_token_t *token, zbx_vector_eval_token_t *stack,
		const zbx_vector_uint64_t *index)
{
	zbx_eval_token_t	*right;
	int			ri;

	ri = (int)index->values[index->values_num - 1];
	right = &stack->values[ri];

	if (ZBX_EVAL_TOKEN_TAG_EXPRESSION != right->type)
		zbx_vector_eval_token_append_ptr(stack, token);
}

/******************************************************************************
 *                                                                            *
 * Function: eval_filter_apply_func                                           *
 *                                                                            *
 * Purpose: apply function to stack                                           *
 *                                                                            *
 * Parameters: ctx   - [IN] the evaluation context                            *
 *             token - [IN] the function token                                *
 *             stack - [IN/OUT] the target stack                              *
 *             index - [IN/OUT] the stack index                               *
 *                                                                            *
 ******************************************************************************/
static void	eval_filter_apply_func(zbx_eval_context_t *ctx, zbx_eval_token_t *token,
		zbx_vector_eval_token_t *stack, const zbx_vector_uint64_t *index)
{
	zbx_eval_token_t	*left;
	int			li;

	if (ZBX_CONST_STRLEN("tag") == token->loc.r - token->loc.l + 1 &&
			0 == memcmp(ctx->expression + token->loc.l, "tag", ZBX_CONST_STRLEN("tag")))
	{
		li = (int)index->values[index->values_num - 1];
		left = &stack->values[li];

		left->type = ZBX_EVAL_TOKEN_TAG_EXPRESSION;
		stack->values_num = li + 1;
	}
	else
		zbx_vector_eval_token_append_ptr(stack, token);
}

/******************************************************************************
 *                                                                            *
 * Function: eval_op_str                                                      *
 *                                                                            *
 * Purpose: get operator in text format                                       *
 *                                                                            *
 * Parameters: op - [IN] the operator type                                    *
 *                                                                            *
 * Return value: The operator in text format.                                 *
 *                                                                            *
 * Comments: This function will return 'unsupported operator' for unsupported *
 *           operators, causing the expression evaluation to fail. However    *
 *           this should not happen as the supported operators are verified   *
 *           during expression parsing.                                       *
 *                                                                            *
 ******************************************************************************/
static const char	*eval_op_str(zbx_token_type_t op)
{
	switch (op)
	{
		case ZBX_EVAL_TOKEN_OP_EQ:
			return "=";
		case ZBX_EVAL_TOKEN_OP_NE:
			return "<>";
		case ZBX_EVAL_TOKEN_OP_AND:
			return " and ";
		case ZBX_EVAL_TOKEN_OP_OR:
			return " or ";
		case ZBX_EVAL_TOKEN_OP_NOT:
			return "not ";
		default:
			return "unsupported operator";
	}
}

/******************************************************************************
 *                                                                            *
 * Function: eval_unquote_str                                                 *
 *                                                                            *
 * Purpose: unquote string                                                    *
 *                                                                            *
 * Parameters: str - [IN] the string to unquote                               *
 *                                                                            *
 * Return value: The unquoted string.                                         *
 *                                                                            *
 * Comments: The string is unquoted in the same buffer.                       *
 *                                                                            *
 ******************************************************************************/
static char	*eval_unquote_str(char *str)
{
	char	*dst, *src;

	if ('\"' != *str)
		return str;

	src = str;
	dst = src++;

	while ('\0' != *src && '"' != *src)
	{
		if ('\\' == *src)
		{

			if ('\0' == *(++src))
				break;
		}

		*dst++ = *src++;
	}

	*dst = '\0';

	return str;
}

/******************************************************************************
 *                                                                            *
 * Function: eval_generate_filter                                             *
 *                                                                            *
 * Purpose: generate filter expression from the specified stack               *
 *                                                                            *
 * Parameters: ctx    - [IN] the evaluation context                           *
 *             stack  - [IN] the expression stack                             *
 *             groups - [OUT] the group values to match                       *
 *             filter - [OUT] the generated filter                            *
 *             error  - [OUT] the error message                               *
 *                                                                            *
 * Return value: SUCCEED - the filter expression was successfully generated   *
 *               FAIL    - otherwise                                          *
 *                                                                            *
 ******************************************************************************/
static int	eval_generate_filter(const zbx_eval_context_t *ctx, const zbx_vector_eval_token_t *stack,
		zbx_vector_str_t *groups, char **filter, char **error)
{
	zbx_vector_str_t	out;
	int			i, ret = FAIL;
	char			*tmp;

	if (0 == stack->values_num)
	{
		*error = zbx_strdup(NULL, "invalid filter expression");
		return FAIL;
	}

	if (ZBX_EVAL_TOKEN_TAG_EXPRESSION == stack->values[0].type)
	{
		*filter = NULL;
		return SUCCEED;
	}

	zbx_vector_str_create(&out);

	for (i = 0; i < stack->values_num; i++)
	{
		zbx_eval_token_t	*token = &stack->values[i];

		if (0 != (token->type & ZBX_EVAL_CLASS_OPERATOR2))
		{
			if (2 > out.values_num)
			{
				*error = zbx_strdup(NULL, "not enough values on stack for binary operation");
				goto out;
			}

			tmp = zbx_dsprintf(NULL, "(%s%s%s)", out.values[out.values_num - 2], eval_op_str(token->type),
					out.values[out.values_num - 1]);

			zbx_free(out.values[out.values_num - 2]);
			zbx_free(out.values[out.values_num - 1]);
			out.values_num -= 1;
			out.values[out.values_num - 1] = tmp;
		}
		else if (0 != (token->type & ZBX_EVAL_CLASS_OPERATOR1))
		{
			if (1 > out.values_num)
			{
				*error = zbx_strdup(NULL, "not enough values on stack for unary operation");
				goto out;
			}

			tmp = zbx_dsprintf(NULL, "%s%s", eval_op_str(token->type), out.values[out.values_num - 1]);

			zbx_free(out.values[out.values_num - 1]);
			out.values[out.values_num - 1] = tmp;
		}
		else if (ZBX_EVAL_TOKEN_FUNCTION == token->type)
		{
			if (1 > out.values_num)
			{
				*error = zbx_strdup(NULL, "not enough values on stack for property comparison");
				goto out;
			}

			tmp = zbx_dsprintf(NULL, "{%d}", groups->values_num);
			zbx_vector_str_append(groups, eval_unquote_str(out.values[out.values_num - 1]));
			out.values[out.values_num - 1] = tmp;
		}
		else if (ZBX_EVAL_TOKEN_NOP != token->type)
			zbx_vector_str_append(&out, zbx_substr(ctx->expression, token->loc.l, token->loc.r));
	}

	if (1 != out.values_num)
	{
		*error = zbx_strdup(NULL, "too many values left on stack after generating filter expression");
		goto out;
	}

	*filter = out.values[0];
	out.values_num = 0;

	ret = SUCCEED;
out:
	zbx_vector_str_clear_ext(&out, zbx_str_free);
	zbx_vector_str_destroy(&out);

	return ret;
}

/******************************************************************************
 *                                                                            *
 * Function: zbx_eval_get_group_filter                                        *
 *                                                                            *
 * Purpose: generate group SQL filter expression from item filter             *
 *                                                                            *
 * Parameters: ctx    - [IN] the filter expression evaluation context         *
 *             groups - [OUT] the group values to match                       *
 *             filter - [OUT] the generated filter                            *
 *             error  - [OUT] the error message                               *
 *                                                                            *
 * Return value: SUCCEED - the filter expression was successfully generated   *
 *               FAIL    - otherwise                                          *
 *                                                                            *
 * Comments: The filter SQL is generated in two steps.                        *
 *           1) The filter expression token stack is simplified by removing   *
 *              tag related expression parts, so tags do not affect selection *
 *              item candidates.                                              *
 *              This is done by 'evaluating' the original filter expression   *
 *              token stack according to the following rules:                 *
 *                * group comparisons are copied without changes              *
 *                * tag comparisons are replaced with TAG_EXPRESSION token    *
 *                * TAG_EXPRESSION and <expression> is replaced with          *
 *                  <expression>                                              *
 *                * TAG_EXPRESSION or <expression> is replaced with           *
 *                  TAG_EXPRESSION                                            *
 *                * not TAG_EXPRESSION is replaced with TAG_EXPRESSION        *
 *                                                                            *
 *           2) At this point the simplified stack will contain either only   *
 *              group comparisons/logical operators or TAG_EXPRESSION token.  *
 *              In the first case the filter is generated by replacing group  *
 *              comparisons with {N}, where N is the index of value to        *
 *              compare in groups vector.                                     *
 *              In the second case it means that selection of item candidates *
 *              cannot be restricted by groups and the filter must be NULL.   *
 *                                                                            *
 ******************************************************************************/
int	zbx_eval_get_group_filter(zbx_eval_context_t *ctx, zbx_vector_str_t *groups, char **filter, char **error)
{
	zbx_vector_eval_token_t	stack;	/* simplified filter expression token stack */
	zbx_vector_uint64_t	output;	/* pseudo output stack, containing indexes of expression     */
					/* fragments in the simplified filter expression token stack */
	int			i, ret = FAIL;

	zbx_vector_eval_token_create(&stack);
	zbx_vector_uint64_create(&output);

	for (i = 0; i < ctx->stack.values_num; i++)
	{
		zbx_eval_token_t	*token = &ctx->stack.values[i];

		if (0 != (token->type & ZBX_EVAL_CLASS_OPERATOR2))
		{
			if (2 > output.values_num)
			{
				*error = zbx_strdup(NULL, "not enough values on stack for binary operation");
				goto out;
			}

			eval_filter_apply_op2(token, &stack, &output);
		}
		else if (0 != (token->type & ZBX_EVAL_CLASS_OPERATOR1))
		{
			if (1 > output.values_num)
			{
				*error = zbx_strdup(NULL, "not enough values on stack for unary operation");
				goto out;
			}

			eval_filter_apply_op1(token, &stack, &output);
		}
		else if (ZBX_EVAL_TOKEN_FUNCTION == token->type)
		{
			eval_filter_apply_func(ctx, token, &stack, &output);
		}
		else if (ZBX_EVAL_TOKEN_NOP != token->type)
		{
			zbx_vector_uint64_append(&output, stack.values_num);
			zbx_vector_eval_token_append_ptr(&stack, token);
		}
	}

	ret = eval_generate_filter(ctx, &stack, groups, filter, error);
out:
	zbx_vector_uint64_destroy(&output);
	zbx_vector_eval_token_destroy(&stack);

	return ret;
}