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