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