2 * collectd - src/curl_xml.c
3 * Copyright (C) 2009 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
38 struct cx_values_s /* {{{ */
40 char path[DATA_MAX_NAME_LEN];
43 typedef struct cx_values_s cx_values_t;
46 struct cx_xpath_s /* {{{ */
52 char *instance_prefix;
57 typedef struct cx_xpath_s cx_xpath_t;
74 char curl_errbuf[CURL_ERROR_SIZE];
79 c_avl_tree_t *tree; /* tree of xpath blocks */
81 typedef struct cx_s cx_t; /* }}} */
83 static int cx_read (user_data_t *ud);
84 static int cx_curl_perform (cx_t *db, CURL *curl);
85 static int cx_parse_stats_xml (xmlChar *ptr, cx_t *db);
86 static int cx_submit_statistics(xmlDocPtr doc, xmlXPathContextPtr xpath_ctx,
88 static int cx_submit_xpath_values (char *plugin_instance, xmlXPathContextPtr xpath_ctx,
89 char *base_xpath, cx_xpath_t *xpath);
90 static int cx_if_not_text_node (xmlNodePtr node);
91 static xmlXPathObjectPtr cx_evaluate_xpath (xmlXPathContextPtr xpath_ctx,
93 static int cx_check_type (cx_xpath_t *xpath);
95 static size_t cx_curl_callback (void *buf, /* {{{ */
96 size_t size, size_t nmemb, void *user_data)
98 size_t len = size * nmemb;
104 ERROR ("curl_xml plugin: cx_curl_callback: "
105 "user_data pointer is NULL.");
112 if ((db->buffer_fill + len) >= db->buffer_size)
116 temp = (char *) realloc (db->buffer,
117 db->buffer_fill + len + 1);
120 ERROR ("curl_xml plugin: realloc failed.");
124 db->buffer_size = db->buffer_fill + len + 1;
127 memcpy (db->buffer + db->buffer_fill, (char *) buf, len);
128 db->buffer_fill += len;
129 db->buffer[db->buffer_fill] = 0;
132 } /* }}} size_t cx_curl_callback */
134 static void cx_xpath_free (cx_xpath_t *xpath) /* {{{ */
141 sfree (xpath->instance_prefix);
142 sfree (xpath->instance);
143 sfree (xpath->values);
145 } /* }}} void cx_xpath_free */
147 static void cx_tree_free (c_avl_tree_t *tree) /* {{{ */
152 while (c_avl_pick (tree, (void *) &name, (void *) &value) == 0)
154 cx_xpath_t *key = (cx_xpath_t *)value;
159 cx_tree_free ((c_avl_tree_t *)value);
164 c_avl_destroy (tree);
165 } /* }}} void cx_tree_free */
167 static void cx_free (void *arg) /* {{{ */
171 DEBUG ("curl_xml plugin: cx_free (arg = %p);", arg);
178 if (db->curl != NULL)
179 curl_easy_cleanup (db->curl);
182 if (db->tree != NULL)
183 cx_tree_free (db->tree);
187 sfree (db->instance);
193 sfree (db->credentials);
197 } /* }}} void cx_free */
199 /* Configuration handling functions {{{ */
201 static int cx_config_add_values (const char *name, cx_xpath_t *xpath, /* {{{ */
206 if (ci->values_num < 1)
208 WARNING ("curl_xml plugin: `Values' needs at least one argument.");
212 for (i = 0; i < ci->values_num; i++)
213 if (ci->values[i].type != OCONFIG_TYPE_STRING)
215 WARNING ("curl_xml plugin: `Values' needs only string argument.");
219 sfree (xpath->values);
221 xpath->values_len = 0;
222 xpath->values = (cx_values_t *) malloc (sizeof (cx_values_t) * ci->values_num);
223 if (xpath->values == NULL)
225 xpath->values_len = ci->values_num;
227 /* populate cx_values_t structure */
228 for (i = 0; i < ci->values_num; i++)
230 xpath->values[i].path_len = sizeof (ci->values[i].value.string);
231 sstrncpy (xpath->values[i].path, ci->values[i].value.string, sizeof (xpath->values[i].path));
235 } /* }}} cx_config_add_values */
237 static int cx_config_add_string (const char *name, char **dest, /* {{{ */
240 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
242 WARNING ("curl_xml plugin: `%s' needs exactly one string argument.", name);
247 *dest = strdup (ci->values[0].value.string);
252 } /* }}} int cx_config_add_string */
254 static int cx_config_set_boolean (const char *name, int *dest, /* {{{ */
257 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
259 WARNING ("curl_xml plugin: `%s' needs exactly one boolean argument.", name);
263 *dest = ci->values[0].value.boolean ? 1 : 0;
266 } /* }}} int cx_config_set_boolean */
268 static c_avl_tree_t *cx_avl_create(void) /* {{{ */
270 return c_avl_create ((int (*) (const void *, const void *)) strcmp);
271 } /* }}} cx_avl_create */
273 static int cx_config_add_xpath (cx_t *db, /* {{{ */
280 if ((ci->values_num != 1)
281 || (ci->values[0].type != OCONFIG_TYPE_STRING))
283 WARNING ("curl_xml plugin: The `xpath' block "
284 "needs exactly one string argument.");
288 xpath = (cx_xpath_t *) malloc (sizeof (*xpath));
291 ERROR ("curl_xml plugin: malloc failed.");
294 memset (xpath, 0, sizeof (*xpath));
295 xpath->magic = CX_KEY_MAGIC;
297 if (strcasecmp ("xpath", ci->key) == 0)
299 status = cx_config_add_string ("xpath", &xpath->path, ci);
308 ERROR ("curl_xml plugin: cx_config: "
309 "Invalid key: %s", ci->key);
314 for (i = 0; i < ci->children_num; i++)
316 oconfig_item_t *child = ci->children + i;
318 if (strcasecmp ("Type", child->key) == 0)
319 status = cx_config_add_string ("Type", &xpath->type, child);
320 else if (strcasecmp ("InstancePrefix", child->key) == 0)
321 status = cx_config_add_string ("InstancePrefix", &xpath->instance_prefix, child);
322 else if (strcasecmp ("Instance", child->key) == 0)
323 status = cx_config_add_string ("Instance", &xpath->instance, child);
324 else if (strcasecmp ("Values", child->key) == 0)
325 status = cx_config_add_values ("Values", xpath, child);
328 WARNING ("curl_xml plugin: Option `%s' not allowed here.", child->key);
334 } /* for (i = 0; i < ci->children_num; i++) */
338 if (xpath->type == NULL)
340 WARNING ("curl_xml plugin: `Type' missing in `xpath' block.");
345 } /* while (status == 0) */
352 if (db->tree == NULL)
353 db->tree = cx_avl_create();
359 c_avl_insert (tree, strdup(name), xpath);
362 ERROR ("curl_xml plugin: invalid key: %s", xpath->path);
368 } /* }}} int cx_config_add_xpath */
370 static int cx_init_curl (cx_t *db) /* {{{ */
372 db->curl = curl_easy_init ();
373 if (db->curl == NULL)
375 ERROR ("curl_xml plugin: curl_easy_init failed.");
379 curl_easy_setopt (db->curl, CURLOPT_WRITEFUNCTION, cx_curl_callback);
380 curl_easy_setopt (db->curl, CURLOPT_WRITEDATA, db);
381 curl_easy_setopt (db->curl, CURLOPT_USERAGENT,
382 PACKAGE_NAME"/"PACKAGE_VERSION);
383 curl_easy_setopt (db->curl, CURLOPT_ERRORBUFFER, db->curl_errbuf);
384 curl_easy_setopt (db->curl, CURLOPT_URL, db->url);
386 if (db->user != NULL)
388 size_t credentials_size;
390 credentials_size = strlen (db->user) + 2;
391 if (db->pass != NULL)
392 credentials_size += strlen (db->pass);
394 db->credentials = (char *) malloc (credentials_size);
395 if (db->credentials == NULL)
397 ERROR ("curl_xml plugin: malloc failed.");
401 ssnprintf (db->credentials, credentials_size, "%s:%s",
402 db->user, (db->pass == NULL) ? "" : db->pass);
403 curl_easy_setopt (db->curl, CURLOPT_USERPWD, db->credentials);
406 curl_easy_setopt (db->curl, CURLOPT_SSL_VERIFYPEER, db->verify_peer);
407 curl_easy_setopt (db->curl, CURLOPT_SSL_VERIFYHOST,
408 db->verify_host ? 2 : 0);
409 if (db->cacert != NULL)
410 curl_easy_setopt (db->curl, CURLOPT_CAINFO, db->cacert);
413 } /* }}} int cx_init_curl */
415 static int cx_config_add_url (oconfig_item_t *ci) /* {{{ */
421 if ((ci->values_num != 1)
422 || (ci->values[0].type != OCONFIG_TYPE_STRING))
424 WARNING ("curl_xml plugin: The `URL' block "
425 "needs exactly one string argument.");
429 db = (cx_t *) malloc (sizeof (*db));
432 ERROR ("curl_xml plugin: malloc failed.");
435 memset (db, 0, sizeof (*db));
437 if (strcasecmp ("URL", ci->key) == 0)
439 status = cx_config_add_string ("URL", &db->url, ci);
448 ERROR ("curl_xml plugin: cx_config: "
449 "Invalid key: %s", ci->key);
453 /* Fill the `cx_t' structure.. */
454 for (i = 0; i < ci->children_num; i++)
456 oconfig_item_t *child = ci->children + i;
458 if (strcasecmp ("Instance", child->key) == 0)
459 status = cx_config_add_string ("Instance", &db->instance, child);
460 else if (strcasecmp ("Host", child->key) == 0)
461 status = cx_config_add_string ("Host", &db->host, child);
462 else if (strcasecmp ("User", child->key) == 0)
463 status = cx_config_add_string ("User", &db->user, child);
464 else if (strcasecmp ("Password", child->key) == 0)
465 status = cx_config_add_string ("Password", &db->pass, child);
466 else if (strcasecmp ("VerifyPeer", child->key) == 0)
467 status = cx_config_set_boolean ("VerifyPeer", &db->verify_peer, child);
468 else if (strcasecmp ("VerifyHost", child->key) == 0)
469 status = cx_config_set_boolean ("VerifyHost", &db->verify_host, child);
470 else if (strcasecmp ("CACert", child->key) == 0)
471 status = cx_config_add_string ("CACert", &db->cacert, child);
472 else if (strcasecmp ("xpath", child->key) == 0)
473 status = cx_config_add_xpath (db, child);
476 WARNING ("curl_xml plugin: Option `%s' not allowed here.", child->key);
486 if (db->tree == NULL)
488 WARNING ("curl_xml plugin: No (valid) `Key' block "
489 "within `URL' block `%s'.", db->url);
493 status = cx_init_curl (db);
496 /* If all went well, register this database for reading */
500 char cb_name[DATA_MAX_NAME_LEN];
502 if (db->instance == NULL)
503 db->instance = strdup("default");
505 DEBUG ("curl_xml plugin: Registering new read callback: %s",
508 memset (&ud, 0, sizeof (ud));
509 ud.data = (void *) db;
510 ud.free_func = cx_free;
512 ssnprintf (cb_name, sizeof (cb_name), "curl_xml-%s-%s",
513 db->instance, db->url);
515 plugin_register_complex_read (cb_name, cx_read,
516 /* interval = */ NULL, &ud);
525 } /* }}} int cx_config_add_url */
527 static int cx_config (oconfig_item_t *ci) /* {{{ */
537 for (i = 0; i < ci->children_num; i++)
539 oconfig_item_t *child = ci->children + i;
541 if (strcasecmp ("URL", child->key) == 0)
543 status = cx_config_add_url (child);
551 WARNING ("curl_xml plugin: Option `%s' not allowed here.", child->key);
556 if ((success == 0) && (errors > 0))
558 ERROR ("curl_xml plugin: All statements failed.");
563 } /* }}} int cx_config */
565 /* }}} End of configuration handling functions */
567 static int cx_read (user_data_t *ud) /* {{{ */
571 if ((ud == NULL) || (ud->data == NULL))
573 ERROR ("curl_xml plugin: cx_read: Invalid user data.");
577 db = (cx_t *) ud->data;
579 return cx_curl_perform (db, db->curl);
580 } /* }}} int cx_read */
582 static int cx_curl_perform (cx_t *db, CURL *curl) /* {{{ */
590 status = curl_easy_perform (curl);
592 curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &url);
593 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &rc);
597 ERROR ("curl_xml plugin: curl_easy_perform failed with response code %ld (%s)",
604 ERROR ("curl_xml plugin: curl_easy_perform failed with status %i: %s (%s)",
605 status, db->curl_errbuf, url);
611 status = cx_parse_stats_xml(BAD_CAST ptr, db);
615 } /* }}} int cx_curl_perform */
617 static int cx_parse_stats_xml(xmlChar* xml, cx_t *db) /* {{{ */
621 xmlXPathContextPtr xpath_ctx;
624 doc = xmlParseDoc(xml);
627 ERROR ("curl_xml plugin: Failed to parse the xml document - %s", xml);
631 xpath_ctx = xmlXPathNewContext(doc);
632 if(xpath_ctx == NULL)
634 ERROR ("curl_xml plugin: Failed to create the xml context");
639 status = cx_submit_statistics (doc, xpath_ctx, db);
641 xmlXPathFreeContext(xpath_ctx);
644 } /* }}} cx_parse_stats_xml */
646 static int cx_submit_statistics(xmlDocPtr doc, /* {{{ */
647 xmlXPathContextPtr xpath_ctx, cx_t *db)
649 c_avl_iterator_t *iter;
654 iter = c_avl_get_iterator (db->tree);
655 while (c_avl_iterator_next (iter, (void *) &key, (void *) &value) == 0)
657 if (cx_check_type(value) == -1)
660 if (cx_submit_xpath_values(db->instance, xpath_ctx, key, value) == 0)
661 status = 0; /* we got atleast one success */
662 } /* while (c_avl_iterator_next) */
665 } /* }}} cx_submit_statistics */
667 static int cx_check_type (cx_xpath_t *xpath) /* {{{ */
669 const data_set_t *ds;
671 ds = plugin_get_ds (xpath->type);
674 WARNING ("curl_xml plugin: DataSet `%s' not defined.", xpath->type);
678 if (ds->ds_num != xpath->values_len)
680 WARNING ("curl_xml plugin: DataSet `%s' requires %i values, but config talks about %i",
681 xpath->type, ds->ds_num, xpath->values_len);
686 } /* }}} cx_check_type */
688 static xmlXPathObjectPtr /* {{{ */
689 cx_evaluate_xpath (xmlXPathContextPtr xpath_ctx, xmlChar *expr)
691 xmlXPathObjectPtr xpath_obj;
693 // XXX: When to free this?
694 xpath_obj = xmlXPathEvalExpression(BAD_CAST expr, xpath_ctx);
695 if (xpath_obj == NULL)
697 WARNING ("curl_xml plugin: "
698 "Error unable to evaluate xpath expression \"%s\". Skipping...", expr);
703 } /* }}} cx_evaluate_xpath */
705 static int cx_if_not_text_node (xmlNodePtr node) /* {{{ */
707 if (node->type == XML_TEXT_NODE || node->type == XML_ATTRIBUTE_NODE)
710 WARNING ("curl_xml plugin: "
711 "Node \"%s\" doesn't seem to be a text node. Skipping...", node->name);
713 } /* }}} cx_if_not_text_node */
716 cx_submit_xpath_values (char *plugin_instance,
717 xmlXPathContextPtr xpath_ctx,
718 char *base_xpath, cx_xpath_t *xpath)
727 xmlXPathObjectPtr base_node_obj = NULL;
728 xmlXPathObjectPtr instance_node_obj = NULL;
729 xmlXPathObjectPtr values_node_obj = NULL;
730 xmlNodeSetPtr base_nodes = NULL;
731 xmlNodeSetPtr instance_node = NULL;
732 xmlNodeSetPtr values_node = NULL;
734 value_list_t vl = VALUE_LIST_INIT;
735 const data_set_t *ds;
737 base_node_obj = cx_evaluate_xpath (xpath_ctx, BAD_CAST base_xpath);
738 if (base_node_obj == NULL)
739 return -1; /* error is logged already */
741 base_nodes = base_node_obj->nodesetval;
742 total_nodes = (base_nodes) ? base_nodes->nodeNr : 0;
744 if (total_nodes == 0)
746 ERROR ("curl_xml plugin: "
747 "xpath expression \"%s\" doesn't match any of the node. Skipping...", base_xpath);
748 xmlXPathFreeObject (base_node_obj);
752 /* If base_xpath returned multiple results, then */
753 /* Instance in the xpath block is required */
754 if (total_nodes > 1 && xpath->instance == NULL)
756 ERROR ("curl_xml plugin: "
757 "Instance is must in xpath block since the base xpath expression \"%s\" "
758 "returned multiple results. Skipping the xpath block...", base_xpath);
762 /* set the values for the value_list */
763 ds = plugin_get_ds (xpath->type);
764 vl.values_len = ds->ds_num;
765 sstrncpy (vl.type, xpath->type, sizeof (vl.type));
766 sstrncpy (vl.plugin, "curl_xml", sizeof (vl.plugin));
767 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
768 if (plugin_instance != NULL)
769 sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
771 for (i = 0; i < total_nodes; i++)
773 xpath_ctx->node = base_nodes->nodeTab[i];
775 /* instance has to be an xpath expression */
776 if (xpath->instance != NULL)
778 instance_node_obj = cx_evaluate_xpath (xpath_ctx, BAD_CAST xpath->instance);
779 if (instance_node_obj == NULL)
780 continue; /* error is logged already */
782 instance_node = instance_node_obj->nodesetval;
783 tmp_size = (instance_node) ? instance_node->nodeNr : 0;
785 if ( (tmp_size == 0) && (total_nodes > 1) )
787 WARNING ("curl_xml plugin: "
788 "relative xpath expression for 'Instance' \"%s\" doesn't match "
789 "any of the nodes. Skipping the node - %s",
790 xpath->instance, base_nodes->nodeTab[i]->name);
791 xmlXPathFreeObject (instance_node_obj);
797 WARNING ("curl_xml plugin: "
798 "relative xpath expression for 'Instance' \"%s\" is expected "
799 "to return only one text node. Skipping the node - %s",
800 xpath->instance, base_nodes->nodeTab[i]->name);
801 xmlXPathFreeObject (instance_node_obj);
805 // ignoring the element if other than textnode/attribute
806 if (cx_if_not_text_node(instance_node->nodeTab[0]))
808 WARNING ("curl_xml plugin: "
809 "relative xpath expression \"%s\" is expected to return only text node "
810 "which is not the case. Skipping the node - %s",
811 xpath->instance, base_nodes->nodeTab[i]->name);
812 xmlXPathFreeObject (instance_node_obj);
817 for (j = 0; j < xpath->values_len; j++)
819 values_node_obj = cx_evaluate_xpath (xpath_ctx, BAD_CAST xpath->values[j].path);
820 values_node = values_node_obj->nodesetval;
821 tmp_size = (values_node) ? values_node->nodeNr : 0;
825 WARNING ("curl_xml plugin: "
826 "relative xpath expression \"%s\" doesn't match any of the nodes. "
827 "Skipping...", xpath->values[j].path);
828 xmlXPathFreeObject (values_node_obj);
834 WARNING ("curl_xml plugin: "
835 "relative xpath expression \"%s\" is expected to return "
836 "only one node. Skipping...", xpath->values[j].path);
837 xmlXPathFreeObject (values_node_obj);
841 /* ignoring the element if other than textnode/attribute*/
842 if (cx_if_not_text_node(values_node->nodeTab[0]))
844 WARNING ("curl_xml plugin: "
845 "relative xpath expression \"%s\" is expected to return "
846 "only text/attribute node which is not the case. Skipping...",
847 xpath->values[j].path);
848 xmlXPathFreeObject (values_node_obj);
852 vl.values = (value_t *) malloc (sizeof (value_t) * vl.values_len);
853 if (vl.values == NULL)
855 ERROR ("curl_xml plugin: malloc failed.");
856 xmlXPathFreeObject (base_node_obj);
857 xmlXPathFreeObject (instance_node_obj);
858 xmlXPathFreeObject (values_node_obj);
862 node_value = (char *) xmlNodeGetContent(values_node->nodeTab[0]);
863 switch (ds->ds[j].type)
865 case DS_TYPE_COUNTER:
866 vl.values[j].counter = atoi(node_value);
869 vl.values[j].derive = atoi(node_value);
871 case DS_TYPE_ABSOLUTE:
872 vl.values[j].absolute = atoi(node_value);
875 vl.values[j].absolute = atoi(node_value);
878 if (xpath->instance_prefix != NULL)
880 if (instance_node != NULL)
881 ssnprintf (vl.type_instance, sizeof (vl.type_instance),"%s-%s",
882 xpath->instance_prefix, (char *) xmlNodeGetContent(instance_node->nodeTab[0]));
884 sstrncpy (vl.type_instance, xpath->instance_prefix,
885 sizeof (vl.type_instance));
889 /* If instance_prefix and instance_node are NULL , then */
890 /* don't set the type_instance */
891 if (instance_node != NULL)
892 sstrncpy (vl.type_instance, (char *) xmlNodeGetContent(instance_node->nodeTab[0]),
893 sizeof (vl.type_instance));
897 xmlXPathFreeObject (values_node_obj);
899 /* We have reached here which means that */
900 /* we have got something to work */
904 /* submit the values */
906 plugin_dispatch_values (&vl);
909 if (instance_node_obj != NULL)
910 xmlXPathFreeObject (instance_node_obj);
913 /* free up the allocated memory */
914 xmlXPathFreeObject (base_node_obj);
917 } /* }}} cx_submit_xpath_values */
919 void module_register (void)
921 plugin_register_complex_config ("curl_xml", cx_config);
922 } /* void module_register */
924 /* vim: set sw=2 sts=2 et fdm=marker : */