2 * collectd - src/curl_xml.c
3 * Copyright (C) 2009,2010 Amit Gupta
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.
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.
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
19 * Amit Gupta <amit.gupta221 at gmail.com>
25 #include "configfile.h"
26 #include "utils_avltree.h"
28 #include <libxml/parser.h>
29 #include <libxml/tree.h>
30 #include <libxml/xpath.h>
32 #include <curl/curl.h>
34 #define CX_DEFAULT_HOST "localhost"
35 #define CX_KEY_MAGIC 0x43484b59UL /* CHKY */
36 #define CX_IS_KEY(key) (key)->magic == CX_KEY_MAGIC
39 * Private data structures
41 struct cx_values_s /* {{{ */
43 char path[DATA_MAX_NAME_LEN];
46 typedef struct cx_values_s cx_values_t;
49 struct cx_xpath_s /* {{{ */
55 char *instance_prefix;
60 typedef struct cx_xpath_s cx_xpath_t;
77 char curl_errbuf[CURL_ERROR_SIZE];
82 c_avl_tree_t *tree; /* tree of xpath blocks */
84 typedef struct cx_s cx_t; /* }}} */
89 static size_t cx_curl_callback (void *buf, /* {{{ */
90 size_t size, size_t nmemb, void *user_data)
92 size_t len = size * nmemb;
98 ERROR ("curl_xml plugin: cx_curl_callback: "
99 "user_data pointer is NULL.");
106 if ((db->buffer_fill + len) >= db->buffer_size)
110 temp = (char *) realloc (db->buffer,
111 db->buffer_fill + len + 1);
114 ERROR ("curl_xml plugin: realloc failed.");
118 db->buffer_size = db->buffer_fill + len + 1;
121 memcpy (db->buffer + db->buffer_fill, (char *) buf, len);
122 db->buffer_fill += len;
123 db->buffer[db->buffer_fill] = 0;
126 } /* }}} size_t cx_curl_callback */
128 static void cx_xpath_free (cx_xpath_t *xpath) /* {{{ */
135 sfree (xpath->instance_prefix);
136 sfree (xpath->instance);
137 sfree (xpath->values);
139 } /* }}} void cx_xpath_free */
141 static void cx_tree_free (c_avl_tree_t *tree) /* {{{ */
146 while (c_avl_pick (tree, (void *) &name, (void *) &value) == 0)
148 cx_xpath_t *key = (cx_xpath_t *)value;
153 cx_tree_free ((c_avl_tree_t *)value);
158 c_avl_destroy (tree);
159 } /* }}} void cx_tree_free */
161 static void cx_free (void *arg) /* {{{ */
165 DEBUG ("curl_xml plugin: cx_free (arg = %p);", arg);
172 if (db->curl != NULL)
173 curl_easy_cleanup (db->curl);
176 if (db->tree != NULL)
177 cx_tree_free (db->tree);
181 sfree (db->instance);
187 sfree (db->credentials);
191 } /* }}} void cx_free */
193 static int cx_check_type (cx_xpath_t *xpath) /* {{{ */
195 const data_set_t *ds;
197 ds = plugin_get_ds (xpath->type);
200 WARNING ("curl_xml plugin: DataSet `%s' not defined.", xpath->type);
204 if (ds->ds_num != xpath->values_len)
206 WARNING ("curl_xml plugin: DataSet `%s' requires %i values, but config talks about %i",
207 xpath->type, ds->ds_num, xpath->values_len);
212 } /* }}} cx_check_type */
214 static xmlXPathObjectPtr cx_evaluate_xpath (xmlXPathContextPtr xpath_ctx, /* {{{ */
217 xmlXPathObjectPtr xpath_obj;
219 /* XXX: When to free this? */
220 xpath_obj = xmlXPathEvalExpression(BAD_CAST expr, xpath_ctx);
221 if (xpath_obj == NULL)
223 WARNING ("curl_xml plugin: "
224 "Error unable to evaluate xpath expression \"%s\". Skipping...", expr);
229 } /* }}} cx_evaluate_xpath */
231 static int cx_if_not_text_node (xmlNodePtr node) /* {{{ */
233 if (node->type == XML_TEXT_NODE || node->type == XML_ATTRIBUTE_NODE)
236 WARNING ("curl_xml plugin: "
237 "Node \"%s\" doesn't seem to be a text node. Skipping...", node->name);
239 } /* }}} cx_if_not_text_node */
241 static int cx_handle_single_value_xpath (xmlXPathContextPtr xpath_ctx, /* {{{ */
243 const data_set_t *ds, value_list_t *vl, int index)
245 xmlXPathObjectPtr values_node_obj;
246 xmlNodeSetPtr values_node;
250 values_node_obj = cx_evaluate_xpath (xpath_ctx, BAD_CAST xpath->values[index].path);
251 if (values_node_obj == NULL)
252 return (-1); /* Error already logged. */
254 values_node = values_node_obj->nodesetval;
255 tmp_size = (values_node) ? values_node->nodeNr : 0;
259 WARNING ("curl_xml plugin: "
260 "relative xpath expression \"%s\" doesn't match any of the nodes. "
261 "Skipping...", xpath->values[index].path);
262 xmlXPathFreeObject (values_node_obj);
268 WARNING ("curl_xml plugin: "
269 "relative xpath expression \"%s\" is expected to return "
270 "only one node. Skipping...", xpath->values[index].path);
271 xmlXPathFreeObject (values_node_obj);
275 /* ignoring the element if other than textnode/attribute*/
276 if (cx_if_not_text_node(values_node->nodeTab[0]))
278 WARNING ("curl_xml plugin: "
279 "relative xpath expression \"%s\" is expected to return "
280 "only text/attribute node which is not the case. Skipping...",
281 xpath->values[index].path);
282 xmlXPathFreeObject (values_node_obj);
286 node_value = (char *) xmlNodeGetContent(values_node->nodeTab[0]);
287 switch (ds->ds[index].type)
289 case DS_TYPE_COUNTER:
290 vl->values[index].counter = (counter_t) strtoull (node_value,
291 /* endptr = */ NULL, /* base = */ 0);
294 vl->values[index].derive = (derive_t) strtoll (node_value,
295 /* endptr = */ NULL, /* base = */ 0);
297 case DS_TYPE_ABSOLUTE:
298 vl->values[index].absolute = (absolute_t) strtoull (node_value,
299 /* endptr = */ NULL, /* base = */ 0);
302 vl->values[index].gauge = (gauge_t) strtod (node_value,
303 /* endptr = */ NULL);
307 xmlXPathFreeObject (values_node_obj);
309 /* We have reached here which means that
310 * we have got something to work */
312 } /* }}} int cx_handle_single_value_xpath */
314 static int cx_handle_all_value_xpaths (xmlXPathContextPtr xpath_ctx, /* {{{ */
316 const data_set_t *ds, value_list_t *vl)
318 value_t values[xpath->values_len];
322 assert (xpath->values_len > 0);
323 assert (xpath->values_len == vl->values_len);
324 assert (xpath->values_len == ds->ds_num);
327 for (i = 0; i < xpath->values_len; i++)
329 status = cx_handle_single_value_xpath (xpath_ctx, xpath, ds, vl, i);
331 return (-1); /* An error has been printed. */
332 } /* for (i = 0; i < xpath->values_len; i++) */
334 plugin_dispatch_values (vl);
338 } /* }}} int cx_handle_all_value_xpaths */
340 static int cx_handle_instance_xpath (xmlXPathContextPtr xpath_ctx, /* {{{ */
341 cx_xpath_t *xpath, value_list_t *vl,
344 xmlXPathObjectPtr instance_node_obj = NULL;
345 xmlNodeSetPtr instance_node = NULL;
347 memset (vl->type_instance, 0, sizeof (vl->type_instance));
349 /* If the base xpath returns more than one block, the result is assumed to be
350 * a table. The `Instnce' option is not optional in this case. Check for the
351 * condition and inform the user. */
352 if (is_table && (vl->type_instance == NULL))
354 WARNING ("curl_xml plugin: "
355 "Base-XPath %s is a table (more than one result was returned), "
356 "but no instance-XPath has been defined.",
361 /* instance has to be an xpath expression */
362 if (xpath->instance != NULL)
366 instance_node_obj = cx_evaluate_xpath (xpath_ctx, BAD_CAST xpath->instance);
367 if (instance_node_obj == NULL)
368 return (-1); /* error is logged already */
370 instance_node = instance_node_obj->nodesetval;
371 tmp_size = (instance_node) ? instance_node->nodeNr : 0;
373 if ( (tmp_size == 0) && (is_table) )
375 WARNING ("curl_xml plugin: "
376 "relative xpath expression for 'Instance' \"%s\" doesn't match "
377 "any of the nodes. Skipping the node.", xpath->instance);
378 xmlXPathFreeObject (instance_node_obj);
384 WARNING ("curl_xml plugin: "
385 "relative xpath expression for 'Instance' \"%s\" is expected "
386 "to return only one text node. Skipping the node.", xpath->instance);
387 xmlXPathFreeObject (instance_node_obj);
391 /* ignoring the element if other than textnode/attribute */
392 if (cx_if_not_text_node(instance_node->nodeTab[0]))
394 WARNING ("curl_xml plugin: "
395 "relative xpath expression \"%s\" is expected to return only text node "
396 "which is not the case. Skipping the node.", xpath->instance);
397 xmlXPathFreeObject (instance_node_obj);
400 } /* if (xpath->instance != NULL) */
402 if (xpath->instance_prefix != NULL)
404 if (instance_node != NULL)
405 ssnprintf (vl->type_instance, sizeof (vl->type_instance),"%s-%s",
406 xpath->instance_prefix, (char *) xmlNodeGetContent(instance_node->nodeTab[0]));
408 sstrncpy (vl->type_instance, xpath->instance_prefix,
409 sizeof (vl->type_instance));
413 /* If instance_prefix and instance_node are NULL, then
414 * don't set the type_instance */
415 if (instance_node != NULL)
416 sstrncpy (vl->type_instance, (char *) xmlNodeGetContent(instance_node->nodeTab[0]),
417 sizeof (vl->type_instance));
420 /* Free `instance_node_obj' this late, because `instance_node' points to
421 * somewhere inside this structure. */
422 xmlXPathFreeObject (instance_node_obj);
425 } /* }}} int cx_handle_instance_xpath */
427 static int cx_handle_base_xpath (char *plugin_instance, /* {{{ */
428 xmlXPathContextPtr xpath_ctx,
429 char *base_xpath, cx_xpath_t *xpath)
434 xmlXPathObjectPtr base_node_obj = NULL;
435 xmlNodeSetPtr base_nodes = NULL;
437 value_list_t vl = VALUE_LIST_INIT;
438 const data_set_t *ds;
440 base_node_obj = cx_evaluate_xpath (xpath_ctx, BAD_CAST base_xpath);
441 if (base_node_obj == NULL)
442 return -1; /* error is logged already */
444 base_nodes = base_node_obj->nodesetval;
445 total_nodes = (base_nodes) ? base_nodes->nodeNr : 0;
447 if (total_nodes == 0)
449 ERROR ("curl_xml plugin: "
450 "xpath expression \"%s\" doesn't match any of the node. Skipping...", base_xpath);
451 xmlXPathFreeObject (base_node_obj);
455 /* If base_xpath returned multiple results, then */
456 /* Instance in the xpath block is required */
457 if (total_nodes > 1 && xpath->instance == NULL)
459 ERROR ("curl_xml plugin: "
460 "Instance is must in xpath block since the base xpath expression \"%s\" "
461 "returned multiple results. Skipping the xpath block...", base_xpath);
465 /* set the values for the value_list */
466 ds = plugin_get_ds (xpath->type);
467 vl.values_len = ds->ds_num;
468 sstrncpy (vl.type, xpath->type, sizeof (vl.type));
469 sstrncpy (vl.plugin, "curl_xml", sizeof (vl.plugin));
470 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
471 if (plugin_instance != NULL)
472 sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
474 for (i = 0; i < total_nodes; i++)
478 xpath_ctx->node = base_nodes->nodeTab[i];
480 status = cx_handle_instance_xpath (xpath_ctx, xpath, &vl,
481 /* is_table = */ (total_nodes > 1));
483 continue; /* An error has already been reported. */
485 status = cx_handle_all_value_xpaths (xpath_ctx, xpath, ds, &vl);
487 continue; /* An error has been logged. */
488 } /* for (i = 0; i < total_nodes; i++) */
490 /* free up the allocated memory */
491 xmlXPathFreeObject (base_node_obj);
494 } /* }}} cx_handle_base_xpath */
496 static int cx_handle_parsed_xml(xmlDocPtr doc, /* {{{ */
497 xmlXPathContextPtr xpath_ctx, cx_t *db)
499 c_avl_iterator_t *iter;
504 iter = c_avl_get_iterator (db->tree);
505 while (c_avl_iterator_next (iter, (void *) &key, (void *) &value) == 0)
507 if (cx_check_type(value) == -1)
510 if (cx_handle_base_xpath(db->instance, xpath_ctx, key, value) == 0)
511 status = 0; /* we got atleast one success */
512 } /* while (c_avl_iterator_next) */
515 } /* }}} cx_handle_parsed_xml */
517 static int cx_parse_stats_xml(xmlChar* xml, cx_t *db) /* {{{ */
521 xmlXPathContextPtr xpath_ctx;
524 doc = xmlParseDoc(xml);
527 ERROR ("curl_xml plugin: Failed to parse the xml document - %s", xml);
531 xpath_ctx = xmlXPathNewContext(doc);
532 if(xpath_ctx == NULL)
534 ERROR ("curl_xml plugin: Failed to create the xml context");
539 status = cx_handle_parsed_xml (doc, xpath_ctx, db);
541 xmlXPathFreeContext(xpath_ctx);
544 } /* }}} cx_parse_stats_xml */
546 static int cx_curl_perform (cx_t *db, CURL *curl) /* {{{ */
554 status = curl_easy_perform (curl);
556 curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &url);
557 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &rc);
561 ERROR ("curl_xml plugin: curl_easy_perform failed with response code %ld (%s)",
568 ERROR ("curl_xml plugin: curl_easy_perform failed with status %i: %s (%s)",
569 status, db->curl_errbuf, url);
575 status = cx_parse_stats_xml(BAD_CAST ptr, db);
579 } /* }}} int cx_curl_perform */
581 static int cx_read (user_data_t *ud) /* {{{ */
585 if ((ud == NULL) || (ud->data == NULL))
587 ERROR ("curl_xml plugin: cx_read: Invalid user data.");
591 db = (cx_t *) ud->data;
593 return cx_curl_perform (db, db->curl);
594 } /* }}} int cx_read */
596 /* Configuration handling functions {{{ */
598 static int cx_config_add_values (const char *name, cx_xpath_t *xpath, /* {{{ */
603 if (ci->values_num < 1)
605 WARNING ("curl_xml plugin: `Values' needs at least one argument.");
609 for (i = 0; i < ci->values_num; i++)
610 if (ci->values[i].type != OCONFIG_TYPE_STRING)
612 WARNING ("curl_xml plugin: `Values' needs only string argument.");
616 sfree (xpath->values);
618 xpath->values_len = 0;
619 xpath->values = (cx_values_t *) malloc (sizeof (cx_values_t) * ci->values_num);
620 if (xpath->values == NULL)
622 xpath->values_len = ci->values_num;
624 /* populate cx_values_t structure */
625 for (i = 0; i < ci->values_num; i++)
627 xpath->values[i].path_len = sizeof (ci->values[i].value.string);
628 sstrncpy (xpath->values[i].path, ci->values[i].value.string, sizeof (xpath->values[i].path));
632 } /* }}} cx_config_add_values */
634 static c_avl_tree_t *cx_avl_create(void) /* {{{ */
636 return c_avl_create ((int (*) (const void *, const void *)) strcmp);
637 } /* }}} cx_avl_create */
639 static int cx_config_add_xpath (cx_t *db, /* {{{ */
646 if ((ci->values_num != 1)
647 || (ci->values[0].type != OCONFIG_TYPE_STRING))
649 WARNING ("curl_xml plugin: The `xpath' block "
650 "needs exactly one string argument.");
654 xpath = (cx_xpath_t *) malloc (sizeof (*xpath));
657 ERROR ("curl_xml plugin: malloc failed.");
660 memset (xpath, 0, sizeof (*xpath));
661 xpath->magic = CX_KEY_MAGIC;
663 if (strcasecmp ("xpath", ci->key) == 0)
665 status = cf_util_get_string (ci, &xpath->path);
674 ERROR ("curl_xml plugin: cx_config: "
675 "Invalid key: %s", ci->key);
680 for (i = 0; i < ci->children_num; i++)
682 oconfig_item_t *child = ci->children + i;
684 if (strcasecmp ("Type", child->key) == 0)
685 status = cf_util_get_string (child, &xpath->type);
686 else if (strcasecmp ("InstancePrefix", child->key) == 0)
687 status = cf_util_get_string (child, &xpath->instance_prefix);
688 else if (strcasecmp ("Instance", child->key) == 0)
689 status = cf_util_get_string (child, &xpath->instance);
690 else if (strcasecmp ("Values", child->key) == 0)
691 status = cx_config_add_values ("Values", xpath, child);
694 WARNING ("curl_xml plugin: Option `%s' not allowed here.", child->key);
700 } /* for (i = 0; i < ci->children_num; i++) */
704 if (xpath->type == NULL)
706 WARNING ("curl_xml plugin: `Type' missing in `xpath' block.");
711 } /* while (status == 0) */
718 if (db->tree == NULL)
719 db->tree = cx_avl_create();
725 c_avl_insert (tree, strdup(name), xpath);
728 ERROR ("curl_xml plugin: invalid key: %s", xpath->path);
734 } /* }}} int cx_config_add_xpath */
736 /* Initialize db->curl */
737 static int cx_init_curl (cx_t *db) /* {{{ */
739 db->curl = curl_easy_init ();
740 if (db->curl == NULL)
742 ERROR ("curl_xml plugin: curl_easy_init failed.");
746 curl_easy_setopt (db->curl, CURLOPT_WRITEFUNCTION, cx_curl_callback);
747 curl_easy_setopt (db->curl, CURLOPT_WRITEDATA, db);
748 curl_easy_setopt (db->curl, CURLOPT_USERAGENT,
749 PACKAGE_NAME"/"PACKAGE_VERSION);
750 curl_easy_setopt (db->curl, CURLOPT_ERRORBUFFER, db->curl_errbuf);
751 curl_easy_setopt (db->curl, CURLOPT_URL, db->url);
753 if (db->user != NULL)
755 size_t credentials_size;
757 credentials_size = strlen (db->user) + 2;
758 if (db->pass != NULL)
759 credentials_size += strlen (db->pass);
761 db->credentials = (char *) malloc (credentials_size);
762 if (db->credentials == NULL)
764 ERROR ("curl_xml plugin: malloc failed.");
768 ssnprintf (db->credentials, credentials_size, "%s:%s",
769 db->user, (db->pass == NULL) ? "" : db->pass);
770 curl_easy_setopt (db->curl, CURLOPT_USERPWD, db->credentials);
773 curl_easy_setopt (db->curl, CURLOPT_SSL_VERIFYPEER, db->verify_peer);
774 curl_easy_setopt (db->curl, CURLOPT_SSL_VERIFYHOST,
775 db->verify_host ? 2 : 0);
776 if (db->cacert != NULL)
777 curl_easy_setopt (db->curl, CURLOPT_CAINFO, db->cacert);
780 } /* }}} int cx_init_curl */
782 static int cx_config_add_url (oconfig_item_t *ci) /* {{{ */
788 if ((ci->values_num != 1)
789 || (ci->values[0].type != OCONFIG_TYPE_STRING))
791 WARNING ("curl_xml plugin: The `URL' block "
792 "needs exactly one string argument.");
796 db = (cx_t *) malloc (sizeof (*db));
799 ERROR ("curl_xml plugin: malloc failed.");
802 memset (db, 0, sizeof (*db));
804 if (strcasecmp ("URL", ci->key) == 0)
806 status = cf_util_get_string (ci, &db->url);
815 ERROR ("curl_xml plugin: cx_config: "
816 "Invalid key: %s", ci->key);
820 /* Fill the `cx_t' structure.. */
821 for (i = 0; i < ci->children_num; i++)
823 oconfig_item_t *child = ci->children + i;
825 if (strcasecmp ("Instance", child->key) == 0)
826 status = cf_util_get_string (child, &db->instance);
827 else if (strcasecmp ("Host", child->key) == 0)
828 status = cf_util_get_string (child, &db->host);
829 else if (strcasecmp ("User", child->key) == 0)
830 status = cf_util_get_string (child, &db->user);
831 else if (strcasecmp ("Password", child->key) == 0)
832 status = cf_util_get_string (child, &db->pass);
833 else if (strcasecmp ("VerifyPeer", child->key) == 0)
834 status = cf_util_get_boolean (child, &db->verify_peer);
835 else if (strcasecmp ("VerifyHost", child->key) == 0)
836 status = cf_util_get_boolean (child, &db->verify_host);
837 else if (strcasecmp ("CACert", child->key) == 0)
838 status = cf_util_get_string (child, &db->cacert);
839 else if (strcasecmp ("xpath", child->key) == 0)
840 status = cx_config_add_xpath (db, child);
843 WARNING ("curl_xml plugin: Option `%s' not allowed here.", child->key);
853 if (db->tree == NULL)
855 WARNING ("curl_xml plugin: No (valid) `Key' block "
856 "within `URL' block `%s'.", db->url);
860 status = cx_init_curl (db);
863 /* If all went well, register this database for reading */
867 char cb_name[DATA_MAX_NAME_LEN];
869 if (db->instance == NULL)
870 db->instance = strdup("default");
872 DEBUG ("curl_xml plugin: Registering new read callback: %s",
875 memset (&ud, 0, sizeof (ud));
876 ud.data = (void *) db;
877 ud.free_func = cx_free;
879 ssnprintf (cb_name, sizeof (cb_name), "curl_xml-%s-%s",
880 db->instance, db->url);
882 plugin_register_complex_read (cb_name, cx_read,
883 /* interval = */ NULL, &ud);
892 } /* }}} int cx_config_add_url */
894 /* }}} End of configuration handling functions */
896 static int cx_config (oconfig_item_t *ci) /* {{{ */
906 for (i = 0; i < ci->children_num; i++)
908 oconfig_item_t *child = ci->children + i;
910 if (strcasecmp ("URL", child->key) == 0)
912 status = cx_config_add_url (child);
920 WARNING ("curl_xml plugin: Option `%s' not allowed here.", child->key);
925 if ((success == 0) && (errors > 0))
927 ERROR ("curl_xml plugin: All statements failed.");
932 } /* }}} int cx_config */
934 void module_register (void)
936 plugin_register_complex_config ("curl_xml", cx_config);
937 } /* void module_register */
939 /* vim: set sw=2 sts=2 et fdm=marker : */