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 "utils_curl_stats.h"
27 #include "utils_llist.h"
29 #include <libxml/parser.h>
30 #include <libxml/tree.h>
31 #include <libxml/xpath.h>
32 #include <libxml/xpathInternals.h>
34 #include <curl/curl.h>
36 #define CX_DEFAULT_HOST "localhost"
38 /*
39 * Private data structures
40 */
41 struct cx_values_s /* {{{ */
42 {
43 char path[DATA_MAX_NAME_LEN];
44 size_t path_len;
45 };
46 typedef struct cx_values_s cx_values_t;
47 /* }}} */
49 struct cx_xpath_s /* {{{ */
50 {
51 char *path;
52 char *type;
53 cx_values_t *values;
54 size_t values_len;
55 char *instance_prefix;
56 char *instance;
57 int is_table;
58 unsigned long magic;
59 };
60 typedef struct cx_xpath_s cx_xpath_t;
61 /* }}} */
63 struct cx_namespace_s /* {{{ */
64 {
65 char *prefix;
66 char *url;
67 };
68 typedef struct cx_namespace_s cx_namespace_t;
69 /* }}} */
71 struct cx_s /* {{{ */
72 {
73 char *instance;
74 char *host;
76 char *url;
77 char *user;
78 char *pass;
79 char *credentials;
80 _Bool digest;
81 _Bool verify_peer;
82 _Bool verify_host;
83 char *cacert;
84 char *post_body;
85 int timeout;
86 struct curl_slist *headers;
87 curl_stats_t *stats;
89 cx_namespace_t *namespaces;
90 size_t namespaces_num;
92 CURL *curl;
93 char curl_errbuf[CURL_ERROR_SIZE];
94 char *buffer;
95 size_t buffer_size;
96 size_t buffer_fill;
98 llist_t *list; /* list of xpath blocks */
99 };
100 typedef struct cx_s cx_t; /* }}} */
102 /*
103 * Private functions
104 */
105 static size_t cx_curl_callback (void *buf, /* {{{ */
106 size_t size, size_t nmemb, void *user_data)
107 {
108 size_t len = size * nmemb;
109 cx_t *db;
111 db = user_data;
112 if (db == NULL)
113 {
114 ERROR ("curl_xml plugin: cx_curl_callback: "
115 "user_data pointer is NULL.");
116 return (0);
117 }
119 if (len == 0)
120 return (len);
122 if ((db->buffer_fill + len) >= db->buffer_size)
123 {
124 char *temp;
126 temp = realloc (db->buffer,
127 db->buffer_fill + len + 1);
128 if (temp == NULL)
129 {
130 ERROR ("curl_xml plugin: realloc failed.");
131 return (0);
132 }
133 db->buffer = temp;
134 db->buffer_size = db->buffer_fill + len + 1;
135 }
137 memcpy (db->buffer + db->buffer_fill, (char *) buf, len);
138 db->buffer_fill += len;
139 db->buffer[db->buffer_fill] = 0;
141 return (len);
142 } /* }}} size_t cx_curl_callback */
144 static void cx_xpath_free (cx_xpath_t *xpath) /* {{{ */
145 {
146 if (xpath == NULL)
147 return;
149 sfree (xpath->path);
150 sfree (xpath->type);
151 sfree (xpath->instance_prefix);
152 sfree (xpath->instance);
153 sfree (xpath->values);
154 sfree (xpath);
155 } /* }}} void cx_xpath_free */
157 static void cx_list_free (llist_t *list) /* {{{ */
158 {
159 llentry_t *le;
161 le = llist_head (list);
162 while (le != NULL)
163 {
164 llentry_t *le_next;
166 le_next = le->next;
168 sfree (le->key);
169 cx_xpath_free (le->value);
171 le = le_next;
172 }
174 llist_destroy (list);
175 } /* }}} void cx_list_free */
177 static void cx_free (void *arg) /* {{{ */
178 {
179 cx_t *db;
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);
206 curl_stats_destroy (db->stats);
208 for (size_t i = 0; i < db->namespaces_num; i++)
209 {
210 sfree (db->namespaces[i].prefix);
211 sfree (db->namespaces[i].url);
212 }
213 sfree (db->namespaces);
215 sfree (db);
216 } /* }}} void cx_free */
218 static const char *cx_host (cx_t *db) /* {{{ */
219 {
220 if (db->host == NULL)
221 return hostname_g;
222 return db->host;
223 } /* }}} cx_host */
225 static int cx_config_append_string (const char *name, struct curl_slist **dest, /* {{{ */
226 oconfig_item_t *ci)
227 {
228 struct curl_slist *temp = NULL;
229 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
230 {
231 WARNING ("curl_xml plugin: `%s' needs exactly one string argument.", name);
232 return (-1);
233 }
235 temp = curl_slist_append(*dest, ci->values[0].value.string);
236 if (temp == NULL)
237 return (-1);
239 *dest = temp;
241 return (0);
242 } /* }}} int cx_config_append_string */
244 static int cx_check_type (const data_set_t *ds, cx_xpath_t *xpath) /* {{{ */
245 {
246 if (!ds)
247 {
248 WARNING ("curl_xml plugin: DataSet `%s' not defined.", xpath->type);
249 return (-1);
250 }
252 if (ds->ds_num != xpath->values_len)
253 {
254 WARNING ("curl_xml plugin: DataSet `%s' requires %zu values, but config talks about %zu",
255 xpath->type, ds->ds_num, xpath->values_len);
256 return (-1);
257 }
259 return (0);
260 } /* }}} cx_check_type */
262 static xmlXPathObjectPtr cx_evaluate_xpath (xmlXPathContextPtr xpath_ctx, /* {{{ */
263 xmlChar *expr)
264 {
265 xmlXPathObjectPtr xpath_obj;
267 /* XXX: When to free this? */
268 xpath_obj = xmlXPathEvalExpression(BAD_CAST expr, xpath_ctx);
269 if (xpath_obj == NULL)
270 {
271 WARNING ("curl_xml plugin: "
272 "Error unable to evaluate xpath expression \"%s\". Skipping...", expr);
273 return NULL;
274 }
276 return xpath_obj;
277 } /* }}} cx_evaluate_xpath */
279 static int cx_if_not_text_node (xmlNodePtr node) /* {{{ */
280 {
281 if (node->type == XML_TEXT_NODE || node->type == XML_ATTRIBUTE_NODE ||
282 node->type == XML_ELEMENT_NODE)
283 return (0);
285 WARNING ("curl_xml plugin: "
286 "Node \"%s\" doesn't seem to be a text node. Skipping...", node->name);
287 return -1;
288 } /* }}} cx_if_not_text_node */
290 static int cx_handle_single_value_xpath (xmlXPathContextPtr xpath_ctx, /* {{{ */
291 cx_xpath_t *xpath,
292 const data_set_t *ds, value_list_t *vl, int index)
293 {
294 xmlXPathObjectPtr values_node_obj;
295 xmlNodeSetPtr values_node;
296 int tmp_size;
297 char *node_value;
299 values_node_obj = cx_evaluate_xpath (xpath_ctx, BAD_CAST xpath->values[index].path);
300 if (values_node_obj == NULL)
301 return (-1); /* Error already logged. */
303 values_node = values_node_obj->nodesetval;
304 tmp_size = (values_node) ? values_node->nodeNr : 0;
306 if (tmp_size == 0)
307 {
308 WARNING ("curl_xml plugin: "
309 "relative xpath expression \"%s\" doesn't match any of the nodes. "
310 "Skipping...", xpath->values[index].path);
311 xmlXPathFreeObject (values_node_obj);
312 return (-1);
313 }
315 if (tmp_size > 1)
316 {
317 WARNING ("curl_xml plugin: "
318 "relative xpath expression \"%s\" is expected to return "
319 "only one node. Skipping...", xpath->values[index].path);
320 xmlXPathFreeObject (values_node_obj);
321 return (-1);
322 }
324 /* ignoring the element if other than textnode/attribute*/
325 if (cx_if_not_text_node(values_node->nodeTab[0]))
326 {
327 WARNING ("curl_xml plugin: "
328 "relative xpath expression \"%s\" is expected to return "
329 "only text/attribute node which is not the case. Skipping...",
330 xpath->values[index].path);
331 xmlXPathFreeObject (values_node_obj);
332 return (-1);
333 }
335 node_value = (char *) xmlNodeGetContent(values_node->nodeTab[0]);
336 switch (ds->ds[index].type)
337 {
338 case DS_TYPE_COUNTER:
339 vl->values[index].counter = (counter_t) strtoull (node_value,
340 /* endptr = */ NULL, /* base = */ 0);
341 break;
342 case DS_TYPE_DERIVE:
343 vl->values[index].derive = (derive_t) strtoll (node_value,
344 /* endptr = */ NULL, /* base = */ 0);
345 break;
346 case DS_TYPE_ABSOLUTE:
347 vl->values[index].absolute = (absolute_t) strtoull (node_value,
348 /* endptr = */ NULL, /* base = */ 0);
349 break;
350 case DS_TYPE_GAUGE:
351 vl->values[index].gauge = (gauge_t) strtod (node_value,
352 /* endptr = */ NULL);
353 }
355 /* free up object */
356 xmlXPathFreeObject (values_node_obj);
357 sfree (node_value);
359 /* We have reached here which means that
360 * we have got something to work */
361 return (0);
362 } /* }}} int cx_handle_single_value_xpath */
364 static int cx_handle_all_value_xpaths (xmlXPathContextPtr xpath_ctx, /* {{{ */
365 cx_xpath_t *xpath,
366 const data_set_t *ds, value_list_t *vl)
367 {
368 value_t values[xpath->values_len];
369 int status;
371 assert (xpath->values_len > 0);
372 assert (xpath->values_len == vl->values_len);
373 assert (xpath->values_len == ds->ds_num);
374 vl->values = values;
376 for (size_t i = 0; i < xpath->values_len; i++)
377 {
378 status = cx_handle_single_value_xpath (xpath_ctx, xpath, ds, vl, i);
379 if (status != 0)
380 return (-1); /* An error has been printed. */
381 } /* for (i = 0; i < xpath->values_len; i++) */
383 plugin_dispatch_values (vl);
384 vl->values = NULL;
386 return (0);
387 } /* }}} int cx_handle_all_value_xpaths */
389 static int cx_handle_instance_xpath (xmlXPathContextPtr xpath_ctx, /* {{{ */
390 cx_xpath_t *xpath, value_list_t *vl,
391 _Bool is_table)
392 {
393 xmlXPathObjectPtr instance_node_obj = NULL;
394 xmlNodeSetPtr instance_node = NULL;
396 memset (vl->type_instance, 0, sizeof (vl->type_instance));
398 /* If the base xpath returns more than one block, the result is assumed to be
399 * a table. The `Instance' option is not optional in this case. Check for the
400 * condition and inform the user. */
401 if (is_table && (xpath->instance == NULL))
402 {
403 WARNING ("curl_xml plugin: "
404 "Base-XPath %s is a table (more than one result was returned), "
405 "but no instance-XPath has been defined.",
406 xpath->path);
407 return (-1);
408 }
410 /* instance has to be an xpath expression */
411 if (xpath->instance != NULL)
412 {
413 int tmp_size;
415 instance_node_obj = cx_evaluate_xpath (xpath_ctx, BAD_CAST xpath->instance);
416 if (instance_node_obj == NULL)
417 return (-1); /* error is logged already */
419 instance_node = instance_node_obj->nodesetval;
420 tmp_size = (instance_node) ? instance_node->nodeNr : 0;
422 if (tmp_size <= 0)
423 {
424 WARNING ("curl_xml plugin: "
425 "relative xpath expression for 'InstanceFrom' \"%s\" doesn't match "
426 "any of the nodes. Skipping the node.", xpath->instance);
427 xmlXPathFreeObject (instance_node_obj);
428 return (-1);
429 }
431 if (tmp_size > 1)
432 {
433 WARNING ("curl_xml plugin: "
434 "relative xpath expression for 'InstanceFrom' \"%s\" is expected "
435 "to return only one text node. Skipping the node.", xpath->instance);
436 xmlXPathFreeObject (instance_node_obj);
437 return (-1);
438 }
440 /* ignoring the element if other than textnode/attribute */
441 if (cx_if_not_text_node(instance_node->nodeTab[0]))
442 {
443 WARNING ("curl_xml plugin: "
444 "relative xpath expression \"%s\" is expected to return only text node "
445 "which is not the case. Skipping the node.", xpath->instance);
446 xmlXPathFreeObject (instance_node_obj);
447 return (-1);
448 }
449 } /* if (xpath->instance != NULL) */
451 if (xpath->instance_prefix != NULL)
452 {
453 if (instance_node != NULL)
454 {
455 char *node_value = (char *) xmlNodeGetContent(instance_node->nodeTab[0]);
456 ssnprintf (vl->type_instance, sizeof (vl->type_instance),"%s%s",
457 xpath->instance_prefix, node_value);
458 sfree (node_value);
459 }
460 else
461 sstrncpy (vl->type_instance, xpath->instance_prefix,
462 sizeof (vl->type_instance));
463 }
464 else
465 {
466 /* If instance_prefix and instance_node are NULL, then
467 * don't set the type_instance */
468 if (instance_node != NULL)
469 {
470 char *node_value = (char *) xmlNodeGetContent(instance_node->nodeTab[0]);
471 sstrncpy (vl->type_instance, node_value, sizeof (vl->type_instance));
472 sfree (node_value);
473 }
474 }
476 /* Free `instance_node_obj' this late, because `instance_node' points to
477 * somewhere inside this structure. */
478 xmlXPathFreeObject (instance_node_obj);
480 return (0);
481 } /* }}} int cx_handle_instance_xpath */
483 static int cx_handle_base_xpath (char const *plugin_instance, /* {{{ */
484 char const *host,
485 xmlXPathContextPtr xpath_ctx, const data_set_t *ds,
486 char *base_xpath, cx_xpath_t *xpath)
487 {
488 int total_nodes;
490 xmlXPathObjectPtr base_node_obj = NULL;
491 xmlNodeSetPtr base_nodes = NULL;
493 value_list_t vl = VALUE_LIST_INIT;
495 base_node_obj = cx_evaluate_xpath (xpath_ctx, BAD_CAST base_xpath);
496 if (base_node_obj == NULL)
497 return -1; /* error is logged already */
499 base_nodes = base_node_obj->nodesetval;
500 total_nodes = (base_nodes) ? base_nodes->nodeNr : 0;
502 if (total_nodes == 0)
503 {
504 ERROR ("curl_xml plugin: "
505 "xpath expression \"%s\" doesn't match any of the nodes. "
506 "Skipping the xpath block...", base_xpath);
507 xmlXPathFreeObject (base_node_obj);
508 return -1;
509 }
511 /* If base_xpath returned multiple results, then */
512 /* Instance in the xpath block is required */
513 if (total_nodes > 1 && xpath->instance == NULL)
514 {
515 ERROR ("curl_xml plugin: "
516 "InstanceFrom is must in xpath block since the base xpath expression \"%s\" "
517 "returned multiple results. Skipping the xpath block...", base_xpath);
518 return -1;
519 }
521 /* set the values for the value_list */
522 vl.values_len = ds->ds_num;
523 sstrncpy (vl.type, xpath->type, sizeof (vl.type));
524 sstrncpy (vl.plugin, "curl_xml", sizeof (vl.plugin));
525 sstrncpy (vl.host, host, sizeof (vl.host));
526 if (plugin_instance != NULL)
527 sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
529 for (int i = 0; i < total_nodes; i++)
530 {
531 int status;
533 xpath_ctx->node = base_nodes->nodeTab[i];
535 status = cx_handle_instance_xpath (xpath_ctx, xpath, &vl,
536 /* is_table = */ (total_nodes > 1));
537 if (status != 0)
538 continue; /* An error has already been reported. */
540 status = cx_handle_all_value_xpaths (xpath_ctx, xpath, ds, &vl);
541 if (status != 0)
542 continue; /* An error has been logged. */
543 } /* for (i = 0; i < total_nodes; i++) */
545 /* free up the allocated memory */
546 xmlXPathFreeObject (base_node_obj);
548 return (0);
549 } /* }}} cx_handle_base_xpath */
551 static int cx_handle_parsed_xml(xmlDocPtr doc, /* {{{ */
552 xmlXPathContextPtr xpath_ctx, cx_t *db)
553 {
554 llentry_t *le;
555 const data_set_t *ds;
556 cx_xpath_t *xpath;
557 int status=-1;
560 le = llist_head (db->list);
561 while (le != NULL)
562 {
563 /* get the ds */
564 xpath = (cx_xpath_t *) le->value;
565 ds = plugin_get_ds (xpath->type);
567 if ( (cx_check_type(ds, xpath) == 0) &&
568 (cx_handle_base_xpath(db->instance, cx_host (db),
569 xpath_ctx, ds, le->key, xpath) == 0) )
570 status = 0; /* we got atleast one success */
572 le = le->next;
573 } /* while (le != NULL) */
575 return status;
576 } /* }}} cx_handle_parsed_xml */
578 static int cx_parse_stats_xml(xmlChar* xml, cx_t *db) /* {{{ */
579 {
580 int status;
581 xmlDocPtr doc;
582 xmlXPathContextPtr xpath_ctx;
584 /* Load the XML */
585 doc = xmlParseDoc(xml);
586 if (doc == NULL)
587 {
588 ERROR ("curl_xml plugin: Failed to parse the xml document - %s", xml);
589 return (-1);
590 }
592 xpath_ctx = xmlXPathNewContext(doc);
593 if(xpath_ctx == NULL)
594 {
595 ERROR ("curl_xml plugin: Failed to create the xml context");
596 xmlFreeDoc(doc);
597 return (-1);
598 }
600 for (size_t i = 0; i < db->namespaces_num; i++)
601 {
602 cx_namespace_t const *ns = db->namespaces + i;
603 status = xmlXPathRegisterNs (xpath_ctx,
604 BAD_CAST ns->prefix, BAD_CAST ns->url);
605 if (status != 0)
606 {
607 ERROR ("curl_xml plugin: "
608 "unable to register NS with prefix=\"%s\" and href=\"%s\"\n",
609 ns->prefix, ns->url);
610 xmlXPathFreeContext(xpath_ctx);
611 xmlFreeDoc (doc);
612 return (status);
613 }
614 }
616 status = cx_handle_parsed_xml (doc, xpath_ctx, db);
617 /* Cleanup */
618 xmlXPathFreeContext(xpath_ctx);
619 xmlFreeDoc(doc);
620 return status;
621 } /* }}} cx_parse_stats_xml */
623 static int cx_curl_perform (cx_t *db, CURL *curl) /* {{{ */
624 {
625 int status;
626 long rc;
627 char *ptr;
628 char *url;
629 url = db->url;
631 db->buffer_fill = 0;
632 status = curl_easy_perform (curl);
633 if (status != CURLE_OK)
634 {
635 ERROR ("curl_xml plugin: curl_easy_perform failed with status %i: %s (%s)",
636 status, db->curl_errbuf, url);
637 return (-1);
638 }
639 if (db->stats != NULL)
640 curl_stats_dispatch (db->stats, db->curl, cx_host (db), "curl_xml", db->instance);
642 curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &url);
643 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &rc);
645 /* The response code is zero if a non-HTTP transport was used. */
646 if ((rc != 0) && (rc != 200))
647 {
648 ERROR ("curl_xml plugin: curl_easy_perform failed with response code %ld (%s)",
649 rc, url);
650 return (-1);
651 }
653 ptr = db->buffer;
655 status = cx_parse_stats_xml(BAD_CAST ptr, db);
656 db->buffer_fill = 0;
658 return status;
659 } /* }}} int cx_curl_perform */
661 static int cx_read (user_data_t *ud) /* {{{ */
662 {
663 cx_t *db;
665 if ((ud == NULL) || (ud->data == NULL))
666 {
667 ERROR ("curl_xml plugin: cx_read: Invalid user data.");
668 return (-1);
669 }
671 db = (cx_t *) ud->data;
673 return cx_curl_perform (db, db->curl);
674 } /* }}} int cx_read */
676 /* Configuration handling functions {{{ */
678 static int cx_config_add_values (const char *name, cx_xpath_t *xpath, /* {{{ */
679 oconfig_item_t *ci)
680 {
681 if (ci->values_num < 1)
682 {
683 WARNING ("curl_xml plugin: `ValuesFrom' needs at least one argument.");
684 return (-1);
685 }
687 for (int i = 0; i < ci->values_num; i++)
688 if (ci->values[i].type != OCONFIG_TYPE_STRING)
689 {
690 WARNING ("curl_xml plugin: `ValuesFrom' needs only string argument.");
691 return (-1);
692 }
694 sfree (xpath->values);
696 xpath->values_len = 0;
697 xpath->values = malloc (sizeof (cx_values_t) * ci->values_num);
698 if (xpath->values == NULL)
699 return (-1);
700 xpath->values_len = (size_t) ci->values_num;
702 /* populate cx_values_t structure */
703 for (int i = 0; i < ci->values_num; i++)
704 {
705 xpath->values[i].path_len = sizeof (ci->values[i].value.string);
706 sstrncpy (xpath->values[i].path, ci->values[i].value.string, sizeof (xpath->values[i].path));
707 }
709 return (0);
710 } /* }}} cx_config_add_values */
712 static int cx_config_add_xpath (cx_t *db, oconfig_item_t *ci) /* {{{ */
713 {
714 cx_xpath_t *xpath;
715 char *name;
716 llentry_t *le;
717 int status;
719 xpath = calloc (1, sizeof (*xpath));
720 if (xpath == NULL)
721 {
722 ERROR ("curl_xml plugin: calloc failed.");
723 return (-1);
724 }
726 status = cf_util_get_string (ci, &xpath->path);
727 if (status != 0)
728 {
729 cx_xpath_free (xpath);
730 return (status);
731 }
733 /* error out if xpath->path is an empty string */
734 if (strlen (xpath->path) == 0)
735 {
736 ERROR ("curl_xml plugin: invalid xpath. "
737 "xpath value can't be an empty string");
738 cx_xpath_free (xpath);
739 return (-1);
740 }
742 status = 0;
743 for (int i = 0; i < ci->children_num; i++)
744 {
745 oconfig_item_t *child = ci->children + i;
747 if (strcasecmp ("Type", child->key) == 0)
748 status = cf_util_get_string (child, &xpath->type);
749 else if (strcasecmp ("InstancePrefix", child->key) == 0)
750 status = cf_util_get_string (child, &xpath->instance_prefix);
751 else if (strcasecmp ("InstanceFrom", child->key) == 0)
752 status = cf_util_get_string (child, &xpath->instance);
753 else if (strcasecmp ("ValuesFrom", child->key) == 0)
754 status = cx_config_add_values ("ValuesFrom", xpath, child);
755 else
756 {
757 WARNING ("curl_xml plugin: Option `%s' not allowed here.", child->key);
758 status = -1;
759 }
761 if (status != 0)
762 break;
763 } /* for (i = 0; i < ci->children_num; i++) */
765 if (status != 0)
766 {
767 cx_xpath_free (xpath);
768 return status;
769 }
771 if (xpath->type == NULL)
772 {
773 WARNING ("curl_xml plugin: `Type' missing in `xpath' block.");
774 cx_xpath_free (xpath);
775 return -1;
776 }
778 if (db->list == NULL)
779 {
780 db->list = llist_create();
781 if (db->list == NULL)
782 {
783 ERROR ("curl_xml plugin: list creation failed.");
784 cx_xpath_free (xpath);
785 return (-1);
786 }
787 }
789 name = strdup (xpath->path);
790 if (name == NULL)
791 {
792 ERROR ("curl_xml plugin: strdup failed.");
793 cx_xpath_free (xpath);
794 return (-1);
795 }
797 le = llentry_create (name, xpath);
798 if (le == NULL)
799 {
800 ERROR ("curl_xml plugin: llentry_create failed.");
801 cx_xpath_free (xpath);
802 sfree (name);
803 return (-1);
804 }
806 llist_append (db->list, le);
807 return (0);
808 } /* }}} int cx_config_add_xpath */
810 static int cx_config_add_namespace (cx_t *db, /* {{{ */
811 oconfig_item_t *ci)
812 {
813 cx_namespace_t *ns;
815 if ((ci->values_num != 2)
816 || (ci->values[0].type != OCONFIG_TYPE_STRING)
817 || (ci->values[1].type != OCONFIG_TYPE_STRING))
818 {
819 WARNING ("curl_xml plugin: The `Namespace' option "
820 "needs exactly two string arguments.");
821 return (EINVAL);
822 }
824 ns = realloc (db->namespaces, sizeof (*db->namespaces)
825 * (db->namespaces_num + 1));
826 if (ns == NULL)
827 {
828 ERROR ("curl_xml plugin: realloc failed.");
829 return (ENOMEM);
830 }
831 db->namespaces = ns;
832 ns = db->namespaces + db->namespaces_num;
833 memset (ns, 0, sizeof (*ns));
835 ns->prefix = strdup (ci->values[0].value.string);
836 ns->url = strdup (ci->values[1].value.string);
838 if ((ns->prefix == NULL) || (ns->url == NULL))
839 {
840 sfree (ns->prefix);
841 sfree (ns->url);
842 ERROR ("curl_xml plugin: strdup failed.");
843 return (ENOMEM);
844 }
846 db->namespaces_num++;
847 return (0);
848 } /* }}} int cx_config_add_namespace */
850 /* Initialize db->curl */
851 static int cx_init_curl (cx_t *db) /* {{{ */
852 {
853 db->curl = curl_easy_init ();
854 if (db->curl == NULL)
855 {
856 ERROR ("curl_xml plugin: curl_easy_init failed.");
857 return (-1);
858 }
860 curl_easy_setopt (db->curl, CURLOPT_NOSIGNAL, 1L);
861 curl_easy_setopt (db->curl, CURLOPT_WRITEFUNCTION, cx_curl_callback);
862 curl_easy_setopt (db->curl, CURLOPT_WRITEDATA, db);
863 curl_easy_setopt (db->curl, CURLOPT_USERAGENT, COLLECTD_USERAGENT);
864 curl_easy_setopt (db->curl, CURLOPT_ERRORBUFFER, db->curl_errbuf);
865 curl_easy_setopt (db->curl, CURLOPT_URL, db->url);
866 curl_easy_setopt (db->curl, CURLOPT_FOLLOWLOCATION, 1L);
867 curl_easy_setopt (db->curl, CURLOPT_MAXREDIRS, 50L);
869 if (db->user != NULL)
870 {
871 #ifdef HAVE_CURLOPT_USERNAME
872 curl_easy_setopt (db->curl, CURLOPT_USERNAME, db->user);
873 curl_easy_setopt (db->curl, CURLOPT_PASSWORD,
874 (db->pass == NULL) ? "" : db->pass);
875 #else
876 size_t credentials_size;
878 credentials_size = strlen (db->user) + 2;
879 if (db->pass != NULL)
880 credentials_size += strlen (db->pass);
882 db->credentials = malloc (credentials_size);
883 if (db->credentials == NULL)
884 {
885 ERROR ("curl_xml plugin: malloc failed.");
886 return (-1);
887 }
889 ssnprintf (db->credentials, credentials_size, "%s:%s",
890 db->user, (db->pass == NULL) ? "" : db->pass);
891 curl_easy_setopt (db->curl, CURLOPT_USERPWD, db->credentials);
892 #endif
894 if (db->digest)
895 curl_easy_setopt (db->curl, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST);
896 }
898 curl_easy_setopt (db->curl, CURLOPT_SSL_VERIFYPEER, db->verify_peer ? 1L : 0L);
899 curl_easy_setopt (db->curl, CURLOPT_SSL_VERIFYHOST,
900 db->verify_host ? 2L : 0L);
901 if (db->cacert != NULL)
902 curl_easy_setopt (db->curl, CURLOPT_CAINFO, db->cacert);
903 if (db->headers != NULL)
904 curl_easy_setopt (db->curl, CURLOPT_HTTPHEADER, db->headers);
905 if (db->post_body != NULL)
906 curl_easy_setopt (db->curl, CURLOPT_POSTFIELDS, db->post_body);
908 #ifdef HAVE_CURLOPT_TIMEOUT_MS
909 if (db->timeout >= 0)
910 curl_easy_setopt (db->curl, CURLOPT_TIMEOUT_MS, (long) db->timeout);
911 else
912 curl_easy_setopt (db->curl, CURLOPT_TIMEOUT_MS, (long) CDTIME_T_TO_MS(plugin_get_interval()));
913 #endif
915 return (0);
916 } /* }}} int cx_init_curl */
918 static int cx_config_add_url (oconfig_item_t *ci) /* {{{ */
919 {
920 cx_t *db;
921 int status = 0;
923 if ((ci->values_num != 1)
924 || (ci->values[0].type != OCONFIG_TYPE_STRING))
925 {
926 WARNING ("curl_xml plugin: The `URL' block "
927 "needs exactly one string argument.");
928 return (-1);
929 }
931 db = calloc (1, sizeof (*db));
932 if (db == NULL)
933 {
934 ERROR ("curl_xml plugin: calloc failed.");
935 return (-1);
936 }
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 (int 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 if (strcasecmp ("Statistics", child->key) == 0)
989 {
990 db->stats = curl_stats_from_config (child);
991 if (db->stats == NULL)
992 status = -1;
993 }
994 else
995 {
996 WARNING ("curl_xml plugin: Option `%s' not allowed here.", child->key);
997 status = -1;
998 }
1000 if (status != 0)
1001 break;
1002 }
1004 if (status == 0)
1005 {
1006 if (db->list == NULL)
1007 {
1008 WARNING ("curl_xml plugin: No (valid) `Key' block "
1009 "within `URL' block `%s'.", db->url);
1010 status = -1;
1011 }
1012 if (status == 0)
1013 status = cx_init_curl (db);
1014 }
1016 /* If all went well, register this database for reading */
1017 if (status == 0)
1018 {
1019 char *cb_name;
1021 if (db->instance == NULL)
1022 db->instance = strdup("default");
1024 DEBUG ("curl_xml plugin: Registering new read callback: %s",
1025 db->instance);
1027 cb_name = ssnprintf_alloc ("curl_xml-%s-%s", db->instance, db->url);
1029 plugin_register_complex_read (/* group = */ "curl_xml", cb_name, cx_read,
1030 /* interval = */ 0,
1031 &(user_data_t) {
1032 .data = db,
1033 .free_func = cx_free,
1034 });
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 : */