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 struct curl_slist *headers;
86 cx_namespace_t *namespaces;
87 size_t namespaces_num;
89 CURL *curl;
90 char curl_errbuf[CURL_ERROR_SIZE];
91 char *buffer;
92 size_t buffer_size;
93 size_t buffer_fill;
95 llist_t *list; /* list of xpath blocks */
96 };
97 typedef struct cx_s cx_t; /* }}} */
99 /*
100 * Private functions
101 */
102 static size_t cx_curl_callback (void *buf, /* {{{ */
103 size_t size, size_t nmemb, void *user_data)
104 {
105 size_t len = size * nmemb;
106 cx_t *db;
108 db = user_data;
109 if (db == NULL)
110 {
111 ERROR ("curl_xml plugin: cx_curl_callback: "
112 "user_data pointer is NULL.");
113 return (0);
114 }
116 if (len <= 0)
117 return (len);
119 if ((db->buffer_fill + len) >= db->buffer_size)
120 {
121 char *temp;
123 temp = (char *) realloc (db->buffer,
124 db->buffer_fill + len + 1);
125 if (temp == NULL)
126 {
127 ERROR ("curl_xml plugin: realloc failed.");
128 return (0);
129 }
130 db->buffer = temp;
131 db->buffer_size = db->buffer_fill + len + 1;
132 }
134 memcpy (db->buffer + db->buffer_fill, (char *) buf, len);
135 db->buffer_fill += len;
136 db->buffer[db->buffer_fill] = 0;
138 return (len);
139 } /* }}} size_t cx_curl_callback */
141 static void cx_xpath_free (cx_xpath_t *xpath) /* {{{ */
142 {
143 if (xpath == NULL)
144 return;
146 sfree (xpath->path);
147 sfree (xpath->type);
148 sfree (xpath->instance_prefix);
149 sfree (xpath->instance);
150 sfree (xpath->values);
151 sfree (xpath);
152 } /* }}} void cx_xpath_free */
154 static void cx_list_free (llist_t *list) /* {{{ */
155 {
156 llentry_t *le;
158 le = llist_head (list);
159 while (le != NULL)
160 {
161 llentry_t *le_next;
163 le_next = le->next;
165 sfree (le->key);
166 cx_xpath_free (le->value);
168 le = le_next;
169 }
171 llist_destroy (list);
172 list = NULL;
173 } /* }}} void cx_list_free */
175 static void cx_free (void *arg) /* {{{ */
176 {
177 cx_t *db;
178 size_t i;
180 DEBUG ("curl_xml plugin: cx_free (arg = %p);", arg);
182 db = (cx_t *) arg;
184 if (db == NULL)
185 return;
187 if (db->curl != NULL)
188 curl_easy_cleanup (db->curl);
189 db->curl = NULL;
191 if (db->list != NULL)
192 cx_list_free (db->list);
194 sfree (db->buffer);
195 sfree (db->instance);
196 sfree (db->host);
198 sfree (db->url);
199 sfree (db->user);
200 sfree (db->pass);
201 sfree (db->credentials);
202 sfree (db->cacert);
203 sfree (db->post_body);
204 curl_slist_free_all (db->headers);
206 for (i = 0; i < db->namespaces_num; i++)
207 {
208 sfree (db->namespaces[i].prefix);
209 sfree (db->namespaces[i].url);
210 }
211 sfree (db->namespaces);
213 sfree (db);
214 } /* }}} void cx_free */
216 static int cx_config_append_string (const char *name, struct curl_slist **dest, /* {{{ */
217 oconfig_item_t *ci)
218 {
219 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
220 {
221 WARNING ("curl_xml plugin: `%s' needs exactly one string argument.", name);
222 return (-1);
223 }
225 *dest = curl_slist_append(*dest, ci->values[0].value.string);
226 if (*dest == NULL)
227 return (-1);
229 return (0);
230 } /* }}} int cx_config_append_string */
232 static int cx_check_type (const data_set_t *ds, cx_xpath_t *xpath) /* {{{ */
233 {
234 if (!ds)
235 {
236 WARNING ("curl_xml plugin: DataSet `%s' not defined.", xpath->type);
237 return (-1);
238 }
240 if (ds->ds_num != xpath->values_len)
241 {
242 WARNING ("curl_xml plugin: DataSet `%s' requires %i values, but config talks about %i",
243 xpath->type, ds->ds_num, xpath->values_len);
244 return (-1);
245 }
247 return (0);
248 } /* }}} cx_check_type */
250 static xmlXPathObjectPtr cx_evaluate_xpath (xmlXPathContextPtr xpath_ctx, /* {{{ */
251 xmlChar *expr)
252 {
253 xmlXPathObjectPtr xpath_obj;
255 /* XXX: When to free this? */
256 xpath_obj = xmlXPathEvalExpression(BAD_CAST expr, xpath_ctx);
257 if (xpath_obj == NULL)
258 {
259 WARNING ("curl_xml plugin: "
260 "Error unable to evaluate xpath expression \"%s\". Skipping...", expr);
261 return NULL;
262 }
264 return xpath_obj;
265 } /* }}} cx_evaluate_xpath */
267 static int cx_if_not_text_node (xmlNodePtr node) /* {{{ */
268 {
269 if (node->type == XML_TEXT_NODE || node->type == XML_ATTRIBUTE_NODE ||
270 node->type == XML_ELEMENT_NODE)
271 return (0);
273 WARNING ("curl_xml plugin: "
274 "Node \"%s\" doesn't seem to be a text node. Skipping...", node->name);
275 return -1;
276 } /* }}} cx_if_not_text_node */
278 static int cx_handle_single_value_xpath (xmlXPathContextPtr xpath_ctx, /* {{{ */
279 cx_xpath_t *xpath,
280 const data_set_t *ds, value_list_t *vl, int index)
281 {
282 xmlXPathObjectPtr values_node_obj;
283 xmlNodeSetPtr values_node;
284 int tmp_size;
285 char *node_value;
287 values_node_obj = cx_evaluate_xpath (xpath_ctx, BAD_CAST xpath->values[index].path);
288 if (values_node_obj == NULL)
289 return (-1); /* Error already logged. */
291 values_node = values_node_obj->nodesetval;
292 tmp_size = (values_node) ? values_node->nodeNr : 0;
294 if (tmp_size == 0)
295 {
296 WARNING ("curl_xml plugin: "
297 "relative xpath expression \"%s\" doesn't match any of the nodes. "
298 "Skipping...", xpath->values[index].path);
299 xmlXPathFreeObject (values_node_obj);
300 return (-1);
301 }
303 if (tmp_size > 1)
304 {
305 WARNING ("curl_xml plugin: "
306 "relative xpath expression \"%s\" is expected to return "
307 "only one node. Skipping...", xpath->values[index].path);
308 xmlXPathFreeObject (values_node_obj);
309 return (-1);
310 }
312 /* ignoring the element if other than textnode/attribute*/
313 if (cx_if_not_text_node(values_node->nodeTab[0]))
314 {
315 WARNING ("curl_xml plugin: "
316 "relative xpath expression \"%s\" is expected to return "
317 "only text/attribute node which is not the case. Skipping...",
318 xpath->values[index].path);
319 xmlXPathFreeObject (values_node_obj);
320 return (-1);
321 }
323 node_value = (char *) xmlNodeGetContent(values_node->nodeTab[0]);
324 switch (ds->ds[index].type)
325 {
326 case DS_TYPE_COUNTER:
327 vl->values[index].counter = (counter_t) strtoull (node_value,
328 /* endptr = */ NULL, /* base = */ 0);
329 break;
330 case DS_TYPE_DERIVE:
331 vl->values[index].derive = (derive_t) strtoll (node_value,
332 /* endptr = */ NULL, /* base = */ 0);
333 break;
334 case DS_TYPE_ABSOLUTE:
335 vl->values[index].absolute = (absolute_t) strtoull (node_value,
336 /* endptr = */ NULL, /* base = */ 0);
337 break;
338 case DS_TYPE_GAUGE:
339 vl->values[index].gauge = (gauge_t) strtod (node_value,
340 /* endptr = */ NULL);
341 }
343 /* free up object */
344 xmlXPathFreeObject (values_node_obj);
346 /* We have reached here which means that
347 * we have got something to work */
348 return (0);
349 } /* }}} int cx_handle_single_value_xpath */
351 static int cx_handle_all_value_xpaths (xmlXPathContextPtr xpath_ctx, /* {{{ */
352 cx_xpath_t *xpath,
353 const data_set_t *ds, value_list_t *vl)
354 {
355 value_t values[xpath->values_len];
356 int status;
357 int i;
359 assert (xpath->values_len > 0);
360 assert (xpath->values_len == vl->values_len);
361 assert (xpath->values_len == ds->ds_num);
362 vl->values = values;
364 for (i = 0; i < xpath->values_len; i++)
365 {
366 status = cx_handle_single_value_xpath (xpath_ctx, xpath, ds, vl, i);
367 if (status != 0)
368 return (-1); /* An error has been printed. */
369 } /* for (i = 0; i < xpath->values_len; i++) */
371 plugin_dispatch_values (vl);
372 vl->values = NULL;
374 return (0);
375 } /* }}} int cx_handle_all_value_xpaths */
377 static int cx_handle_instance_xpath (xmlXPathContextPtr xpath_ctx, /* {{{ */
378 cx_xpath_t *xpath, value_list_t *vl,
379 _Bool is_table)
380 {
381 xmlXPathObjectPtr instance_node_obj = NULL;
382 xmlNodeSetPtr instance_node = NULL;
384 memset (vl->type_instance, 0, sizeof (vl->type_instance));
386 /* If the base xpath returns more than one block, the result is assumed to be
387 * a table. The `Instance' option is not optional in this case. Check for the
388 * condition and inform the user. */
389 if (is_table && (vl->type_instance == NULL))
390 {
391 WARNING ("curl_xml plugin: "
392 "Base-XPath %s is a table (more than one result was returned), "
393 "but no instance-XPath has been defined.",
394 xpath->path);
395 return (-1);
396 }
398 /* instance has to be an xpath expression */
399 if (xpath->instance != NULL)
400 {
401 int tmp_size;
403 instance_node_obj = cx_evaluate_xpath (xpath_ctx, BAD_CAST xpath->instance);
404 if (instance_node_obj == NULL)
405 return (-1); /* error is logged already */
407 instance_node = instance_node_obj->nodesetval;
408 tmp_size = (instance_node) ? instance_node->nodeNr : 0;
410 if (tmp_size <= 0)
411 {
412 WARNING ("curl_xml plugin: "
413 "relative xpath expression for 'InstanceFrom' \"%s\" doesn't match "
414 "any of the nodes. Skipping the node.", xpath->instance);
415 xmlXPathFreeObject (instance_node_obj);
416 return (-1);
417 }
419 if (tmp_size > 1)
420 {
421 WARNING ("curl_xml plugin: "
422 "relative xpath expression for 'InstanceFrom' \"%s\" is expected "
423 "to return only one text node. Skipping the node.", xpath->instance);
424 xmlXPathFreeObject (instance_node_obj);
425 return (-1);
426 }
428 /* ignoring the element if other than textnode/attribute */
429 if (cx_if_not_text_node(instance_node->nodeTab[0]))
430 {
431 WARNING ("curl_xml plugin: "
432 "relative xpath expression \"%s\" is expected to return only text node "
433 "which is not the case. Skipping the node.", xpath->instance);
434 xmlXPathFreeObject (instance_node_obj);
435 return (-1);
436 }
437 } /* if (xpath->instance != NULL) */
439 if (xpath->instance_prefix != NULL)
440 {
441 if (instance_node != NULL)
442 ssnprintf (vl->type_instance, sizeof (vl->type_instance),"%s%s",
443 xpath->instance_prefix, (char *) xmlNodeGetContent(instance_node->nodeTab[0]));
444 else
445 sstrncpy (vl->type_instance, xpath->instance_prefix,
446 sizeof (vl->type_instance));
447 }
448 else
449 {
450 /* If instance_prefix and instance_node are NULL, then
451 * don't set the type_instance */
452 if (instance_node != NULL)
453 sstrncpy (vl->type_instance, (char *) xmlNodeGetContent(instance_node->nodeTab[0]),
454 sizeof (vl->type_instance));
455 }
457 /* Free `instance_node_obj' this late, because `instance_node' points to
458 * somewhere inside this structure. */
459 xmlXPathFreeObject (instance_node_obj);
461 return (0);
462 } /* }}} int cx_handle_instance_xpath */
464 static int cx_handle_base_xpath (char const *plugin_instance, /* {{{ */
465 char const *host,
466 xmlXPathContextPtr xpath_ctx, const data_set_t *ds,
467 char *base_xpath, cx_xpath_t *xpath)
468 {
469 int total_nodes;
470 int i;
472 xmlXPathObjectPtr base_node_obj = NULL;
473 xmlNodeSetPtr base_nodes = NULL;
475 value_list_t vl = VALUE_LIST_INIT;
477 base_node_obj = cx_evaluate_xpath (xpath_ctx, BAD_CAST base_xpath);
478 if (base_node_obj == NULL)
479 return -1; /* error is logged already */
481 base_nodes = base_node_obj->nodesetval;
482 total_nodes = (base_nodes) ? base_nodes->nodeNr : 0;
484 if (total_nodes == 0)
485 {
486 ERROR ("curl_xml plugin: "
487 "xpath expression \"%s\" doesn't match any of the nodes. "
488 "Skipping the xpath block...", base_xpath);
489 xmlXPathFreeObject (base_node_obj);
490 return -1;
491 }
493 /* If base_xpath returned multiple results, then */
494 /* Instance in the xpath block is required */
495 if (total_nodes > 1 && xpath->instance == NULL)
496 {
497 ERROR ("curl_xml plugin: "
498 "InstanceFrom is must in xpath block since the base xpath expression \"%s\" "
499 "returned multiple results. Skipping the xpath block...", base_xpath);
500 return -1;
501 }
503 /* set the values for the value_list */
504 vl.values_len = ds->ds_num;
505 sstrncpy (vl.type, xpath->type, sizeof (vl.type));
506 sstrncpy (vl.plugin, "curl_xml", sizeof (vl.plugin));
507 sstrncpy (vl.host, (host != NULL) ? host : hostname_g, sizeof (vl.host));
508 if (plugin_instance != NULL)
509 sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
511 for (i = 0; i < total_nodes; i++)
512 {
513 int status;
515 xpath_ctx->node = base_nodes->nodeTab[i];
517 status = cx_handle_instance_xpath (xpath_ctx, xpath, &vl,
518 /* is_table = */ (total_nodes > 1));
519 if (status != 0)
520 continue; /* An error has already been reported. */
522 status = cx_handle_all_value_xpaths (xpath_ctx, xpath, ds, &vl);
523 if (status != 0)
524 continue; /* An error has been logged. */
525 } /* for (i = 0; i < total_nodes; i++) */
527 /* free up the allocated memory */
528 xmlXPathFreeObject (base_node_obj);
530 return (0);
531 } /* }}} cx_handle_base_xpath */
533 static int cx_handle_parsed_xml(xmlDocPtr doc, /* {{{ */
534 xmlXPathContextPtr xpath_ctx, cx_t *db)
535 {
536 llentry_t *le;
537 const data_set_t *ds;
538 cx_xpath_t *xpath;
539 int status=-1;
542 le = llist_head (db->list);
543 while (le != NULL)
544 {
545 /* get the ds */
546 xpath = (cx_xpath_t *) le->value;
547 ds = plugin_get_ds (xpath->type);
549 if ( (cx_check_type(ds, xpath) == 0) &&
550 (cx_handle_base_xpath(db->instance, db->host,
551 xpath_ctx, ds, le->key, xpath) == 0) )
552 status = 0; /* we got atleast one success */
554 le = le->next;
555 } /* while (le != NULL) */
557 return status;
558 } /* }}} cx_handle_parsed_xml */
560 static int cx_parse_stats_xml(xmlChar* xml, cx_t *db) /* {{{ */
561 {
562 int status;
563 xmlDocPtr doc;
564 xmlXPathContextPtr xpath_ctx;
565 size_t i;
567 /* Load the XML */
568 doc = xmlParseDoc(xml);
569 if (doc == NULL)
570 {
571 ERROR ("curl_xml plugin: Failed to parse the xml document - %s", xml);
572 return (-1);
573 }
575 xpath_ctx = xmlXPathNewContext(doc);
576 if(xpath_ctx == NULL)
577 {
578 ERROR ("curl_xml plugin: Failed to create the xml context");
579 xmlFreeDoc(doc);
580 return (-1);
581 }
583 for (i = 0; i < db->namespaces_num; i++)
584 {
585 cx_namespace_t const *ns = db->namespaces + i;
586 status = xmlXPathRegisterNs (xpath_ctx,
587 BAD_CAST ns->prefix, BAD_CAST ns->url);
588 if (status != 0)
589 {
590 ERROR ("curl_xml plugin: "
591 "unable to register NS with prefix=\"%s\" and href=\"%s\"\n",
592 ns->prefix, ns->url);
593 xmlXPathFreeContext(xpath_ctx);
594 xmlFreeDoc (doc);
595 return (status);
596 }
597 }
599 status = cx_handle_parsed_xml (doc, xpath_ctx, db);
600 /* Cleanup */
601 xmlXPathFreeContext(xpath_ctx);
602 xmlFreeDoc(doc);
603 return status;
604 } /* }}} cx_parse_stats_xml */
606 static int cx_curl_perform (cx_t *db, CURL *curl) /* {{{ */
607 {
608 int status;
609 long rc;
610 char *ptr;
611 char *url;
612 url = db->url;
614 db->buffer_fill = 0;
615 status = curl_easy_perform (curl);
616 if (status != CURLE_OK)
617 {
618 ERROR ("curl_xml plugin: curl_easy_perform failed with status %i: %s (%s)",
619 status, db->curl_errbuf, url);
620 return (-1);
621 }
623 curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &url);
624 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &rc);
626 /* The response code is zero if a non-HTTP transport was used. */
627 if ((rc != 0) && (rc != 200))
628 {
629 ERROR ("curl_xml plugin: curl_easy_perform failed with response code %ld (%s)",
630 rc, url);
631 return (-1);
632 }
634 ptr = db->buffer;
636 status = cx_parse_stats_xml(BAD_CAST ptr, db);
637 db->buffer_fill = 0;
639 return status;
640 } /* }}} int cx_curl_perform */
642 static int cx_read (user_data_t *ud) /* {{{ */
643 {
644 cx_t *db;
646 if ((ud == NULL) || (ud->data == NULL))
647 {
648 ERROR ("curl_xml plugin: cx_read: Invalid user data.");
649 return (-1);
650 }
652 db = (cx_t *) ud->data;
654 return cx_curl_perform (db, db->curl);
655 } /* }}} int cx_read */
657 /* Configuration handling functions {{{ */
659 static int cx_config_add_values (const char *name, cx_xpath_t *xpath, /* {{{ */
660 oconfig_item_t *ci)
661 {
662 int i;
664 if (ci->values_num < 1)
665 {
666 WARNING ("curl_xml plugin: `ValuesFrom' needs at least one argument.");
667 return (-1);
668 }
670 for (i = 0; i < ci->values_num; i++)
671 if (ci->values[i].type != OCONFIG_TYPE_STRING)
672 {
673 WARNING ("curl_xml plugin: `ValuesFrom' needs only string argument.");
674 return (-1);
675 }
677 sfree (xpath->values);
679 xpath->values_len = 0;
680 xpath->values = (cx_values_t *) malloc (sizeof (cx_values_t) * ci->values_num);
681 if (xpath->values == NULL)
682 return (-1);
683 xpath->values_len = ci->values_num;
685 /* populate cx_values_t structure */
686 for (i = 0; i < ci->values_num; i++)
687 {
688 xpath->values[i].path_len = sizeof (ci->values[i].value.string);
689 sstrncpy (xpath->values[i].path, ci->values[i].value.string, sizeof (xpath->values[i].path));
690 }
692 return (0);
693 } /* }}} cx_config_add_values */
695 static int cx_config_add_xpath (cx_t *db, /* {{{ */
696 oconfig_item_t *ci)
697 {
698 cx_xpath_t *xpath;
699 int status;
700 int i;
702 xpath = (cx_xpath_t *) malloc (sizeof (*xpath));
703 if (xpath == NULL)
704 {
705 ERROR ("curl_xml plugin: malloc failed.");
706 return (-1);
707 }
708 memset (xpath, 0, sizeof (*xpath));
710 status = cf_util_get_string (ci, &xpath->path);
711 if (status != 0)
712 {
713 sfree (xpath);
714 return (status);
715 }
717 /* error out if xpath->path is an empty string */
718 if (*xpath->path == 0)
719 {
720 ERROR ("curl_xml plugin: invalid xpath. "
721 "xpath value can't be an empty string");
722 return (-1);
723 }
725 status = 0;
726 for (i = 0; i < ci->children_num; i++)
727 {
728 oconfig_item_t *child = ci->children + i;
730 if (strcasecmp ("Type", child->key) == 0)
731 status = cf_util_get_string (child, &xpath->type);
732 else if (strcasecmp ("InstancePrefix", child->key) == 0)
733 status = cf_util_get_string (child, &xpath->instance_prefix);
734 else if (strcasecmp ("InstanceFrom", child->key) == 0)
735 status = cf_util_get_string (child, &xpath->instance);
736 else if (strcasecmp ("ValuesFrom", child->key) == 0)
737 status = cx_config_add_values ("ValuesFrom", xpath, child);
738 else
739 {
740 WARNING ("curl_xml plugin: Option `%s' not allowed here.", child->key);
741 status = -1;
742 }
744 if (status != 0)
745 break;
746 } /* for (i = 0; i < ci->children_num; i++) */
748 if (status == 0 && xpath->type == NULL)
749 {
750 WARNING ("curl_xml plugin: `Type' missing in `xpath' block.");
751 status = -1;
752 }
754 if (status == 0)
755 {
756 char *name;
757 llentry_t *le;
759 if (db->list == NULL)
760 {
761 db->list = llist_create();
762 if (db->list == NULL)
763 {
764 ERROR ("curl_xml plugin: list creation failed.");
765 return (-1);
766 }
767 }
769 name = strdup(xpath->path);
770 if (name == NULL)
771 {
772 ERROR ("curl_xml plugin: strdup failed.");
773 return (-1);
774 }
776 le = llentry_create (name, xpath);
777 if (le == NULL)
778 {
779 ERROR ("curl_xml plugin: llentry_create failed.");
780 return (-1);
781 }
783 llist_append (db->list, le);
784 }
786 return (status);
787 } /* }}} int cx_config_add_xpath */
789 static int cx_config_add_namespace (cx_t *db, /* {{{ */
790 oconfig_item_t *ci)
791 {
792 cx_namespace_t *ns;
794 if ((ci->values_num != 2)
795 || (ci->values[0].type != OCONFIG_TYPE_STRING)
796 || (ci->values[1].type != OCONFIG_TYPE_STRING))
797 {
798 WARNING ("curl_xml plugin: The `Namespace' option "
799 "needs exactly two string arguments.");
800 return (EINVAL);
801 }
803 ns = realloc (db->namespaces, sizeof (*db->namespaces)
804 * (db->namespaces_num + 1));
805 if (ns == NULL)
806 {
807 ERROR ("curl_xml plugin: realloc failed.");
808 return (ENOMEM);
809 }
810 db->namespaces = ns;
811 ns = db->namespaces + db->namespaces_num;
812 memset (ns, 0, sizeof (*ns));
814 ns->prefix = strdup (ci->values[0].value.string);
815 ns->url = strdup (ci->values[1].value.string);
817 if ((ns->prefix == NULL) || (ns->url == NULL))
818 {
819 sfree (ns->prefix);
820 sfree (ns->url);
821 ERROR ("curl_xml plugin: strdup failed.");
822 return (ENOMEM);
823 }
825 db->namespaces_num++;
826 return (0);
827 } /* }}} int cx_config_add_namespace */
829 /* Initialize db->curl */
830 static int cx_init_curl (cx_t *db) /* {{{ */
831 {
832 db->curl = curl_easy_init ();
833 if (db->curl == NULL)
834 {
835 ERROR ("curl_xml plugin: curl_easy_init failed.");
836 return (-1);
837 }
839 curl_easy_setopt (db->curl, CURLOPT_NOSIGNAL, 1L);
840 curl_easy_setopt (db->curl, CURLOPT_WRITEFUNCTION, cx_curl_callback);
841 curl_easy_setopt (db->curl, CURLOPT_WRITEDATA, db);
842 curl_easy_setopt (db->curl, CURLOPT_USERAGENT, COLLECTD_USERAGENT);
843 curl_easy_setopt (db->curl, CURLOPT_ERRORBUFFER, db->curl_errbuf);
844 curl_easy_setopt (db->curl, CURLOPT_URL, db->url);
846 if (db->user != NULL)
847 {
848 size_t credentials_size;
850 credentials_size = strlen (db->user) + 2;
851 if (db->pass != NULL)
852 credentials_size += strlen (db->pass);
854 db->credentials = (char *) malloc (credentials_size);
855 if (db->credentials == NULL)
856 {
857 ERROR ("curl_xml plugin: malloc failed.");
858 return (-1);
859 }
861 ssnprintf (db->credentials, credentials_size, "%s:%s",
862 db->user, (db->pass == NULL) ? "" : db->pass);
863 curl_easy_setopt (db->curl, CURLOPT_USERPWD, db->credentials);
865 if (db->digest)
866 {
867 curl_easy_setopt (db->curl, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
868 curl_easy_setopt (db->curl, CURLOPT_USERNAME, db->user);
869 curl_easy_setopt (db->curl, CURLOPT_PASSWORD, db->pass);
870 }
871 }
873 curl_easy_setopt (db->curl, CURLOPT_SSL_VERIFYPEER, db->verify_peer ? 1L : 0L);
874 curl_easy_setopt (db->curl, CURLOPT_SSL_VERIFYHOST,
875 db->verify_host ? 2L : 0L);
876 if (db->cacert != NULL)
877 curl_easy_setopt (db->curl, CURLOPT_CAINFO, db->cacert);
878 if (db->headers != NULL)
879 curl_easy_setopt (db->curl, CURLOPT_HTTPHEADER, db->headers);
880 if (db->post_body != NULL)
881 curl_easy_setopt (db->curl, CURLOPT_POSTFIELDS, db->post_body);
883 return (0);
884 } /* }}} int cx_init_curl */
886 static int cx_config_add_url (oconfig_item_t *ci) /* {{{ */
887 {
888 cx_t *db;
889 int status = 0;
890 int i;
892 if ((ci->values_num != 1)
893 || (ci->values[0].type != OCONFIG_TYPE_STRING))
894 {
895 WARNING ("curl_xml plugin: The `URL' block "
896 "needs exactly one string argument.");
897 return (-1);
898 }
900 db = (cx_t *) malloc (sizeof (*db));
901 if (db == NULL)
902 {
903 ERROR ("curl_xml plugin: malloc failed.");
904 return (-1);
905 }
906 memset (db, 0, sizeof (*db));
908 if (strcasecmp ("URL", ci->key) == 0)
909 {
910 status = cf_util_get_string (ci, &db->url);
911 if (status != 0)
912 {
913 sfree (db);
914 return (status);
915 }
916 }
917 else
918 {
919 ERROR ("curl_xml plugin: cx_config: "
920 "Invalid key: %s", ci->key);
921 return (-1);
922 }
924 /* Fill the `cx_t' structure.. */
925 for (i = 0; i < ci->children_num; i++)
926 {
927 oconfig_item_t *child = ci->children + i;
929 if (strcasecmp ("Instance", child->key) == 0)
930 status = cf_util_get_string (child, &db->instance);
931 else if (strcasecmp ("Host", child->key) == 0)
932 status = cf_util_get_string (child, &db->host);
933 else if (strcasecmp ("User", child->key) == 0)
934 status = cf_util_get_string (child, &db->user);
935 else if (strcasecmp ("Password", child->key) == 0)
936 status = cf_util_get_string (child, &db->pass);
937 else if (strcasecmp ("Digest", child->key) == 0)
938 status = cf_util_get_boolean (child, &db->digest);
939 else if (strcasecmp ("VerifyPeer", child->key) == 0)
940 status = cf_util_get_boolean (child, &db->verify_peer);
941 else if (strcasecmp ("VerifyHost", child->key) == 0)
942 status = cf_util_get_boolean (child, &db->verify_host);
943 else if (strcasecmp ("CACert", child->key) == 0)
944 status = cf_util_get_string (child, &db->cacert);
945 else if (strcasecmp ("xpath", child->key) == 0)
946 status = cx_config_add_xpath (db, child);
947 else if (strcasecmp ("Header", child->key) == 0)
948 status = cx_config_append_string ("Header", &db->headers, child);
949 else if (strcasecmp ("Post", child->key) == 0)
950 status = cf_util_get_string (child, &db->post_body);
951 else if (strcasecmp ("Namespace", child->key) == 0)
952 status = cx_config_add_namespace (db, child);
953 else
954 {
955 WARNING ("curl_xml plugin: Option `%s' not allowed here.", child->key);
956 status = -1;
957 }
959 if (status != 0)
960 break;
961 }
963 if (status == 0)
964 {
965 if (db->list == NULL)
966 {
967 WARNING ("curl_xml plugin: No (valid) `Key' block "
968 "within `URL' block `%s'.", db->url);
969 status = -1;
970 }
971 if (status == 0)
972 status = cx_init_curl (db);
973 }
975 /* If all went well, register this database for reading */
976 if (status == 0)
977 {
978 user_data_t ud;
979 char *cb_name;
981 if (db->instance == NULL)
982 db->instance = strdup("default");
984 DEBUG ("curl_xml plugin: Registering new read callback: %s",
985 db->instance);
987 memset (&ud, 0, sizeof (ud));
988 ud.data = (void *) db;
989 ud.free_func = cx_free;
991 cb_name = ssnprintf_alloc ("curl_xml-%s-%s", db->instance, db->url);
992 plugin_register_complex_read (/* group = */ "curl_xml", cb_name, cx_read,
993 /* interval = */ NULL, &ud);
994 sfree (cb_name);
995 }
996 else
997 {
998 cx_free (db);
999 return (-1);
1000 }
1002 return (0);
1003 } /* }}} int cx_config_add_url */
1005 /* }}} End of configuration handling functions */
1007 static int cx_config (oconfig_item_t *ci) /* {{{ */
1008 {
1009 int success;
1010 int errors;
1011 int status;
1012 int i;
1014 success = 0;
1015 errors = 0;
1017 for (i = 0; i < ci->children_num; i++)
1018 {
1019 oconfig_item_t *child = ci->children + i;
1021 if (strcasecmp ("URL", child->key) == 0)
1022 {
1023 status = cx_config_add_url (child);
1024 if (status == 0)
1025 success++;
1026 else
1027 errors++;
1028 }
1029 else
1030 {
1031 WARNING ("curl_xml plugin: Option `%s' not allowed here.", child->key);
1032 errors++;
1033 }
1034 }
1036 if ((success == 0) && (errors > 0))
1037 {
1038 ERROR ("curl_xml plugin: All statements failed.");
1039 return (-1);
1040 }
1042 return (0);
1043 } /* }}} int cx_config */
1045 void module_register (void)
1046 {
1047 plugin_register_complex_config ("curl_xml", cx_config);
1048 } /* void module_register */
1050 /* vim: set sw=2 sts=2 et fdm=marker : */