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 verify_peer;
80 _Bool verify_host;
81 char *cacert;
82 char *post_body;
83 struct curl_slist *headers;
85 cx_namespace_t *namespaces;
86 size_t namespaces_num;
88 CURL *curl;
89 char curl_errbuf[CURL_ERROR_SIZE];
90 char *buffer;
91 size_t buffer_size;
92 size_t buffer_fill;
94 llist_t *list; /* list of xpath blocks */
95 };
96 typedef struct cx_s cx_t; /* }}} */
98 /*
99 * Private functions
100 */
101 static size_t cx_curl_callback (void *buf, /* {{{ */
102 size_t size, size_t nmemb, void *user_data)
103 {
104 size_t len = size * nmemb;
105 cx_t *db;
107 db = user_data;
108 if (db == NULL)
109 {
110 ERROR ("curl_xml plugin: cx_curl_callback: "
111 "user_data pointer is NULL.");
112 return (0);
113 }
115 if (len <= 0)
116 return (len);
118 if ((db->buffer_fill + len) >= db->buffer_size)
119 {
120 char *temp;
122 temp = (char *) realloc (db->buffer,
123 db->buffer_fill + len + 1);
124 if (temp == NULL)
125 {
126 ERROR ("curl_xml plugin: realloc failed.");
127 return (0);
128 }
129 db->buffer = temp;
130 db->buffer_size = db->buffer_fill + len + 1;
131 }
133 memcpy (db->buffer + db->buffer_fill, (char *) buf, len);
134 db->buffer_fill += len;
135 db->buffer[db->buffer_fill] = 0;
137 return (len);
138 } /* }}} size_t cx_curl_callback */
140 static void cx_xpath_free (cx_xpath_t *xpath) /* {{{ */
141 {
142 if (xpath == NULL)
143 return;
145 sfree (xpath->path);
146 sfree (xpath->type);
147 sfree (xpath->instance_prefix);
148 sfree (xpath->instance);
149 sfree (xpath->values);
150 sfree (xpath);
151 } /* }}} void cx_xpath_free */
153 static void cx_list_free (llist_t *list) /* {{{ */
154 {
155 llentry_t *le;
157 le = llist_head (list);
158 while (le != NULL)
159 {
160 llentry_t *le_next;
162 le_next = le->next;
164 sfree (le->key);
165 cx_xpath_free (le->value);
167 le = le_next;
168 }
170 llist_destroy (list);
171 list = NULL;
172 } /* }}} void cx_list_free */
174 static void cx_free (void *arg) /* {{{ */
175 {
176 cx_t *db;
177 size_t i;
179 DEBUG ("curl_xml plugin: cx_free (arg = %p);", arg);
181 db = (cx_t *) arg;
183 if (db == NULL)
184 return;
186 if (db->curl != NULL)
187 curl_easy_cleanup (db->curl);
188 db->curl = NULL;
190 if (db->list != NULL)
191 cx_list_free (db->list);
193 sfree (db->buffer);
194 sfree (db->instance);
195 sfree (db->host);
197 sfree (db->url);
198 sfree (db->user);
199 sfree (db->pass);
200 sfree (db->credentials);
201 sfree (db->cacert);
202 sfree (db->post_body);
203 curl_slist_free_all (db->headers);
205 for (i = 0; i < db->namespaces_num; i++)
206 {
207 sfree (db->namespaces[i].prefix);
208 sfree (db->namespaces[i].url);
209 }
210 sfree (db->namespaces);
212 sfree (db);
213 } /* }}} void cx_free */
215 static int cx_config_append_string (const char *name, struct curl_slist **dest, /* {{{ */
216 oconfig_item_t *ci)
217 {
218 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
219 {
220 WARNING ("curl_xml plugin: `%s' needs exactly one string argument.", name);
221 return (-1);
222 }
224 *dest = curl_slist_append(*dest, ci->values[0].value.string);
225 if (*dest == NULL)
226 return (-1);
228 return (0);
229 } /* }}} int cx_config_append_string */
231 static int cx_check_type (const data_set_t *ds, cx_xpath_t *xpath) /* {{{ */
232 {
233 if (!ds)
234 {
235 WARNING ("curl_xml plugin: DataSet `%s' not defined.", xpath->type);
236 return (-1);
237 }
239 if (ds->ds_num != xpath->values_len)
240 {
241 WARNING ("curl_xml plugin: DataSet `%s' requires %i values, but config talks about %i",
242 xpath->type, ds->ds_num, xpath->values_len);
243 return (-1);
244 }
246 return (0);
247 } /* }}} cx_check_type */
249 static xmlXPathObjectPtr cx_evaluate_xpath (xmlXPathContextPtr xpath_ctx, /* {{{ */
250 xmlChar *expr)
251 {
252 xmlXPathObjectPtr xpath_obj;
254 /* XXX: When to free this? */
255 xpath_obj = xmlXPathEvalExpression(BAD_CAST expr, xpath_ctx);
256 if (xpath_obj == NULL)
257 {
258 WARNING ("curl_xml plugin: "
259 "Error unable to evaluate xpath expression \"%s\". Skipping...", expr);
260 return NULL;
261 }
263 return xpath_obj;
264 } /* }}} cx_evaluate_xpath */
266 static int cx_if_not_text_node (xmlNodePtr node) /* {{{ */
267 {
268 if (node->type == XML_TEXT_NODE || node->type == XML_ATTRIBUTE_NODE ||
269 node->type == XML_ELEMENT_NODE)
270 return (0);
272 WARNING ("curl_xml plugin: "
273 "Node \"%s\" doesn't seem to be a text node. Skipping...", node->name);
274 return -1;
275 } /* }}} cx_if_not_text_node */
277 static int cx_handle_single_value_xpath (xmlXPathContextPtr xpath_ctx, /* {{{ */
278 cx_xpath_t *xpath,
279 const data_set_t *ds, value_list_t *vl, int index)
280 {
281 xmlXPathObjectPtr values_node_obj;
282 xmlNodeSetPtr values_node;
283 int tmp_size;
284 char *node_value;
286 values_node_obj = cx_evaluate_xpath (xpath_ctx, BAD_CAST xpath->values[index].path);
287 if (values_node_obj == NULL)
288 return (-1); /* Error already logged. */
290 values_node = values_node_obj->nodesetval;
291 tmp_size = (values_node) ? values_node->nodeNr : 0;
293 if (tmp_size == 0)
294 {
295 WARNING ("curl_xml plugin: "
296 "relative xpath expression \"%s\" doesn't match any of the nodes. "
297 "Skipping...", xpath->values[index].path);
298 xmlXPathFreeObject (values_node_obj);
299 return (-1);
300 }
302 if (tmp_size > 1)
303 {
304 WARNING ("curl_xml plugin: "
305 "relative xpath expression \"%s\" is expected to return "
306 "only one node. Skipping...", xpath->values[index].path);
307 xmlXPathFreeObject (values_node_obj);
308 return (-1);
309 }
311 /* ignoring the element if other than textnode/attribute*/
312 if (cx_if_not_text_node(values_node->nodeTab[0]))
313 {
314 WARNING ("curl_xml plugin: "
315 "relative xpath expression \"%s\" is expected to return "
316 "only text/attribute node which is not the case. Skipping...",
317 xpath->values[index].path);
318 xmlXPathFreeObject (values_node_obj);
319 return (-1);
320 }
322 node_value = (char *) xmlNodeGetContent(values_node->nodeTab[0]);
323 switch (ds->ds[index].type)
324 {
325 case DS_TYPE_COUNTER:
326 vl->values[index].counter = (counter_t) strtoull (node_value,
327 /* endptr = */ NULL, /* base = */ 0);
328 break;
329 case DS_TYPE_DERIVE:
330 vl->values[index].derive = (derive_t) strtoll (node_value,
331 /* endptr = */ NULL, /* base = */ 0);
332 break;
333 case DS_TYPE_ABSOLUTE:
334 vl->values[index].absolute = (absolute_t) strtoull (node_value,
335 /* endptr = */ NULL, /* base = */ 0);
336 break;
337 case DS_TYPE_GAUGE:
338 vl->values[index].gauge = (gauge_t) strtod (node_value,
339 /* endptr = */ NULL);
340 }
342 /* free up object */
343 xmlXPathFreeObject (values_node_obj);
344 sfree (node_value);
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 && (xpath->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 {
443 char *node_value = (char *) xmlNodeGetContent(instance_node->nodeTab[0]);
444 ssnprintf (vl->type_instance, sizeof (vl->type_instance),"%s%s",
445 xpath->instance_prefix, node_value);
446 sfree (node_value);
447 }
448 else
449 sstrncpy (vl->type_instance, xpath->instance_prefix,
450 sizeof (vl->type_instance));
451 }
452 else
453 {
454 /* If instance_prefix and instance_node are NULL, then
455 * don't set the type_instance */
456 if (instance_node != NULL)
457 {
458 char *node_value = (char *) xmlNodeGetContent(instance_node->nodeTab[0]);
459 sstrncpy (vl->type_instance, node_value, sizeof (vl->type_instance));
460 sfree (node_value);
461 }
462 }
464 /* Free `instance_node_obj' this late, because `instance_node' points to
465 * somewhere inside this structure. */
466 xmlXPathFreeObject (instance_node_obj);
468 return (0);
469 } /* }}} int cx_handle_instance_xpath */
471 static int cx_handle_base_xpath (char const *plugin_instance, /* {{{ */
472 char const *host,
473 xmlXPathContextPtr xpath_ctx, const data_set_t *ds,
474 char *base_xpath, cx_xpath_t *xpath)
475 {
476 int total_nodes;
477 int i;
479 xmlXPathObjectPtr base_node_obj = NULL;
480 xmlNodeSetPtr base_nodes = NULL;
482 value_list_t vl = VALUE_LIST_INIT;
484 base_node_obj = cx_evaluate_xpath (xpath_ctx, BAD_CAST base_xpath);
485 if (base_node_obj == NULL)
486 return -1; /* error is logged already */
488 base_nodes = base_node_obj->nodesetval;
489 total_nodes = (base_nodes) ? base_nodes->nodeNr : 0;
491 if (total_nodes == 0)
492 {
493 ERROR ("curl_xml plugin: "
494 "xpath expression \"%s\" doesn't match any of the nodes. "
495 "Skipping the xpath block...", base_xpath);
496 xmlXPathFreeObject (base_node_obj);
497 return -1;
498 }
500 /* If base_xpath returned multiple results, then */
501 /* Instance in the xpath block is required */
502 if (total_nodes > 1 && xpath->instance == NULL)
503 {
504 ERROR ("curl_xml plugin: "
505 "InstanceFrom is must in xpath block since the base xpath expression \"%s\" "
506 "returned multiple results. Skipping the xpath block...", base_xpath);
507 return -1;
508 }
510 /* set the values for the value_list */
511 vl.values_len = ds->ds_num;
512 sstrncpy (vl.type, xpath->type, sizeof (vl.type));
513 sstrncpy (vl.plugin, "curl_xml", sizeof (vl.plugin));
514 sstrncpy (vl.host, (host != NULL) ? host : hostname_g, sizeof (vl.host));
515 if (plugin_instance != NULL)
516 sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
518 for (i = 0; i < total_nodes; i++)
519 {
520 int status;
522 xpath_ctx->node = base_nodes->nodeTab[i];
524 status = cx_handle_instance_xpath (xpath_ctx, xpath, &vl,
525 /* is_table = */ (total_nodes > 1));
526 if (status != 0)
527 continue; /* An error has already been reported. */
529 status = cx_handle_all_value_xpaths (xpath_ctx, xpath, ds, &vl);
530 if (status != 0)
531 continue; /* An error has been logged. */
532 } /* for (i = 0; i < total_nodes; i++) */
534 /* free up the allocated memory */
535 xmlXPathFreeObject (base_node_obj);
537 return (0);
538 } /* }}} cx_handle_base_xpath */
540 static int cx_handle_parsed_xml(xmlDocPtr doc, /* {{{ */
541 xmlXPathContextPtr xpath_ctx, cx_t *db)
542 {
543 llentry_t *le;
544 const data_set_t *ds;
545 cx_xpath_t *xpath;
546 int status=-1;
549 le = llist_head (db->list);
550 while (le != NULL)
551 {
552 /* get the ds */
553 xpath = (cx_xpath_t *) le->value;
554 ds = plugin_get_ds (xpath->type);
556 if ( (cx_check_type(ds, xpath) == 0) &&
557 (cx_handle_base_xpath(db->instance, db->host,
558 xpath_ctx, ds, le->key, xpath) == 0) )
559 status = 0; /* we got atleast one success */
561 le = le->next;
562 } /* while (le != NULL) */
564 return status;
565 } /* }}} cx_handle_parsed_xml */
567 static int cx_parse_stats_xml(xmlChar* xml, cx_t *db) /* {{{ */
568 {
569 int status;
570 xmlDocPtr doc;
571 xmlXPathContextPtr xpath_ctx;
572 size_t i;
574 /* Load the XML */
575 doc = xmlParseDoc(xml);
576 if (doc == NULL)
577 {
578 ERROR ("curl_xml plugin: Failed to parse the xml document - %s", xml);
579 return (-1);
580 }
582 xpath_ctx = xmlXPathNewContext(doc);
583 if(xpath_ctx == NULL)
584 {
585 ERROR ("curl_xml plugin: Failed to create the xml context");
586 xmlFreeDoc(doc);
587 return (-1);
588 }
590 for (i = 0; i < db->namespaces_num; i++)
591 {
592 cx_namespace_t const *ns = db->namespaces + i;
593 status = xmlXPathRegisterNs (xpath_ctx,
594 BAD_CAST ns->prefix, BAD_CAST ns->url);
595 if (status != 0)
596 {
597 ERROR ("curl_xml plugin: "
598 "unable to register NS with prefix=\"%s\" and href=\"%s\"\n",
599 ns->prefix, ns->url);
600 xmlXPathFreeContext(xpath_ctx);
601 xmlFreeDoc (doc);
602 return (status);
603 }
604 }
606 status = cx_handle_parsed_xml (doc, xpath_ctx, db);
607 /* Cleanup */
608 xmlXPathFreeContext(xpath_ctx);
609 xmlFreeDoc(doc);
610 return status;
611 } /* }}} cx_parse_stats_xml */
613 static int cx_curl_perform (cx_t *db, CURL *curl) /* {{{ */
614 {
615 int status;
616 long rc;
617 char *ptr;
618 char *url;
619 url = db->url;
621 db->buffer_fill = 0;
622 status = curl_easy_perform (curl);
623 if (status != CURLE_OK)
624 {
625 ERROR ("curl_xml plugin: curl_easy_perform failed with status %i: %s (%s)",
626 status, db->curl_errbuf, url);
627 return (-1);
628 }
630 curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &url);
631 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &rc);
633 /* The response code is zero if a non-HTTP transport was used. */
634 if ((rc != 0) && (rc != 200))
635 {
636 ERROR ("curl_xml plugin: curl_easy_perform failed with response code %ld (%s)",
637 rc, url);
638 return (-1);
639 }
641 ptr = db->buffer;
643 status = cx_parse_stats_xml(BAD_CAST ptr, db);
644 db->buffer_fill = 0;
646 return status;
647 } /* }}} int cx_curl_perform */
649 static int cx_read (user_data_t *ud) /* {{{ */
650 {
651 cx_t *db;
653 if ((ud == NULL) || (ud->data == NULL))
654 {
655 ERROR ("curl_xml plugin: cx_read: Invalid user data.");
656 return (-1);
657 }
659 db = (cx_t *) ud->data;
661 return cx_curl_perform (db, db->curl);
662 } /* }}} int cx_read */
664 /* Configuration handling functions {{{ */
666 static int cx_config_add_values (const char *name, cx_xpath_t *xpath, /* {{{ */
667 oconfig_item_t *ci)
668 {
669 int i;
671 if (ci->values_num < 1)
672 {
673 WARNING ("curl_xml plugin: `ValuesFrom' needs at least one argument.");
674 return (-1);
675 }
677 for (i = 0; i < ci->values_num; i++)
678 if (ci->values[i].type != OCONFIG_TYPE_STRING)
679 {
680 WARNING ("curl_xml plugin: `ValuesFrom' needs only string argument.");
681 return (-1);
682 }
684 sfree (xpath->values);
686 xpath->values_len = 0;
687 xpath->values = (cx_values_t *) malloc (sizeof (cx_values_t) * ci->values_num);
688 if (xpath->values == NULL)
689 return (-1);
690 xpath->values_len = ci->values_num;
692 /* populate cx_values_t structure */
693 for (i = 0; i < ci->values_num; i++)
694 {
695 xpath->values[i].path_len = sizeof (ci->values[i].value.string);
696 sstrncpy (xpath->values[i].path, ci->values[i].value.string, sizeof (xpath->values[i].path));
697 }
699 return (0);
700 } /* }}} cx_config_add_values */
702 static int cx_config_add_xpath (cx_t *db, oconfig_item_t *ci) /* {{{ */
703 {
704 cx_xpath_t *xpath;
705 char *name;
706 llentry_t *le;
707 int status;
708 int i;
710 xpath = malloc (sizeof (*xpath));
711 if (xpath == NULL)
712 {
713 ERROR ("curl_xml plugin: malloc failed.");
714 return (-1);
715 }
716 memset (xpath, 0, sizeof (*xpath));
718 status = cf_util_get_string (ci, &xpath->path);
719 if (status != 0)
720 {
721 cx_xpath_free (xpath);
722 return (status);
723 }
725 /* error out if xpath->path is an empty string */
726 if (strlen (xpath->path) == 0)
727 {
728 ERROR ("curl_xml plugin: invalid xpath. "
729 "xpath value can't be an empty string");
730 cx_xpath_free (xpath);
731 return (-1);
732 }
734 status = 0;
735 for (i = 0; i < ci->children_num; i++)
736 {
737 oconfig_item_t *child = ci->children + i;
739 if (strcasecmp ("Type", child->key) == 0)
740 status = cf_util_get_string (child, &xpath->type);
741 else if (strcasecmp ("InstancePrefix", child->key) == 0)
742 status = cf_util_get_string (child, &xpath->instance_prefix);
743 else if (strcasecmp ("InstanceFrom", child->key) == 0)
744 status = cf_util_get_string (child, &xpath->instance);
745 else if (strcasecmp ("ValuesFrom", child->key) == 0)
746 status = cx_config_add_values ("ValuesFrom", xpath, child);
747 else
748 {
749 WARNING ("curl_xml plugin: Option `%s' not allowed here.", child->key);
750 status = -1;
751 }
753 if (status != 0)
754 break;
755 } /* for (i = 0; i < ci->children_num; i++) */
757 if (status != 0)
758 {
759 cx_xpath_free (xpath);
760 return status;
761 }
763 if (xpath->type == NULL)
764 {
765 WARNING ("curl_xml plugin: `Type' missing in `xpath' block.");
766 cx_xpath_free (xpath);
767 return -1;
768 }
770 if (db->list == NULL)
771 {
772 db->list = llist_create();
773 if (db->list == NULL)
774 {
775 ERROR ("curl_xml plugin: list creation failed.");
776 cx_xpath_free (xpath);
777 return (-1);
778 }
779 }
781 name = strdup (xpath->path);
782 if (name == NULL)
783 {
784 ERROR ("curl_xml plugin: strdup failed.");
785 cx_xpath_free (xpath);
786 return (-1);
787 }
789 le = llentry_create (name, xpath);
790 if (le == NULL)
791 {
792 ERROR ("curl_xml plugin: llentry_create failed.");
793 cx_xpath_free (xpath);
794 sfree (name);
795 return (-1);
796 }
798 llist_append (db->list, le);
799 return (0);
800 } /* }}} int cx_config_add_xpath */
802 static int cx_config_add_namespace (cx_t *db, /* {{{ */
803 oconfig_item_t *ci)
804 {
805 cx_namespace_t *ns;
807 if ((ci->values_num != 2)
808 || (ci->values[0].type != OCONFIG_TYPE_STRING)
809 || (ci->values[1].type != OCONFIG_TYPE_STRING))
810 {
811 WARNING ("curl_xml plugin: The `Namespace' option "
812 "needs exactly two string arguments.");
813 return (EINVAL);
814 }
816 ns = realloc (db->namespaces, sizeof (*db->namespaces)
817 * (db->namespaces_num + 1));
818 if (ns == NULL)
819 {
820 ERROR ("curl_xml plugin: realloc failed.");
821 return (ENOMEM);
822 }
823 db->namespaces = ns;
824 ns = db->namespaces + db->namespaces_num;
825 memset (ns, 0, sizeof (*ns));
827 ns->prefix = strdup (ci->values[0].value.string);
828 ns->url = strdup (ci->values[1].value.string);
830 if ((ns->prefix == NULL) || (ns->url == NULL))
831 {
832 sfree (ns->prefix);
833 sfree (ns->url);
834 ERROR ("curl_xml plugin: strdup failed.");
835 return (ENOMEM);
836 }
838 db->namespaces_num++;
839 return (0);
840 } /* }}} int cx_config_add_namespace */
842 /* Initialize db->curl */
843 static int cx_init_curl (cx_t *db) /* {{{ */
844 {
845 db->curl = curl_easy_init ();
846 if (db->curl == NULL)
847 {
848 ERROR ("curl_xml plugin: curl_easy_init failed.");
849 return (-1);
850 }
852 curl_easy_setopt (db->curl, CURLOPT_NOSIGNAL, 1L);
853 curl_easy_setopt (db->curl, CURLOPT_WRITEFUNCTION, cx_curl_callback);
854 curl_easy_setopt (db->curl, CURLOPT_WRITEDATA, db);
855 curl_easy_setopt (db->curl, CURLOPT_USERAGENT,
856 PACKAGE_NAME"/"PACKAGE_VERSION);
857 curl_easy_setopt (db->curl, CURLOPT_ERRORBUFFER, db->curl_errbuf);
858 curl_easy_setopt (db->curl, CURLOPT_URL, db->url);
860 if (db->user != NULL)
861 {
862 size_t credentials_size;
864 credentials_size = strlen (db->user) + 2;
865 if (db->pass != NULL)
866 credentials_size += strlen (db->pass);
868 db->credentials = (char *) malloc (credentials_size);
869 if (db->credentials == NULL)
870 {
871 ERROR ("curl_xml plugin: malloc failed.");
872 return (-1);
873 }
875 ssnprintf (db->credentials, credentials_size, "%s:%s",
876 db->user, (db->pass == NULL) ? "" : db->pass);
877 curl_easy_setopt (db->curl, CURLOPT_USERPWD, db->credentials);
878 }
880 curl_easy_setopt (db->curl, CURLOPT_SSL_VERIFYPEER, db->verify_peer ? 1L : 0L);
881 curl_easy_setopt (db->curl, CURLOPT_SSL_VERIFYHOST,
882 db->verify_host ? 2L : 0L);
883 if (db->cacert != NULL)
884 curl_easy_setopt (db->curl, CURLOPT_CAINFO, db->cacert);
885 if (db->headers != NULL)
886 curl_easy_setopt (db->curl, CURLOPT_HTTPHEADER, db->headers);
887 if (db->post_body != NULL)
888 curl_easy_setopt (db->curl, CURLOPT_POSTFIELDS, db->post_body);
890 return (0);
891 } /* }}} int cx_init_curl */
893 static int cx_config_add_url (oconfig_item_t *ci) /* {{{ */
894 {
895 cx_t *db;
896 int status = 0;
897 int i;
899 if ((ci->values_num != 1)
900 || (ci->values[0].type != OCONFIG_TYPE_STRING))
901 {
902 WARNING ("curl_xml plugin: The `URL' block "
903 "needs exactly one string argument.");
904 return (-1);
905 }
907 db = (cx_t *) malloc (sizeof (*db));
908 if (db == NULL)
909 {
910 ERROR ("curl_xml plugin: malloc failed.");
911 return (-1);
912 }
913 memset (db, 0, sizeof (*db));
915 if (strcasecmp ("URL", ci->key) == 0)
916 {
917 status = cf_util_get_string (ci, &db->url);
918 if (status != 0)
919 {
920 sfree (db);
921 return (status);
922 }
923 }
924 else
925 {
926 ERROR ("curl_xml plugin: cx_config: "
927 "Invalid key: %s", ci->key);
928 cx_free (db);
929 return (-1);
930 }
932 /* Fill the `cx_t' structure.. */
933 for (i = 0; i < ci->children_num; i++)
934 {
935 oconfig_item_t *child = ci->children + i;
937 if (strcasecmp ("Instance", child->key) == 0)
938 status = cf_util_get_string (child, &db->instance);
939 else if (strcasecmp ("Host", child->key) == 0)
940 status = cf_util_get_string (child, &db->host);
941 else if (strcasecmp ("User", child->key) == 0)
942 status = cf_util_get_string (child, &db->user);
943 else if (strcasecmp ("Password", child->key) == 0)
944 status = cf_util_get_string (child, &db->pass);
945 else if (strcasecmp ("VerifyPeer", child->key) == 0)
946 status = cf_util_get_boolean (child, &db->verify_peer);
947 else if (strcasecmp ("VerifyHost", child->key) == 0)
948 status = cf_util_get_boolean (child, &db->verify_host);
949 else if (strcasecmp ("CACert", child->key) == 0)
950 status = cf_util_get_string (child, &db->cacert);
951 else if (strcasecmp ("xpath", child->key) == 0)
952 status = cx_config_add_xpath (db, child);
953 else if (strcasecmp ("Header", child->key) == 0)
954 status = cx_config_append_string ("Header", &db->headers, child);
955 else if (strcasecmp ("Post", child->key) == 0)
956 status = cf_util_get_string (child, &db->post_body);
957 else if (strcasecmp ("Namespace", child->key) == 0)
958 status = cx_config_add_namespace (db, child);
959 else
960 {
961 WARNING ("curl_xml plugin: Option `%s' not allowed here.", child->key);
962 status = -1;
963 }
965 if (status != 0)
966 break;
967 }
969 if (status == 0)
970 {
971 if (db->list == NULL)
972 {
973 WARNING ("curl_xml plugin: No (valid) `Key' block "
974 "within `URL' block `%s'.", db->url);
975 status = -1;
976 }
977 if (status == 0)
978 status = cx_init_curl (db);
979 }
981 /* If all went well, register this database for reading */
982 if (status == 0)
983 {
984 user_data_t ud;
985 char *cb_name;
987 if (db->instance == NULL)
988 db->instance = strdup("default");
990 DEBUG ("curl_xml plugin: Registering new read callback: %s",
991 db->instance);
993 memset (&ud, 0, sizeof (ud));
994 ud.data = (void *) db;
995 ud.free_func = cx_free;
997 cb_name = ssnprintf_alloc ("curl_xml-%s-%s", db->instance, db->url);
998 plugin_register_complex_read (/* group = */ "curl_xml", cb_name, cx_read,
999 /* interval = */ NULL, &ud);
1000 sfree (cb_name);
1001 }
1002 else
1003 {
1004 cx_free (db);
1005 return (-1);
1006 }
1008 return (0);
1009 } /* }}} int cx_config_add_url */
1011 /* }}} End of configuration handling functions */
1013 static int cx_config (oconfig_item_t *ci) /* {{{ */
1014 {
1015 int success;
1016 int errors;
1017 int status;
1018 int i;
1020 success = 0;
1021 errors = 0;
1023 for (i = 0; i < ci->children_num; i++)
1024 {
1025 oconfig_item_t *child = ci->children + i;
1027 if (strcasecmp ("URL", child->key) == 0)
1028 {
1029 status = cx_config_add_url (child);
1030 if (status == 0)
1031 success++;
1032 else
1033 errors++;
1034 }
1035 else
1036 {
1037 WARNING ("curl_xml plugin: Option `%s' not allowed here.", child->key);
1038 errors++;
1039 }
1040 }
1042 if ((success == 0) && (errors > 0))
1043 {
1044 ERROR ("curl_xml plugin: All statements failed.");
1045 return (-1);
1046 }
1048 return (0);
1049 } /* }}} int cx_config */
1051 static int cx_init (void) /* {{{ */
1052 {
1053 /* Call this while collectd is still single-threaded to avoid
1054 * initialization issues in libgcrypt. */
1055 curl_global_init (CURL_GLOBAL_SSL);
1056 return (0);
1057 } /* }}} int cx_init */
1059 void module_register (void)
1060 {
1061 plugin_register_complex_config ("curl_xml", cx_config);
1062 plugin_register_init ("curl_xml", cx_init);
1063 } /* void module_register */
1065 /* vim: set sw=2 sts=2 et fdm=marker : */