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 verplant.org>
21 * Aman Gupta <aman at tmm1.net>
27 #include "configfile.h"
28 #include "utils_match.h"
30 #include <curl/curl.h>
36 typedef struct web_match_s web_match_t;
37 struct web_match_s /* {{{ */
50 typedef struct web_page_s web_page_t;
51 struct web_page_s /* {{{ */
65 char curl_errbuf[CURL_ERROR_SIZE];
78 /* static CURLM *curl = NULL; */
79 static web_page_t *pages_g = NULL;
84 static size_t cc_curl_callback (void *buf, /* {{{ */
85 size_t size, size_t nmemb, void *user_data)
98 if ((wp->buffer_fill + len) >= wp->buffer_size)
103 temp_size = wp->buffer_fill + len + 1;
104 temp = (char *) realloc (wp->buffer, temp_size);
107 ERROR ("curl plugin: realloc failed.");
111 wp->buffer_size = temp_size;
114 memcpy (wp->buffer + wp->buffer_fill, (char *) buf, len);
115 wp->buffer_fill += len;
116 wp->buffer[wp->buffer_fill] = 0;
119 } /* }}} size_t cc_curl_callback */
121 static void cc_web_match_free (web_match_t *wm) /* {{{ */
128 sfree (wm->instance);
129 match_destroy (wm->match);
130 cc_web_match_free (wm->next);
132 } /* }}} void cc_web_match_free */
134 static void cc_web_page_free (web_page_t *wp) /* {{{ */
139 if (wp->curl != NULL)
140 curl_easy_cleanup (wp->curl);
143 sfree (wp->instance);
148 sfree (wp->credentials);
153 cc_web_match_free (wp->matches);
154 cc_web_page_free (wp->next);
156 } /* }}} void cc_web_page_free */
158 static int cc_config_add_string (const char *name, char **dest, /* {{{ */
161 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
163 WARNING ("curl plugin: `%s' needs exactly one string argument.", name);
168 *dest = strdup (ci->values[0].value.string);
173 } /* }}} int cc_config_add_string */
175 static int cc_config_set_boolean (const char *name, int *dest, /* {{{ */
178 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
180 WARNING ("curl plugin: `%s' needs exactly one boolean argument.", name);
184 *dest = ci->values[0].value.boolean ? 1 : 0;
187 } /* }}} int cc_config_set_boolean */
189 static int cc_config_add_match_dstype (int *dstype_ret, /* {{{ */
194 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
196 WARNING ("curl plugin: `DSType' needs exactly one string argument.");
200 if (strncasecmp ("Gauge", ci->values[0].value.string,
201 strlen ("Gauge")) == 0)
203 dstype = UTILS_MATCH_DS_TYPE_GAUGE;
204 if (strcasecmp ("GaugeAverage", ci->values[0].value.string) == 0)
205 dstype |= UTILS_MATCH_CF_GAUGE_AVERAGE;
206 else if (strcasecmp ("GaugeMin", ci->values[0].value.string) == 0)
207 dstype |= UTILS_MATCH_CF_GAUGE_MIN;
208 else if (strcasecmp ("GaugeMax", ci->values[0].value.string) == 0)
209 dstype |= UTILS_MATCH_CF_GAUGE_MAX;
210 else if (strcasecmp ("GaugeLast", ci->values[0].value.string) == 0)
211 dstype |= UTILS_MATCH_CF_GAUGE_LAST;
215 else if (strncasecmp ("Counter", ci->values[0].value.string,
216 strlen ("Counter")) == 0)
218 dstype = UTILS_MATCH_DS_TYPE_COUNTER;
219 if (strcasecmp ("CounterSet", ci->values[0].value.string) == 0)
220 dstype |= UTILS_MATCH_CF_COUNTER_SET;
221 else if (strcasecmp ("CounterAdd", ci->values[0].value.string) == 0)
222 dstype |= UTILS_MATCH_CF_COUNTER_ADD;
223 else if (strcasecmp ("CounterInc", ci->values[0].value.string) == 0)
224 dstype |= UTILS_MATCH_CF_COUNTER_INC;
228 else if (strncasecmp ("Derive", ci->values[0].value.string,
229 strlen ("Derive")) == 0)
231 dstype = UTILS_MATCH_DS_TYPE_DERIVE;
232 if (strcasecmp ("DeriveSet", ci->values[0].value.string) == 0)
233 dstype |= UTILS_MATCH_CF_DERIVE_SET;
234 else if (strcasecmp ("DeriveAdd", ci->values[0].value.string) == 0)
235 dstype |= UTILS_MATCH_CF_DERIVE_ADD;
236 else if (strcasecmp ("DeriveInc", ci->values[0].value.string) == 0)
237 dstype |= UTILS_MATCH_CF_DERIVE_INC;
241 else if (strncasecmp ("Absolute", ci->values[0].value.string,
242 strlen ("Absolute")) == 0)
244 dstype = UTILS_MATCH_DS_TYPE_ABSOLUTE;
245 if (strcasecmp ("AbsoluteSet", ci->values[0].value.string) == 0) /* Absolute DS is reset-on-read so no sense doin anything else but set */
246 dstype |= UTILS_MATCH_CF_ABSOLUTE_SET;
258 WARNING ("curl plugin: `%s' is not a valid argument to `DSType'.",
259 ci->values[0].value.string);
263 *dstype_ret = dstype;
265 } /* }}} int cc_config_add_match_dstype */
267 static int cc_config_add_match (web_page_t *page, /* {{{ */
274 if (ci->values_num != 0)
276 WARNING ("curl plugin: Ignoring arguments for the `Match' block.");
279 match = (web_match_t *) malloc (sizeof (*match));
282 ERROR ("curl plugin: malloc failed.");
285 memset (match, 0, sizeof (*match));
288 for (i = 0; i < ci->children_num; i++)
290 oconfig_item_t *child = ci->children + i;
292 if (strcasecmp ("Regex", child->key) == 0)
293 status = cc_config_add_string ("Regex", &match->regex, child);
294 else if (strcasecmp ("DSType", child->key) == 0)
295 status = cc_config_add_match_dstype (&match->dstype, child);
296 else if (strcasecmp ("Type", child->key) == 0)
297 status = cc_config_add_string ("Type", &match->type, child);
298 else if (strcasecmp ("Instance", child->key) == 0)
299 status = cc_config_add_string ("Instance", &match->instance, child);
302 WARNING ("curl plugin: Option `%s' not allowed here.", child->key);
308 } /* for (i = 0; i < ci->children_num; i++) */
312 if (match->regex == NULL)
314 WARNING ("curl plugin: `Regex' missing in `Match' block.");
318 if (match->type == NULL)
320 WARNING ("curl plugin: `Type' missing in `Match' block.");
324 if (match->dstype == 0)
326 WARNING ("curl plugin: `DSType' missing in `Match' block.");
331 } /* while (status == 0) */
336 match->match = match_create_simple (match->regex, NULL, match->dstype);
337 if (match->match == NULL)
339 ERROR ("curl plugin: tail_match_add_match_simple failed.");
340 cc_web_match_free (match);
347 prev = page->matches;
348 while ((prev != NULL) && (prev->next != NULL))
352 page->matches = match;
358 } /* }}} int cc_config_add_match */
360 static int cc_page_init_curl (web_page_t *wp) /* {{{ */
362 wp->curl = curl_easy_init ();
363 if (wp->curl == NULL)
365 ERROR ("curl plugin: curl_easy_init failed.");
369 curl_easy_setopt (wp->curl, CURLOPT_WRITEFUNCTION, cc_curl_callback);
370 curl_easy_setopt (wp->curl, CURLOPT_WRITEDATA, wp);
371 curl_easy_setopt (wp->curl, CURLOPT_USERAGENT,
372 PACKAGE_NAME"/"PACKAGE_VERSION);
373 curl_easy_setopt (wp->curl, CURLOPT_ERRORBUFFER, wp->curl_errbuf);
374 curl_easy_setopt (wp->curl, CURLOPT_URL, wp->url);
375 curl_easy_setopt (wp->curl, CURLOPT_FOLLOWLOCATION, 1);
377 if (wp->user != NULL)
379 size_t credentials_size;
381 credentials_size = strlen (wp->user) + 2;
382 if (wp->pass != NULL)
383 credentials_size += strlen (wp->pass);
385 wp->credentials = (char *) malloc (credentials_size);
386 if (wp->credentials == NULL)
388 ERROR ("curl plugin: malloc failed.");
392 ssnprintf (wp->credentials, credentials_size, "%s:%s",
393 wp->user, (wp->pass == NULL) ? "" : wp->pass);
394 curl_easy_setopt (wp->curl, CURLOPT_USERPWD, wp->credentials);
397 curl_easy_setopt (wp->curl, CURLOPT_SSL_VERIFYPEER, wp->verify_peer);
398 curl_easy_setopt (wp->curl, CURLOPT_SSL_VERIFYHOST,
399 wp->verify_host ? 2 : 0);
400 if (wp->cacert != NULL)
401 curl_easy_setopt (wp->curl, CURLOPT_CAINFO, wp->cacert);
404 } /* }}} int cc_page_init_curl */
406 static int cc_config_add_page (oconfig_item_t *ci) /* {{{ */
412 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
414 WARNING ("curl plugin: `Page' blocks need exactly one string argument.");
418 page = (web_page_t *) malloc (sizeof (*page));
421 ERROR ("curl plugin: malloc failed.");
424 memset (page, 0, sizeof (*page));
428 page->verify_peer = 1;
429 page->verify_host = 1;
430 page->response_time = 0;
432 page->instance = strdup (ci->values[0].value.string);
433 if (page->instance == NULL)
435 ERROR ("curl plugin: strdup failed.");
440 /* Process all children */
442 for (i = 0; i < ci->children_num; i++)
444 oconfig_item_t *child = ci->children + i;
446 if (strcasecmp ("URL", child->key) == 0)
447 status = cc_config_add_string ("URL", &page->url, child);
448 else if (strcasecmp ("User", child->key) == 0)
449 status = cc_config_add_string ("User", &page->user, child);
450 else if (strcasecmp ("Password", child->key) == 0)
451 status = cc_config_add_string ("Password", &page->pass, child);
452 else if (strcasecmp ("VerifyPeer", child->key) == 0)
453 status = cc_config_set_boolean ("VerifyPeer", &page->verify_peer, child);
454 else if (strcasecmp ("VerifyHost", child->key) == 0)
455 status = cc_config_set_boolean ("VerifyHost", &page->verify_host, child);
456 else if (strcasecmp ("MeasureResponseTime", child->key) == 0)
457 status = cc_config_set_boolean (child->key, &page->response_time, child);
458 else if (strcasecmp ("CACert", child->key) == 0)
459 status = cc_config_add_string ("CACert", &page->cacert, child);
460 else if (strcasecmp ("Match", child->key) == 0)
461 /* Be liberal with failing matches => don't set `status'. */
462 cc_config_add_match (page, child);
465 WARNING ("curl plugin: Option `%s' not allowed here.", child->key);
471 } /* for (i = 0; i < ci->children_num; i++) */
473 /* Additionial sanity checks and libCURL initialization. */
476 if (page->url == NULL)
478 WARNING ("curl plugin: `URL' missing in `Page' block.");
482 if (page->matches == NULL && !page->response_time)
484 assert (page->instance != NULL);
485 WARNING ("curl plugin: No (valid) `Match' block "
486 "or MeasureResponseTime within `Page' block `%s'.", page->instance);
491 status = cc_page_init_curl (page);
494 } /* while (status == 0) */
498 cc_web_page_free (page);
502 /* Add the new page to the linked list */
510 while ((prev != NULL) && (prev->next != NULL))
516 } /* }}} int cc_config_add_page */
518 static int cc_config (oconfig_item_t *ci) /* {{{ */
528 for (i = 0; i < ci->children_num; i++)
530 oconfig_item_t *child = ci->children + i;
532 if (strcasecmp ("Page", child->key) == 0)
534 status = cc_config_add_page (child);
542 WARNING ("curl plugin: Option `%s' not allowed here.", child->key);
547 if ((success == 0) && (errors > 0))
549 ERROR ("curl plugin: All statements failed.");
554 } /* }}} int cc_config */
556 static int cc_init (void) /* {{{ */
560 INFO ("curl plugin: No pages have been defined.");
564 } /* }}} int cc_init */
566 static void cc_submit (const web_page_t *wp, const web_match_t *wm, /* {{{ */
567 const cu_match_value_t *mv)
570 value_list_t vl = VALUE_LIST_INIT;
572 values[0] = mv->value;
576 vl.time = time (NULL);
577 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
578 sstrncpy (vl.plugin, "curl", sizeof (vl.plugin));
579 sstrncpy (vl.plugin_instance, wp->instance, sizeof (vl.plugin_instance));
580 sstrncpy (vl.type, wm->type, sizeof (vl.type));
581 sstrncpy (vl.type_instance, wm->instance, sizeof (vl.type_instance));
583 plugin_dispatch_values (&vl);
584 } /* }}} void cc_submit */
586 static void cc_submit_response_time (const web_page_t *wp, double seconds) /* {{{ */
589 value_list_t vl = VALUE_LIST_INIT;
591 values[0].gauge = seconds;
595 vl.time = time (NULL);
596 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
597 sstrncpy (vl.plugin, "curl", sizeof (vl.plugin));
598 sstrncpy (vl.plugin_instance, wp->instance, sizeof (vl.plugin_instance));
599 sstrncpy (vl.type, "response_time", sizeof (vl.type));
601 plugin_dispatch_values (&vl);
602 } /* }}} void cc_submit_response_time */
604 static int cc_read_page (web_page_t *wp) /* {{{ */
608 struct timeval start, end;
610 if (wp->response_time)
611 gettimeofday (&start, NULL);
614 status = curl_easy_perform (wp->curl);
617 ERROR ("curl plugin: curl_easy_perform failed with staus %i: %s",
618 status, wp->curl_errbuf);
622 if (wp->response_time)
625 gettimeofday (&end, NULL);
626 secs += end.tv_sec - start.tv_sec;
627 secs += (end.tv_usec - start.tv_usec) / 1000000.0;
628 cc_submit_response_time (wp, secs);
631 for (wm = wp->matches; wm != NULL; wm = wm->next)
633 cu_match_value_t *mv;
635 status = match_apply (wm->match, wp->buffer);
638 WARNING ("curl plugin: match_apply failed.");
642 mv = match_get_user_data (wm->match);
645 WARNING ("curl plugin: match_get_user_data returned NULL.");
649 cc_submit (wp, wm, mv);
650 } /* for (wm = wp->matches; wm != NULL; wm = wm->next) */
653 } /* }}} int cc_read_page */
655 static int cc_read (void) /* {{{ */
659 for (wp = pages_g; wp != NULL; wp = wp->next)
663 } /* }}} int cc_read */
665 static int cc_shutdown (void) /* {{{ */
667 cc_web_page_free (pages_g);
671 } /* }}} int cc_shutdown */
673 void module_register (void)
675 plugin_register_complex_config ("curl", cc_config);
676 plugin_register_init ("curl", cc_init);
677 plugin_register_read ("curl", cc_read);
678 plugin_register_shutdown ("curl", cc_shutdown);
679 } /* void module_register */
681 /* vim: set sw=2 sts=2 et fdm=marker : */