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
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
|
/*
** 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 "db.h"
#include "log.h"
#include "dbcache.h"
#include "preproc.h"
#include "zbxserver.h"
#include "zbxregexp.h"
#include "zbxhttp.h"
#include "httptest.h"
#include "httpmacro.h"
typedef struct
{
long rspcode;
double total_time;
double speed_download;
}
zbx_httpstat_t;
extern int CONFIG_HTTPPOLLER_FORKS;
#ifdef HAVE_LIBCURL
typedef struct
{
char *data;
size_t allocated;
size_t offset;
}
zbx_httppage_t;
static zbx_httppage_t page;
static size_t curl_write_cb(void *ptr, size_t size, size_t nmemb, void *userdata)
{
size_t r_size = size * nmemb;
ZBX_UNUSED(userdata);
/* first piece of data */
if (NULL == page.data)
{
page.allocated = MAX(8096, r_size);
page.offset = 0;
page.data = (char *)zbx_malloc(page.data, page.allocated);
}
zbx_strncpy_alloc(&page.data, &page.allocated, &page.offset, (char *)ptr, r_size);
return r_size;
}
static size_t curl_ignore_cb(void *ptr, size_t size, size_t nmemb, void *userdata)
{
ZBX_UNUSED(ptr);
ZBX_UNUSED(userdata);
return size * nmemb;
}
#endif /* HAVE_LIBCURL */
/******************************************************************************
* *
* Function: httptest_remove_macros *
* *
* Purpose: remove all macro variables cached during http test execution *
* *
* Parameters: httptest - [IN] the http test data *
* *
* Author: Andris Zeila *
* *
******************************************************************************/
static void httptest_remove_macros(zbx_httptest_t *httptest)
{
int i;
for (i = 0; i < httptest->macros.values_num; i++)
{
zbx_ptr_pair_t *pair = &httptest->macros.values[i];
zbx_free(pair->first);
zbx_free(pair->second);
}
zbx_vector_ptr_pair_clear(&httptest->macros);
}
static void process_test_data(zbx_uint64_t httptestid, int lastfailedstep, double speed_download,
const char *err_str, zbx_timespec_t *ts)
{
const char *__function_name = "process_test_data";
DB_RESULT result;
DB_ROW row;
unsigned char types[3];
DC_ITEM items[3];
zbx_uint64_t itemids[3];
int errcodes[3];
size_t i, num = 0;
AGENT_RESULT value;
zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name);
result = DBselect("select type,itemid from httptestitem where httptestid=" ZBX_FS_UI64, httptestid);
while (NULL != (row = DBfetch(result)))
{
if (3 == num)
{
THIS_SHOULD_NEVER_HAPPEN;
break;
}
switch (types[num] = (unsigned char)atoi(row[0]))
{
case ZBX_HTTPITEM_TYPE_SPEED:
case ZBX_HTTPITEM_TYPE_LASTSTEP:
break;
case ZBX_HTTPITEM_TYPE_LASTERROR:
if (NULL == err_str)
continue;
break;
default:
THIS_SHOULD_NEVER_HAPPEN;
continue;
}
ZBX_STR2UINT64(itemids[num], row[1]);
num++;
}
DBfree_result(result);
if (0 < num)
{
DCconfig_get_items_by_itemids(items, itemids, errcodes, num);
for (i = 0; i < 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 (HOST_MAINTENANCE_STATUS_ON == items[i].host.maintenance_status &&
MAINTENANCE_TYPE_NODATA == items[i].host.maintenance_type)
{
continue;
}
init_result(&value);
switch (types[i])
{
case ZBX_HTTPITEM_TYPE_SPEED:
SET_UI64_RESULT(&value, speed_download);
break;
case ZBX_HTTPITEM_TYPE_LASTSTEP:
SET_UI64_RESULT(&value, lastfailedstep);
break;
case ZBX_HTTPITEM_TYPE_LASTERROR:
SET_STR_RESULT(&value, zbx_strdup(NULL, err_str));
break;
}
items[i].state = ITEM_STATE_NORMAL;
zbx_preprocess_item_value(items[i].itemid, items[i].value_type, 0, &value, ts, items[i].state,
NULL);
free_result(&value);
}
DCconfig_clean_items(items, errcodes, num);
}
zabbix_log(LOG_LEVEL_DEBUG, "End of %s()", __function_name);
}
/******************************************************************************
* *
* Function: httpstep_pairs_join *
* *
* Purpose: performs concatenation of vector of pairs into delimited string *
* *
* Parameters: str - [IN/OUT] result string *
* alloc_len - [IN/OUT] allocated memory size *
* offset - [IN/OUT] offset within string *
* value_delimiter - [IN] delimiter to be used between name and *
* value *
* pair_delimiter - [IN] delimiter to be used between pairs *
* pairs - [IN] vector of pairs *
* *
******************************************************************************/
static void httpstep_pairs_join(char **str, size_t *alloc_len, size_t *offset, const char *value_delimiter,
const char *pair_delimiter, zbx_vector_ptr_pair_t *pairs)
{
int p;
char *key, *value;
for (p = 0; p < pairs->values_num; p++)
{
key = (char *)pairs->values[p].first;
value = (char *)pairs->values[p].second;
if (0 != p)
zbx_strcpy_alloc(str, alloc_len, offset, pair_delimiter);
zbx_strcpy_alloc(str, alloc_len, offset, key);
zbx_strcpy_alloc(str, alloc_len, offset, value_delimiter);
zbx_strcpy_alloc(str, alloc_len, offset, value);
}
}
/******************************************************************************
* *
* Function: httppairs_free *
* *
* Purpose: frees memory allocated for vector of pairs *
* *
* Parameters: pairs - [IN] vector of pairs *
* *
******************************************************************************/
static void httppairs_free(zbx_vector_ptr_pair_t *pairs)
{
int p;
for (p = 0; p < pairs->values_num; p++)
{
zbx_free(pairs->values[p].first);
zbx_free(pairs->values[p].second);
}
zbx_vector_ptr_pair_destroy(pairs);
}
#ifdef HAVE_LIBCURL
static void process_step_data(zbx_uint64_t httpstepid, zbx_httpstat_t *stat, zbx_timespec_t *ts)
{
const char *__function_name = "process_step_data";
DB_RESULT result;
DB_ROW row;
unsigned char types[3];
DC_ITEM items[3];
zbx_uint64_t itemids[3];
int errcodes[3];
size_t i, num = 0;
AGENT_RESULT value;
zabbix_log(LOG_LEVEL_DEBUG, "In %s() rspcode:%ld time:" ZBX_FS_DBL " speed:" ZBX_FS_DBL,
__function_name, stat->rspcode, stat->total_time, stat->speed_download);
result = DBselect("select type,itemid from httpstepitem where httpstepid=" ZBX_FS_UI64, httpstepid);
while (NULL != (row = DBfetch(result)))
{
if (3 == num)
{
THIS_SHOULD_NEVER_HAPPEN;
break;
}
if (ZBX_HTTPITEM_TYPE_RSPCODE != (types[num] = (unsigned char)atoi(row[0])) &&
ZBX_HTTPITEM_TYPE_TIME != types[num] && ZBX_HTTPITEM_TYPE_SPEED != types[num])
{
THIS_SHOULD_NEVER_HAPPEN;
continue;
}
ZBX_STR2UINT64(itemids[num], row[1]);
num++;
}
DBfree_result(result);
if (0 < num)
{
DCconfig_get_items_by_itemids(items, itemids, errcodes, num);
for (i = 0; i < 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 (HOST_MAINTENANCE_STATUS_ON == items[i].host.maintenance_status &&
MAINTENANCE_TYPE_NODATA == items[i].host.maintenance_type)
{
continue;
}
init_result(&value);
switch (types[i])
{
case ZBX_HTTPITEM_TYPE_RSPCODE:
SET_UI64_RESULT(&value, stat->rspcode);
break;
case ZBX_HTTPITEM_TYPE_TIME:
SET_DBL_RESULT(&value, stat->total_time);
break;
case ZBX_HTTPITEM_TYPE_SPEED:
SET_DBL_RESULT(&value, stat->speed_download);
break;
}
items[i].state = ITEM_STATE_NORMAL;
zbx_preprocess_item_value(items[i].itemid, items[i].value_type, 0, &value, ts, items[i].state,
NULL);
free_result(&value);
}
DCconfig_clean_items(items, errcodes, num);
}
zabbix_log(LOG_LEVEL_DEBUG, "End of %s()", __function_name);
}
/******************************************************************************
* *
* Function: httpstep_load_pairs *
* *
* Purpose: loads http fields of web scenario step *
* *
* Parameters: host - [IN] host to be used in macro expansion *
* httpstep - [IN/OUT] web scenario step *
* *
* Return value: SUCCEED if http fields were loaded and macro expansion was *
* successful. FAIL on error. *
* *
******************************************************************************/
static int httpstep_load_pairs(DC_HOST *host, zbx_httpstep_t *httpstep)
{
int type, ret = SUCCEED;
DB_RESULT result;
DB_ROW row;
size_t alloc_len = 0, offset;
zbx_ptr_pair_t pair;
zbx_vector_ptr_pair_t *vector, headers, query_fields, post_fields;
char *key, *value, *url = NULL, query_delimiter = '?';
httpstep->url = NULL;
httpstep->posts = NULL;
httpstep->headers = NULL;
zbx_vector_ptr_pair_create(&headers);
zbx_vector_ptr_pair_create(&query_fields);
zbx_vector_ptr_pair_create(&post_fields);
zbx_vector_ptr_pair_create(&httpstep->variables);
result = DBselect(
"select name,value,type"
" from httpstep_field"
" where httpstepid=" ZBX_FS_UI64
" order by httpstep_fieldid",
httpstep->httpstep->httpstepid);
while (NULL != (row = DBfetch(result)))
{
type = atoi(row[2]);
value = zbx_strdup(NULL, row[1]);
/* from now on variable values can contain macros so proper URL encoding can be performed */
if (SUCCEED != (ret = substitute_simple_macros(NULL, NULL, NULL, NULL, NULL, host, NULL, NULL, NULL,
&value, MACRO_TYPE_HTTPTEST_FIELD, NULL, 0)))
{
zbx_free(value);
goto out;
}
key = zbx_strdup(NULL, row[0]);
/* variable names cannot contain macros, and both variable names and variable values cannot contain */
/* another variables */
if (ZBX_HTTPFIELD_VARIABLE != type && (SUCCEED != (ret = substitute_simple_macros(NULL, NULL, NULL,
NULL, NULL, host, NULL, NULL, NULL, &key, MACRO_TYPE_HTTPTEST_FIELD, NULL, 0)) ||
SUCCEED != (ret = http_substitute_variables(httpstep->httptest, &key)) ||
SUCCEED != (ret = http_substitute_variables(httpstep->httptest, &value))))
{
httppairs_free(&httpstep->variables);
zbx_free(key);
zbx_free(value);
goto out;
}
/* keys and values of query fields / post fields should be encoded */
if (ZBX_HTTPFIELD_QUERY_FIELD == type || ZBX_HTTPFIELD_POST_FIELD == type)
{
zbx_http_url_encode(key, &key);
zbx_http_url_encode(value, &value);
}
switch (type)
{
case ZBX_HTTPFIELD_HEADER:
vector = &headers;
break;
case ZBX_HTTPFIELD_VARIABLE:
vector = &httpstep->variables;
break;
case ZBX_HTTPFIELD_QUERY_FIELD:
vector = &query_fields;
break;
case ZBX_HTTPFIELD_POST_FIELD:
vector = &post_fields;
break;
default:
THIS_SHOULD_NEVER_HAPPEN;
zbx_free(key);
zbx_free(value);
ret = FAIL;
goto out;
}
pair.first = key;
pair.second = value;
zbx_vector_ptr_pair_append(vector, pair);
}
/* URL is created from httpstep->httpstep->url, query_fields and fragment */
zbx_strcpy_alloc(&url, &alloc_len, &offset, httpstep->httpstep->url);
value = strchr(url, '#');
if (NULL != value)
{
/* URL contains fragment delimiter, so it must be dropped */
zabbix_log(LOG_LEVEL_DEBUG, "URL contains fragment delimiter, fragment part is deleted from URL");
*value = '\0';
offset = value - url;
}
if (0 < query_fields.values_num)
{
/* url can contain '?' so proper delimiter should be selected */
if (NULL != strchr(url, '?'))
query_delimiter = '&';
zbx_chrcpy_alloc(&url, &alloc_len, &offset, query_delimiter);
httpstep_pairs_join(&url, &alloc_len, &offset, "=", "&", &query_fields);
}
if (SUCCEED != (ret = zbx_http_punycode_encode_url(&url)))
{
zabbix_log(LOG_LEVEL_WARNING, "cannot encode unicode URL into punycode");
httppairs_free(&httpstep->variables);
zbx_free(url);
goto out;
}
httpstep->url = url;
/* POST data can be saved as raw data or as form data */
if (ZBX_POSTTYPE_FORM == httpstep->httpstep->post_type)
httpstep_pairs_join(&httpstep->posts, &alloc_len, &offset, "=", "&", &post_fields);
else
httpstep->posts = httpstep->httpstep->posts; /* post data in raw format */
httpstep_pairs_join(&httpstep->headers, &alloc_len, &offset, ":", "\r\n", &headers);
out:
httppairs_free(&headers);
httppairs_free(&query_fields);
httppairs_free(&post_fields);
DBfree_result(result);
return ret;
}
#endif
/******************************************************************************
* *
* Function: httptest_load_pairs *
* *
* Purpose: loads http fields of web scenario *
* *
* Parameters: host - [IN] host to be used in macro expansion *
* httptest - [IN/OUT] web scenario *
* *
* Return value: SUCCEED if http fields were loaded and macro expansion was *
* successful. FAIL on error. *
* *
******************************************************************************/
static int httptest_load_pairs(DC_HOST *host, zbx_httptest_t *httptest)
{
int type, ret = SUCCEED;
DB_RESULT result;
DB_ROW row;
size_t alloc_len = 0, offset;
zbx_ptr_pair_t pair;
zbx_vector_ptr_pair_t *vector, headers;
char *key, *value;
zbx_vector_ptr_pair_create(&headers);
zbx_vector_ptr_pair_create(&httptest->variables);
httptest->headers = NULL;
result = DBselect(
"select name,value,type"
" from httptest_field"
" where httptestid=" ZBX_FS_UI64
" order by httptest_fieldid",
httptest->httptest.httptestid);
while (NULL != (row = DBfetch(result)))
{
type = atoi(row[2]);
value = zbx_strdup(NULL, row[1]);
/* from now on variable values can contain macros so proper URL encoding can be performed */
if (SUCCEED != (ret = substitute_simple_macros(NULL, NULL, NULL, NULL, NULL, host, NULL, NULL, NULL,
&value, MACRO_TYPE_HTTPTEST_FIELD, NULL, 0)))
{
zbx_free(value);
goto out;
}
key = zbx_strdup(NULL, row[0]);
/* variable names cannot contain macros, and both variable names and variable values cannot contain */
/* another variables */
if (ZBX_HTTPFIELD_VARIABLE != type && SUCCEED != (ret = substitute_simple_macros(NULL, NULL, NULL,
NULL, NULL, host, NULL, NULL, NULL, &key, MACRO_TYPE_HTTPTEST_FIELD, NULL, 0)))
{
httppairs_free(&httptest->variables);
zbx_free(key);
zbx_free(value);
goto out;
}
switch (type)
{
case ZBX_HTTPFIELD_HEADER:
vector = &headers;
break;
case ZBX_HTTPFIELD_VARIABLE:
vector = &httptest->variables;
break;
default:
zbx_free(key);
zbx_free(value);
ret = FAIL;
goto out;
}
pair.first = key;
pair.second = value;
zbx_vector_ptr_pair_append(vector, pair);
}
httpstep_pairs_join(&httptest->headers, &alloc_len, &offset, ":", "\r\n", &headers);
out:
httppairs_free(&headers);
DBfree_result(result);
return ret;
}
/******************************************************************************
* *
* Function: process_httptest *
* *
* Purpose: process single scenario of http test *
* *
* Parameters: *
* *
* Return value: *
* *
* Author: Alexei Vladishev *
* *
* Comments: *
* *
******************************************************************************/
static void process_httptest(DC_HOST *host, zbx_httptest_t *httptest)
{
const char *__function_name = "process_httptest";
DB_RESULT result;
DB_HTTPSTEP db_httpstep;
char *err_str = NULL, *buffer = NULL;
int lastfailedstep = 0;
zbx_timespec_t ts;
int delay;
double speed_download = 0;
int speed_download_num = 0;
#ifdef HAVE_LIBCURL
DB_ROW row;
zbx_httpstat_t stat;
char errbuf[CURL_ERROR_SIZE];
CURL *easyhandle = NULL;
CURLcode err;
zbx_httpstep_t httpstep;
#endif
zabbix_log(LOG_LEVEL_DEBUG, "In %s() httptestid:" ZBX_FS_UI64 " name:'%s'",
__function_name, httptest->httptest.httptestid, httptest->httptest.name);
result = DBselect(
"select httpstepid,no,name,url,timeout,posts,required,status_codes,post_type,follow_redirects,"
"retrieve_mode"
" from httpstep"
" where httptestid=" ZBX_FS_UI64
" order by no",
httptest->httptest.httptestid);
buffer = zbx_strdup(buffer, httptest->httptest.delay);
substitute_simple_macros(NULL, NULL, NULL, NULL, &host->hostid, NULL, NULL, NULL, NULL, &buffer,
MACRO_TYPE_COMMON, NULL, 0);
if (SUCCEED != is_time_suffix(buffer, &delay, ZBX_LENGTH_UNLIMITED))
{
err_str = zbx_dsprintf(err_str, "update interval \"%s\" is invalid", buffer);
lastfailedstep = -1;
goto httptest_error;
}
/* Explicitly initialize the name. If we compile without libCURL support, */
/* we avoid the potential usage of unititialized values. */
db_httpstep.name = NULL;
#ifdef HAVE_LIBCURL
if (NULL == (easyhandle = curl_easy_init()))
{
err_str = zbx_strdup(err_str, "cannot initialize cURL library");
goto clean;
}
if (CURLE_OK != (err = curl_easy_setopt(easyhandle, CURLOPT_PROXY, httptest->httptest.http_proxy)) ||
CURLE_OK != (err = curl_easy_setopt(easyhandle, CURLOPT_COOKIEFILE, "")) ||
CURLE_OK != (err = curl_easy_setopt(easyhandle, CURLOPT_USERAGENT, httptest->httptest.agent)) ||
CURLE_OK != (err = curl_easy_setopt(easyhandle, CURLOPT_ERRORBUFFER, errbuf)))
{
err_str = zbx_strdup(err_str, curl_easy_strerror(err));
goto clean;
}
if (SUCCEED != zbx_http_prepare_ssl(easyhandle, httptest->httptest.ssl_cert_file,
httptest->httptest.ssl_key_file, httptest->httptest.ssl_key_password,
httptest->httptest.verify_peer, httptest->httptest.verify_host, &err_str))
{
goto clean;
}
httpstep.httptest = httptest;
httpstep.httpstep = &db_httpstep;
while (NULL != (row = DBfetch(result)))
{
struct curl_slist *headers_slist = NULL;
size_t (*curl_header_cb)(void *ptr, size_t size, size_t nmemb, void *userdata);
size_t (*curl_body_cb)(void *ptr, size_t size, size_t nmemb, void *userdata);
/* NOTE: do not break or return from this block! */
/* process_step_data() call is required! */
ZBX_STR2UINT64(db_httpstep.httpstepid, row[0]);
db_httpstep.httptestid = httptest->httptest.httptestid;
db_httpstep.no = atoi(row[1]);
db_httpstep.name = row[2];
db_httpstep.url = zbx_strdup(NULL, row[3]);
substitute_simple_macros(NULL, NULL, NULL, NULL, NULL, host, NULL, NULL, NULL,
&db_httpstep.url, MACRO_TYPE_HTTPTEST_FIELD, NULL, 0);
http_substitute_variables(httptest, &db_httpstep.url);
db_httpstep.post_type = atoi(row[8]);
if (ZBX_POSTTYPE_RAW == db_httpstep.post_type)
{
db_httpstep.posts = zbx_strdup(NULL, row[5]);
substitute_simple_macros(NULL, NULL, NULL, NULL, NULL, host, NULL, NULL, NULL,
&db_httpstep.posts, MACRO_TYPE_HTTPTEST_FIELD, NULL, 0);
http_substitute_variables(httptest, &db_httpstep.posts);
}
else
db_httpstep.posts = NULL;
if (SUCCEED != httpstep_load_pairs(host, &httpstep))
{
err_str = zbx_strdup(err_str, "cannot load web scenario step data");
goto httpstep_error;
}
buffer = zbx_strdup(buffer, row[4]);
substitute_simple_macros(NULL, NULL, NULL, NULL, &host->hostid, NULL, NULL, NULL, NULL, &buffer,
MACRO_TYPE_COMMON, NULL, 0);
if (SUCCEED != is_time_suffix(buffer, &db_httpstep.timeout, ZBX_LENGTH_UNLIMITED))
{
err_str = zbx_dsprintf(err_str, "timeout \"%s\" is invalid", buffer);
goto httpstep_error;
}
else if (SEC_PER_HOUR < db_httpstep.timeout)
{
err_str = zbx_dsprintf(err_str, "timeout \"%s\" exceeds 1 hour limit", buffer);
goto httpstep_error;
}
db_httpstep.required = zbx_strdup(NULL, row[6]);
substitute_simple_macros(NULL, NULL, NULL, NULL, NULL, host, NULL, NULL, NULL,
&db_httpstep.required, MACRO_TYPE_HTTPTEST_FIELD, NULL, 0);
db_httpstep.status_codes = zbx_strdup(NULL, row[7]);
substitute_simple_macros(NULL, NULL, NULL, NULL, &host->hostid, NULL, NULL, NULL, NULL,
&db_httpstep.status_codes, MACRO_TYPE_COMMON, NULL, 0);
db_httpstep.follow_redirects = atoi(row[9]);
db_httpstep.retrieve_mode = atoi(row[10]);
memset(&stat, 0, sizeof(stat));
zabbix_log(LOG_LEVEL_DEBUG, "%s() use step \"%s\"", __function_name, db_httpstep.name);
zabbix_log(LOG_LEVEL_DEBUG, "%s() use post \"%s\"", __function_name, ZBX_NULL2EMPTY_STR(httpstep.posts));
if (CURLE_OK != (err = curl_easy_setopt(easyhandle, CURLOPT_POSTFIELDS, httpstep.posts)))
{
err_str = zbx_strdup(err_str, curl_easy_strerror(err));
goto httpstep_error;
}
if (CURLE_OK != (err = curl_easy_setopt(easyhandle, CURLOPT_POST, (NULL != httpstep.posts &&
'\0' != *httpstep.posts) ? 1L : 0L)))
{
err_str = zbx_strdup(err_str, curl_easy_strerror(err));
goto httpstep_error;
}
if (CURLE_OK != (err = curl_easy_setopt(easyhandle, CURLOPT_FOLLOWLOCATION,
0 == db_httpstep.follow_redirects ? 0L : 1L)))
{
err_str = zbx_strdup(err_str, curl_easy_strerror(err));
goto httpstep_error;
}
if (0 != db_httpstep.follow_redirects)
{
if (CURLE_OK != (err = curl_easy_setopt(easyhandle, CURLOPT_MAXREDIRS, ZBX_CURLOPT_MAXREDIRS)))
{
err_str = zbx_strdup(err_str, curl_easy_strerror(err));
goto httpstep_error;
}
}
/* headers defined in a step overwrite headers defined in scenario */
if (NULL != httpstep.headers && '\0' != *httpstep.headers)
zbx_http_add_headers(httpstep.headers, &headers_slist);
else if (NULL != httptest->headers && '\0' != *httptest->headers)
zbx_http_add_headers(httptest->headers, &headers_slist);
if (CURLE_OK != (err = curl_easy_setopt(easyhandle, CURLOPT_HTTPHEADER, headers_slist)))
{
err_str = zbx_strdup(err_str, curl_easy_strerror(err));
goto httpstep_error;
}
switch (db_httpstep.retrieve_mode)
{
case ZBX_RETRIEVE_MODE_CONTENT:
curl_header_cb = curl_ignore_cb;
curl_body_cb = curl_write_cb;
break;
case ZBX_RETRIEVE_MODE_BOTH:
curl_header_cb = curl_body_cb = curl_write_cb;
break;
case ZBX_RETRIEVE_MODE_HEADERS:
curl_header_cb = curl_write_cb;
curl_body_cb = curl_ignore_cb;
break;
default:
THIS_SHOULD_NEVER_HAPPEN;
err_str = zbx_strdup(err_str, "invalid retrieve mode");
goto httpstep_error;
}
if (CURLE_OK != (err = curl_easy_setopt(easyhandle, CURLOPT_WRITEFUNCTION, curl_body_cb)) ||
CURLE_OK != (err = curl_easy_setopt(easyhandle, CURLOPT_HEADERFUNCTION, curl_header_cb)))
{
err_str = zbx_strdup(err_str, curl_easy_strerror(err));
goto httpstep_error;
}
/* enable/disable fetching the body */
if (CURLE_OK != (err = curl_easy_setopt(easyhandle, CURLOPT_NOBODY,
ZBX_RETRIEVE_MODE_HEADERS == db_httpstep.retrieve_mode ? 1L : 0L)))
{
err_str = zbx_strdup(err_str, curl_easy_strerror(err));
goto httpstep_error;
}
if (SUCCEED != zbx_http_prepare_auth(easyhandle, httptest->httptest.authentication,
httptest->httptest.http_user, httptest->httptest.http_password, &err_str))
{
goto httpstep_error;
}
zabbix_log(LOG_LEVEL_DEBUG, "%s() go to URL \"%s\"", __function_name, httpstep.url);
if (CURLE_OK != (err = curl_easy_setopt(easyhandle, CURLOPT_TIMEOUT, (long)db_httpstep.timeout)) ||
CURLE_OK != (err = curl_easy_setopt(easyhandle, CURLOPT_URL, httpstep.url)))
{
err_str = zbx_strdup(err_str, curl_easy_strerror(err));
goto httpstep_error;
}
/* try to retrieve page several times depending on number of retries */
do
{
memset(&page, 0, sizeof(page));
if (CURLE_OK == (err = curl_easy_perform(easyhandle)))
break;
zbx_free(page.data);
}
while (0 < --httptest->httptest.retries);
curl_slist_free_all(headers_slist); /* must be called after curl_easy_perform() */
if (CURLE_OK == err)
{
char *var_err_str = NULL;
zabbix_log(LOG_LEVEL_TRACE, "%s() page.data from %s:'%s'", __function_name, httpstep.url,
page.data);
/* first get the data that is needed even if step fails */
if (CURLE_OK != (err = curl_easy_getinfo(easyhandle, CURLINFO_RESPONSE_CODE, &stat.rspcode)))
{
err_str = zbx_strdup(err_str, curl_easy_strerror(err));
}
else if ('\0' != *db_httpstep.status_codes &&
FAIL == int_in_list(db_httpstep.status_codes, stat.rspcode))
{
err_str = zbx_dsprintf(err_str, "response code \"%ld\" did not match any of the"
" required status codes \"%s\"", stat.rspcode,
db_httpstep.status_codes);
}
if (CURLE_OK != (err = curl_easy_getinfo(easyhandle, CURLINFO_TOTAL_TIME, &stat.total_time)) &&
NULL == err_str)
{
err_str = zbx_strdup(err_str, curl_easy_strerror(err));
}
if (CURLE_OK != (err = curl_easy_getinfo(easyhandle, CURLINFO_SPEED_DOWNLOAD,
&stat.speed_download)) && NULL == err_str)
{
err_str = zbx_strdup(err_str, curl_easy_strerror(err));
}
else
{
speed_download += stat.speed_download;
speed_download_num++;
}
/* required pattern */
if (NULL == err_str && '\0' != *db_httpstep.required &&
NULL == zbx_regexp_match(page.data, db_httpstep.required, NULL))
{
err_str = zbx_dsprintf(err_str, "required pattern \"%s\" was not found on %s",
db_httpstep.required, httpstep.url);
}
/* variables defined in scenario */
if (NULL == err_str && FAIL == http_process_variables(httptest, &httptest->variables, page.data,
&var_err_str))
{
char *variables = NULL;
size_t alloc_len = 0, offset;
httpstep_pairs_join(&variables, &alloc_len, &offset, "=", " ", &httptest->variables);
err_str = zbx_dsprintf(err_str, "error in scenario variables \"%s\": %s", variables,
var_err_str);
zbx_free(variables);
}
/* variables defined in a step */
if (NULL == err_str && FAIL == http_process_variables(httptest, &httpstep.variables, page.data,
&var_err_str))
{
char *variables = NULL;
size_t alloc_len = 0, offset;
httpstep_pairs_join(&variables, &alloc_len, &offset, "=", " ", &httpstep.variables);
err_str = zbx_dsprintf(err_str, "error in step variables \"%s\": %s", variables,
var_err_str);
zbx_free(variables);
}
zbx_free(var_err_str);
zbx_timespec(&ts);
process_step_data(db_httpstep.httpstepid, &stat, &ts);
zbx_free(page.data);
}
else
err_str = zbx_dsprintf(err_str, "%s: %s", curl_easy_strerror(err), errbuf);
httpstep_error:
zbx_free(db_httpstep.status_codes);
zbx_free(db_httpstep.required);
zbx_free(db_httpstep.posts);
zbx_free(db_httpstep.url);
httppairs_free(&httpstep.variables);
if (ZBX_POSTTYPE_FORM == httpstep.httpstep->post_type)
zbx_free(httpstep.posts);
zbx_free(httpstep.url);
zbx_free(httpstep.headers);
if (NULL != err_str)
{
lastfailedstep = db_httpstep.no;
break;
}
}
clean:
curl_easy_cleanup(easyhandle);
#else
err_str = zbx_strdup(err_str, "cURL library is required for Web monitoring support");
#endif /* HAVE_LIBCURL */
httptest_error:
zbx_timespec(&ts);
if (0 > lastfailedstep) /* update interval is invalid, delay is uninitialized */
{
zbx_config_t cfg;
zbx_config_get(&cfg, ZBX_CONFIG_FLAGS_REFRESH_UNSUPPORTED);
DBexecute("update httptest set nextcheck=%d where httptestid=" ZBX_FS_UI64,
(0 == cfg.refresh_unsupported || 0 > ts.sec + cfg.refresh_unsupported ?
ZBX_JAN_2038 : ts.sec + cfg.refresh_unsupported), httptest->httptest.httptestid);
zbx_config_clean(&cfg);
}
else if (0 > ts.sec + delay)
{
zabbix_log(LOG_LEVEL_WARNING, "nextcheck update causes overflow for web scenario \"%s\" on host \"%s\"",
httptest->httptest.name, host->name);
DBexecute("update httptest set nextcheck=%d where httptestid=" ZBX_FS_UI64,
ZBX_JAN_2038, httptest->httptest.httptestid);
}
else
{
DBexecute("update httptest set nextcheck=%d where httptestid=" ZBX_FS_UI64,
ts.sec + delay, httptest->httptest.httptestid);
}
if (NULL != err_str)
{
if (0 >= lastfailedstep)
{
/* we are here because web scenario update interval is invalid, */
/* cURL initialization failed or we have been compiled without cURL library */
lastfailedstep = 1;
}
if (NULL != db_httpstep.name)
{
zabbix_log(LOG_LEVEL_DEBUG, "cannot process step \"%s\" of web scenario \"%s\" on host \"%s\": "
"%s", db_httpstep.name, httptest->httptest.name, host->name, err_str);
}
}
DBfree_result(result);
if (0 != speed_download_num)
speed_download /= speed_download_num;
process_test_data(httptest->httptest.httptestid, lastfailedstep, speed_download, err_str, &ts);
zbx_free(buffer);
zbx_free(err_str);
zbx_preprocessor_flush();
zabbix_log(LOG_LEVEL_DEBUG, "End of %s()", __function_name);
}
/******************************************************************************
* *
* Function: process_httptests *
* *
* Purpose: process httptests *
* *
* Parameters: now - current timestamp *
* *
* Return value: number of processed httptests *
* *
* Author: Alexei Vladishev *
* *
* Comments: always SUCCEED *
* *
******************************************************************************/
int process_httptests(int httppoller_num, int now)
{
const char *__function_name = "process_httptests";
DB_RESULT result;
DB_ROW row;
zbx_httptest_t httptest;
DC_HOST host;
int httptests_count = 0;
zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __function_name);
/* create macro cache to use in http tests */
zbx_vector_ptr_pair_create(&httptest.macros);
result = DBselect(
"select h.hostid,h.host,h.name,t.httptestid,t.name,t.agent,"
"t.authentication,t.http_user,t.http_password,t.http_proxy,t.retries,t.ssl_cert_file,"
"t.ssl_key_file,t.ssl_key_password,t.verify_peer,t.verify_host,t.delay"
" from httptest t,hosts h"
" where t.hostid=h.hostid"
" and t.nextcheck<=%d"
" and " ZBX_SQL_MOD(t.httptestid,%d) "=%d"
" and t.status=%d"
" and h.proxy_hostid is null"
" and h.status=%d"
" and (h.maintenance_status=%d or h.maintenance_type=%d)",
now,
CONFIG_HTTPPOLLER_FORKS, httppoller_num - 1,
HTTPTEST_STATUS_MONITORED,
HOST_STATUS_MONITORED,
HOST_MAINTENANCE_STATUS_OFF, MAINTENANCE_TYPE_NORMAL);
while (NULL != (row = DBfetch(result)))
{
ZBX_STR2UINT64(host.hostid, row[0]);
strscpy(host.host, row[1]);
zbx_strlcpy_utf8(host.name, row[2], sizeof(host.name));
ZBX_STR2UINT64(httptest.httptest.httptestid, row[3]);
httptest.httptest.name = row[4];
if (SUCCEED != httptest_load_pairs(&host, &httptest))
{
zabbix_log(LOG_LEVEL_WARNING, "cannot process web scenario \"%s\" on host \"%s\": "
"cannot load web scenario data", httptest.httptest.name, host.name);
THIS_SHOULD_NEVER_HAPPEN;
continue;
}
httptest.httptest.agent = zbx_strdup(NULL, row[5]);
substitute_simple_macros(NULL, NULL, NULL, NULL, &host.hostid, NULL, NULL, NULL, NULL,
&httptest.httptest.agent, MACRO_TYPE_COMMON, NULL, 0);
if (HTTPTEST_AUTH_NONE != (httptest.httptest.authentication = atoi(row[6])))
{
httptest.httptest.http_user = zbx_strdup(NULL, row[7]);
substitute_simple_macros(NULL, NULL, NULL, NULL, &host.hostid, NULL, NULL, NULL, NULL,
&httptest.httptest.http_user, MACRO_TYPE_COMMON, NULL, 0);
httptest.httptest.http_password = zbx_strdup(NULL, row[8]);
substitute_simple_macros(NULL, NULL, NULL, NULL, &host.hostid, NULL, NULL, NULL, NULL,
&httptest.httptest.http_password, MACRO_TYPE_COMMON, NULL, 0);
}
if ('\0' != *row[9])
{
httptest.httptest.http_proxy = zbx_strdup(NULL, row[9]);
substitute_simple_macros(NULL, NULL, NULL, NULL, &host.hostid, NULL, NULL, NULL, NULL,
&httptest.httptest.http_proxy, MACRO_TYPE_COMMON, NULL, 0);
}
else
httptest.httptest.http_proxy = NULL;
httptest.httptest.retries = atoi(row[10]);
httptest.httptest.ssl_cert_file = zbx_strdup(NULL, row[11]);
substitute_simple_macros(NULL, NULL, NULL, NULL, NULL, &host, NULL, NULL, NULL,
&httptest.httptest.ssl_cert_file, MACRO_TYPE_HTTPTEST_FIELD, NULL, 0);
httptest.httptest.ssl_key_file = zbx_strdup(NULL, row[12]);
substitute_simple_macros(NULL, NULL, NULL, NULL, NULL, &host, NULL, NULL, NULL,
&httptest.httptest.ssl_key_file, MACRO_TYPE_HTTPTEST_FIELD, NULL, 0);
httptest.httptest.ssl_key_password = zbx_strdup(NULL, row[13]);
substitute_simple_macros(NULL, NULL, NULL, NULL, &host.hostid, NULL, NULL, NULL, NULL,
&httptest.httptest.ssl_key_password, MACRO_TYPE_COMMON, NULL, 0);
httptest.httptest.verify_peer = atoi(row[14]);
httptest.httptest.verify_host = atoi(row[15]);
httptest.httptest.delay = row[16];
/* add httptest variables to the current test macro cache */
http_process_variables(&httptest, &httptest.variables, NULL, NULL);
process_httptest(&host, &httptest);
zbx_free(httptest.httptest.ssl_key_password);
zbx_free(httptest.httptest.ssl_key_file);
zbx_free(httptest.httptest.ssl_cert_file);
zbx_free(httptest.httptest.http_proxy);
if (HTTPTEST_AUTH_NONE != httptest.httptest.authentication)
{
zbx_free(httptest.httptest.http_password);
zbx_free(httptest.httptest.http_user);
}
zbx_free(httptest.httptest.agent);
zbx_free(httptest.headers);
httppairs_free(&httptest.variables);
/* clear the macro cache used in this http test */
httptest_remove_macros(&httptest);
httptests_count++; /* performance metric */
}
/* destroy the macro cache used in http tests */
zbx_vector_ptr_pair_destroy(&httptest.macros);
DBfree_result(result);
zabbix_log(LOG_LEVEL_DEBUG, "End of %s()", __function_name);
return httptests_count;
}
|