3e5c578e4ef5b0de858f0d43d9ae6d9d8c91d06f
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 verplant.org>
21 * Aman Gupta <aman at tmm1.net>
22 **/
24 #include "collectd.h"
25 #include "common.h"
26 #include "plugin.h"
27 #include "configfile.h"
28 #include "utils_match.h"
30 #include <curl/curl.h>
32 /*
33 * Data types
34 */
35 struct web_match_s;
36 typedef struct web_match_s web_match_t;
37 struct web_match_s /* {{{ */
38 {
39 char *regex;
40 char *exclude_regex;
41 int dstype;
42 char *type;
43 char *instance;
45 cu_match_t *match;
47 web_match_t *next;
48 }; /* }}} */
50 struct web_page_s;
51 typedef struct web_page_s web_page_t;
52 struct web_page_s /* {{{ */
53 {
54 char *instance;
56 char *url;
57 char *user;
58 char *pass;
59 char *credentials;
60 int verify_peer;
61 int verify_host;
62 char *cacert;
63 struct curl_slist *headers;
64 char *post_body;
65 int response_time;
66 int response_code;
68 CURL *curl;
69 char curl_errbuf[CURL_ERROR_SIZE];
70 char *buffer;
71 size_t buffer_size;
72 size_t buffer_fill;
74 web_match_t *matches;
76 web_page_t *next;
77 }; /* }}} */
79 /*
80 * Global variables;
81 */
82 /* static CURLM *curl = NULL; */
83 static web_page_t *pages_g = NULL;
85 /*
86 * Private functions
87 */
88 static size_t cc_curl_callback (void *buf, /* {{{ */
89 size_t size, size_t nmemb, void *user_data)
90 {
91 web_page_t *wp;
92 size_t len;
94 len = size * nmemb;
95 if (len <= 0)
96 return (len);
98 wp = user_data;
99 if (wp == NULL)
100 return (0);
102 if ((wp->buffer_fill + len) >= wp->buffer_size)
103 {
104 char *temp;
105 size_t temp_size;
107 temp_size = wp->buffer_fill + len + 1;
108 temp = (char *) realloc (wp->buffer, temp_size);
109 if (temp == NULL)
110 {
111 ERROR ("curl plugin: realloc failed.");
112 return (0);
113 }
114 wp->buffer = temp;
115 wp->buffer_size = temp_size;
116 }
118 memcpy (wp->buffer + wp->buffer_fill, (char *) buf, len);
119 wp->buffer_fill += len;
120 wp->buffer[wp->buffer_fill] = 0;
122 return (len);
123 } /* }}} size_t cc_curl_callback */
125 static void cc_web_match_free (web_match_t *wm) /* {{{ */
126 {
127 if (wm == NULL)
128 return;
130 sfree (wm->regex);
131 sfree (wm->type);
132 sfree (wm->instance);
133 match_destroy (wm->match);
134 cc_web_match_free (wm->next);
135 sfree (wm);
136 } /* }}} void cc_web_match_free */
138 static void cc_web_page_free (web_page_t *wp) /* {{{ */
139 {
140 if (wp == NULL)
141 return;
143 if (wp->curl != NULL)
144 curl_easy_cleanup (wp->curl);
145 wp->curl = NULL;
147 sfree (wp->instance);
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);
157 sfree (wp->buffer);
159 cc_web_match_free (wp->matches);
160 cc_web_page_free (wp->next);
161 sfree (wp);
162 } /* }}} void cc_web_page_free */
164 static int cc_config_add_string (const char *name, char **dest, /* {{{ */
165 oconfig_item_t *ci)
166 {
167 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
168 {
169 WARNING ("curl plugin: `%s' needs exactly one string argument.", name);
170 return (-1);
171 }
173 sfree (*dest);
174 *dest = strdup (ci->values[0].value.string);
175 if (*dest == NULL)
176 return (-1);
178 return (0);
179 } /* }}} int cc_config_add_string */
181 static int cc_config_append_string (const char *name, struct curl_slist **dest, /* {{{ */
182 oconfig_item_t *ci)
183 {
184 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
185 {
186 WARNING ("curl plugin: `%s' needs exactly one string argument.", name);
187 return (-1);
188 }
190 *dest = curl_slist_append(*dest, ci->values[0].value.string);
191 if (*dest == NULL)
192 return (-1);
194 return (0);
195 } /* }}} int cc_config_append_string */
198 static int cc_config_set_boolean (const char *name, int *dest, /* {{{ */
199 oconfig_item_t *ci)
200 {
201 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
202 {
203 WARNING ("curl plugin: `%s' needs exactly one boolean argument.", name);
204 return (-1);
205 }
207 *dest = ci->values[0].value.boolean ? 1 : 0;
209 return (0);
210 } /* }}} int cc_config_set_boolean */
212 static int cc_config_add_match_dstype (int *dstype_ret, /* {{{ */
213 oconfig_item_t *ci)
214 {
215 int dstype;
217 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
218 {
219 WARNING ("curl plugin: `DSType' needs exactly one string argument.");
220 return (-1);
221 }
223 if (strncasecmp ("Gauge", ci->values[0].value.string,
224 strlen ("Gauge")) == 0)
225 {
226 dstype = UTILS_MATCH_DS_TYPE_GAUGE;
227 if (strcasecmp ("GaugeAverage", ci->values[0].value.string) == 0)
228 dstype |= UTILS_MATCH_CF_GAUGE_AVERAGE;
229 else if (strcasecmp ("GaugeMin", ci->values[0].value.string) == 0)
230 dstype |= UTILS_MATCH_CF_GAUGE_MIN;
231 else if (strcasecmp ("GaugeMax", ci->values[0].value.string) == 0)
232 dstype |= UTILS_MATCH_CF_GAUGE_MAX;
233 else if (strcasecmp ("GaugeLast", ci->values[0].value.string) == 0)
234 dstype |= UTILS_MATCH_CF_GAUGE_LAST;
235 else
236 dstype = 0;
237 }
238 else if (strncasecmp ("Counter", ci->values[0].value.string,
239 strlen ("Counter")) == 0)
240 {
241 dstype = UTILS_MATCH_DS_TYPE_COUNTER;
242 if (strcasecmp ("CounterSet", ci->values[0].value.string) == 0)
243 dstype |= UTILS_MATCH_CF_COUNTER_SET;
244 else if (strcasecmp ("CounterAdd", ci->values[0].value.string) == 0)
245 dstype |= UTILS_MATCH_CF_COUNTER_ADD;
246 else if (strcasecmp ("CounterInc", ci->values[0].value.string) == 0)
247 dstype |= UTILS_MATCH_CF_COUNTER_INC;
248 else
249 dstype = 0;
250 }
251 else if (strncasecmp ("Derive", ci->values[0].value.string,
252 strlen ("Derive")) == 0)
253 {
254 dstype = UTILS_MATCH_DS_TYPE_DERIVE;
255 if (strcasecmp ("DeriveSet", ci->values[0].value.string) == 0)
256 dstype |= UTILS_MATCH_CF_DERIVE_SET;
257 else if (strcasecmp ("DeriveAdd", ci->values[0].value.string) == 0)
258 dstype |= UTILS_MATCH_CF_DERIVE_ADD;
259 else if (strcasecmp ("DeriveInc", ci->values[0].value.string) == 0)
260 dstype |= UTILS_MATCH_CF_DERIVE_INC;
261 else
262 dstype = 0;
263 }
264 else if (strncasecmp ("Absolute", ci->values[0].value.string,
265 strlen ("Absolute")) == 0)
266 {
267 dstype = UTILS_MATCH_DS_TYPE_ABSOLUTE;
268 if (strcasecmp ("AbsoluteSet", ci->values[0].value.string) == 0) /* Absolute DS is reset-on-read so no sense doin anything else but set */
269 dstype |= UTILS_MATCH_CF_ABSOLUTE_SET;
270 else
271 dstype = 0;
272 }
274 else
275 {
276 dstype = 0;
277 }
279 if (dstype == 0)
280 {
281 WARNING ("curl plugin: `%s' is not a valid argument to `DSType'.",
282 ci->values[0].value.string);
283 return (-1);
284 }
286 *dstype_ret = dstype;
287 return (0);
288 } /* }}} int cc_config_add_match_dstype */
290 static int cc_config_add_match (web_page_t *page, /* {{{ */
291 oconfig_item_t *ci)
292 {
293 web_match_t *match;
294 int status;
295 int i;
297 if (ci->values_num != 0)
298 {
299 WARNING ("curl plugin: Ignoring arguments for the `Match' block.");
300 }
302 match = (web_match_t *) malloc (sizeof (*match));
303 if (match == NULL)
304 {
305 ERROR ("curl plugin: malloc failed.");
306 return (-1);
307 }
308 memset (match, 0, sizeof (*match));
310 status = 0;
311 for (i = 0; i < ci->children_num; i++)
312 {
313 oconfig_item_t *child = ci->children + i;
315 if (strcasecmp ("Regex", child->key) == 0)
316 status = cc_config_add_string ("Regex", &match->regex, child);
317 else if (strcasecmp ("ExcludeRegex", child->key) == 0)
318 status = cc_config_add_string ("ExcludeRegex", &match->exclude_regex, child);
319 else if (strcasecmp ("DSType", child->key) == 0)
320 status = cc_config_add_match_dstype (&match->dstype, child);
321 else if (strcasecmp ("Type", child->key) == 0)
322 status = cc_config_add_string ("Type", &match->type, child);
323 else if (strcasecmp ("Instance", child->key) == 0)
324 status = cc_config_add_string ("Instance", &match->instance, child);
325 else
326 {
327 WARNING ("curl plugin: Option `%s' not allowed here.", child->key);
328 status = -1;
329 }
331 if (status != 0)
332 break;
333 } /* for (i = 0; i < ci->children_num; i++) */
335 while (status == 0)
336 {
337 if (match->regex == NULL)
338 {
339 WARNING ("curl plugin: `Regex' missing in `Match' block.");
340 status = -1;
341 }
343 if (match->type == NULL)
344 {
345 WARNING ("curl plugin: `Type' missing in `Match' block.");
346 status = -1;
347 }
349 if (match->dstype == 0)
350 {
351 WARNING ("curl plugin: `DSType' missing in `Match' block.");
352 status = -1;
353 }
355 break;
356 } /* while (status == 0) */
358 if (status != 0)
359 return (status);
361 match->match = match_create_simple (match->regex, match->exclude_regex,
362 match->dstype);
363 if (match->match == NULL)
364 {
365 ERROR ("curl plugin: tail_match_add_match_simple failed.");
366 cc_web_match_free (match);
367 return (-1);
368 }
369 else
370 {
371 web_match_t *prev;
373 prev = page->matches;
374 while ((prev != NULL) && (prev->next != NULL))
375 prev = prev->next;
377 if (prev == NULL)
378 page->matches = match;
379 else
380 prev->next = match;
381 }
383 return (0);
384 } /* }}} int cc_config_add_match */
386 static int cc_page_init_curl (web_page_t *wp) /* {{{ */
387 {
388 wp->curl = curl_easy_init ();
389 if (wp->curl == NULL)
390 {
391 ERROR ("curl plugin: curl_easy_init failed.");
392 return (-1);
393 }
395 curl_easy_setopt (wp->curl, CURLOPT_NOSIGNAL, 1L);
396 curl_easy_setopt (wp->curl, CURLOPT_WRITEFUNCTION, cc_curl_callback);
397 curl_easy_setopt (wp->curl, CURLOPT_WRITEDATA, wp);
398 curl_easy_setopt (wp->curl, CURLOPT_USERAGENT,
399 PACKAGE_NAME"/"PACKAGE_VERSION);
400 curl_easy_setopt (wp->curl, CURLOPT_ERRORBUFFER, wp->curl_errbuf);
401 curl_easy_setopt (wp->curl, CURLOPT_URL, wp->url);
402 curl_easy_setopt (wp->curl, CURLOPT_FOLLOWLOCATION, 1L);
403 curl_easy_setopt (wp->curl, CURLOPT_MAXREDIRS, 50L);
405 if (wp->user != NULL)
406 {
407 size_t credentials_size;
409 credentials_size = strlen (wp->user) + 2;
410 if (wp->pass != NULL)
411 credentials_size += strlen (wp->pass);
413 wp->credentials = (char *) malloc (credentials_size);
414 if (wp->credentials == NULL)
415 {
416 ERROR ("curl plugin: malloc failed.");
417 return (-1);
418 }
420 ssnprintf (wp->credentials, credentials_size, "%s:%s",
421 wp->user, (wp->pass == NULL) ? "" : wp->pass);
422 curl_easy_setopt (wp->curl, CURLOPT_USERPWD, wp->credentials);
423 }
425 curl_easy_setopt (wp->curl, CURLOPT_SSL_VERIFYPEER, (long) wp->verify_peer);
426 curl_easy_setopt (wp->curl, CURLOPT_SSL_VERIFYHOST,
427 wp->verify_host ? 2L : 0L);
428 if (wp->cacert != NULL)
429 curl_easy_setopt (wp->curl, CURLOPT_CAINFO, wp->cacert);
430 if (wp->headers != NULL)
431 curl_easy_setopt (wp->curl, CURLOPT_HTTPHEADER, wp->headers);
432 if (wp->post_body != NULL)
433 curl_easy_setopt (wp->curl, CURLOPT_POSTFIELDS, wp->post_body);
435 return (0);
436 } /* }}} int cc_page_init_curl */
438 static int cc_config_add_page (oconfig_item_t *ci) /* {{{ */
439 {
440 web_page_t *page;
441 int status;
442 int i;
444 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
445 {
446 WARNING ("curl plugin: `Page' blocks need exactly one string argument.");
447 return (-1);
448 }
450 page = (web_page_t *) malloc (sizeof (*page));
451 if (page == NULL)
452 {
453 ERROR ("curl plugin: malloc failed.");
454 return (-1);
455 }
456 memset (page, 0, sizeof (*page));
457 page->url = NULL;
458 page->user = NULL;
459 page->pass = NULL;
460 page->verify_peer = 1;
461 page->verify_host = 1;
462 page->response_time = 0;
463 page->response_code = 0;
465 page->instance = strdup (ci->values[0].value.string);
466 if (page->instance == NULL)
467 {
468 ERROR ("curl plugin: strdup failed.");
469 sfree (page);
470 return (-1);
471 }
473 /* Process all children */
474 status = 0;
475 for (i = 0; i < ci->children_num; i++)
476 {
477 oconfig_item_t *child = ci->children + i;
479 if (strcasecmp ("URL", child->key) == 0)
480 status = cc_config_add_string ("URL", &page->url, child);
481 else if (strcasecmp ("User", child->key) == 0)
482 status = cc_config_add_string ("User", &page->user, child);
483 else if (strcasecmp ("Password", child->key) == 0)
484 status = cc_config_add_string ("Password", &page->pass, child);
485 else if (strcasecmp ("VerifyPeer", child->key) == 0)
486 status = cc_config_set_boolean ("VerifyPeer", &page->verify_peer, child);
487 else if (strcasecmp ("VerifyHost", child->key) == 0)
488 status = cc_config_set_boolean ("VerifyHost", &page->verify_host, child);
489 else if (strcasecmp ("MeasureResponseTime", child->key) == 0)
490 status = cc_config_set_boolean (child->key, &page->response_time, child);
491 else if (strcasecmp ("MeasureResponseCode", child->key) == 0)
492 status = cc_config_set_boolean (child->key, &page->response_code, child);
493 else if (strcasecmp ("CACert", child->key) == 0)
494 status = cc_config_add_string ("CACert", &page->cacert, child);
495 else if (strcasecmp ("Match", child->key) == 0)
496 /* Be liberal with failing matches => don't set `status'. */
497 cc_config_add_match (page, child);
498 else if (strcasecmp ("Header", child->key) == 0)
499 status = cc_config_append_string ("Header", &page->headers, child);
500 else if (strcasecmp ("Post", child->key) == 0)
501 status = cc_config_add_string ("Post", &page->post_body, child);
502 else
503 {
504 WARNING ("curl plugin: Option `%s' not allowed here.", child->key);
505 status = -1;
506 }
508 if (status != 0)
509 break;
510 } /* for (i = 0; i < ci->children_num; i++) */
512 /* Additionial sanity checks and libCURL initialization. */
513 while (status == 0)
514 {
515 if (page->url == NULL)
516 {
517 WARNING ("curl plugin: `URL' missing in `Page' block.");
518 status = -1;
519 }
521 if (page->matches == NULL && !page->response_time && !page->response_code)
522 {
523 assert (page->instance != NULL);
524 WARNING ("curl plugin: No (valid) `Match' block "
525 "or MeasureResponseTime or MeasureResponseCode within "
526 "`Page' block `%s'.", page->instance);
527 status = -1;
528 }
530 if (status == 0)
531 status = cc_page_init_curl (page);
533 break;
534 } /* while (status == 0) */
536 if (status != 0)
537 {
538 cc_web_page_free (page);
539 return (status);
540 }
542 /* Add the new page to the linked list */
543 if (pages_g == NULL)
544 pages_g = page;
545 else
546 {
547 web_page_t *prev;
549 prev = pages_g;
550 while ((prev != NULL) && (prev->next != NULL))
551 prev = prev->next;
552 prev->next = page;
553 }
555 return (0);
556 } /* }}} int cc_config_add_page */
558 static int cc_config (oconfig_item_t *ci) /* {{{ */
559 {
560 int success;
561 int errors;
562 int status;
563 int i;
565 success = 0;
566 errors = 0;
568 for (i = 0; i < ci->children_num; i++)
569 {
570 oconfig_item_t *child = ci->children + i;
572 if (strcasecmp ("Page", child->key) == 0)
573 {
574 status = cc_config_add_page (child);
575 if (status == 0)
576 success++;
577 else
578 errors++;
579 }
580 else
581 {
582 WARNING ("curl plugin: Option `%s' not allowed here.", child->key);
583 errors++;
584 }
585 }
587 if ((success == 0) && (errors > 0))
588 {
589 ERROR ("curl plugin: All statements failed.");
590 return (-1);
591 }
593 return (0);
594 } /* }}} int cc_config */
596 static int cc_init (void) /* {{{ */
597 {
598 if (pages_g == NULL)
599 {
600 INFO ("curl plugin: No pages have been defined.");
601 return (-1);
602 }
603 return (0);
604 } /* }}} int cc_init */
606 static void cc_submit (const web_page_t *wp, const web_match_t *wm, /* {{{ */
607 const cu_match_value_t *mv)
608 {
609 value_t values[1];
610 value_list_t vl = VALUE_LIST_INIT;
612 values[0] = mv->value;
614 vl.values = values;
615 vl.values_len = 1;
616 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
617 sstrncpy (vl.plugin, "curl", sizeof (vl.plugin));
618 sstrncpy (vl.plugin_instance, wp->instance, sizeof (vl.plugin_instance));
619 sstrncpy (vl.type, wm->type, sizeof (vl.type));
620 sstrncpy (vl.type_instance, wm->instance, sizeof (vl.type_instance));
622 plugin_dispatch_values (&vl);
623 } /* }}} void cc_submit */
625 static void cc_submit_response_code (const web_page_t *wp, long code) /* {{{ */
626 {
627 value_t values[1];
628 value_list_t vl = VALUE_LIST_INIT;
630 values[0].gauge = code;
632 vl.values = values;
633 vl.values_len = 1;
634 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
635 sstrncpy (vl.plugin, "curl", sizeof (vl.plugin));
636 sstrncpy (vl.plugin_instance, wp->instance, sizeof (vl.plugin_instance));
637 sstrncpy (vl.type, "response_code", sizeof (vl.type));
639 plugin_dispatch_values (&vl);
640 } /* }}} void cc_submit_response_code */
642 static void cc_submit_response_time (const web_page_t *wp, double seconds) /* {{{ */
643 {
644 value_t values[1];
645 value_list_t vl = VALUE_LIST_INIT;
647 values[0].gauge = seconds;
649 vl.values = values;
650 vl.values_len = 1;
651 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
652 sstrncpy (vl.plugin, "curl", sizeof (vl.plugin));
653 sstrncpy (vl.plugin_instance, wp->instance, sizeof (vl.plugin_instance));
654 sstrncpy (vl.type, "response_time", sizeof (vl.type));
656 plugin_dispatch_values (&vl);
657 } /* }}} void cc_submit_response_time */
659 static int cc_read_page (web_page_t *wp) /* {{{ */
660 {
661 web_match_t *wm;
662 int status;
663 struct timeval start, end;
665 if (wp->response_time)
666 gettimeofday (&start, NULL);
668 wp->buffer_fill = 0;
669 status = curl_easy_perform (wp->curl);
670 if (status != CURLE_OK)
671 {
672 ERROR ("curl plugin: curl_easy_perform failed with staus %i: %s",
673 status, wp->curl_errbuf);
674 return (-1);
675 }
677 if(wp->response_code)
678 {
679 long *response_code;
680 status = curl_easy_getinfo(wp->curl, CURLINFO_RESPONSE_CODE, &response_code);
681 if(status != CURLE_OK) {
682 ERROR ("curl plugin: curl_easy_getinfo failed with staus %i: %s",
683 status, wp->curl_errbuf);
684 return (-1); // TODO: do we need to return in here? this is nonfatal error
685 }
686 cc_submit_response_code(wp, response_code);
687 }
689 if (wp->response_time)
690 {
691 double secs = 0;
692 gettimeofday (&end, NULL);
693 secs += end.tv_sec - start.tv_sec;
694 secs += (end.tv_usec - start.tv_usec) / 1000000.0;
695 cc_submit_response_time (wp, secs);
696 }
698 for (wm = wp->matches; wm != NULL; wm = wm->next)
699 {
700 cu_match_value_t *mv;
702 status = match_apply (wm->match, wp->buffer);
703 if (status != 0)
704 {
705 WARNING ("curl plugin: match_apply failed.");
706 continue;
707 }
709 mv = match_get_user_data (wm->match);
710 if (mv == NULL)
711 {
712 WARNING ("curl plugin: match_get_user_data returned NULL.");
713 continue;
714 }
716 cc_submit (wp, wm, mv);
717 } /* for (wm = wp->matches; wm != NULL; wm = wm->next) */
719 return (0);
720 } /* }}} int cc_read_page */
722 static int cc_read (void) /* {{{ */
723 {
724 web_page_t *wp;
726 for (wp = pages_g; wp != NULL; wp = wp->next)
727 cc_read_page (wp);
729 return (0);
730 } /* }}} int cc_read */
732 static int cc_shutdown (void) /* {{{ */
733 {
734 cc_web_page_free (pages_g);
735 pages_g = NULL;
737 return (0);
738 } /* }}} int cc_shutdown */
740 void module_register (void)
741 {
742 plugin_register_complex_config ("curl", cc_config);
743 plugin_register_init ("curl", cc_init);
744 plugin_register_read ("curl", cc_read);
745 plugin_register_shutdown ("curl", cc_shutdown);
746 } /* void module_register */
748 /* vim: set sw=2 sts=2 et fdm=marker : */