1 /**
2 * collectd - src/curl.c
3 * Copyright (C) 2006-2009 Florian octo Forster
4 * Copyright (C) 2009 Aman Gupta
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; only version 2 of the License is applicable.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * Authors:
20 * Florian octo Forster <octo at collectd.org>
21 * Aman Gupta <aman at tmm1.net>
22 **/
23
24 #include "collectd.h"
25
26 #include "plugin.h"
27 #include "utils/common/common.h"
28 #include "utils/curl_stats/curl_stats.h"
29 #include "utils/match/match.h"
30 #include "utils_time.h"
31
32 #include <curl/curl.h>
33
34 /*
35 * Data types
36 */
37 struct web_match_s;
38 typedef struct web_match_s web_match_t;
39 struct web_match_s /* {{{ */
40 {
41 char *regex;
42 char *exclude_regex;
43 int dstype;
44 char *type;
45 char *instance;
46
47 cu_match_t *match;
48
49 web_match_t *next;
50 }; /* }}} */
51
52 struct web_page_s;
53 typedef struct web_page_s web_page_t;
54 struct web_page_s /* {{{ */
55 {
56 char *plugin_name;
57 char *instance;
58
59 char *url;
60 int address_family;
61 char *user;
62 char *pass;
63 char *credentials;
64 bool digest;
65 bool verify_peer;
66 bool verify_host;
67 char *cacert;
68 struct curl_slist *headers;
69 char *post_body;
70 bool response_time;
71 bool response_code;
72 int timeout;
73 curl_stats_t *stats;
74
75 CURL *curl;
76 char curl_errbuf[CURL_ERROR_SIZE];
77 char *buffer;
78 size_t buffer_size;
79 size_t buffer_fill;
80
81 web_match_t *matches;
82 }; /* }}} */
83
84 /*
85 * Private functions
86 */
87 static int cc_read_page(user_data_t *ud);
88
cc_curl_callback(void * buf,size_t size,size_t nmemb,void * user_data)89 static size_t cc_curl_callback(void *buf, /* {{{ */
90 size_t size, size_t nmemb, void *user_data) {
91 web_page_t *wp;
92 size_t len;
93
94 len = size * nmemb;
95 if (len == 0)
96 return len;
97
98 wp = user_data;
99 if (wp == NULL)
100 return 0;
101
102 if ((wp->buffer_fill + len) >= wp->buffer_size) {
103 char *temp;
104 size_t temp_size;
105
106 temp_size = wp->buffer_fill + len + 1;
107 temp = realloc(wp->buffer, temp_size);
108 if (temp == NULL) {
109 ERROR("curl plugin: realloc failed.");
110 return 0;
111 }
112 wp->buffer = temp;
113 wp->buffer_size = temp_size;
114 }
115
116 memcpy(wp->buffer + wp->buffer_fill, (char *)buf, len);
117 wp->buffer_fill += len;
118 wp->buffer[wp->buffer_fill] = 0;
119
120 return len;
121 } /* }}} size_t cc_curl_callback */
122
cc_web_match_free(web_match_t * wm)123 static void cc_web_match_free(web_match_t *wm) /* {{{ */
124 {
125 if (wm == NULL)
126 return;
127
128 sfree(wm->regex);
129 sfree(wm->type);
130 sfree(wm->instance);
131 match_destroy(wm->match);
132 cc_web_match_free(wm->next);
133 sfree(wm);
134 } /* }}} void cc_web_match_free */
135
cc_web_page_free(void * arg)136 static void cc_web_page_free(void *arg) /* {{{ */
137 {
138 web_page_t *wp = (web_page_t *)arg;
139 if (wp == NULL)
140 return;
141
142 if (wp->curl != NULL)
143 curl_easy_cleanup(wp->curl);
144 wp->curl = NULL;
145
146 sfree(wp->plugin_name);
147 sfree(wp->instance);
148
149 sfree(wp->url);
150 sfree(wp->user);
151 sfree(wp->pass);
152 sfree(wp->credentials);
153 sfree(wp->cacert);
154 sfree(wp->post_body);
155 curl_slist_free_all(wp->headers);
156 curl_stats_destroy(wp->stats);
157
158 sfree(wp->buffer);
159
160 cc_web_match_free(wp->matches);
161 sfree(wp);
162 } /* }}} void cc_web_page_free */
163
cc_config_append_string(const char * name,struct curl_slist ** dest,oconfig_item_t * ci)164 static int cc_config_append_string(const char *name,
165 struct curl_slist **dest, /* {{{ */
166 oconfig_item_t *ci) {
167 struct curl_slist *temp = NULL;
168 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
169 WARNING("curl plugin: `%s' needs exactly one string argument.", name);
170 return -1;
171 }
172
173 temp = curl_slist_append(*dest, ci->values[0].value.string);
174 if (temp == NULL)
175 return -1;
176
177 *dest = temp;
178
179 return 0;
180 } /* }}} int cc_config_append_string */
181
cc_config_add_match_dstype(int * dstype_ret,oconfig_item_t * ci)182 static int cc_config_add_match_dstype(int *dstype_ret, /* {{{ */
183 oconfig_item_t *ci) {
184 int dstype;
185
186 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
187 WARNING("curl plugin: `DSType' needs exactly one string argument.");
188 return -1;
189 }
190
191 if (strncasecmp("Gauge", ci->values[0].value.string, strlen("Gauge")) == 0) {
192 dstype = UTILS_MATCH_DS_TYPE_GAUGE;
193 if (strcasecmp("GaugeAverage", ci->values[0].value.string) == 0)
194 dstype |= UTILS_MATCH_CF_GAUGE_AVERAGE;
195 else if (strcasecmp("GaugeMin", ci->values[0].value.string) == 0)
196 dstype |= UTILS_MATCH_CF_GAUGE_MIN;
197 else if (strcasecmp("GaugeMax", ci->values[0].value.string) == 0)
198 dstype |= UTILS_MATCH_CF_GAUGE_MAX;
199 else if (strcasecmp("GaugeLast", ci->values[0].value.string) == 0)
200 dstype |= UTILS_MATCH_CF_GAUGE_LAST;
201 else
202 dstype = 0;
203 } else if (strncasecmp("Counter", ci->values[0].value.string,
204 strlen("Counter")) == 0) {
205 dstype = UTILS_MATCH_DS_TYPE_COUNTER;
206 if (strcasecmp("CounterSet", ci->values[0].value.string) == 0)
207 dstype |= UTILS_MATCH_CF_COUNTER_SET;
208 else if (strcasecmp("CounterAdd", ci->values[0].value.string) == 0)
209 dstype |= UTILS_MATCH_CF_COUNTER_ADD;
210 else if (strcasecmp("CounterInc", ci->values[0].value.string) == 0)
211 dstype |= UTILS_MATCH_CF_COUNTER_INC;
212 else
213 dstype = 0;
214 } else if (strncasecmp("Derive", ci->values[0].value.string,
215 strlen("Derive")) == 0) {
216 dstype = UTILS_MATCH_DS_TYPE_DERIVE;
217 if (strcasecmp("DeriveSet", ci->values[0].value.string) == 0)
218 dstype |= UTILS_MATCH_CF_DERIVE_SET;
219 else if (strcasecmp("DeriveAdd", ci->values[0].value.string) == 0)
220 dstype |= UTILS_MATCH_CF_DERIVE_ADD;
221 else if (strcasecmp("DeriveInc", ci->values[0].value.string) == 0)
222 dstype |= UTILS_MATCH_CF_DERIVE_INC;
223 else
224 dstype = 0;
225 } else if (strncasecmp("Absolute", ci->values[0].value.string,
226 strlen("Absolute")) == 0) {
227 dstype = UTILS_MATCH_DS_TYPE_ABSOLUTE;
228 if (strcasecmp("AbsoluteSet", ci->values[0].value.string) ==
229 0) /* Absolute DS is reset-on-read so no sense doin anything else but
230 set */
231 dstype |= UTILS_MATCH_CF_ABSOLUTE_SET;
232 else
233 dstype = 0;
234 }
235
236 else {
237 dstype = 0;
238 }
239
240 if (dstype == 0) {
241 WARNING("curl plugin: `%s' is not a valid argument to `DSType'.",
242 ci->values[0].value.string);
243 return -1;
244 }
245
246 *dstype_ret = dstype;
247 return 0;
248 } /* }}} int cc_config_add_match_dstype */
249
cc_config_add_match(web_page_t * page,oconfig_item_t * ci)250 static int cc_config_add_match(web_page_t *page, /* {{{ */
251 oconfig_item_t *ci) {
252 web_match_t *match;
253 int status;
254
255 if (ci->values_num != 0) {
256 WARNING("curl plugin: Ignoring arguments for the `Match' block.");
257 }
258
259 match = calloc(1, sizeof(*match));
260 if (match == NULL) {
261 ERROR("curl plugin: calloc failed.");
262 return -1;
263 }
264
265 status = 0;
266 for (int i = 0; i < ci->children_num; i++) {
267 oconfig_item_t *child = ci->children + i;
268
269 if (strcasecmp("Regex", child->key) == 0)
270 status = cf_util_get_string(child, &match->regex);
271 else if (strcasecmp("ExcludeRegex", child->key) == 0)
272 status = cf_util_get_string(child, &match->exclude_regex);
273 else if (strcasecmp("DSType", child->key) == 0)
274 status = cc_config_add_match_dstype(&match->dstype, child);
275 else if (strcasecmp("Type", child->key) == 0)
276 status = cf_util_get_string(child, &match->type);
277 else if (strcasecmp("Instance", child->key) == 0)
278 status = cf_util_get_string(child, &match->instance);
279 else {
280 WARNING("curl plugin: Option `%s' not allowed here.", child->key);
281 status = -1;
282 }
283
284 if (status != 0)
285 break;
286 } /* for (i = 0; i < ci->children_num; i++) */
287
288 while (status == 0) {
289 if (match->regex == NULL) {
290 WARNING("curl plugin: `Regex' missing in `Match' block.");
291 status = -1;
292 }
293
294 if (match->type == NULL) {
295 WARNING("curl plugin: `Type' missing in `Match' block.");
296 status = -1;
297 }
298
299 if (match->dstype == 0) {
300 WARNING("curl plugin: `DSType' missing in `Match' block.");
301 status = -1;
302 }
303
304 break;
305 } /* while (status == 0) */
306
307 if (status != 0) {
308 cc_web_match_free(match);
309 return status;
310 }
311
312 match->match =
313 match_create_simple(match->regex, match->exclude_regex, match->dstype);
314 if (match->match == NULL) {
315 ERROR("curl plugin: match_create_simple failed.");
316 cc_web_match_free(match);
317 return -1;
318 } else {
319 web_match_t *prev;
320
321 prev = page->matches;
322 while ((prev != NULL) && (prev->next != NULL))
323 prev = prev->next;
324
325 if (prev == NULL)
326 page->matches = match;
327 else
328 prev->next = match;
329 }
330
331 return 0;
332 } /* }}} int cc_config_add_match */
333
cc_page_init_curl(web_page_t * wp)334 static int cc_page_init_curl(web_page_t *wp) /* {{{ */
335 {
336 wp->curl = curl_easy_init();
337 if (wp->curl == NULL) {
338 ERROR("curl plugin: curl_easy_init failed.");
339 return -1;
340 }
341
342 curl_easy_setopt(wp->curl, CURLOPT_NOSIGNAL, 1L);
343 curl_easy_setopt(wp->curl, CURLOPT_WRITEFUNCTION, cc_curl_callback);
344 curl_easy_setopt(wp->curl, CURLOPT_WRITEDATA, wp);
345 curl_easy_setopt(wp->curl, CURLOPT_USERAGENT, COLLECTD_USERAGENT);
346 curl_easy_setopt(wp->curl, CURLOPT_ERRORBUFFER, wp->curl_errbuf);
347 curl_easy_setopt(wp->curl, CURLOPT_FOLLOWLOCATION, 1L);
348 curl_easy_setopt(wp->curl, CURLOPT_MAXREDIRS, 50L);
349 curl_easy_setopt(wp->curl, CURLOPT_IPRESOLVE, wp->address_family);
350
351 if (wp->user != NULL) {
352 #ifdef HAVE_CURLOPT_USERNAME
353 curl_easy_setopt(wp->curl, CURLOPT_USERNAME, wp->user);
354 curl_easy_setopt(wp->curl, CURLOPT_PASSWORD,
355 (wp->pass == NULL) ? "" : wp->pass);
356 #else
357 size_t credentials_size;
358
359 credentials_size = strlen(wp->user) + 2;
360 if (wp->pass != NULL)
361 credentials_size += strlen(wp->pass);
362
363 wp->credentials = malloc(credentials_size);
364 if (wp->credentials == NULL) {
365 ERROR("curl plugin: malloc failed.");
366 return -1;
367 }
368
369 snprintf(wp->credentials, credentials_size, "%s:%s", wp->user,
370 (wp->pass == NULL) ? "" : wp->pass);
371 curl_easy_setopt(wp->curl, CURLOPT_USERPWD, wp->credentials);
372 #endif
373
374 if (wp->digest)
375 curl_easy_setopt(wp->curl, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
376 }
377
378 curl_easy_setopt(wp->curl, CURLOPT_SSL_VERIFYPEER, (long)wp->verify_peer);
379 curl_easy_setopt(wp->curl, CURLOPT_SSL_VERIFYHOST, wp->verify_host ? 2L : 0L);
380 if (wp->cacert != NULL)
381 curl_easy_setopt(wp->curl, CURLOPT_CAINFO, wp->cacert);
382 if (wp->headers != NULL)
383 curl_easy_setopt(wp->curl, CURLOPT_HTTPHEADER, wp->headers);
384 if (wp->post_body != NULL)
385 curl_easy_setopt(wp->curl, CURLOPT_POSTFIELDS, wp->post_body);
386
387 #ifdef HAVE_CURLOPT_TIMEOUT_MS
388 if (wp->timeout >= 0)
389 curl_easy_setopt(wp->curl, CURLOPT_TIMEOUT_MS, (long)wp->timeout);
390 else
391 curl_easy_setopt(wp->curl, CURLOPT_TIMEOUT_MS,
392 (long)CDTIME_T_TO_MS(plugin_get_interval()));
393 #endif
394
395 return 0;
396 } /* }}} int cc_page_init_curl */
397
cc_config_add_page(oconfig_item_t * ci)398 static int cc_config_add_page(oconfig_item_t *ci) /* {{{ */
399 {
400 cdtime_t interval = 0;
401 web_page_t *page;
402 int status;
403
404 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
405 WARNING("curl plugin: `Page' blocks need exactly one string argument.");
406 return -1;
407 }
408
409 page = calloc(1, sizeof(*page));
410 if (page == NULL) {
411 ERROR("curl plugin: calloc failed.");
412 return -1;
413 }
414 page->plugin_name = NULL;
415 page->url = NULL;
416 page->address_family = CURL_IPRESOLVE_WHATEVER;
417 page->user = NULL;
418 page->pass = NULL;
419 page->digest = false;
420 page->verify_peer = true;
421 page->verify_host = true;
422 page->response_time = false;
423 page->response_code = false;
424 page->timeout = -1;
425 page->stats = NULL;
426
427 page->instance = strdup(ci->values[0].value.string);
428 if (page->instance == NULL) {
429 ERROR("curl plugin: strdup failed.");
430 sfree(page);
431 return -1;
432 }
433
434 /* Process all children */
435 status = 0;
436 for (int i = 0; i < ci->children_num; i++) {
437 oconfig_item_t *child = ci->children + i;
438
439 if (strcasecmp("Plugin", child->key) == 0)
440 status = cf_util_get_string(child, &page->plugin_name);
441 else if (strcasecmp("URL", child->key) == 0)
442 status = cf_util_get_string(child, &page->url);
443 else if (strcasecmp("AddressFamily", child->key) == 0) {
444 char *af = NULL;
445 status = cf_util_get_string(child, &af);
446 if (status != 0 || af == NULL) {
447 WARNING("curl plugin: Cannot parse value of `%s' "
448 "for instance `%s'.",
449 child->key, page->instance);
450 } else if (strcasecmp("any", af) == 0) {
451 page->address_family = CURL_IPRESOLVE_WHATEVER;
452 } else if (strcasecmp("ipv4", af) == 0) {
453 page->address_family = CURL_IPRESOLVE_V4;
454 } else if (strcasecmp("ipv6", af) == 0) {
455 /* If curl supports ipv6, use it. If not, log a warning and
456 * fall back to default - don't set status to non-zero.
457 */
458 curl_version_info_data *curl_info = curl_version_info(CURLVERSION_NOW);
459 if (curl_info->features & CURL_VERSION_IPV6)
460 page->address_family = CURL_IPRESOLVE_V6;
461 else
462 WARNING("curl plugin: IPv6 not supported by this libCURL. "
463 "Using fallback `any'.");
464 } else {
465 WARNING("curl plugin: Unsupported value of `%s' "
466 "for instance `%s'.",
467 child->key, page->instance);
468 status = -1;
469 }
470 free(af);
471 } else if (strcasecmp("User", child->key) == 0)
472 status = cf_util_get_string(child, &page->user);
473 else if (strcasecmp("Password", child->key) == 0)
474 status = cf_util_get_string(child, &page->pass);
475 else if (strcasecmp("Digest", child->key) == 0)
476 status = cf_util_get_boolean(child, &page->digest);
477 else if (strcasecmp("VerifyPeer", child->key) == 0)
478 status = cf_util_get_boolean(child, &page->verify_peer);
479 else if (strcasecmp("VerifyHost", child->key) == 0)
480 status = cf_util_get_boolean(child, &page->verify_host);
481 else if (strcasecmp("MeasureResponseTime", child->key) == 0)
482 status = cf_util_get_boolean(child, &page->response_time);
483 else if (strcasecmp("MeasureResponseCode", child->key) == 0)
484 status = cf_util_get_boolean(child, &page->response_code);
485 else if (strcasecmp("CACert", child->key) == 0)
486 status = cf_util_get_string(child, &page->cacert);
487 else if (strcasecmp("Match", child->key) == 0)
488 /* Be liberal with failing matches => don't set `status'. */
489 cc_config_add_match(page, child);
490 else if (strcasecmp("Header", child->key) == 0)
491 status = cc_config_append_string("Header", &page->headers, child);
492 else if (strcasecmp("Post", child->key) == 0)
493 status = cf_util_get_string(child, &page->post_body);
494 else if (strcasecmp("Interval", child->key) == 0)
495 status = cf_util_get_cdtime(child, &interval);
496 else if (strcasecmp("Timeout", child->key) == 0)
497 status = cf_util_get_int(child, &page->timeout);
498 else if (strcasecmp("Statistics", child->key) == 0) {
499 page->stats = curl_stats_from_config(child);
500 if (page->stats == NULL)
501 status = -1;
502 } else {
503 WARNING("curl plugin: Option `%s' not allowed here.", child->key);
504 status = -1;
505 }
506
507 if (status != 0)
508 break;
509 } /* for (i = 0; i < ci->children_num; i++) */
510
511 /* Additionial sanity checks and libCURL initialization. */
512 while (status == 0) {
513 if (page->url == NULL) {
514 WARNING("curl plugin: `URL' missing in `Page' block.");
515 status = -1;
516 }
517
518 if (page->matches == NULL && page->stats == NULL && !page->response_time &&
519 !page->response_code) {
520 assert(page->instance != NULL);
521 WARNING("curl plugin: No (valid) `Match' block "
522 "or Statistics or MeasureResponseTime or MeasureResponseCode "
523 "within `Page' block `%s'.",
524 page->instance);
525 status = -1;
526 }
527
528 if (status == 0)
529 status = cc_page_init_curl(page);
530
531 break;
532 } /* while (status == 0) */
533
534 if (status != 0) {
535 cc_web_page_free(page);
536 return status;
537 }
538
539 /* If all went well, register this page for reading */
540 char *cb_name = ssnprintf_alloc("curl-%s-%s", page->instance, page->url);
541
542 plugin_register_complex_read(/* group = */ NULL, cb_name, cc_read_page,
543 interval,
544 &(user_data_t){
545 .data = page,
546 .free_func = cc_web_page_free,
547 });
548 sfree(cb_name);
549
550 return 0;
551 } /* }}} int cc_config_add_page */
552
cc_config(oconfig_item_t * ci)553 static int cc_config(oconfig_item_t *ci) /* {{{ */
554 {
555 int success;
556 int errors;
557 int status;
558
559 success = 0;
560 errors = 0;
561
562 for (int i = 0; i < ci->children_num; i++) {
563 oconfig_item_t *child = ci->children + i;
564
565 if (strcasecmp("Page", child->key) == 0) {
566 status = cc_config_add_page(child);
567 if (status == 0)
568 success++;
569 else
570 errors++;
571 } else {
572 WARNING("curl plugin: Option `%s' not allowed here.", child->key);
573 errors++;
574 }
575 }
576
577 if ((success == 0) && (errors > 0)) {
578 ERROR("curl plugin: All statements failed.");
579 return -1;
580 }
581
582 return 0;
583 } /* }}} int cc_config */
584
cc_init(void)585 static int cc_init(void) /* {{{ */
586 {
587 curl_global_init(CURL_GLOBAL_SSL);
588 return 0;
589 } /* }}} int cc_init */
590
cc_submit(const web_page_t * wp,const web_match_t * wm,value_t value)591 static void cc_submit(const web_page_t *wp, const web_match_t *wm, /* {{{ */
592 value_t value) {
593 value_list_t vl = VALUE_LIST_INIT;
594
595 vl.values = &value;
596 vl.values_len = 1;
597 sstrncpy(vl.plugin, (wp->plugin_name != NULL) ? wp->plugin_name : "curl",
598 sizeof(vl.plugin));
599 sstrncpy(vl.plugin_instance, wp->instance, sizeof(vl.plugin_instance));
600 sstrncpy(vl.type, wm->type, sizeof(vl.type));
601 if (wm->instance != NULL)
602 sstrncpy(vl.type_instance, wm->instance, sizeof(vl.type_instance));
603
604 plugin_dispatch_values(&vl);
605 } /* }}} void cc_submit */
606
cc_submit_response_code(const web_page_t * wp,long code)607 static void cc_submit_response_code(const web_page_t *wp, long code) /* {{{ */
608 {
609 value_list_t vl = VALUE_LIST_INIT;
610
611 vl.values = &(value_t){.gauge = (gauge_t)code};
612 vl.values_len = 1;
613 sstrncpy(vl.plugin, (wp->plugin_name != NULL) ? wp->plugin_name : "curl",
614 sizeof(vl.plugin));
615 sstrncpy(vl.plugin_instance, wp->instance, sizeof(vl.plugin_instance));
616 sstrncpy(vl.type, "response_code", sizeof(vl.type));
617
618 plugin_dispatch_values(&vl);
619 } /* }}} void cc_submit_response_code */
620
cc_submit_response_time(const web_page_t * wp,gauge_t response_time)621 static void cc_submit_response_time(const web_page_t *wp, /* {{{ */
622 gauge_t response_time) {
623 value_list_t vl = VALUE_LIST_INIT;
624
625 vl.values = &(value_t){.gauge = response_time};
626 vl.values_len = 1;
627 sstrncpy(vl.plugin, (wp->plugin_name != NULL) ? wp->plugin_name : "curl",
628 sizeof(vl.plugin));
629 sstrncpy(vl.plugin_instance, wp->instance, sizeof(vl.plugin_instance));
630 sstrncpy(vl.type, "response_time", sizeof(vl.type));
631
632 plugin_dispatch_values(&vl);
633 } /* }}} void cc_submit_response_time */
634
cc_read_page(user_data_t * ud)635 static int cc_read_page(user_data_t *ud) /* {{{ */
636 {
637
638 if ((ud == NULL) || (ud->data == NULL)) {
639 ERROR("curl plugin: cc_read_page: Invalid user data.");
640 return -1;
641 }
642
643 web_page_t *wp = (web_page_t *)ud->data;
644
645 int status;
646 cdtime_t start = 0;
647
648 if (wp->response_time)
649 start = cdtime();
650
651 wp->buffer_fill = 0;
652
653 curl_easy_setopt(wp->curl, CURLOPT_URL, wp->url);
654
655 status = curl_easy_perform(wp->curl);
656 if (status != CURLE_OK) {
657 ERROR("curl plugin: curl_easy_perform failed with status %i: %s", status,
658 wp->curl_errbuf);
659 return -1;
660 }
661
662 if (wp->response_time)
663 cc_submit_response_time(wp, CDTIME_T_TO_DOUBLE(cdtime() - start));
664 if (wp->stats != NULL)
665 curl_stats_dispatch(wp->stats, wp->curl, NULL, "curl", wp->instance);
666
667 if (wp->response_code) {
668 long response_code = 0;
669 status =
670 curl_easy_getinfo(wp->curl, CURLINFO_RESPONSE_CODE, &response_code);
671 if (status != CURLE_OK) {
672 ERROR("curl plugin: Fetching response code failed with status %i: %s",
673 status, wp->curl_errbuf);
674 } else {
675 cc_submit_response_code(wp, response_code);
676 }
677 }
678
679 for (web_match_t *wm = wp->matches; wm != NULL; wm = wm->next) {
680 cu_match_value_t *mv;
681
682 status = match_apply(wm->match, wp->buffer);
683 if (status != 0) {
684 WARNING("curl plugin: match_apply failed.");
685 continue;
686 }
687
688 mv = match_get_user_data(wm->match);
689 if (mv == NULL) {
690 WARNING("curl plugin: match_get_user_data returned NULL.");
691 continue;
692 }
693
694 cc_submit(wp, wm, mv->value);
695 match_value_reset(mv);
696 } /* for (wm = wp->matches; wm != NULL; wm = wm->next) */
697
698 return 0;
699 } /* }}} int cc_read_page */
700
module_register(void)701 void module_register(void) {
702 plugin_register_complex_config("curl", cc_config);
703 plugin_register_init("curl", cc_init);
704 } /* void module_register */
705