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
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
|
/*
** Zabbix
** Copyright (C) 2001-2019 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 "valuecache.h"
#include "dbcache.h"
#include "checks_aggregate.h"
#define ZBX_VALUE_FUNC_MIN 0
#define ZBX_VALUE_FUNC_AVG 1
#define ZBX_VALUE_FUNC_MAX 2
#define ZBX_VALUE_FUNC_SUM 3
#define ZBX_VALUE_FUNC_COUNT 4
#define ZBX_VALUE_FUNC_LAST 5
/******************************************************************************
* *
* Function: evaluate_history_func_min *
* *
* Purpose: calculate minimum value from the history value vector *
* *
* Parameters: values - [IN] a vector containing history values *
* value_type - [IN] the type of values. Only float/uint64 *
* values are supported. *
* result - [OUT] the resulting value *
* *
******************************************************************************/
static void evaluate_history_func_min(zbx_vector_history_record_t *values, int value_type, history_value_t *result)
{
int i;
*result = values->values[0].value;
if (ITEM_VALUE_TYPE_UINT64 == value_type)
{
for (i = 1; i < values->values_num; i++)
if (values->values[i].value.ui64 < result->ui64)
result->ui64 = values->values[i].value.ui64;
}
else
{
for (i = 1; i < values->values_num; i++)
if (values->values[i].value.dbl < result->dbl)
result->dbl = values->values[i].value.dbl;
}
}
/******************************************************************************
* *
* Function: evaluate_history_func_max *
* *
* Purpose: calculate maximum value from the history value vector *
* *
* Parameters: values - [IN] a vector containing history values *
* value_type - [IN] the type of values. Only float/uint64 *
* values are supported. *
* result - [OUT] the resulting value *
* *
******************************************************************************/
static void evaluate_history_func_max(zbx_vector_history_record_t *values, int value_type, history_value_t *result)
{
int i;
*result = values->values[0].value;
if (ITEM_VALUE_TYPE_UINT64 == value_type)
{
for (i = 1; i < values->values_num; i++)
if (values->values[i].value.ui64 > result->ui64)
result->ui64 = values->values[i].value.ui64;
}
else
{
for (i = 1; i < values->values_num; i++)
if (values->values[i].value.dbl > result->dbl)
result->dbl = values->values[i].value.dbl;
}
}
/******************************************************************************
* *
* Function: evaluate_history_func_sum *
* *
* Purpose: calculate sum of values from the history value vector *
* *
* Parameters: values - [IN] a vector containing history values *
* value_type - [IN] the type of values. Only float/uint64 *
* values are supported. *
* result - [OUT] the resulting value *
* *
******************************************************************************/
static void evaluate_history_func_sum(zbx_vector_history_record_t *values, int value_type, history_value_t *result)
{
int i;
if (ITEM_VALUE_TYPE_UINT64 == value_type)
{
result->ui64 = 0;
for (i = 0; i < values->values_num; i++)
result->ui64 += values->values[i].value.ui64;
}
else
{
result->dbl = 0;
for (i = 0; i < values->values_num; i++)
result->dbl += values->values[i].value.dbl;
}
}
/******************************************************************************
* *
* Function: evaluate_history_func_avg *
* *
* Purpose: calculate average value of values from the history value vector *
* *
* Parameters: values - [IN] a vector containing history values *
* value_type - [IN] the type of values. Only float/uint64 *
* values are supported. *
* result - [OUT] the resulting value *
* *
******************************************************************************/
static void evaluate_history_func_avg(zbx_vector_history_record_t *values, int value_type, history_value_t *result)
{
evaluate_history_func_sum(values, value_type, result);
if (ITEM_VALUE_TYPE_UINT64 == value_type)
result->ui64 /= values->values_num;
else
result->dbl /= values->values_num;
}
/******************************************************************************
* *
* Function: evaluate_history_func_count *
* *
* Purpose: calculate number of values in value vector *
* *
* Parameters: values - [IN] a vector containing history values *
* value_type - [IN] the type of values. Only float/uint64 *
* values are supported. *
* result - [OUT] the resulting value *
* *
******************************************************************************/
static void evaluate_history_func_count(zbx_vector_history_record_t *values, int value_type,
history_value_t *result)
{
if (ITEM_VALUE_TYPE_UINT64 == value_type)
result->ui64 = values->values_num;
else
result->dbl = values->values_num;
}
/******************************************************************************
* *
* Function: evaluate_history_func_last *
* *
* Purpose: calculate the last (newest) value in value vector *
* *
* Parameters: values - [IN] a vector containing history values *
* result - [OUT] the resulting value *
* *
******************************************************************************/
static void evaluate_history_func_last(zbx_vector_history_record_t *values, history_value_t *result)
{
*result = values->values[0].value;
}
/******************************************************************************
* *
* Function: evaluate_history_func *
* *
* Purpose: calculate function with values from value vector *
* *
* Parameters: values - [IN] a vector containing history values *
* value_type - [IN] the type of values. Only float/uint64 *
* values are supported. *
* func - [IN] the function to calculate. Only *
* ZBX_VALUE_FUNC_MIN, ZBX_VALUE_FUNC_AVG, *
* ZBX_VALUE_FUNC_MAX, ZBX_VALUE_FUNC_SUM, *
* ZBX_VALUE_FUNC_COUNT, ZBX_VALUE_FUNC_LAST *
* functions are supported. *
* result - [OUT] the resulting value *
* *
******************************************************************************/
static void evaluate_history_func(zbx_vector_history_record_t *values, int value_type, int func,
history_value_t *result)
{
switch (func)
{
case ZBX_VALUE_FUNC_MIN:
evaluate_history_func_min(values, value_type, result);
break;
case ZBX_VALUE_FUNC_AVG:
evaluate_history_func_avg(values, value_type, result);
break;
case ZBX_VALUE_FUNC_MAX:
evaluate_history_func_max(values, value_type, result);
break;
case ZBX_VALUE_FUNC_SUM:
evaluate_history_func_sum(values, value_type, result);
break;
case ZBX_VALUE_FUNC_COUNT:
evaluate_history_func_count(values, value_type, result);
break;
case ZBX_VALUE_FUNC_LAST:
evaluate_history_func_last(values, result);
break;
}
}
/******************************************************************************
* *
* Function: quote_string *
* *
* Purpose: quotes string by enclosing it in double quotes and escaping *
* double quotes inside string with '\'. *
* *
* Parameters: str - [IN/OUT] the string to quote *
* sz_str - [IN] the string length *
* *
* Comments: The '\' character itself is not quoted. As the result if string *
* ends with '\' it can be quoted (for example for error messages), *
* but it's impossible to unquote it. *
* *
******************************************************************************/
static void quote_string(char **str, size_t sz_src)
{
size_t sz_dst;
sz_dst = zbx_get_escape_string_len(*str, "\"") + 3;
*str = (char *)zbx_realloc(*str, sz_dst);
(*str)[--sz_dst] = '\0';
(*str)[--sz_dst] = '"';
while (0 < sz_src)
{
(*str)[--sz_dst] = (*str)[--sz_src];
if ('"' == (*str)[sz_src])
(*str)[--sz_dst] = '\\';
}
(*str)[--sz_dst] = '"';
}
/******************************************************************************
* *
* Function: aggregate_quote_groups *
* *
* Purpose: quotes the individual groups in the list if necessary *
* *
******************************************************************************/
static void aggregate_quote_groups(char **str, size_t *str_alloc, size_t *str_offset, const char *groups)
{
int i, num;
char *group, *separator = "";
num = num_param(groups);
for (i = 1; i <= num; i++)
{
if (NULL == (group = get_param_dyn(groups, i)))
continue;
zbx_strcpy_alloc(str, str_alloc, str_offset, separator);
separator = (char *)", ";
quote_string(&group, strlen(group));
zbx_strcpy_alloc(str, str_alloc, str_offset, group);
zbx_free(group);
}
}
/******************************************************************************
* *
* Function: aggregate_get_items *
* *
* Purpose: get array of items specified by key for selected groups *
* (including nested groups) *
* *
* Parameters: itemids - [OUT] list of item ids *
* groups - [IN] list of comma-separated host groups *
* itemkey - [IN] item key to aggregate *
* error - [OUT] the error message *
* *
* Return value: SUCCEED - item identifier(s) were retrieved successfully *
* FAIL - no items matching the specified groups or keys *
* *
******************************************************************************/
static int aggregate_get_items(zbx_vector_uint64_t *itemids, const char *groups, const char *itemkey, char **error)
{
const char *__function_name = "aggregate_get_items";
char *group, *esc;
DB_RESULT result;
DB_ROW row;
zbx_uint64_t itemid;
char *sql = NULL;
size_t sql_alloc = ZBX_KIBIBYTE, sql_offset = 0, error_alloc = 0, error_offset = 0;
int num, n, ret = FAIL;
zbx_vector_uint64_t groupids;
zbx_vector_str_t group_names;
zabbix_log(LOG_LEVEL_DEBUG, "In %s() groups:'%s' itemkey:'%s'", __function_name, groups, itemkey);
zbx_vector_uint64_create(&groupids);
zbx_vector_str_create(&group_names);
num = num_param(groups);
for (n = 1; n <= num; n++)
{
if (NULL == (group = get_param_dyn(groups, n)))
continue;
zbx_vector_str_append(&group_names, group);
}
zbx_dc_get_nested_hostgroupids_by_names(group_names.values, group_names.values_num, &groupids);
zbx_vector_str_clear_ext(&group_names, zbx_str_free);
zbx_vector_str_destroy(&group_names);
if (0 == groupids.values_num)
{
zbx_strcpy_alloc(error, &error_alloc, &error_offset, "None of the groups in list ");
aggregate_quote_groups(error, &error_alloc, &error_offset, groups);
zbx_strcpy_alloc(error, &error_alloc, &error_offset, " is correct.");
goto out;
}
sql = (char *)zbx_malloc(sql, sql_alloc);
esc = DBdyn_escape_string(itemkey);
zbx_snprintf_alloc(&sql, &sql_alloc, &sql_offset,
"select distinct i.itemid"
" from items i,hosts h,hosts_groups hg"
" where i.hostid=h.hostid"
" and h.hostid=hg.hostid"
" and i.key_='%s'"
" and i.status=%d"
" and i.state=%d"
" and h.status=%d"
" and",
esc, ITEM_STATUS_ACTIVE, ITEM_STATE_NORMAL, HOST_STATUS_MONITORED);
zbx_free(esc);
DBadd_condition_alloc(&sql, &sql_alloc, &sql_offset, "hg.groupid", groupids.values, groupids.values_num);
result = DBselect("%s", sql);
zbx_free(sql);
while (NULL != (row = DBfetch(result)))
{
ZBX_STR2UINT64(itemid, row[0]);
zbx_vector_uint64_append(itemids, itemid);
}
DBfree_result(result);
if (0 == itemids->values_num)
{
zbx_snprintf_alloc(error, &error_alloc, &error_offset, "No items for key \"%s\" in group(s) ", itemkey);
aggregate_quote_groups(error, &error_alloc, &error_offset, groups);
zbx_chrcpy_alloc(error, &error_alloc, &error_offset, '.');
goto out;
}
zbx_vector_uint64_sort(itemids, ZBX_DEFAULT_UINT64_COMPARE_FUNC);
ret = SUCCEED;
out:
zbx_vector_uint64_destroy(&groupids);
zabbix_log(LOG_LEVEL_DEBUG, "End of %s()", __function_name);
return ret;
}
/******************************************************************************
* *
* Function: evaluate_aggregate *
* *
* Parameters: item - [IN] aggregated item *
* grp_func - [IN] one of ZBX_GRP_FUNC_* *
* groups - [IN] list of comma-separated host groups *
* itemkey - [IN] item key to aggregate *
* item_func - [IN] one of ZBX_VALUE_FUNC_* *
* param - [IN] item_func parameter (optional) *
* *
* Return value: SUCCEED - aggregate item evaluated successfully *
* FAIL - otherwise *
* *
******************************************************************************/
static int evaluate_aggregate(DC_ITEM *item, AGENT_RESULT *res, int grp_func, const char *groups,
const char *itemkey, int item_func, const char *param)
{
const char *__function_name = "evaluate_aggregate";
zbx_vector_uint64_t itemids;
history_value_t value, item_result;
zbx_history_record_t group_value;
int ret = FAIL, *errcodes = NULL, i, count, seconds;
DC_ITEM *items = NULL;
zbx_vector_history_record_t values, group_values;
char *error = NULL;
zbx_timespec_t ts;
zabbix_log(LOG_LEVEL_DEBUG, "In %s() grp_func:%d groups:'%s' itemkey:'%s' item_func:%d param:'%s'",
__function_name, grp_func, groups, itemkey, item_func, ZBX_NULL2STR(param));
zbx_timespec(&ts);
zbx_vector_uint64_create(&itemids);
if (FAIL == aggregate_get_items(&itemids, groups, itemkey, &error))
{
SET_MSG_RESULT(res, error);
goto clean1;
}
memset(&value, 0, sizeof(value));
zbx_history_record_vector_create(&group_values);
items = (DC_ITEM *)zbx_malloc(items, sizeof(DC_ITEM) * itemids.values_num);
errcodes = (int *)zbx_malloc(errcodes, sizeof(int) * itemids.values_num);
DCconfig_get_items_by_itemids(items, itemids.values, errcodes, itemids.values_num);
if (ZBX_VALUE_FUNC_LAST == item_func)
{
count = 1;
seconds = 0;
}
else
{
if (FAIL == is_time_suffix(param, &seconds, ZBX_LENGTH_UNLIMITED))
{
SET_MSG_RESULT(res, zbx_strdup(NULL, "Invalid fourth parameter."));
goto clean2;
}
count = 0;
}
for (i = 0; i < itemids.values_num; i++)
{
if (SUCCEED != errcodes[i])
continue;
if (ITEM_STATUS_ACTIVE != items[i].status)
continue;
if (HOST_STATUS_MONITORED != items[i].host.status)
continue;
if (ITEM_VALUE_TYPE_FLOAT != items[i].value_type && ITEM_VALUE_TYPE_UINT64 != items[i].value_type)
continue;
zbx_history_record_vector_create(&values);
if (SUCCEED == zbx_vc_get_values(items[i].itemid, items[i].value_type, &values, seconds, count, &ts) &&
0 < values.values_num)
{
evaluate_history_func(&values, items[i].value_type, item_func, &item_result);
if (item->value_type == items[i].value_type)
group_value.value = item_result;
else
{
if (ITEM_VALUE_TYPE_UINT64 == item->value_type)
group_value.value.ui64 = (zbx_uint64_t)item_result.dbl;
else
group_value.value.dbl = (double)item_result.ui64;
}
zbx_vector_history_record_append_ptr(&group_values, &group_value);
}
zbx_history_record_vector_destroy(&values, items[i].value_type);
}
if (0 == group_values.values_num)
{
char *tmp = NULL;
size_t tmp_alloc = 0, tmp_offset = 0;
aggregate_quote_groups(&tmp, &tmp_alloc, &tmp_offset, groups);
SET_MSG_RESULT(res, zbx_dsprintf(NULL, "No values for key \"%s\" in group(s) %s.", itemkey, tmp));
zbx_free(tmp);
goto clean2;
}
evaluate_history_func(&group_values, item->value_type, grp_func, &value);
if (ITEM_VALUE_TYPE_FLOAT == item->value_type)
SET_DBL_RESULT(res, value.dbl);
else
SET_UI64_RESULT(res, value.ui64);
ret = SUCCEED;
clean2:
DCconfig_clean_items(items, errcodes, itemids.values_num);
zbx_free(errcodes);
zbx_free(items);
zbx_history_record_vector_destroy(&group_values, item->value_type);
clean1:
zbx_vector_uint64_destroy(&itemids);
zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, zbx_result_string(ret));
return ret;
}
/******************************************************************************
* *
* Function: get_value_aggregate *
* *
* Purpose: retrieve data from Zabbix server (aggregate items) *
* *
* Parameters: item - item we are interested in *
* *
* Return value: SUCCEED - data successfully retrieved and stored in result *
* and result_str (as string) *
* NOTSUPPORTED - requested item is not supported *
* *
* Author: Alexei Vladishev *
* *
******************************************************************************/
int get_value_aggregate(DC_ITEM *item, AGENT_RESULT *result)
{
const char *__function_name = "get_value_aggregate";
AGENT_REQUEST request;
int ret = NOTSUPPORTED;
const char *tmp, *groups, *itemkey, *funcp = NULL;
int grp_func, item_func, params_num;
zabbix_log(LOG_LEVEL_DEBUG, "In %s() key:'%s'", __function_name, item->key_orig);
init_request(&request);
if (ITEM_VALUE_TYPE_FLOAT != item->value_type && ITEM_VALUE_TYPE_UINT64 != item->value_type)
{
SET_MSG_RESULT(result, zbx_strdup(NULL, "Value type must be Numeric for aggregate items"));
goto out;
}
if (SUCCEED != parse_item_key(item->key, &request))
{
SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid item key format."));
goto out;
}
if (0 == strcmp(get_rkey(&request), "grpmin"))
{
grp_func = ZBX_VALUE_FUNC_MIN;
}
else if (0 == strcmp(get_rkey(&request), "grpavg"))
{
grp_func = ZBX_VALUE_FUNC_AVG;
}
else if (0 == strcmp(get_rkey(&request), "grpmax"))
{
grp_func = ZBX_VALUE_FUNC_MAX;
}
else if (0 == strcmp(get_rkey(&request), "grpsum"))
{
grp_func = ZBX_VALUE_FUNC_SUM;
}
else
{
SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid item key."));
goto out;
}
params_num = get_rparams_num(&request);
if (3 > params_num || params_num > 4)
{
SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid number of parameters."));
goto out;
}
groups = get_rparam(&request, 0);
itemkey = get_rparam(&request, 1);
tmp = get_rparam(&request, 2);
if (0 == strcmp(tmp, "min"))
item_func = ZBX_VALUE_FUNC_MIN;
else if (0 == strcmp(tmp, "avg"))
item_func = ZBX_VALUE_FUNC_AVG;
else if (0 == strcmp(tmp, "max"))
item_func = ZBX_VALUE_FUNC_MAX;
else if (0 == strcmp(tmp, "sum"))
item_func = ZBX_VALUE_FUNC_SUM;
else if (0 == strcmp(tmp, "count"))
item_func = ZBX_VALUE_FUNC_COUNT;
else if (0 == strcmp(tmp, "last"))
item_func = ZBX_VALUE_FUNC_LAST;
else
{
SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid third parameter."));
goto out;
}
if (4 == params_num)
{
funcp = get_rparam(&request, 3);
}
else if (3 == params_num && ZBX_VALUE_FUNC_LAST != item_func)
{
SET_MSG_RESULT(result, zbx_strdup(NULL, "Invalid number of parameters."));
goto out;
}
if (SUCCEED != evaluate_aggregate(item, result, grp_func, groups, itemkey, item_func, funcp))
goto out;
ret = SUCCEED;
out:
free_request(&request);
zabbix_log(LOG_LEVEL_DEBUG, "End of %s():%s", __function_name, zbx_result_string(ret));
return ret;
}
|