2 * collectd - src/curl.c
3 * Copyright (C) 2006-2009 Florian octo Forster
4 * Copyright (C) 2009 Aman Gupta
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.
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.
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
20 * Florian octo Forster <octo at collectd.org>
21 * Aman Gupta <aman at tmm1.net>
28 #include "utils_curl_stats.h"
29 #include "utils_match.h"
30 #include "utils_time.h"
32 #include <curl/curl.h>
38 typedef struct web_match_s web_match_t;
39 struct web_match_s /* {{{ */
53 typedef struct web_page_s web_page_t;
54 struct web_page_s /* {{{ */
67 struct curl_slist *headers;
75 char curl_errbuf[CURL_ERROR_SIZE];
86 static int cc_read_page(user_data_t *ud);
88 static size_t cc_curl_callback(void *buf, /* {{{ */
89 size_t size, size_t nmemb, void *user_data) {
101 if ((wp->buffer_fill + len) >= wp->buffer_size) {
105 temp_size = wp->buffer_fill + len + 1;
106 temp = realloc(wp->buffer, temp_size);
108 ERROR("curl plugin: realloc failed.");
112 wp->buffer_size = temp_size;
115 memcpy(wp->buffer + wp->buffer_fill, (char *)buf, len);
116 wp->buffer_fill += len;
117 wp->buffer[wp->buffer_fill] = 0;
120 } /* }}} size_t cc_curl_callback */
122 static void cc_web_match_free(web_match_t *wm) /* {{{ */
130 match_destroy(wm->match);
131 cc_web_match_free(wm->next);
133 } /* }}} void cc_web_match_free */
135 static void cc_web_page_free(void *arg) /* {{{ */
137 web_page_t *wp = (web_page_t *)arg;
141 if (wp->curl != NULL)
142 curl_easy_cleanup(wp->curl);
145 sfree(wp->plugin_name);
151 sfree(wp->credentials);
153 sfree(wp->post_body);
154 curl_slist_free_all(wp->headers);
155 curl_stats_destroy(wp->stats);
159 cc_web_match_free(wp->matches);
161 } /* }}} void cc_web_page_free */
163 static int cc_config_append_string(const char *name,
164 struct curl_slist **dest, /* {{{ */
165 oconfig_item_t *ci) {
166 struct curl_slist *temp = NULL;
167 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
168 WARNING("curl plugin: `%s' needs exactly one string argument.", name);
172 temp = curl_slist_append(*dest, ci->values[0].value.string);
179 } /* }}} int cc_config_append_string */
181 static int cc_config_add_match_dstype(int *dstype_ret, /* {{{ */
182 oconfig_item_t *ci) {
185 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
186 WARNING("curl plugin: `DSType' needs exactly one string argument.");
190 if (strncasecmp("Gauge", ci->values[0].value.string, strlen("Gauge")) == 0) {
191 dstype = UTILS_MATCH_DS_TYPE_GAUGE;
192 if (strcasecmp("GaugeAverage", ci->values[0].value.string) == 0)
193 dstype |= UTILS_MATCH_CF_GAUGE_AVERAGE;
194 else if (strcasecmp("GaugeMin", ci->values[0].value.string) == 0)
195 dstype |= UTILS_MATCH_CF_GAUGE_MIN;
196 else if (strcasecmp("GaugeMax", ci->values[0].value.string) == 0)
197 dstype |= UTILS_MATCH_CF_GAUGE_MAX;
198 else if (strcasecmp("GaugeLast", ci->values[0].value.string) == 0)
199 dstype |= UTILS_MATCH_CF_GAUGE_LAST;
202 } else if (strncasecmp("Counter", ci->values[0].value.string,
203 strlen("Counter")) == 0) {
204 dstype = UTILS_MATCH_DS_TYPE_COUNTER;
205 if (strcasecmp("CounterSet", ci->values[0].value.string) == 0)
206 dstype |= UTILS_MATCH_CF_COUNTER_SET;
207 else if (strcasecmp("CounterAdd", ci->values[0].value.string) == 0)
208 dstype |= UTILS_MATCH_CF_COUNTER_ADD;
209 else if (strcasecmp("CounterInc", ci->values[0].value.string) == 0)
210 dstype |= UTILS_MATCH_CF_COUNTER_INC;
213 } else if (strncasecmp("Derive", ci->values[0].value.string,
214 strlen("Derive")) == 0) {
215 dstype = UTILS_MATCH_DS_TYPE_DERIVE;
216 if (strcasecmp("DeriveSet", ci->values[0].value.string) == 0)
217 dstype |= UTILS_MATCH_CF_DERIVE_SET;
218 else if (strcasecmp("DeriveAdd", ci->values[0].value.string) == 0)
219 dstype |= UTILS_MATCH_CF_DERIVE_ADD;
220 else if (strcasecmp("DeriveInc", ci->values[0].value.string) == 0)
221 dstype |= UTILS_MATCH_CF_DERIVE_INC;
224 } else if (strncasecmp("Absolute", ci->values[0].value.string,
225 strlen("Absolute")) == 0) {
226 dstype = UTILS_MATCH_DS_TYPE_ABSOLUTE;
227 if (strcasecmp("AbsoluteSet", ci->values[0].value.string) ==
228 0) /* Absolute DS is reset-on-read so no sense doin anything else but
230 dstype |= UTILS_MATCH_CF_ABSOLUTE_SET;
240 WARNING("curl plugin: `%s' is not a valid argument to `DSType'.",
241 ci->values[0].value.string);
245 *dstype_ret = dstype;
247 } /* }}} int cc_config_add_match_dstype */
249 static int cc_config_add_match(web_page_t *page, /* {{{ */
250 oconfig_item_t *ci) {
254 if (ci->values_num != 0) {
255 WARNING("curl plugin: Ignoring arguments for the `Match' block.");
258 match = calloc(1, sizeof(*match));
260 ERROR("curl plugin: calloc failed.");
265 for (int i = 0; i < ci->children_num; i++) {
266 oconfig_item_t *child = ci->children + i;
268 if (strcasecmp("Regex", child->key) == 0)
269 status = cf_util_get_string(child, &match->regex);
270 else if (strcasecmp("ExcludeRegex", child->key) == 0)
271 status = cf_util_get_string(child, &match->exclude_regex);
272 else if (strcasecmp("DSType", child->key) == 0)
273 status = cc_config_add_match_dstype(&match->dstype, child);
274 else if (strcasecmp("Type", child->key) == 0)
275 status = cf_util_get_string(child, &match->type);
276 else if (strcasecmp("Instance", child->key) == 0)
277 status = cf_util_get_string(child, &match->instance);
279 WARNING("curl plugin: Option `%s' not allowed here.", child->key);
285 } /* for (i = 0; i < ci->children_num; i++) */
287 while (status == 0) {
288 if (match->regex == NULL) {
289 WARNING("curl plugin: `Regex' missing in `Match' block.");
293 if (match->type == NULL) {
294 WARNING("curl plugin: `Type' missing in `Match' block.");
298 if (match->dstype == 0) {
299 WARNING("curl plugin: `DSType' missing in `Match' block.");
304 } /* while (status == 0) */
307 cc_web_match_free(match);
312 match_create_simple(match->regex, match->exclude_regex, match->dstype);
313 if (match->match == NULL) {
314 ERROR("curl plugin: match_create_simple failed.");
315 cc_web_match_free(match);
320 prev = page->matches;
321 while ((prev != NULL) && (prev->next != NULL))
325 page->matches = match;
331 } /* }}} int cc_config_add_match */
333 static int cc_page_init_curl(web_page_t *wp) /* {{{ */
335 wp->curl = curl_easy_init();
336 if (wp->curl == NULL) {
337 ERROR("curl plugin: curl_easy_init failed.");
341 curl_easy_setopt(wp->curl, CURLOPT_NOSIGNAL, 1L);
342 curl_easy_setopt(wp->curl, CURLOPT_WRITEFUNCTION, cc_curl_callback);
343 curl_easy_setopt(wp->curl, CURLOPT_WRITEDATA, wp);
344 curl_easy_setopt(wp->curl, CURLOPT_USERAGENT, COLLECTD_USERAGENT);
345 curl_easy_setopt(wp->curl, CURLOPT_ERRORBUFFER, wp->curl_errbuf);
346 curl_easy_setopt(wp->curl, CURLOPT_FOLLOWLOCATION, 1L);
347 curl_easy_setopt(wp->curl, CURLOPT_MAXREDIRS, 50L);
349 if (wp->user != NULL) {
350 #ifdef HAVE_CURLOPT_USERNAME
351 curl_easy_setopt(wp->curl, CURLOPT_USERNAME, wp->user);
352 curl_easy_setopt(wp->curl, CURLOPT_PASSWORD,
353 (wp->pass == NULL) ? "" : wp->pass);
355 size_t credentials_size;
357 credentials_size = strlen(wp->user) + 2;
358 if (wp->pass != NULL)
359 credentials_size += strlen(wp->pass);
361 wp->credentials = malloc(credentials_size);
362 if (wp->credentials == NULL) {
363 ERROR("curl plugin: malloc failed.");
367 snprintf(wp->credentials, credentials_size, "%s:%s", wp->user,
368 (wp->pass == NULL) ? "" : wp->pass);
369 curl_easy_setopt(wp->curl, CURLOPT_USERPWD, wp->credentials);
373 curl_easy_setopt(wp->curl, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
376 curl_easy_setopt(wp->curl, CURLOPT_SSL_VERIFYPEER, (long)wp->verify_peer);
377 curl_easy_setopt(wp->curl, CURLOPT_SSL_VERIFYHOST, wp->verify_host ? 2L : 0L);
378 if (wp->cacert != NULL)
379 curl_easy_setopt(wp->curl, CURLOPT_CAINFO, wp->cacert);
380 if (wp->headers != NULL)
381 curl_easy_setopt(wp->curl, CURLOPT_HTTPHEADER, wp->headers);
382 if (wp->post_body != NULL)
383 curl_easy_setopt(wp->curl, CURLOPT_POSTFIELDS, wp->post_body);
385 #ifdef HAVE_CURLOPT_TIMEOUT_MS
386 if (wp->timeout >= 0)
387 curl_easy_setopt(wp->curl, CURLOPT_TIMEOUT_MS, (long)wp->timeout);
389 curl_easy_setopt(wp->curl, CURLOPT_TIMEOUT_MS,
390 (long)CDTIME_T_TO_MS(plugin_get_interval()));
394 } /* }}} int cc_page_init_curl */
396 static int cc_config_add_page(oconfig_item_t *ci) /* {{{ */
398 cdtime_t interval = 0;
402 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) {
403 WARNING("curl plugin: `Page' blocks need exactly one string argument.");
407 page = calloc(1, sizeof(*page));
409 ERROR("curl plugin: calloc failed.");
412 page->plugin_name = NULL;
416 page->digest = false;
417 page->verify_peer = true;
418 page->verify_host = true;
419 page->response_time = false;
420 page->response_code = false;
424 page->instance = strdup(ci->values[0].value.string);
425 if (page->instance == NULL) {
426 ERROR("curl plugin: strdup failed.");
431 /* Process all children */
433 for (int i = 0; i < ci->children_num; i++) {
434 oconfig_item_t *child = ci->children + i;
436 if (strcasecmp("Plugin", child->key) == 0)
437 status = cf_util_get_string(child, &page->plugin_name);
438 else if (strcasecmp("URL", child->key) == 0)
439 status = cf_util_get_string(child, &page->url);
440 else if (strcasecmp("User", child->key) == 0)
441 status = cf_util_get_string(child, &page->user);
442 else if (strcasecmp("Password", child->key) == 0)
443 status = cf_util_get_string(child, &page->pass);
444 else if (strcasecmp("Digest", child->key) == 0)
445 status = cf_util_get_boolean(child, &page->digest);
446 else if (strcasecmp("VerifyPeer", child->key) == 0)
447 status = cf_util_get_boolean(child, &page->verify_peer);
448 else if (strcasecmp("VerifyHost", child->key) == 0)
449 status = cf_util_get_boolean(child, &page->verify_host);
450 else if (strcasecmp("MeasureResponseTime", child->key) == 0)
451 status = cf_util_get_boolean(child, &page->response_time);
452 else if (strcasecmp("MeasureResponseCode", child->key) == 0)
453 status = cf_util_get_boolean(child, &page->response_code);
454 else if (strcasecmp("CACert", child->key) == 0)
455 status = cf_util_get_string(child, &page->cacert);
456 else if (strcasecmp("Match", child->key) == 0)
457 /* Be liberal with failing matches => don't set `status'. */
458 cc_config_add_match(page, child);
459 else if (strcasecmp("Header", child->key) == 0)
460 status = cc_config_append_string("Header", &page->headers, child);
461 else if (strcasecmp("Post", child->key) == 0)
462 status = cf_util_get_string(child, &page->post_body);
463 else if (strcasecmp("Interval", child->key) == 0)
464 status = cf_util_get_cdtime(child, &interval);
465 else if (strcasecmp("Timeout", child->key) == 0)
466 status = cf_util_get_int(child, &page->timeout);
467 else if (strcasecmp("Statistics", child->key) == 0) {
468 page->stats = curl_stats_from_config(child);
469 if (page->stats == NULL)
472 WARNING("curl plugin: Option `%s' not allowed here.", child->key);
478 } /* for (i = 0; i < ci->children_num; i++) */
480 /* Additionial sanity checks and libCURL initialization. */
481 while (status == 0) {
482 if (page->url == NULL) {
483 WARNING("curl plugin: `URL' missing in `Page' block.");
487 if (page->matches == NULL && page->stats == NULL && !page->response_time &&
488 !page->response_code) {
489 assert(page->instance != NULL);
490 WARNING("curl plugin: No (valid) `Match' block "
491 "or Statistics or MeasureResponseTime or MeasureResponseCode "
492 "within `Page' block `%s'.",
498 status = cc_page_init_curl(page);
501 } /* while (status == 0) */
504 cc_web_page_free(page);
508 /* If all went well, register this page for reading */
509 char *cb_name = ssnprintf_alloc("curl-%s-%s", page->instance, page->url);
511 plugin_register_complex_read(/* group = */ NULL, cb_name, cc_read_page,
514 .data = page, .free_func = cc_web_page_free,
519 } /* }}} int cc_config_add_page */
521 static int cc_config(oconfig_item_t *ci) /* {{{ */
530 for (int i = 0; i < ci->children_num; i++) {
531 oconfig_item_t *child = ci->children + i;
533 if (strcasecmp("Page", child->key) == 0) {
534 status = cc_config_add_page(child);
540 WARNING("curl plugin: Option `%s' not allowed here.", child->key);
545 if ((success == 0) && (errors > 0)) {
546 ERROR("curl plugin: All statements failed.");
551 } /* }}} int cc_config */
553 static int cc_init(void) /* {{{ */
555 curl_global_init(CURL_GLOBAL_SSL);
557 } /* }}} int cc_init */
559 static void cc_submit(const web_page_t *wp, const web_match_t *wm, /* {{{ */
561 value_list_t vl = VALUE_LIST_INIT;
565 sstrncpy(vl.plugin, (wp->plugin_name != NULL) ? wp->plugin_name : "curl",
567 sstrncpy(vl.plugin_instance, wp->instance, sizeof(vl.plugin_instance));
568 sstrncpy(vl.type, wm->type, sizeof(vl.type));
569 if (wm->instance != NULL)
570 sstrncpy(vl.type_instance, wm->instance, sizeof(vl.type_instance));
572 plugin_dispatch_values(&vl);
573 } /* }}} void cc_submit */
575 static void cc_submit_response_code(const web_page_t *wp, long code) /* {{{ */
577 value_list_t vl = VALUE_LIST_INIT;
579 vl.values = &(value_t){.gauge = (gauge_t)code};
581 sstrncpy(vl.plugin, (wp->plugin_name != NULL) ? wp->plugin_name : "curl",
583 sstrncpy(vl.plugin_instance, wp->instance, sizeof(vl.plugin_instance));
584 sstrncpy(vl.type, "response_code", sizeof(vl.type));
586 plugin_dispatch_values(&vl);
587 } /* }}} void cc_submit_response_code */
589 static void cc_submit_response_time(const web_page_t *wp, /* {{{ */
590 gauge_t response_time) {
591 value_list_t vl = VALUE_LIST_INIT;
593 vl.values = &(value_t){.gauge = response_time};
595 sstrncpy(vl.plugin, (wp->plugin_name != NULL) ? wp->plugin_name : "curl",
597 sstrncpy(vl.plugin_instance, wp->instance, sizeof(vl.plugin_instance));
598 sstrncpy(vl.type, "response_time", sizeof(vl.type));
600 plugin_dispatch_values(&vl);
601 } /* }}} void cc_submit_response_time */
603 static int cc_read_page(user_data_t *ud) /* {{{ */
606 if ((ud == NULL) || (ud->data == NULL)) {
607 ERROR("curl plugin: cc_read_page: Invalid user data.");
611 web_page_t *wp = (web_page_t *)ud->data;
616 if (wp->response_time)
621 curl_easy_setopt(wp->curl, CURLOPT_URL, wp->url);
623 status = curl_easy_perform(wp->curl);
624 if (status != CURLE_OK) {
625 ERROR("curl plugin: curl_easy_perform failed with status %i: %s", status,
630 if (wp->response_time)
631 cc_submit_response_time(wp, CDTIME_T_TO_DOUBLE(cdtime() - start));
632 if (wp->stats != NULL)
633 curl_stats_dispatch(wp->stats, wp->curl, NULL, "curl", wp->instance);
635 if (wp->response_code) {
636 long response_code = 0;
638 curl_easy_getinfo(wp->curl, CURLINFO_RESPONSE_CODE, &response_code);
639 if (status != CURLE_OK) {
640 ERROR("curl plugin: Fetching response code failed with status %i: %s",
641 status, wp->curl_errbuf);
643 cc_submit_response_code(wp, response_code);
647 for (web_match_t *wm = wp->matches; wm != NULL; wm = wm->next) {
648 cu_match_value_t *mv;
650 status = match_apply(wm->match, wp->buffer);
652 WARNING("curl plugin: match_apply failed.");
656 mv = match_get_user_data(wm->match);
658 WARNING("curl plugin: match_get_user_data returned NULL.");
662 cc_submit(wp, wm, mv->value);
663 match_value_reset(mv);
664 } /* for (wm = wp->matches; wm != NULL; wm = wm->next) */
667 } /* }}} int cc_read_page */
669 void module_register(void) {
670 plugin_register_complex_config("curl", cc_config);
671 plugin_register_init("curl", cc_init);
672 } /* void module_register */