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
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
|
/*
** 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 "proxy.h"
#include "zbxserver.h"
#include "zbxserialize.h"
#include "zbxipcservice.h"
#include "preproc.h"
#include "preprocessing.h"
#include "preproc_history.h"
#define PACKED_FIELD_RAW 0
#define PACKED_FIELD_STRING 1
#define MAX_VALUES_LOCAL 256
/* packed field data description */
typedef struct
{
const void *value; /* value to be packed */
zbx_uint32_t size; /* size of a value (can be 0 for strings) */
unsigned char type; /* field type */
}
zbx_packed_field_t;
#define PACKED_FIELD(value, size) \
(zbx_packed_field_t){(value), (size), (0 == (size) ? PACKED_FIELD_STRING : PACKED_FIELD_RAW)};
static zbx_ipc_message_t cached_message;
static int cached_values;
/******************************************************************************
* *
* Function: message_pack_data *
* *
* Purpose: helper for data packing based on defined format *
* *
* Parameters: message - [OUT] IPC message, can be NULL for buffer size *
* calculations *
* fields - [IN] the definition of data to be packed *
* count - [IN] field count *
* *
* Return value: size of packed data *
* *
******************************************************************************/
static zbx_uint32_t message_pack_data(zbx_ipc_message_t *message, zbx_packed_field_t *fields, int count)
{
int i;
zbx_uint32_t field_size, data_size = 0;
unsigned char *offset = NULL;
if (NULL != message)
{
/* recursive call to calculate required buffer size */
data_size = message_pack_data(NULL, fields, count);
message->size += data_size;
message->data = (unsigned char *)zbx_realloc(message->data, message->size);
offset = message->data + (message->size - data_size);
}
for (i = 0; i < count; i++)
{
field_size = fields[i].size;
if (NULL != offset)
{
/* data packing */
if (PACKED_FIELD_STRING == fields[i].type)
{
memcpy(offset, (zbx_uint32_t *)&field_size, sizeof(zbx_uint32_t));
if (0 != field_size && NULL != fields[i].value)
memcpy(offset + sizeof(zbx_uint32_t), fields[i].value, field_size);
field_size += sizeof(zbx_uint32_t);
}
else
memcpy(offset, fields[i].value, field_size);
offset += field_size;
}
else
{
/* size calculation */
if (PACKED_FIELD_STRING == fields[i].type)
{
field_size = (NULL != fields[i].value) ? strlen((const char *)fields[i].value) + 1 : 0;
fields[i].size = field_size;
field_size += sizeof(zbx_uint32_t);
}
data_size += field_size;
}
}
return data_size;
}
/******************************************************************************
* *
* Function: preprocessor_pack_value *
* *
* Purpose: pack item value data into a single buffer that can be used in IPC *
* *
* Parameters: message - [OUT] IPC message *
* value - [IN] value to be packed *
* *
* Return value: size of packed data *
* *
******************************************************************************/
static zbx_uint32_t preprocessor_pack_value(zbx_ipc_message_t *message, zbx_preproc_item_value_t *value)
{
zbx_packed_field_t fields[23], *offset = fields; /* 23 - max field count */
unsigned char ts_marker, result_marker, log_marker;
ts_marker = (NULL != value->ts);
result_marker = (NULL != value->result);
*offset++ = PACKED_FIELD(&value->itemid, sizeof(zbx_uint64_t));
*offset++ = PACKED_FIELD(&value->item_value_type, sizeof(unsigned char));
*offset++ = PACKED_FIELD(&value->item_flags, sizeof(unsigned char));
*offset++ = PACKED_FIELD(&value->state, sizeof(unsigned char));
*offset++ = PACKED_FIELD(value->error, 0);
*offset++ = PACKED_FIELD(&ts_marker, sizeof(unsigned char));
if (NULL != value->ts)
{
*offset++ = PACKED_FIELD(&value->ts->sec, sizeof(int));
*offset++ = PACKED_FIELD(&value->ts->ns, sizeof(int));
}
*offset++ = PACKED_FIELD(&result_marker, sizeof(unsigned char));
if (NULL != value->result)
{
*offset++ = PACKED_FIELD(&value->result->lastlogsize, sizeof(zbx_uint64_t));
*offset++ = PACKED_FIELD(&value->result->ui64, sizeof(zbx_uint64_t));
*offset++ = PACKED_FIELD(&value->result->dbl, sizeof(double));
*offset++ = PACKED_FIELD(value->result->str, 0);
*offset++ = PACKED_FIELD(value->result->text, 0);
*offset++ = PACKED_FIELD(value->result->msg, 0);
*offset++ = PACKED_FIELD(&value->result->type, sizeof(int));
*offset++ = PACKED_FIELD(&value->result->mtime, sizeof(int));
log_marker = (NULL != value->result->log);
*offset++ = PACKED_FIELD(&log_marker, sizeof(unsigned char));
if (NULL != value->result->log)
{
*offset++ = PACKED_FIELD(value->result->log->value, 0);
*offset++ = PACKED_FIELD(value->result->log->source, 0);
*offset++ = PACKED_FIELD(&value->result->log->timestamp, sizeof(int));
*offset++ = PACKED_FIELD(&value->result->log->severity, sizeof(int));
*offset++ = PACKED_FIELD(&value->result->log->logeventid, sizeof(int));
}
}
return message_pack_data(message, fields, offset - fields);
}
/******************************************************************************
* *
* Function: preprocessor_pack_variant *
* *
* Purpose: packs variant value for serialization *
* *
* Parameters: fields - [OUT] the packed fields *
* value - [IN] the value to pack *
* *
* Return value: The number of fields used. *
* *
* Comments: Don't pack local variables, only ones passed in parameters! *
* *
******************************************************************************/
static int preprocessor_pack_variant(zbx_packed_field_t *fields, const zbx_variant_t *value)
{
int offset = 0;
fields[offset++] = PACKED_FIELD(&value->type, sizeof(unsigned char));
switch (value->type)
{
case ZBX_VARIANT_UI64:
fields[offset++] = PACKED_FIELD(&value->data.ui64, sizeof(zbx_uint64_t));
break;
case ZBX_VARIANT_DBL:
fields[offset++] = PACKED_FIELD(&value->data.dbl, sizeof(double));
break;
case ZBX_VARIANT_STR:
fields[offset++] = PACKED_FIELD(value->data.str, 0);
break;
case ZBX_VARIANT_BIN:
fields[offset++] = PACKED_FIELD(value->data.bin, sizeof(zbx_uint32_t) +
zbx_variant_data_bin_get(value->data.bin, NULL));
break;
}
return offset;
}
/******************************************************************************
* *
* Function: preprocessor_pack_history *
* *
* Purpose: packs preprocessing history for serialization *
* *
* Parameters: fields - [OUT] the packed fields *
* history - [IN] the history to pack *
* *
* Return value: The number of fields used. *
* *
* Comments: Don't pack local variables, only ones passed in parameters! *
* *
******************************************************************************/
static int preprocessor_pack_history(zbx_packed_field_t *fields, const zbx_vector_ptr_t *history,
const int *history_num)
{
int i, offset = 0;
fields[offset++] = PACKED_FIELD(history_num, sizeof(int));
for (i = 0; i < *history_num; i++)
{
zbx_preproc_op_history_t *ophistory = (zbx_preproc_op_history_t *)history->values[i];
fields[offset++] = PACKED_FIELD(&ophistory->index, sizeof(int));
offset += preprocessor_pack_variant(&fields[offset], &ophistory->value);
fields[offset++] = PACKED_FIELD(&ophistory->ts.sec, sizeof(int));
fields[offset++] = PACKED_FIELD(&ophistory->ts.ns, sizeof(int));
}
return offset;
}
/******************************************************************************
* *
* Function: preprocessor_pack_step *
* *
* Purpose: packs preprocessing step for serialization *
* *
* Parameters: fields - [OUT] the packed fields *
* step - [IN] the step to pack *
* *
* Return value: The number of fields used. *
* *
* Comments: Don't pack local variables, only ones passed in parameters! *
* *
******************************************************************************/
static int preprocessor_pack_step(zbx_packed_field_t *fields, const zbx_preproc_op_t *step)
{
int offset = 0;
fields[offset++] = PACKED_FIELD(&step->type, sizeof(char));
fields[offset++] = PACKED_FIELD(step->params, 0);
fields[offset++] = PACKED_FIELD(&step->error_handler, sizeof(char));
fields[offset++] = PACKED_FIELD(step->error_handler_params, 0);
return offset;
}
/******************************************************************************
* *
* Function: preprocessor_pack_steps *
* *
* Purpose: packs preprocessing steps for serialization *
* *
* Parameters: fields - [OUT] the packed fields *
* steps - [IN] the steps to pack *
* steps_num - [IN] the number of steps *
* *
* Return value: The number of fields used. *
* *
* Comments: Don't pack local variables, only ones passed in parameters! *
* *
******************************************************************************/
static int preprocessor_pack_steps(zbx_packed_field_t *fields, const zbx_preproc_op_t *steps, const int *steps_num)
{
int i, offset = 0;
fields[offset++] = PACKED_FIELD(steps_num, sizeof(int));
for (i = 0; i < *steps_num; i++)
offset += preprocessor_pack_step(&fields[offset], &steps[i]);
return offset;
}
/******************************************************************************
* *
* Function: preprocesser_unpack_variant *
* *
* Purpose: unpacks serialized variant value *
* *
* Parameters: data - [IN] the serialized data *
* value - [OUT] the value *
* *
* Return value: The number of bytes parsed. *
* *
******************************************************************************/
static int preprocesser_unpack_variant(const unsigned char *data, zbx_variant_t *value)
{
const unsigned char *offset = data;
zbx_uint32_t value_len;
offset += zbx_deserialize_char(offset, &value->type);
switch (value->type)
{
case ZBX_VARIANT_UI64:
offset += zbx_deserialize_uint64(offset, &value->data.ui64);
break;
case ZBX_VARIANT_DBL:
offset += zbx_deserialize_double(offset, &value->data.dbl);
break;
case ZBX_VARIANT_STR:
offset += zbx_deserialize_str(offset, &value->data.str, value_len);
break;
case ZBX_VARIANT_BIN:
offset += zbx_deserialize_bin(offset, &value->data.bin, value_len);
break;
}
return offset - data;
}
/******************************************************************************
* *
* Function: preprocesser_unpack_history *
* *
* Purpose: unpacks serialized preprocessing history *
* *
* Parameters: data - [IN] the serialized data *
* history - [OUT] the history *
* *
* Return value: The number of bytes parsed. *
* *
******************************************************************************/
static int preprocesser_unpack_history(const unsigned char *data, zbx_vector_ptr_t *history)
{
const unsigned char *offset = data;
int i, history_num;
offset += zbx_deserialize_int(offset, &history_num);
if (0 != history_num)
{
zbx_vector_ptr_reserve(history, history_num);
for (i = 0; i < history_num; i++)
{
zbx_preproc_op_history_t *ophistory;
ophistory = zbx_malloc(NULL, sizeof(zbx_preproc_op_history_t));
offset += zbx_deserialize_int(offset, &ophistory->index);
offset += preprocesser_unpack_variant(offset, &ophistory->value);
offset += zbx_deserialize_int(offset, &ophistory->ts.sec);
offset += zbx_deserialize_int(offset, &ophistory->ts.ns);
zbx_vector_ptr_append(history, ophistory);
}
}
return offset - data;
}
/******************************************************************************
* *
* Function: preprocessor_unpack_step *
* *
* Purpose: unpacks serialized preprocessing step *
* *
* Parameters: data - [IN] the serialized data *
* step - [OUT] the preprocessing step *
* *
* Return value: The number of bytes parsed. *
* *
******************************************************************************/
static int preprocessor_unpack_step(const unsigned char *data, zbx_preproc_op_t *step)
{
const unsigned char *offset = data;
zbx_uint32_t value_len;
offset += zbx_deserialize_char(offset, &step->type);
offset += zbx_deserialize_str_ptr(offset, step->params, value_len);
offset += zbx_deserialize_char(offset, &step->error_handler);
offset += zbx_deserialize_str_ptr(offset, step->error_handler_params, value_len);
return offset - data;
}
/******************************************************************************
* *
* Function: preprocessor_unpack_steps *
* *
* Purpose: unpacks serialized preprocessing steps *
* *
* Parameters: data - [IN] the serialized data *
* steps - [OUT] the preprocessing steps *
* steps_num - [OUT] the number of steps *
* *
* Return value: The number of bytes parsed. *
* *
******************************************************************************/
static int preprocessor_unpack_steps(const unsigned char *data, zbx_preproc_op_t **steps, int *steps_num)
{
const unsigned char *offset = data;
int i;
offset += zbx_deserialize_int(offset, steps_num);
if (0 < *steps_num)
{
*steps = (zbx_preproc_op_t *)zbx_malloc(NULL, sizeof(zbx_preproc_op_t) * (*steps_num));
for (i = 0; i < *steps_num; i++)
offset += preprocessor_unpack_step(offset, *steps + i);
}
else
*steps = NULL;
return offset - data;
}
/******************************************************************************
* *
* Function: zbx_preprocessor_pack_task *
* *
* Purpose: pack preprocessing task data into a single buffer that can be *
* used in IPC *
* *
* Parameters: data - [OUT] memory buffer for packed data *
* itemid - [IN] item id *
* value_type - [IN] item value type *
* ts - [IN] value timestamp *
* value - [IN] item value *
* history - [IN] history data (can be NULL) *
* steps - [IN] preprocessing steps *
* steps_num - [IN] preprocessing step count *
* *
* Return value: size of packed data *
* *
******************************************************************************/
zbx_uint32_t zbx_preprocessor_pack_task(unsigned char **data, zbx_uint64_t itemid, unsigned char value_type,
zbx_timespec_t *ts, zbx_variant_t *value, const zbx_vector_ptr_t *history,
const zbx_preproc_op_t *steps, int steps_num)
{
zbx_packed_field_t *offset, *fields;
unsigned char ts_marker;
zbx_uint32_t size;
int history_num;
zbx_ipc_message_t message;
history_num = (NULL != history ? history->values_num : 0);
/* 9 is a max field count (without preprocessing step and history fields) */
fields = (zbx_packed_field_t *)zbx_malloc(NULL, (9 + steps_num * 4 + history_num * 5)
* sizeof(zbx_packed_field_t));
offset = fields;
ts_marker = (NULL != ts);
*offset++ = PACKED_FIELD(&itemid, sizeof(zbx_uint64_t));
*offset++ = PACKED_FIELD(&value_type, sizeof(unsigned char));
*offset++ = PACKED_FIELD(&ts_marker, sizeof(unsigned char));
if (NULL != ts)
{
*offset++ = PACKED_FIELD(&ts->sec, sizeof(int));
*offset++ = PACKED_FIELD(&ts->ns, sizeof(int));
}
offset += preprocessor_pack_variant(offset, value);
offset += preprocessor_pack_history(offset, history, &history_num);
offset += preprocessor_pack_steps(offset, steps, &steps_num);
zbx_ipc_message_init(&message);
size = message_pack_data(&message, fields, offset - fields);
*data = message.data;
zbx_free(fields);
return size;
}
/******************************************************************************
* *
* Function: zbx_preprocessor_pack_result *
* *
* Purpose: pack preprocessing result data into a single buffer that can be *
* used in IPC *
* *
* Parameters: data - [OUT] memory buffer for packed data *
* value - [IN] result value *
* history - [IN] item history data *
* error - [IN] preprocessing error *
* *
* Return value: size of packed data *
* *
******************************************************************************/
zbx_uint32_t zbx_preprocessor_pack_result(unsigned char **data, zbx_variant_t *value,
const zbx_vector_ptr_t *history, char *error)
{
zbx_packed_field_t *offset, *fields;
zbx_uint32_t size;
zbx_ipc_message_t message;
int history_num;
history_num = history->values_num;
/* 4 is a max field count (without history fields) */
fields = (zbx_packed_field_t *)zbx_malloc(NULL, (4 + history_num * 5) * sizeof(zbx_packed_field_t));
offset = fields;
offset += preprocessor_pack_variant(offset, value);
offset += preprocessor_pack_history(offset, history, &history_num);
*offset++ = PACKED_FIELD(error, 0);
zbx_ipc_message_init(&message);
size = message_pack_data(&message, fields, offset - fields);
*data = message.data;
zbx_free(fields);
return size;
}
/******************************************************************************
* *
* Function: zbx_preprocessor_pack_test_result *
* *
* Purpose: pack preprocessing result data into a single buffer that can be *
* used in IPC *
* *
* Parameters: data - [OUT] memory buffer for packed data *
* ret - [IN] return code *
* results - [IN] the preprocessing step results *
* results_num - [IN] the number of preprocessing step results *
* history - [IN] item history data *
* error - [IN] preprocessing error *
* *
* Return value: size of packed data *
* *
******************************************************************************/
zbx_uint32_t zbx_preprocessor_pack_test_result(unsigned char **data, const zbx_preproc_result_t *results,
int results_num, const zbx_vector_ptr_t *history, const char *error)
{
zbx_packed_field_t *offset, *fields;
zbx_uint32_t size;
zbx_ipc_message_t message;
int i, history_num;
history_num = history->values_num;
fields = (zbx_packed_field_t *)zbx_malloc(NULL, (3 + history_num * 5 + results_num * 4) *
sizeof(zbx_packed_field_t));
offset = fields;
*offset++ = PACKED_FIELD(&results_num, sizeof(int));
for (i = 0; i < results_num; i++)
{
offset += preprocessor_pack_variant(offset, &results[i].value);
*offset++ = PACKED_FIELD(results[i].error, 0);
*offset++ = PACKED_FIELD(&results[i].action, sizeof(unsigned char));
}
offset += preprocessor_pack_history(offset, history, &history_num);
*offset++ = PACKED_FIELD(error, 0);
zbx_ipc_message_init(&message);
size = message_pack_data(&message, fields, offset - fields);
*data = message.data;
zbx_free(fields);
return size;
}
/******************************************************************************
* *
* Function: zbx_preprocessor_unpack_value *
* *
* Purpose: unpack item value data from IPC data buffer *
* *
* Parameters: value - [OUT] unpacked item value *
* data - [IN] IPC data buffer *
* *
* Return value: size of packed data *
* *
******************************************************************************/
zbx_uint32_t zbx_preprocessor_unpack_value(zbx_preproc_item_value_t *value, unsigned char *data)
{
zbx_uint32_t value_len;
zbx_timespec_t *timespec = NULL;
AGENT_RESULT *agent_result = NULL;
zbx_log_t *log = NULL;
unsigned char *offset = data, ts_marker, result_marker, log_marker;
offset += zbx_deserialize_uint64(offset, &value->itemid);
offset += zbx_deserialize_char(offset, &value->item_value_type);
offset += zbx_deserialize_char(offset, &value->item_flags);
offset += zbx_deserialize_char(offset, &value->state);
offset += zbx_deserialize_str(offset, &value->error, value_len);
offset += zbx_deserialize_char(offset, &ts_marker);
if (0 != ts_marker)
{
timespec = (zbx_timespec_t *)zbx_malloc(NULL, sizeof(zbx_timespec_t));
offset += zbx_deserialize_int(offset, ×pec->sec);
offset += zbx_deserialize_int(offset, ×pec->ns);
}
value->ts = timespec;
offset += zbx_deserialize_char(offset, &result_marker);
if (0 != result_marker)
{
agent_result = (AGENT_RESULT *)zbx_malloc(NULL, sizeof(AGENT_RESULT));
offset += zbx_deserialize_uint64(offset, &agent_result->lastlogsize);
offset += zbx_deserialize_uint64(offset, &agent_result->ui64);
offset += zbx_deserialize_double(offset, &agent_result->dbl);
offset += zbx_deserialize_str(offset, &agent_result->str, value_len);
offset += zbx_deserialize_str(offset, &agent_result->text, value_len);
offset += zbx_deserialize_str(offset, &agent_result->msg, value_len);
offset += zbx_deserialize_int(offset, &agent_result->type);
offset += zbx_deserialize_int(offset, &agent_result->mtime);
offset += zbx_deserialize_char(offset, &log_marker);
if (0 != log_marker)
{
log = (zbx_log_t *)zbx_malloc(NULL, sizeof(zbx_log_t));
offset += zbx_deserialize_str(offset, &log->value, value_len);
offset += zbx_deserialize_str(offset, &log->source, value_len);
offset += zbx_deserialize_int(offset, &log->timestamp);
offset += zbx_deserialize_int(offset, &log->severity);
offset += zbx_deserialize_int(offset, &log->logeventid);
}
agent_result->log = log;
}
value->result = agent_result;
return offset - data;
}
/******************************************************************************
* *
* Function: zbx_preprocessor_unpack_task *
* *
* Purpose: unpack preprocessing task data from IPC data buffer *
* *
* Parameters: itemid - [OUT] itemid *
* value_type - [OUT] item value type *
* ts - [OUT] value timestamp *
* value - [OUT] item value *
* history - [OUT] history data *
* steps - [OUT] preprocessing steps *
* steps_num - [OUT] preprocessing step count *
* data - [IN] IPC data buffer *
* *
******************************************************************************/
void zbx_preprocessor_unpack_task(zbx_uint64_t *itemid, unsigned char *value_type, zbx_timespec_t **ts,
zbx_variant_t *value, zbx_vector_ptr_t *history, zbx_preproc_op_t **steps,
int *steps_num, const unsigned char *data)
{
const unsigned char *offset = data;
unsigned char ts_marker;
zbx_timespec_t *timespec = NULL;
offset += zbx_deserialize_uint64(offset, itemid);
offset += zbx_deserialize_char(offset, value_type);
offset += zbx_deserialize_char(offset, &ts_marker);
if (0 != ts_marker)
{
timespec = (zbx_timespec_t *)zbx_malloc(NULL, sizeof(zbx_timespec_t));
offset += zbx_deserialize_int(offset, ×pec->sec);
offset += zbx_deserialize_int(offset, ×pec->ns);
}
*ts = timespec;
offset += preprocesser_unpack_variant(offset, value);
offset += preprocesser_unpack_history(offset, history);
(void)preprocessor_unpack_steps(offset, steps, steps_num);
}
/******************************************************************************
* *
* Function: zbx_preprocessor_unpack_result *
* *
* Purpose: unpack preprocessing task data from IPC data buffer *
* *
* Parameters: value - [OUT] result value *
* history - [OUT] item history data *
* error - [OUT] preprocessing error *
* data - [IN] IPC data buffer *
* *
******************************************************************************/
void zbx_preprocessor_unpack_result(zbx_variant_t *value, zbx_vector_ptr_t *history, char **error,
const unsigned char *data)
{
zbx_uint32_t value_len;
const unsigned char *offset = data;
offset += preprocesser_unpack_variant(offset, value);
offset += preprocesser_unpack_history(offset, history);
(void)zbx_deserialize_str(offset, error, value_len);
}
/******************************************************************************
* *
* Function: zbx_preprocessor_unpack_test_result *
* *
* Purpose: unpack preprocessing test data from IPC data buffer *
* *
* Parameters: results - [OUT] the preprocessing step results *
* history - [OUT] item history data *
* error - [OUT] preprocessing error *
* data - [IN] IPC data buffer *
* *
******************************************************************************/
void zbx_preprocessor_unpack_test_result(zbx_vector_ptr_t *results, zbx_vector_ptr_t *history,
char **error, const unsigned char *data)
{
zbx_uint32_t value_len;
const unsigned char *offset = data;
int i, results_num;
zbx_preproc_result_t *result;
offset += zbx_deserialize_int(offset, &results_num);
zbx_vector_ptr_reserve(results, results_num);
for (i = 0; i < results_num; i++)
{
result = (zbx_preproc_result_t *)zbx_malloc(NULL, sizeof(zbx_preproc_result_t));
offset += preprocesser_unpack_variant(offset, &result->value);
offset += zbx_deserialize_str(offset, &result->error, value_len);
offset += zbx_deserialize_char(offset, &result->action);
zbx_vector_ptr_append(results, result);
}
offset += preprocesser_unpack_history(offset, history);
(void)zbx_deserialize_str(offset, error, value_len);
}
/******************************************************************************
* *
* Function: preprocessor_send *
* *
* Purpose: sends command to preprocessor manager *
* *
* Parameters: code - [IN] message code *
* data - [IN] message data *
* size - [IN] message data size *
* response - [OUT] response message (can be NULL if response is *
* not requested) *
* *
******************************************************************************/
static void preprocessor_send(zbx_uint32_t code, unsigned char *data, zbx_uint32_t size,
zbx_ipc_message_t *response)
{
char *error = NULL;
static zbx_ipc_socket_t socket = {0};
/* each process has a permanent connection to preprocessing manager */
if (0 == socket.fd && FAIL == zbx_ipc_socket_open(&socket, ZBX_IPC_SERVICE_PREPROCESSING, SEC_PER_MIN,
&error))
{
zabbix_log(LOG_LEVEL_CRIT, "cannot connect to preprocessing service: %s", error);
exit(EXIT_FAILURE);
}
if (FAIL == zbx_ipc_socket_write(&socket, code, data, size))
{
zabbix_log(LOG_LEVEL_CRIT, "cannot send data to preprocessing service");
exit(EXIT_FAILURE);
}
if (NULL != response && FAIL == zbx_ipc_socket_read(&socket, response))
{
zabbix_log(LOG_LEVEL_CRIT, "cannot receive data from preprocessing service");
exit(EXIT_FAILURE);
}
}
/******************************************************************************
* *
* Function: zbx_preprocess_item_value *
* *
* Purpose: perform item value preprocessing and dependend item processing *
* *
* Parameters: itemid - [IN] the itemid *
* item_value_type - [IN] the item value type *
* item_flags - [IN] the item flags (e. g. lld rule) *
* result - [IN] agent result containing the value *
* to add *
* ts - [IN] the value timestamp *
* state - [IN] the item state *
* error - [IN] the error message in case item state is *
* ITEM_STATE_NOTSUPPORTED *
* *
******************************************************************************/
void zbx_preprocess_item_value(zbx_uint64_t itemid, unsigned char item_value_type, unsigned char item_flags,
AGENT_RESULT *result, zbx_timespec_t *ts, unsigned char state, char *error)
{
zbx_preproc_item_value_t value = {.itemid = itemid, .item_value_type = item_value_type, .result = result,
.error = error, .item_flags = item_flags, .state = state, .ts = ts};
zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __func__);
preprocessor_pack_value(&cached_message, &value);
if (MAX_VALUES_LOCAL < ++cached_values)
zbx_preprocessor_flush();
zabbix_log(LOG_LEVEL_DEBUG, "End of %s()", __func__);
}
/******************************************************************************
* *
* Function: zbx_preprocessor_flush *
* *
* Purpose: send flush command to preprocessing manager *
* *
******************************************************************************/
void zbx_preprocessor_flush(void)
{
if (0 < cached_message.size)
{
preprocessor_send(ZBX_IPC_PREPROCESSOR_REQUEST, cached_message.data, cached_message.size, NULL);
zbx_ipc_message_clean(&cached_message);
zbx_ipc_message_init(&cached_message);
cached_values = 0;
}
}
/******************************************************************************
* *
* Function: zbx_preprocessor_get_queue_size *
* *
* Purpose: get queue size (enqueued value count) of preprocessing manager *
* *
* Return value: enqueued item count *
* *
******************************************************************************/
zbx_uint64_t zbx_preprocessor_get_queue_size(void)
{
zbx_uint64_t size;
zbx_ipc_message_t message;
zbx_ipc_message_init(&message);
preprocessor_send(ZBX_IPC_PREPROCESSOR_QUEUE, NULL, 0, &message);
memcpy(&size, message.data, sizeof(zbx_uint64_t));
zbx_ipc_message_clean(&message);
return size;
}
/******************************************************************************
* *
* Function: zbx_preproc_op_free *
* *
* Purpose: frees preprocessing step *
* *
******************************************************************************/
void zbx_preproc_op_free(zbx_preproc_op_t *op)
{
zbx_free(op->params);
zbx_free(op->error_handler_params);
zbx_free(op);
}
/******************************************************************************
* *
* Function: zbx_preproc_result_free *
* *
* Purpose: frees preprocessing step test result *
* *
******************************************************************************/
void zbx_preproc_result_free(zbx_preproc_result_t *result)
{
zbx_variant_clear(&result->value);
zbx_free(result->error);
zbx_free(result);
}
/******************************************************************************
* *
* Function: preprocessor_pack_test_request *
* *
* Purpose: packs preprocessing step request for serialization *
* *
* Return value: The size of packed data *
* *
******************************************************************************/
static zbx_uint32_t preprocessor_pack_test_request(unsigned char **data, unsigned char value_type,
const char *value, const zbx_timespec_t *ts, const zbx_vector_ptr_t *history,
const zbx_vector_ptr_t *steps)
{
zbx_packed_field_t *offset, *fields;
zbx_uint32_t size;
int i, history_num;
zbx_ipc_message_t message;
history_num = (NULL != history ? history->values_num : 0);
/* 6 is a max field count (without preprocessing step and history fields) */
fields = (zbx_packed_field_t *)zbx_malloc(NULL, (6 + steps->values_num * 4 + history_num * 5)
* sizeof(zbx_packed_field_t));
offset = fields;
*offset++ = PACKED_FIELD(&value_type, sizeof(unsigned char));
*offset++ = PACKED_FIELD(value, 0);
*offset++ = PACKED_FIELD(&ts->sec, sizeof(int));
*offset++ = PACKED_FIELD(&ts->ns, sizeof(int));
offset += preprocessor_pack_history(offset, history, &history_num);
*offset++ = PACKED_FIELD(&steps->values_num, sizeof(int));
for (i = 0; i < steps->values_num; i++)
offset += preprocessor_pack_step(offset, (zbx_preproc_op_t *)steps->values[i]);
zbx_ipc_message_init(&message);
size = message_pack_data(&message, fields, offset - fields);
*data = message.data;
zbx_free(fields);
return size;
}
/******************************************************************************
* *
* Function: zbx_preprocessor_unpack_test_request *
* *
* Purpose: unpack preprocessing test request data from IPC data buffer *
* *
* Parameters: value_type - [OUT] item value type *
* value - [OUT] the value *
* ts - [OUT] value timestamp *
* value - [OUT] item value *
* history - [OUT] history data *
* steps - [OUT] preprocessing steps *
* steps_num - [OUT] preprocessing step count *
* data - [IN] IPC data buffer *
* *
******************************************************************************/
void zbx_preprocessor_unpack_test_request(unsigned char *value_type, char **value, zbx_timespec_t *ts,
zbx_vector_ptr_t *history, zbx_preproc_op_t **steps, int *steps_num, const unsigned char *data)
{
zbx_uint32_t value_len;
const unsigned char *offset = data;
offset += zbx_deserialize_char(offset, value_type);
offset += zbx_deserialize_str(offset, value, value_len);
offset += zbx_deserialize_int(offset, &ts->sec);
offset += zbx_deserialize_int(offset, &ts->ns);
offset += preprocesser_unpack_history(offset, history);
(void)preprocessor_unpack_steps(offset, steps, steps_num);
}
/******************************************************************************
* *
* Function: zbx_preprocessor_test *
* *
* Purpose: tests item preprocessing with the specified input value and steps *
* *
******************************************************************************/
int zbx_preprocessor_test(unsigned char value_type, const char *value, const char *last_value, const char *last_ts,
const zbx_vector_ptr_t *steps, zbx_vector_ptr_t *results, char **preproc_error, char **error)
{
unsigned char *data = NULL;
zbx_uint32_t size;
zbx_timespec_t ts, timestamps[2];
int ret = FAIL, i, values_num = 0;
zbx_vector_ptr_t history;
const char *values[2];
zbx_timespec(&ts);
zbx_vector_ptr_create(&history);
if (NULL != last_value)
{
const char *ptr;
int delay;
values[values_num] = last_value;
timestamps[values_num] = ts;
if (0 != strncmp(last_ts, "now", ZBX_CONST_STRLEN("now")))
{
*error = zbx_dsprintf(NULL, "invalid history value timestamp: %s", last_ts);
goto out;
}
ptr = last_ts + ZBX_CONST_STRLEN("now");
if ('\0' != *ptr)
{
if ('-' != *ptr || FAIL == is_time_suffix(ptr + 1, &delay, strlen(ptr + 1)))
{
*error = zbx_dsprintf(NULL, "invalid history value timestamp: %s", last_ts);
goto out;
}
timestamps[values_num].sec -= delay;
}
values_num++;
}
values[values_num] = value;
timestamps[values_num++] = ts;
for (i = 0; i < values_num; i++)
{
unsigned char *result;
zbx_free(data);
size = preprocessor_pack_test_request(&data, value_type, values[i], ×tamps[i], &history, steps);
if (SUCCEED != zbx_ipc_async_exchange(ZBX_IPC_SERVICE_PREPROCESSING, ZBX_IPC_PREPROCESSOR_TEST_REQUEST,
SEC_PER_MIN, data, size, &result, error))
{
goto out;
}
zbx_vector_ptr_clear_ext(results, (zbx_clean_func_t)zbx_preproc_result_free);
zbx_free(*preproc_error);
zbx_preprocessor_unpack_test_result(results, &history, preproc_error, result);
zbx_free(result);
}
ret = SUCCEED;
out:
zbx_vector_ptr_clear_ext(&history, (zbx_clean_func_t)zbx_preproc_op_history_free);
zbx_vector_ptr_destroy(&history);
zbx_free(data);
return ret;
}
|