1 /**
2 * collectd - src/curl_xml.c
3 * Copyright (C) 2009,2010 Amit Gupta
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 * Amit Gupta <amit.gupta221 at gmail.com>
20 **/
22 #include "collectd.h"
23 #include "common.h"
24 #include "plugin.h"
25 #include "configfile.h"
26 #include "utils_llist.h"
28 #include <libxml/parser.h>
29 #include <libxml/tree.h>
30 #include <libxml/xpath.h>
31 #include <libxml/xpathInternals.h>
33 #include <curl/curl.h>
35 #define CX_DEFAULT_HOST "localhost"
37 /*
38 * Private data structures
39 */
40 struct cx_values_s /* {{{ */
41 {
42 char path[DATA_MAX_NAME_LEN];
43 size_t path_len;
44 };
45 typedef struct cx_values_s cx_values_t;
46 /* }}} */
48 struct cx_xpath_s /* {{{ */
49 {
50 char *path;
51 char *type;
52 cx_values_t *values;
53 int values_len;
54 char *instance_prefix;
55 char *instance;
56 int is_table;
57 unsigned long magic;
58 };
59 typedef struct cx_xpath_s cx_xpath_t;
60 /* }}} */
62 struct cx_namespace_s /* {{{ */
63 {
64 char *prefix;
65 char *url;
66 };
67 typedef struct cx_namespace_s cx_namespace_t;
68 /* }}} */
70 struct cx_s /* {{{ */
71 {
72 char *instance;
73 char *host;
75 char *url;
76 char *user;
77 char *pass;
78 char *credentials;
79 _Bool digest;
80 _Bool verify_peer;
81 _Bool verify_host;
82 char *cacert;
83 char *post_body;
84 int timeout;
85 struct curl_slist *headers;
87 cx_namespace_t *namespaces;
88 size_t namespaces_num;
90 CURL *curl;
91 char curl_errbuf[CURL_ERROR_SIZE];
92 char *buffer;
93 size_t buffer_size;
94 size_t buffer_fill;
96 llist_t *list; /* list of xpath blocks */
97 };
98 typedef struct cx_s cx_t; /* }}} */
100 /*
101 * Private functions
102 */
103 static size_t cx_curl_callback (void *buf, /* {{{ */
104 size_t size, size_t nmemb, void *user_data)
105 {
106 size_t len = size * nmemb;
107 cx_t *db;
109 db = user_data;
110 if (db == NULL)
111 {
112 ERROR ("curl_xml plugin: cx_curl_callback: "
113 "user_data pointer is NULL.");
114 return (0);
115 }
117 if (len <= 0)
118 return (len);
120 if ((db->buffer_fill + len) >= db->buffer_size)
121 {
122 char *temp;
124 temp = (char *) realloc (db->buffer,
125 db->buffer_fill + len + 1);
126 if (temp == NULL)
127 {
128 ERROR ("curl_xml plugin: realloc failed.");
129 return (0);
130 }
131 db->buffer = temp;
132 db->buffer_size = db->buffer_fill + len + 1;
133 }
135 memcpy (db->buffer + db->buffer_fill, (char *) buf, len);
136 db->buffer_fill += len;
137 db->buffer[db->buffer_fill] = 0;
139 return (len);
140 } /* }}} size_t cx_curl_callback */
142 static void cx_xpath_free (cx_xpath_t *xpath) /* {{{ */
143 {
144 if (xpath == NULL)
145 return;
147 sfree (xpath->path);
148 sfree (xpath->type);
149 sfree (xpath->instance_prefix);
150 sfree (xpath->instance);
151 sfree (xpath->values);
152 sfree (xpath);
153 } /* }}} void cx_xpath_free */
155 static void cx_list_free (llist_t *list) /* {{{ */
156 {
157 llentry_t *le;
159 le = llist_head (list);
160 while (le != NULL)
161 {
162 llentry_t *le_next;
164 le_next = le->next;
166 sfree (le->key);
167 cx_xpath_free (le->value);
169 le = le_next;
170 }
172 llist_destroy (list);
173 list = NULL;
174 } /* }}} void cx_list_free */
176 static void cx_free (void *arg) /* {{{ */
177 {
178 cx_t *db;
179 size_t i;
181 DEBUG ("curl_xml plugin: cx_free (arg = %p);", arg);
183 db = (cx_t *) arg;
185 if (db == NULL)
186 return;
188 if (db->curl != NULL)
189 curl_easy_cleanup (db->curl);
190 db->curl = NULL;
192 if (db->list != NULL)
193 cx_list_free (db->list);
195 sfree (db->buffer);
196 sfree (db->instance);
197 sfree (db->host);
199 sfree (db->url);
200 sfree (db->user);
201 sfree (db->pass);
202 sfree (db->credentials);
203 sfree (db->cacert);
204 sfree (db->post_body);
205 curl_slist_free_all (db->headers);
207 for (i = 0; i < db->namespaces_num; i++)
208 {
209 sfree (db->namespaces[i].prefix);
210 sfree (db->namespaces[i].url);
211 }
212 sfree (db->namespaces);
214 sfree (db);
215 } /* }}} void cx_free */
217 static int cx_config_append_string (const char *name, struct curl_slist **dest, /* {{{ */
218 oconfig_item_t *ci)
219 {
220 struct curl_slist *temp = NULL;
221 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
222 {
223 WARNING ("curl_xml plugin: `%s' needs exactly one string argument.", name);
224 return (-1);
225 }
227 temp = curl_slist_append(*dest, ci->values[0].value.string);
228 if (temp == NULL)
229 return (-1);
231 *dest = temp;
233 return (0);
234 } /* }}} int cx_config_append_string */
236 static int cx_check_type (const data_set_t *ds, cx_xpath_t *xpath) /* {{{ */
237 {
238 if (!ds)
239 {
240 WARNING ("curl_xml plugin: DataSet `%s' not defined.", xpath->type);
241 return (-1);
242 }
244 if (ds->ds_num != xpath->values_len)
245 {
246 WARNING ("curl_xml plugin: DataSet `%s' requires %i values, but config talks about %i",
247 xpath->type, ds->ds_num, xpath->values_len);
248 return (-1);
249 }
251 return (0);
252 } /* }}} cx_check_type */
254 static xmlXPathObjectPtr cx_evaluate_xpath (xmlXPathContextPtr xpath_ctx, /* {{{ */
255 xmlChar *expr)
256 {
257 xmlXPathObjectPtr xpath_obj;
259 /* XXX: When to free this? */
260 xpath_obj = xmlXPathEvalExpression(BAD_CAST expr, xpath_ctx);
261 if (xpath_obj == NULL)
262 {
263 WARNING ("curl_xml plugin: "
264 "Error unable to evaluate xpath expression \"%s\". Skipping...", expr);
265 return NULL;
266 }
268 return xpath_obj;
269 } /* }}} cx_evaluate_xpath */
271 static int cx_if_not_text_node (xmlNodePtr node) /* {{{ */
272 {
273 if (node->type == XML_TEXT_NODE || node->type == XML_ATTRIBUTE_NODE ||
274 node->type == XML_ELEMENT_NODE)
275 return (0);
277 WARNING ("curl_xml plugin: "
278 "Node \"%s\" doesn't seem to be a text node. Skipping...", node->name);
279 return -1;
280 } /* }}} cx_if_not_text_node */
282 static int cx_handle_single_value_xpath (xmlXPathContextPtr xpath_ctx, /* {{{ */
283 cx_xpath_t *xpath,
284 const data_set_t *ds, value_list_t *vl, int index)
285 {
286 xmlXPathObjectPtr values_node_obj;
287 xmlNodeSetPtr values_node;
288 int tmp_size;
289 char *node_value;
291 values_node_obj = cx_evaluate_xpath (xpath_ctx, BAD_CAST xpath->values[index].path);
292 if (values_node_obj == NULL)
293 return (-1); /* Error already logged. */
295 values_node = values_node_obj->nodesetval;
296 tmp_size = (values_node) ? values_node->nodeNr : 0;
298 if (tmp_size == 0)
299 {
300 WARNING ("curl_xml plugin: "
301 "relative xpath expression \"%s\" doesn't match any of the nodes. "
302 "Skipping...", xpath->values[index].path);
303 xmlXPathFreeObject (values_node_obj);
304 return (-1);
305 }
307 if (tmp_size > 1)
308 {
309 WARNING ("curl_xml plugin: "
310 "relative xpath expression \"%s\" is expected to return "
311 "only one node. Skipping...", xpath->values[index].path);
312 xmlXPathFreeObject (values_node_obj);
313 return (-1);
314 }
316 /* ignoring the element if other than textnode/attribute*/
317 if (cx_if_not_text_node(values_node->nodeTab[0]))
318 {
319 WARNING ("curl_xml plugin: "
320 "relative xpath expression \"%s\" is expected to return "
321 "only text/attribute node which is not the case. Skipping...",
322 xpath->values[index].path);
323 xmlXPathFreeObject (values_node_obj);
324 return (-1);
325 }
327 node_value = (char *) xmlNodeGetContent(values_node->nodeTab[0]);
328 switch (ds->ds[index].type)
329 {
330 case DS_TYPE_COUNTER:
331 vl->values[index].counter = (counter_t) strtoull (node_value,
332 /* endptr = */ NULL, /* base = */ 0);
333 break;
334 case DS_TYPE_DERIVE:
335 vl->values[index].derive = (derive_t) strtoll (node_value,
336 /* endptr = */ NULL, /* base = */ 0);
337 break;
338 case DS_TYPE_ABSOLUTE:
339 vl->values[index].absolute = (absolute_t) strtoull (node_value,
340 /* endptr = */ NULL, /* base = */ 0);
341 break;
342 case DS_TYPE_GAUGE:
343 vl->values[index].gauge = (gauge_t) strtod (node_value,
344 /* endptr = */ NULL);
345 }
347 /* free up object */
348 xmlXPathFreeObject (values_node_obj);
349 sfree (node_value);
351 /* We have reached here which means that
352 * we have got something to work */
353 return (0);
354 } /* }}} int cx_handle_single_value_xpath */
356 static int cx_handle_all_value_xpaths (xmlXPathContextPtr xpath_ctx, /* {{{ */
357 cx_xpath_t *xpath,
358 const data_set_t *ds, value_list_t *vl)
359 {
360 value_t values[xpath->values_len];
361 int status;
362 int i;
364 assert (xpath->values_len > 0);
365 assert (xpath->values_len == vl->values_len);
366 assert (xpath->values_len == ds->ds_num);
367 vl->values = values;
369 for (i = 0; i < xpath->values_len; i++)
370 {
371 status = cx_handle_single_value_xpath (xpath_ctx, xpath, ds, vl, i);
372 if (status != 0)
373 return (-1); /* An error has been printed. */
374 } /* for (i = 0; i < xpath->values_len; i++) */
376 plugin_dispatch_values (vl);
377 vl->values = NULL;
379 return (0);
380 } /* }}} int cx_handle_all_value_xpaths */
382 static int cx_handle_instance_xpath (xmlXPathContextPtr xpath_ctx, /* {{{ */
383 cx_xpath_t *xpath, value_list_t *vl,
384 _Bool is_table)
385 {
386 xmlXPathObjectPtr instance_node_obj = NULL;
387 xmlNodeSetPtr instance_node = NULL;
389 memset (vl->type_instance, 0, sizeof (vl->type_instance));
391 /* If the base xpath returns more than one block, the result is assumed to be
392 * a table. The `Instance' option is not optional in this case. Check for the
393 * condition and inform the user. */
394 if (is_table && (xpath->instance == NULL))
395 {
396 WARNING ("curl_xml plugin: "
397 "Base-XPath %s is a table (more than one result was returned), "
398 "but no instance-XPath has been defined.",
399 xpath->path);
400 return (-1);
401 }
403 /* instance has to be an xpath expression */
404 if (xpath->instance != NULL)
405 {
406 int tmp_size;
408 instance_node_obj = cx_evaluate_xpath (xpath_ctx, BAD_CAST xpath->instance);
409 if (instance_node_obj == NULL)
410 return (-1); /* error is logged already */
412 instance_node = instance_node_obj->nodesetval;
413 tmp_size = (instance_node) ? instance_node->nodeNr : 0;
415 if (tmp_size <= 0)
416 {
417 WARNING ("curl_xml plugin: "
418 "relative xpath expression for 'InstanceFrom' \"%s\" doesn't match "
419 "any of the nodes. Skipping the node.", xpath->instance);
420 xmlXPathFreeObject (instance_node_obj);
421 return (-1);
422 }
424 if (tmp_size > 1)
425 {
426 WARNING ("curl_xml plugin: "
427 "relative xpath expression for 'InstanceFrom' \"%s\" is expected "
428 "to return only one text node. Skipping the node.", xpath->instance);
429 xmlXPathFreeObject (instance_node_obj);
430 return (-1);
431 }
433 /* ignoring the element if other than textnode/attribute */
434 if (cx_if_not_text_node(instance_node->nodeTab[0]))
435 {
436 WARNING ("curl_xml plugin: "
437 "relative xpath expression \"%s\" is expected to return only text node "
438 "which is not the case. Skipping the node.", xpath->instance);
439 xmlXPathFreeObject (instance_node_obj);
440 return (-1);
441 }
442 } /* if (xpath->instance != NULL) */
444 if (xpath->instance_prefix != NULL)
445 {
446 if (instance_node != NULL)
447 {
448 char *node_value = (char *) xmlNodeGetContent(instance_node->nodeTab[0]);
449 ssnprintf (vl->type_instance, sizeof (vl->type_instance),"%s%s",
450 xpath->instance_prefix, node_value);
451 sfree (node_value);
452 }
453 else
454 sstrncpy (vl->type_instance, xpath->instance_prefix,
455 sizeof (vl->type_instance));
456 }
457 else
458 {
459 /* If instance_prefix and instance_node are NULL, then
460 * don't set the type_instance */
461 if (instance_node != NULL)
462 {
463 char *node_value = (char *) xmlNodeGetContent(instance_node->nodeTab[0]);
464 sstrncpy (vl->type_instance, node_value, sizeof (vl->type_instance));
465 sfree (node_value);
466 }
467 }
469 /* Free `instance_node_obj' this late, because `instance_node' points to
470 * somewhere inside this structure. */
471 xmlXPathFreeObject (instance_node_obj);
473 return (0);
474 } /* }}} int cx_handle_instance_xpath */
476 static int cx_handle_base_xpath (char const *plugin_instance, /* {{{ */
477 char const *host,
478 xmlXPathContextPtr xpath_ctx, const data_set_t *ds,
479 char *base_xpath, cx_xpath_t *xpath)
480 {
481 int total_nodes;
482 int i;
484 xmlXPathObjectPtr base_node_obj = NULL;
485 xmlNodeSetPtr base_nodes = NULL;
487 value_list_t vl = VALUE_LIST_INIT;
489 base_node_obj = cx_evaluate_xpath (xpath_ctx, BAD_CAST base_xpath);
490 if (base_node_obj == NULL)
491 return -1; /* error is logged already */
493 base_nodes = base_node_obj->nodesetval;
494 total_nodes = (base_nodes) ? base_nodes->nodeNr : 0;
496 if (total_nodes == 0)
497 {
498 ERROR ("curl_xml plugin: "
499 "xpath expression \"%s\" doesn't match any of the nodes. "
500 "Skipping the xpath block...", base_xpath);
501 xmlXPathFreeObject (base_node_obj);
502 return -1;
503 }
505 /* If base_xpath returned multiple results, then */
506 /* Instance in the xpath block is required */
507 if (total_nodes > 1 && xpath->instance == NULL)
508 {
509 ERROR ("curl_xml plugin: "
510 "InstanceFrom is must in xpath block since the base xpath expression \"%s\" "
511 "returned multiple results. Skipping the xpath block...", base_xpath);
512 return -1;
513 }
515 /* set the values for the value_list */
516 vl.values_len = ds->ds_num;
517 sstrncpy (vl.type, xpath->type, sizeof (vl.type));
518 sstrncpy (vl.plugin, "curl_xml", sizeof (vl.plugin));
519 sstrncpy (vl.host, (host != NULL) ? host : hostname_g, sizeof (vl.host));
520 if (plugin_instance != NULL)
521 sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
523 for (i = 0; i < total_nodes; i++)
524 {
525 int status;
527 xpath_ctx->node = base_nodes->nodeTab[i];
529 status = cx_handle_instance_xpath (xpath_ctx, xpath, &vl,
530 /* is_table = */ (total_nodes > 1));
531 if (status != 0)
532 continue; /* An error has already been reported. */
534 status = cx_handle_all_value_xpaths (xpath_ctx, xpath, ds, &vl);
535 if (status != 0)
536 continue; /* An error has been logged. */
537 } /* for (i = 0; i < total_nodes; i++) */
539 /* free up the allocated memory */
540 xmlXPathFreeObject (base_node_obj);
542 return (0);
543 } /* }}} cx_handle_base_xpath */
545 static int cx_handle_parsed_xml(xmlDocPtr doc, /* {{{ */
546 xmlXPathContextPtr xpath_ctx, cx_t *db)
547 {
548 llentry_t *le;
549 const data_set_t *ds;
550 cx_xpath_t *xpath;
551 int status=-1;
554 le = llist_head (db->list);
555 while (le != NULL)
556 {
557 /* get the ds */
558 xpath = (cx_xpath_t *) le->value;
559 ds = plugin_get_ds (xpath->type);
561 if ( (cx_check_type(ds, xpath) == 0) &&
562 (cx_handle_base_xpath(db->instance, db->host,
563 xpath_ctx, ds, le->key, xpath) == 0) )
564 status = 0; /* we got atleast one success */
566 le = le->next;
567 } /* while (le != NULL) */
569 return status;
570 } /* }}} cx_handle_parsed_xml */
572 static int cx_parse_stats_xml(xmlChar* xml, cx_t *db) /* {{{ */
573 {
574 int status;
575 xmlDocPtr doc;
576 xmlXPathContextPtr xpath_ctx;
577 size_t i;
579 /* Load the XML */
580 doc = xmlParseDoc(xml);
581 if (doc == NULL)
582 {
583 ERROR ("curl_xml plugin: Failed to parse the xml document - %s", xml);
584 return (-1);
585 }
587 xpath_ctx = xmlXPathNewContext(doc);
588 if(xpath_ctx == NULL)
589 {
590 ERROR ("curl_xml plugin: Failed to create the xml context");
591 xmlFreeDoc(doc);
592 return (-1);
593 }
595 for (i = 0; i < db->namespaces_num; i++)
596 {
597 cx_namespace_t const *ns = db->namespaces + i;
598 status = xmlXPathRegisterNs (xpath_ctx,
599 BAD_CAST ns->prefix, BAD_CAST ns->url);
600 if (status != 0)
601 {
602 ERROR ("curl_xml plugin: "
603 "unable to register NS with prefix=\"%s\" and href=\"%s\"\n",
604 ns->prefix, ns->url);
605 xmlXPathFreeContext(xpath_ctx);
606 xmlFreeDoc (doc);
607 return (status);
608 }
609 }
611 status = cx_handle_parsed_xml (doc, xpath_ctx, db);
612 /* Cleanup */
613 xmlXPathFreeContext(xpath_ctx);
614 xmlFreeDoc(doc);
615 return status;
616 } /* }}} cx_parse_stats_xml */
618 static int cx_curl_perform (cx_t *db, CURL *curl) /* {{{ */
619 {
620 int status;
621 long rc;
622 char *ptr;
623 char *url;
624 url = db->url;
626 db->buffer_fill = 0;
627 status = curl_easy_perform (curl);
628 if (status != CURLE_OK)
629 {
630 ERROR ("curl_xml plugin: curl_easy_perform failed with status %i: %s (%s)",
631 status, db->curl_errbuf, url);
632 return (-1);
633 }
635 curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &url);
636 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &rc);
638 /* The response code is zero if a non-HTTP transport was used. */
639 if ((rc != 0) && (rc != 200))
640 {
641 ERROR ("curl_xml plugin: curl_easy_perform failed with response code %ld (%s)",
642 rc, url);
643 return (-1);
644 }
646 ptr = db->buffer;
648 status = cx_parse_stats_xml(BAD_CAST ptr, db);
649 db->buffer_fill = 0;
651 return status;
652 } /* }}} int cx_curl_perform */
654 static int cx_read (user_data_t *ud) /* {{{ */
655 {
656 cx_t *db;
658 if ((ud == NULL) || (ud->data == NULL))
659 {
660 ERROR ("curl_xml plugin: cx_read: Invalid user data.");
661 return (-1);
662 }
664 db = (cx_t *) ud->data;
666 return cx_curl_perform (db, db->curl);
667 } /* }}} int cx_read */
669 /* Configuration handling functions {{{ */
671 static int cx_config_add_values (const char *name, cx_xpath_t *xpath, /* {{{ */
672 oconfig_item_t *ci)
673 {
674 int i;
676 if (ci->values_num < 1)
677 {
678 WARNING ("curl_xml plugin: `ValuesFrom' needs at least one argument.");
679 return (-1);
680 }
682 for (i = 0; i < ci->values_num; i++)
683 if (ci->values[i].type != OCONFIG_TYPE_STRING)
684 {
685 WARNING ("curl_xml plugin: `ValuesFrom' needs only string argument.");
686 return (-1);
687 }
689 sfree (xpath->values);
691 xpath->values_len = 0;
692 xpath->values = (cx_values_t *) malloc (sizeof (cx_values_t) * ci->values_num);
693 if (xpath->values == NULL)
694 return (-1);
695 xpath->values_len = ci->values_num;
697 /* populate cx_values_t structure */
698 for (i = 0; i < ci->values_num; i++)
699 {
700 xpath->values[i].path_len = sizeof (ci->values[i].value.string);
701 sstrncpy (xpath->values[i].path, ci->values[i].value.string, sizeof (xpath->values[i].path));
702 }
704 return (0);
705 } /* }}} cx_config_add_values */
707 static int cx_config_add_xpath (cx_t *db, oconfig_item_t *ci) /* {{{ */
708 {
709 cx_xpath_t *xpath;
710 char *name;
711 llentry_t *le;
712 int status;
713 int i;
715 xpath = malloc (sizeof (*xpath));
716 if (xpath == NULL)
717 {
718 ERROR ("curl_xml plugin: malloc failed.");
719 return (-1);
720 }
721 memset (xpath, 0, sizeof (*xpath));
723 status = cf_util_get_string (ci, &xpath->path);
724 if (status != 0)
725 {
726 cx_xpath_free (xpath);
727 return (status);
728 }
730 /* error out if xpath->path is an empty string */
731 if (strlen (xpath->path) == 0)
732 {
733 ERROR ("curl_xml plugin: invalid xpath. "
734 "xpath value can't be an empty string");
735 cx_xpath_free (xpath);
736 return (-1);
737 }
739 status = 0;
740 for (i = 0; i < ci->children_num; i++)
741 {
742 oconfig_item_t *child = ci->children + i;
744 if (strcasecmp ("Type", child->key) == 0)
745 status = cf_util_get_string (child, &xpath->type);
746 else if (strcasecmp ("InstancePrefix", child->key) == 0)
747 status = cf_util_get_string (child, &xpath->instance_prefix);
748 else if (strcasecmp ("InstanceFrom", child->key) == 0)
749 status = cf_util_get_string (child, &xpath->instance);
750 else if (strcasecmp ("ValuesFrom", child->key) == 0)
751 status = cx_config_add_values ("ValuesFrom", xpath, child);
752 else
753 {
754 WARNING ("curl_xml plugin: Option `%s' not allowed here.", child->key);
755 status = -1;
756 }
758 if (status != 0)
759 break;
760 } /* for (i = 0; i < ci->children_num; i++) */
762 if (status != 0)
763 {
764 cx_xpath_free (xpath);
765 return status;
766 }
768 if (xpath->type == NULL)
769 {
770 WARNING ("curl_xml plugin: `Type' missing in `xpath' block.");
771 cx_xpath_free (xpath);
772 return -1;
773 }
775 if (db->list == NULL)
776 {
777 db->list = llist_create();
778 if (db->list == NULL)
779 {
780 ERROR ("curl_xml plugin: list creation failed.");
781 cx_xpath_free (xpath);
782 return (-1);
783 }
784 }
786 name = strdup (xpath->path);
787 if (name == NULL)
788 {
789 ERROR ("curl_xml plugin: strdup failed.");
790 cx_xpath_free (xpath);
791 return (-1);
792 }
794 le = llentry_create (name, xpath);
795 if (le == NULL)
796 {
797 ERROR ("curl_xml plugin: llentry_create failed.");
798 cx_xpath_free (xpath);
799 sfree (name);
800 return (-1);
801 }
803 llist_append (db->list, le);
804 return (0);
805 } /* }}} int cx_config_add_xpath */
807 static int cx_config_add_namespace (cx_t *db, /* {{{ */
808 oconfig_item_t *ci)
809 {
810 cx_namespace_t *ns;
812 if ((ci->values_num != 2)
813 || (ci->values[0].type != OCONFIG_TYPE_STRING)
814 || (ci->values[1].type != OCONFIG_TYPE_STRING))
815 {
816 WARNING ("curl_xml plugin: The `Namespace' option "
817 "needs exactly two string arguments.");
818 return (EINVAL);
819 }
821 ns = realloc (db->namespaces, sizeof (*db->namespaces)
822 * (db->namespaces_num + 1));
823 if (ns == NULL)
824 {
825 ERROR ("curl_xml plugin: realloc failed.");
826 return (ENOMEM);
827 }
828 db->namespaces = ns;
829 ns = db->namespaces + db->namespaces_num;
830 memset (ns, 0, sizeof (*ns));
832 ns->prefix = strdup (ci->values[0].value.string);
833 ns->url = strdup (ci->values[1].value.string);
835 if ((ns->prefix == NULL) || (ns->url == NULL))
836 {
837 sfree (ns->prefix);
838 sfree (ns->url);
839 ERROR ("curl_xml plugin: strdup failed.");
840 return (ENOMEM);
841 }
843 db->namespaces_num++;
844 return (0);
845 } /* }}} int cx_config_add_namespace */
847 /* Initialize db->curl */
848 static int cx_init_curl (cx_t *db) /* {{{ */
849 {
850 db->curl = curl_easy_init ();
851 if (db->curl == NULL)
852 {
853 ERROR ("curl_xml plugin: curl_easy_init failed.");
854 return (-1);
855 }
857 curl_easy_setopt (db->curl, CURLOPT_NOSIGNAL, 1L);
858 curl_easy_setopt (db->curl, CURLOPT_WRITEFUNCTION, cx_curl_callback);
859 curl_easy_setopt (db->curl, CURLOPT_WRITEDATA, db);
860 curl_easy_setopt (db->curl, CURLOPT_USERAGENT, COLLECTD_USERAGENT);
861 curl_easy_setopt (db->curl, CURLOPT_ERRORBUFFER, db->curl_errbuf);
862 curl_easy_setopt (db->curl, CURLOPT_URL, db->url);
863 curl_easy_setopt (db->curl, CURLOPT_FOLLOWLOCATION, 1L);
864 curl_easy_setopt (db->curl, CURLOPT_MAXREDIRS, 50L);
866 if (db->user != NULL)
867 {
868 #ifdef HAVE_CURLOPT_USERNAME
869 curl_easy_setopt (db->curl, CURLOPT_USERNAME, db->user);
870 curl_easy_setopt (db->curl, CURLOPT_PASSWORD,
871 (db->pass == NULL) ? "" : db->pass);
872 #else
873 size_t credentials_size;
875 credentials_size = strlen (db->user) + 2;
876 if (db->pass != NULL)
877 credentials_size += strlen (db->pass);
879 db->credentials = (char *) malloc (credentials_size);
880 if (db->credentials == NULL)
881 {
882 ERROR ("curl_xml plugin: malloc failed.");
883 return (-1);
884 }
886 ssnprintf (db->credentials, credentials_size, "%s:%s",
887 db->user, (db->pass == NULL) ? "" : db->pass);
888 curl_easy_setopt (db->curl, CURLOPT_USERPWD, db->credentials);
889 #endif
891 if (db->digest)
892 curl_easy_setopt (db->curl, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
893 }
895 curl_easy_setopt (db->curl, CURLOPT_SSL_VERIFYPEER, db->verify_peer ? 1L : 0L);
896 curl_easy_setopt (db->curl, CURLOPT_SSL_VERIFYHOST,
897 db->verify_host ? 2L : 0L);
898 if (db->cacert != NULL)
899 curl_easy_setopt (db->curl, CURLOPT_CAINFO, db->cacert);
900 if (db->headers != NULL)
901 curl_easy_setopt (db->curl, CURLOPT_HTTPHEADER, db->headers);
902 if (db->post_body != NULL)
903 curl_easy_setopt (db->curl, CURLOPT_POSTFIELDS, db->post_body);
905 #ifdef HAVE_CURLOPT_TIMEOUT_MS
906 if (db->timeout >= 0)
907 curl_easy_setopt (db->curl, CURLOPT_TIMEOUT_MS, (long) db->timeout);
908 else
909 curl_easy_setopt (db->curl, CURLOPT_TIMEOUT_MS,
910 CDTIME_T_TO_MS(plugin_get_interval()));
911 #endif
913 return (0);
914 } /* }}} int cx_init_curl */
916 static int cx_config_add_url (oconfig_item_t *ci) /* {{{ */
917 {
918 cx_t *db;
919 int status = 0;
920 int i;
922 if ((ci->values_num != 1)
923 || (ci->values[0].type != OCONFIG_TYPE_STRING))
924 {
925 WARNING ("curl_xml plugin: The `URL' block "
926 "needs exactly one string argument.");
927 return (-1);
928 }
930 db = (cx_t *) malloc (sizeof (*db));
931 if (db == NULL)
932 {
933 ERROR ("curl_xml plugin: malloc failed.");
934 return (-1);
935 }
936 memset (db, 0, sizeof (*db));
938 db->timeout = -1;
940 if (strcasecmp ("URL", ci->key) == 0)
941 {
942 status = cf_util_get_string (ci, &db->url);
943 if (status != 0)
944 {
945 sfree (db);
946 return (status);
947 }
948 }
949 else
950 {
951 ERROR ("curl_xml plugin: cx_config: "
952 "Invalid key: %s", ci->key);
953 cx_free (db);
954 return (-1);
955 }
957 /* Fill the `cx_t' structure.. */
958 for (i = 0; i < ci->children_num; i++)
959 {
960 oconfig_item_t *child = ci->children + i;
962 if (strcasecmp ("Instance", child->key) == 0)
963 status = cf_util_get_string (child, &db->instance);
964 else if (strcasecmp ("Host", child->key) == 0)
965 status = cf_util_get_string (child, &db->host);
966 else if (strcasecmp ("User", child->key) == 0)
967 status = cf_util_get_string (child, &db->user);
968 else if (strcasecmp ("Password", child->key) == 0)
969 status = cf_util_get_string (child, &db->pass);
970 else if (strcasecmp ("Digest", child->key) == 0)
971 status = cf_util_get_boolean (child, &db->digest);
972 else if (strcasecmp ("VerifyPeer", child->key) == 0)
973 status = cf_util_get_boolean (child, &db->verify_peer);
974 else if (strcasecmp ("VerifyHost", child->key) == 0)
975 status = cf_util_get_boolean (child, &db->verify_host);
976 else if (strcasecmp ("CACert", child->key) == 0)
977 status = cf_util_get_string (child, &db->cacert);
978 else if (strcasecmp ("xpath", child->key) == 0)
979 status = cx_config_add_xpath (db, child);
980 else if (strcasecmp ("Header", child->key) == 0)
981 status = cx_config_append_string ("Header", &db->headers, child);
982 else if (strcasecmp ("Post", child->key) == 0)
983 status = cf_util_get_string (child, &db->post_body);
984 else if (strcasecmp ("Namespace", child->key) == 0)
985 status = cx_config_add_namespace (db, child);
986 else if (strcasecmp ("Timeout", child->key) == 0)
987 status = cf_util_get_int (child, &db->timeout);
988 else
989 {
990 WARNING ("curl_xml plugin: Option `%s' not allowed here.", child->key);
991 status = -1;
992 }
994 if (status != 0)
995 break;
996 }
998 if (status == 0)
999 {
1000 if (db->list == NULL)
1001 {
1002 WARNING ("curl_xml plugin: No (valid) `Key' block "
1003 "within `URL' block `%s'.", db->url);
1004 status = -1;
1005 }
1006 if (status == 0)
1007 status = cx_init_curl (db);
1008 }
1010 /* If all went well, register this database for reading */
1011 if (status == 0)
1012 {
1013 user_data_t ud;
1014 char *cb_name;
1016 if (db->instance == NULL)
1017 db->instance = strdup("default");
1019 DEBUG ("curl_xml plugin: Registering new read callback: %s",
1020 db->instance);
1022 memset (&ud, 0, sizeof (ud));
1023 ud.data = (void *) db;
1024 ud.free_func = cx_free;
1026 cb_name = ssnprintf_alloc ("curl_xml-%s-%s", db->instance, db->url);
1027 plugin_register_complex_read (/* group = */ "curl_xml", cb_name, cx_read,
1028 /* interval = */ NULL, &ud);
1029 sfree (cb_name);
1030 }
1031 else
1032 {
1033 cx_free (db);
1034 return (-1);
1035 }
1037 return (0);
1038 } /* }}} int cx_config_add_url */
1040 /* }}} End of configuration handling functions */
1042 static int cx_config (oconfig_item_t *ci) /* {{{ */
1043 {
1044 int success;
1045 int errors;
1046 int status;
1047 int i;
1049 success = 0;
1050 errors = 0;
1052 for (i = 0; i < ci->children_num; i++)
1053 {
1054 oconfig_item_t *child = ci->children + i;
1056 if (strcasecmp ("URL", child->key) == 0)
1057 {
1058 status = cx_config_add_url (child);
1059 if (status == 0)
1060 success++;
1061 else
1062 errors++;
1063 }
1064 else
1065 {
1066 WARNING ("curl_xml plugin: Option `%s' not allowed here.", child->key);
1067 errors++;
1068 }
1069 }
1071 if ((success == 0) && (errors > 0))
1072 {
1073 ERROR ("curl_xml plugin: All statements failed.");
1074 return (-1);
1075 }
1077 return (0);
1078 } /* }}} int cx_config */
1080 static int cx_init (void) /* {{{ */
1081 {
1082 /* Call this while collectd is still single-threaded to avoid
1083 * initialization issues in libgcrypt. */
1084 curl_global_init (CURL_GLOBAL_SSL);
1085 return (0);
1086 } /* }}} int cx_init */
1088 void module_register (void)
1089 {
1090 plugin_register_complex_config ("curl_xml", cx_config);
1091 plugin_register_init ("curl_xml", cx_init);
1092 } /* void module_register */
1094 /* vim: set sw=2 sts=2 et fdm=marker : */