1 /**
2 * collectd - src/curl.c
3 * Copyright (C) 2006-2009 Florian octo Forster
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the License is applicable.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 *
18 * Authors:
19 * Florian octo Forster <octo at verplant.org>
20 **/
22 #include "collectd.h"
23 #include "common.h"
24 #include "plugin.h"
25 #include "configfile.h"
26 #include "utils_match.h"
28 #include <curl/curl.h>
30 /*
31 * Data types
32 */
33 struct web_match_s;
34 typedef struct web_match_s web_match_t;
35 struct web_match_s /* {{{ */
36 {
37 char *regex;
38 int dstype;
39 char *type;
40 char *instance;
42 cu_match_t *match;
44 web_match_t *next;
45 }; /* }}} */
47 struct web_page_s;
48 typedef struct web_page_s web_page_t;
49 struct web_page_s /* {{{ */
50 {
51 char *instance;
53 char *url;
54 char *user;
55 char *pass;
56 char *credentials;
57 int verify_peer;
58 int verify_host;
59 char *cacert;
61 CURL *curl;
62 char curl_errbuf[CURL_ERROR_SIZE];
63 char *buffer;
64 size_t buffer_size;
65 size_t buffer_fill;
67 web_match_t *matches;
69 web_page_t *next;
70 }; /* }}} */
72 /*
73 * Global variables;
74 */
75 /* static CURLM *curl = NULL; */
76 static web_page_t *pages_g = NULL;
78 /*
79 * Private functions
80 */
81 static size_t cc_curl_callback (void *buf, /* {{{ */
82 size_t size, size_t nmemb, void *user_data)
83 {
84 web_page_t *wp;
85 size_t len;
87 len = size * nmemb;
88 if (len <= 0)
89 return (len);
91 wp = user_data;
92 if (wp == NULL)
93 return (0);
95 if ((wp->buffer_fill + len) >= wp->buffer_size)
96 {
97 char *temp;
98 size_t temp_size;
100 temp_size = wp->buffer_fill + len + 1;
101 temp = (char *) realloc (wp->buffer, temp_size);
102 if (temp == NULL)
103 {
104 ERROR ("curl plugin: realloc failed.");
105 return (0);
106 }
107 wp->buffer = temp;
108 wp->buffer_size = temp_size;
109 }
111 memcpy (wp->buffer + wp->buffer_fill, (char *) buf, len);
112 wp->buffer_fill += len;
113 wp->buffer[wp->buffer_fill] = 0;
115 return (len);
116 } /* }}} size_t cc_curl_callback */
118 static void cc_web_match_free (web_match_t *wm) /* {{{ */
119 {
120 if (wm == NULL)
121 return;
123 sfree (wm->regex);
124 sfree (wm->type);
125 sfree (wm->instance);
126 match_destroy (wm->match);
127 cc_web_match_free (wm->next);
128 sfree (wm);
129 } /* }}} void cc_web_match_free */
131 static void cc_web_page_free (web_page_t *wp) /* {{{ */
132 {
133 if (wp == NULL)
134 return;
136 if (wp->curl != NULL)
137 curl_easy_cleanup (wp->curl);
138 wp->curl = NULL;
140 sfree (wp->instance);
142 sfree (wp->url);
143 sfree (wp->user);
144 sfree (wp->pass);
145 sfree (wp->credentials);
146 sfree (wp->cacert);
148 sfree (wp->buffer);
150 cc_web_match_free (wp->matches);
151 cc_web_page_free (wp->next);
152 sfree (wp);
153 } /* }}} void cc_web_page_free */
155 static int cc_config_add_string (const char *name, char **dest, /* {{{ */
156 oconfig_item_t *ci)
157 {
158 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
159 {
160 WARNING ("curl plugin: `%s' needs exactly one string argument.", name);
161 return (-1);
162 }
164 sfree (*dest);
165 *dest = strdup (ci->values[0].value.string);
166 if (*dest == NULL)
167 return (-1);
169 return (0);
170 } /* }}} int cc_config_add_string */
172 static int cc_config_set_boolean (const char *name, int *dest, /* {{{ */
173 oconfig_item_t *ci)
174 {
175 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
176 {
177 WARNING ("curl plugin: `%s' needs exactly one boolean argument.", name);
178 return (-1);
179 }
181 *dest = ci->values[0].value.boolean ? 1 : 0;
183 return (0);
184 } /* }}} int cc_config_set_boolean */
186 static int cc_config_add_match_dstype (int *dstype_ret, /* {{{ */
187 oconfig_item_t *ci)
188 {
189 int dstype;
191 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
192 {
193 WARNING ("curl plugin: `DSType' needs exactly one string argument.");
194 return (-1);
195 }
197 if (strncasecmp ("Gauge", ci->values[0].value.string,
198 strlen ("Gauge")) == 0)
199 {
200 dstype = UTILS_MATCH_DS_TYPE_GAUGE;
201 if (strcasecmp ("GaugeAverage", ci->values[0].value.string) == 0)
202 dstype |= UTILS_MATCH_CF_GAUGE_AVERAGE;
203 else if (strcasecmp ("GaugeMin", ci->values[0].value.string) == 0)
204 dstype |= UTILS_MATCH_CF_GAUGE_MIN;
205 else if (strcasecmp ("GaugeMax", ci->values[0].value.string) == 0)
206 dstype |= UTILS_MATCH_CF_GAUGE_MAX;
207 else if (strcasecmp ("GaugeLast", ci->values[0].value.string) == 0)
208 dstype |= UTILS_MATCH_CF_GAUGE_LAST;
209 else
210 dstype = 0;
211 }
212 else if (strncasecmp ("Counter", ci->values[0].value.string,
213 strlen ("Counter")) == 0)
214 {
215 dstype = UTILS_MATCH_DS_TYPE_COUNTER;
216 if (strcasecmp ("CounterSet", ci->values[0].value.string) == 0)
217 dstype |= UTILS_MATCH_CF_COUNTER_SET;
218 else if (strcasecmp ("CounterAdd", ci->values[0].value.string) == 0)
219 dstype |= UTILS_MATCH_CF_COUNTER_ADD;
220 else if (strcasecmp ("CounterInc", ci->values[0].value.string) == 0)
221 dstype |= UTILS_MATCH_CF_COUNTER_INC;
222 else
223 dstype = 0;
224 }
225 else
226 {
227 dstype = 0;
228 }
230 if (dstype == 0)
231 {
232 WARNING ("curl plugin: `%s' is not a valid argument to `DSType'.",
233 ci->values[0].value.string);
234 return (-1);
235 }
237 *dstype_ret = dstype;
238 return (0);
239 } /* }}} int cc_config_add_match_dstype */
241 static int cc_config_add_match (web_page_t *page, /* {{{ */
242 oconfig_item_t *ci)
243 {
244 web_match_t *match;
245 int status;
246 int i;
248 if (ci->values_num != 0)
249 {
250 WARNING ("curl plugin: Ignoring arguments for the `Match' block.");
251 }
253 match = (web_match_t *) malloc (sizeof (*match));
254 if (match == NULL)
255 {
256 ERROR ("curl plugin: malloc failed.");
257 return (-1);
258 }
259 memset (match, 0, sizeof (*match));
261 status = 0;
262 for (i = 0; i < ci->children_num; i++)
263 {
264 oconfig_item_t *child = ci->children + i;
266 if (strcasecmp ("Regex", child->key) == 0)
267 status = cc_config_add_string ("Regex", &match->regex, child);
268 else if (strcasecmp ("DSType", child->key) == 0)
269 status = cc_config_add_match_dstype (&match->dstype, child);
270 else if (strcasecmp ("Type", child->key) == 0)
271 status = cc_config_add_string ("Type", &match->type, child);
272 else if (strcasecmp ("Instance", child->key) == 0)
273 status = cc_config_add_string ("Instance", &match->instance, child);
274 else
275 {
276 WARNING ("curl plugin: Option `%s' not allowed here.", child->key);
277 status = -1;
278 }
280 if (status != 0)
281 break;
282 } /* for (i = 0; i < ci->children_num; i++) */
284 while (status == 0)
285 {
286 if (match->regex == NULL)
287 {
288 WARNING ("curl plugin: `Regex' missing in `Match' block.");
289 status = -1;
290 }
292 if (match->type == NULL)
293 {
294 WARNING ("curl plugin: `Type' missing in `Match' block.");
295 status = -1;
296 }
298 if (match->dstype == 0)
299 {
300 WARNING ("curl plugin: `DSType' missing in `Match' block.");
301 status = -1;
302 }
304 break;
305 } /* while (status == 0) */
307 if (status != 0)
308 return (status);
310 match->match = match_create_simple (match->regex, match->dstype);
311 if (match->match == NULL)
312 {
313 ERROR ("curl plugin: tail_match_add_match_simple failed.");
314 cc_web_match_free (match);
315 return (-1);
316 }
317 else
318 {
319 web_match_t *prev;
321 prev = page->matches;
322 while ((prev != NULL) && (prev->next != NULL))
323 prev = prev->next;
325 if (prev == NULL)
326 page->matches = match;
327 else
328 prev->next = match;
329 }
331 return (0);
332 } /* }}} int cc_config_add_match */
334 static int cc_page_init_curl (web_page_t *wp) /* {{{ */
335 {
336 wp->curl = curl_easy_init ();
337 if (wp->curl == NULL)
338 {
339 ERROR ("curl plugin: curl_easy_init failed.");
340 return (-1);
341 }
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,
346 PACKAGE_NAME"/"PACKAGE_VERSION);
347 curl_easy_setopt (wp->curl, CURLOPT_ERRORBUFFER, wp->curl_errbuf);
348 curl_easy_setopt (wp->curl, CURLOPT_URL, wp->url);
350 if (wp->user != NULL)
351 {
352 size_t credentials_size;
354 credentials_size = strlen (wp->user) + 2;
355 if (wp->pass != NULL)
356 credentials_size += strlen (wp->pass);
358 wp->credentials = (char *) malloc (credentials_size);
359 if (wp->credentials == NULL)
360 {
361 ERROR ("curl plugin: malloc failed.");
362 return (-1);
363 }
365 ssnprintf (wp->credentials, credentials_size, "%s:%s",
366 wp->user, (wp->pass == NULL) ? "" : wp->pass);
367 curl_easy_setopt (wp->curl, CURLOPT_USERPWD, wp->credentials);
368 }
370 curl_easy_setopt (wp->curl, CURLOPT_SSL_VERIFYPEER, wp->verify_peer);
371 curl_easy_setopt (wp->curl, CURLOPT_SSL_VERIFYHOST,
372 wp->verify_host ? 2 : 0);
373 if (wp->cacert != NULL)
374 curl_easy_setopt (wp->curl, CURLOPT_CAINFO, wp->cacert);
376 return (0);
377 } /* }}} int cc_page_init_curl */
379 static int cc_config_add_page (oconfig_item_t *ci) /* {{{ */
380 {
381 web_page_t *page;
382 int status;
383 int i;
385 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
386 {
387 WARNING ("curl plugin: `Page' blocks need exactly one string argument.");
388 return (-1);
389 }
391 page = (web_page_t *) malloc (sizeof (*page));
392 if (page == NULL)
393 {
394 ERROR ("curl plugin: malloc failed.");
395 return (-1);
396 }
397 memset (page, 0, sizeof (*page));
398 page->url = NULL;
399 page->user = NULL;
400 page->pass = NULL;
401 page->verify_peer = 1;
402 page->verify_host = 1;
404 page->instance = strdup (ci->values[0].value.string);
405 if (page->instance == NULL)
406 {
407 ERROR ("curl plugin: strdup failed.");
408 sfree (page);
409 return (-1);
410 }
412 /* Process all children */
413 status = 0;
414 for (i = 0; i < ci->children_num; i++)
415 {
416 oconfig_item_t *child = ci->children + i;
418 if (strcasecmp ("URL", child->key) == 0)
419 status = cc_config_add_string ("URL", &page->url, child);
420 else if (strcasecmp ("User", child->key) == 0)
421 status = cc_config_add_string ("User", &page->user, child);
422 else if (strcasecmp ("Password", child->key) == 0)
423 status = cc_config_add_string ("Password", &page->pass, child);
424 else if (strcasecmp ("VerifyPeer", child->key) == 0)
425 status = cc_config_set_boolean ("VerifyPeer", &page->verify_peer, child);
426 else if (strcasecmp ("VerifyHost", child->key) == 0)
427 status = cc_config_set_boolean ("VerifyHost", &page->verify_host, child);
428 else if (strcasecmp ("CACert", child->key) == 0)
429 status = cc_config_add_string ("CACert", &page->cacert, child);
430 else if (strcasecmp ("Match", child->key) == 0)
431 /* Be liberal with failing matches => don't set `status'. */
432 cc_config_add_match (page, child);
433 else
434 {
435 WARNING ("curl plugin: Option `%s' not allowed here.", child->key);
436 status = -1;
437 }
439 if (status != 0)
440 break;
441 } /* for (i = 0; i < ci->children_num; i++) */
443 /* Additionial sanity checks and libCURL initialization. */
444 while (status == 0)
445 {
446 if (page->url == NULL)
447 {
448 WARNING ("curl plugin: `URL' missing in `Page' block.");
449 status = -1;
450 }
452 if (page->matches == NULL)
453 {
454 assert (page->instance != NULL);
455 WARNING ("curl plugin: No (valid) `Match' block "
456 "within `Page' block `%s'.", page->instance);
457 status = -1;
458 }
460 if (status == 0)
461 status = cc_page_init_curl (page);
463 break;
464 } /* while (status == 0) */
466 if (status != 0)
467 {
468 cc_web_page_free (page);
469 return (status);
470 }
472 /* Add the new page to the linked list */
473 if (pages_g == NULL)
474 pages_g = page;
475 else
476 {
477 web_page_t *prev;
479 prev = pages_g;
480 while ((prev != NULL) && (prev->next != NULL))
481 prev = prev->next;
482 prev->next = page;
483 }
485 return (0);
486 } /* }}} int cc_config_add_page */
488 static int cc_config (oconfig_item_t *ci) /* {{{ */
489 {
490 int success;
491 int errors;
492 int status;
493 int i;
495 success = 0;
496 errors = 0;
498 for (i = 0; i < ci->children_num; i++)
499 {
500 oconfig_item_t *child = ci->children + i;
502 if (strcasecmp ("Page", child->key) == 0)
503 {
504 status = cc_config_add_page (child);
505 if (status == 0)
506 success++;
507 else
508 errors++;
509 }
510 else
511 {
512 WARNING ("curl plugin: Option `%s' not allowed here.", child->key);
513 errors++;
514 }
515 }
517 if ((success == 0) && (errors > 0))
518 {
519 ERROR ("curl plugin: All statements failed.");
520 return (-1);
521 }
523 return (0);
524 } /* }}} int cc_config */
526 static int cc_init (void) /* {{{ */
527 {
528 if (pages_g == NULL)
529 {
530 INFO ("curl plugin: No pages have been defined.");
531 return (-1);
532 }
533 return (0);
534 } /* }}} int cc_init */
536 static void cc_submit (const web_page_t *wp, const web_match_t *wm, /* {{{ */
537 const cu_match_value_t *mv)
538 {
539 value_t values[1];
540 value_list_t vl = VALUE_LIST_INIT;
542 values[0] = mv->value;
544 vl.values = values;
545 vl.values_len = 1;
546 vl.time = time (NULL);
547 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
548 sstrncpy (vl.plugin, "curl", sizeof (vl.plugin));
549 sstrncpy (vl.plugin_instance, wp->instance, sizeof (vl.plugin_instance));
550 sstrncpy (vl.type, wm->type, sizeof (vl.type));
551 sstrncpy (vl.type_instance, wm->instance, sizeof (vl.type_instance));
553 plugin_dispatch_values (&vl);
554 } /* }}} void cc_submit */
556 static int cc_read_page (web_page_t *wp) /* {{{ */
557 {
558 web_match_t *wm;
559 int status;
561 wp->buffer_fill = 0;
562 status = curl_easy_perform (wp->curl);
563 if (status != 0)
564 {
565 ERROR ("curl plugin: curl_easy_perform failed with staus %i: %s",
566 status, wp->curl_errbuf);
567 return (-1);
568 }
570 for (wm = wp->matches; wm != NULL; wm = wm->next)
571 {
572 cu_match_value_t *mv;
574 status = match_apply (wm->match, wp->buffer);
575 if (status != 0)
576 {
577 WARNING ("curl plugin: match_apply failed.");
578 continue;
579 }
581 mv = match_get_user_data (wm->match);
582 if (mv == NULL)
583 {
584 WARNING ("curl plugin: match_get_user_data returned NULL.");
585 continue;
586 }
588 cc_submit (wp, wm, mv);
589 } /* for (wm = wp->matches; wm != NULL; wm = wm->next) */
591 return (0);
592 } /* }}} int cc_read_page */
594 static int cc_read (void) /* {{{ */
595 {
596 web_page_t *wp;
598 for (wp = pages_g; wp != NULL; wp = wp->next)
599 cc_read_page (wp);
601 return (0);
602 } /* }}} int cc_read */
604 static int cc_shutdown (void) /* {{{ */
605 {
606 cc_web_page_free (pages_g);
607 pages_g = NULL;
609 return (0);
610 } /* }}} int cc_shutdown */
612 void module_register (void)
613 {
614 plugin_register_complex_config ("curl", cc_config);
615 plugin_register_init ("curl", cc_init);
616 plugin_register_read ("curl", cc_read);
617 plugin_register_shutdown ("curl", cc_shutdown);
618 } /* void module_register */
620 /* vim: set sw=2 sts=2 et fdm=marker : */