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_submit_xpath_values (char *plugin_instance, /* {{{ */
242 xmlXPathContextPtr xpath_ctx,
243 char *base_xpath, cx_xpath_t *xpath)
252 xmlXPathObjectPtr base_node_obj = NULL;
253 xmlXPathObjectPtr instance_node_obj = NULL;
254 xmlNodeSetPtr base_nodes = NULL;
255 xmlNodeSetPtr instance_node = NULL;
256 xmlNodeSetPtr values_node = NULL;
258 value_list_t vl = VALUE_LIST_INIT;
259 const data_set_t *ds;
261 base_node_obj = cx_evaluate_xpath (xpath_ctx, BAD_CAST base_xpath);
262 if (base_node_obj == NULL)
263 return -1; /* error is logged already */
265 base_nodes = base_node_obj->nodesetval;
266 total_nodes = (base_nodes) ? base_nodes->nodeNr : 0;
268 if (total_nodes == 0)
270 ERROR ("curl_xml plugin: "
271 "xpath expression \"%s\" doesn't match any of the node. Skipping...", base_xpath);
272 xmlXPathFreeObject (base_node_obj);
276 /* If base_xpath returned multiple results, then */
277 /* Instance in the xpath block is required */
278 if (total_nodes > 1 && xpath->instance == NULL)
280 ERROR ("curl_xml plugin: "
281 "Instance is must in xpath block since the base xpath expression \"%s\" "
282 "returned multiple results. Skipping the xpath block...", base_xpath);
286 /* set the values for the value_list */
287 ds = plugin_get_ds (xpath->type);
288 vl.values_len = ds->ds_num;
289 sstrncpy (vl.type, xpath->type, sizeof (vl.type));
290 sstrncpy (vl.plugin, "curl_xml", sizeof (vl.plugin));
291 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
292 if (plugin_instance != NULL)
293 sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
295 for (i = 0; i < total_nodes; i++)
297 xpath_ctx->node = base_nodes->nodeTab[i];
299 /* instance has to be an xpath expression */
300 if (xpath->instance != NULL)
302 instance_node_obj = cx_evaluate_xpath (xpath_ctx, BAD_CAST xpath->instance);
303 if (instance_node_obj == NULL)
304 continue; /* error is logged already */
306 instance_node = instance_node_obj->nodesetval;
307 tmp_size = (instance_node) ? instance_node->nodeNr : 0;
309 if ( (tmp_size == 0) && (total_nodes > 1) )
311 WARNING ("curl_xml plugin: "
312 "relative xpath expression for 'Instance' \"%s\" doesn't match "
313 "any of the nodes. Skipping the node - %s",
314 xpath->instance, base_nodes->nodeTab[i]->name);
315 xmlXPathFreeObject (instance_node_obj);
321 WARNING ("curl_xml plugin: "
322 "relative xpath expression for 'Instance' \"%s\" is expected "
323 "to return only one text node. Skipping the node - %s",
324 xpath->instance, base_nodes->nodeTab[i]->name);
325 xmlXPathFreeObject (instance_node_obj);
329 /* ignoring the element if other than textnode/attribute */
330 if (cx_if_not_text_node(instance_node->nodeTab[0]))
332 WARNING ("curl_xml plugin: "
333 "relative xpath expression \"%s\" is expected to return only text node "
334 "which is not the case. Skipping the node - %s",
335 xpath->instance, base_nodes->nodeTab[i]->name);
336 xmlXPathFreeObject (instance_node_obj);
339 } /* if (xpath->instance != NULL) */
341 for (j = 0; j < xpath->values_len; j++)
343 xmlXPathObjectPtr values_node_obj;
345 values_node_obj = cx_evaluate_xpath (xpath_ctx, BAD_CAST xpath->values[j].path);
346 if (values_node_obj == NULL)
347 continue; /* Error already logged. */
349 values_node = values_node_obj->nodesetval;
350 tmp_size = (values_node) ? values_node->nodeNr : 0;
354 WARNING ("curl_xml plugin: "
355 "relative xpath expression \"%s\" doesn't match any of the nodes. "
356 "Skipping...", xpath->values[j].path);
357 xmlXPathFreeObject (values_node_obj);
363 WARNING ("curl_xml plugin: "
364 "relative xpath expression \"%s\" is expected to return "
365 "only one node. Skipping...", xpath->values[j].path);
366 xmlXPathFreeObject (values_node_obj);
370 /* ignoring the element if other than textnode/attribute*/
371 if (cx_if_not_text_node(values_node->nodeTab[0]))
373 WARNING ("curl_xml plugin: "
374 "relative xpath expression \"%s\" is expected to return "
375 "only text/attribute node which is not the case. Skipping...",
376 xpath->values[j].path);
377 xmlXPathFreeObject (values_node_obj);
381 vl.values = (value_t *) malloc (sizeof (value_t) * vl.values_len);
382 if (vl.values == NULL)
384 ERROR ("curl_xml plugin: malloc failed.");
385 xmlXPathFreeObject (base_node_obj);
386 xmlXPathFreeObject (instance_node_obj);
387 xmlXPathFreeObject (values_node_obj);
391 node_value = (char *) xmlNodeGetContent(values_node->nodeTab[0]);
392 switch (ds->ds[j].type)
394 case DS_TYPE_COUNTER:
395 vl.values[j].counter = atoi(node_value);
398 vl.values[j].derive = atoi(node_value);
400 case DS_TYPE_ABSOLUTE:
401 vl.values[j].absolute = atoi(node_value);
404 vl.values[j].absolute = atoi(node_value);
407 if (xpath->instance_prefix != NULL)
409 if (instance_node != NULL)
410 ssnprintf (vl.type_instance, sizeof (vl.type_instance),"%s-%s",
411 xpath->instance_prefix, (char *) xmlNodeGetContent(instance_node->nodeTab[0]));
413 sstrncpy (vl.type_instance, xpath->instance_prefix,
414 sizeof (vl.type_instance));
418 /* If instance_prefix and instance_node are NULL, then
419 * don't set the type_instance */
420 if (instance_node != NULL)
421 sstrncpy (vl.type_instance, (char *) xmlNodeGetContent(instance_node->nodeTab[0]),
422 sizeof (vl.type_instance));
424 vl.type_instance[0] = 0;
428 xmlXPathFreeObject (values_node_obj);
430 /* We have reached here which means that
431 * we have got something to work */
433 } /* for (j = 0; j < xpath->values_len; j++) */
435 /* submit the values */
437 plugin_dispatch_values (&vl);
440 if (instance_node_obj != NULL)
441 xmlXPathFreeObject (instance_node_obj);
444 /* free up the allocated memory */
445 xmlXPathFreeObject (base_node_obj);
448 } /* }}} cx_submit_xpath_values */
450 static int cx_submit_statistics(xmlDocPtr doc, /* {{{ */
451 xmlXPathContextPtr xpath_ctx, cx_t *db)
453 c_avl_iterator_t *iter;
458 iter = c_avl_get_iterator (db->tree);
459 while (c_avl_iterator_next (iter, (void *) &key, (void *) &value) == 0)
461 if (cx_check_type(value) == -1)
464 if (cx_submit_xpath_values(db->instance, xpath_ctx, key, value) == 0)
465 status = 0; /* we got atleast one success */
466 } /* while (c_avl_iterator_next) */
469 } /* }}} cx_submit_statistics */
471 static int cx_parse_stats_xml(xmlChar* xml, cx_t *db) /* {{{ */
475 xmlXPathContextPtr xpath_ctx;
478 doc = xmlParseDoc(xml);
481 ERROR ("curl_xml plugin: Failed to parse the xml document - %s", xml);
485 xpath_ctx = xmlXPathNewContext(doc);
486 if(xpath_ctx == NULL)
488 ERROR ("curl_xml plugin: Failed to create the xml context");
493 status = cx_submit_statistics (doc, xpath_ctx, db);
495 xmlXPathFreeContext(xpath_ctx);
498 } /* }}} cx_parse_stats_xml */
500 static int cx_curl_perform (cx_t *db, CURL *curl) /* {{{ */
508 status = curl_easy_perform (curl);
510 curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &url);
511 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &rc);
515 ERROR ("curl_xml plugin: curl_easy_perform failed with response code %ld (%s)",
522 ERROR ("curl_xml plugin: curl_easy_perform failed with status %i: %s (%s)",
523 status, db->curl_errbuf, url);
529 status = cx_parse_stats_xml(BAD_CAST ptr, db);
533 } /* }}} int cx_curl_perform */
535 static int cx_read (user_data_t *ud) /* {{{ */
539 if ((ud == NULL) || (ud->data == NULL))
541 ERROR ("curl_xml plugin: cx_read: Invalid user data.");
545 db = (cx_t *) ud->data;
547 return cx_curl_perform (db, db->curl);
548 } /* }}} int cx_read */
550 /* Configuration handling functions {{{ */
552 static int cx_config_add_values (const char *name, cx_xpath_t *xpath, /* {{{ */
557 if (ci->values_num < 1)
559 WARNING ("curl_xml plugin: `Values' needs at least one argument.");
563 for (i = 0; i < ci->values_num; i++)
564 if (ci->values[i].type != OCONFIG_TYPE_STRING)
566 WARNING ("curl_xml plugin: `Values' needs only string argument.");
570 sfree (xpath->values);
572 xpath->values_len = 0;
573 xpath->values = (cx_values_t *) malloc (sizeof (cx_values_t) * ci->values_num);
574 if (xpath->values == NULL)
576 xpath->values_len = ci->values_num;
578 /* populate cx_values_t structure */
579 for (i = 0; i < ci->values_num; i++)
581 xpath->values[i].path_len = sizeof (ci->values[i].value.string);
582 sstrncpy (xpath->values[i].path, ci->values[i].value.string, sizeof (xpath->values[i].path));
586 } /* }}} cx_config_add_values */
588 static c_avl_tree_t *cx_avl_create(void) /* {{{ */
590 return c_avl_create ((int (*) (const void *, const void *)) strcmp);
591 } /* }}} cx_avl_create */
593 static int cx_config_add_xpath (cx_t *db, /* {{{ */
600 if ((ci->values_num != 1)
601 || (ci->values[0].type != OCONFIG_TYPE_STRING))
603 WARNING ("curl_xml plugin: The `xpath' block "
604 "needs exactly one string argument.");
608 xpath = (cx_xpath_t *) malloc (sizeof (*xpath));
611 ERROR ("curl_xml plugin: malloc failed.");
614 memset (xpath, 0, sizeof (*xpath));
615 xpath->magic = CX_KEY_MAGIC;
617 if (strcasecmp ("xpath", ci->key) == 0)
619 status = cf_util_get_string (ci, &xpath->path);
628 ERROR ("curl_xml plugin: cx_config: "
629 "Invalid key: %s", ci->key);
634 for (i = 0; i < ci->children_num; i++)
636 oconfig_item_t *child = ci->children + i;
638 if (strcasecmp ("Type", child->key) == 0)
639 status = cf_util_get_string (child, &xpath->type);
640 else if (strcasecmp ("InstancePrefix", child->key) == 0)
641 status = cf_util_get_string (child, &xpath->instance_prefix);
642 else if (strcasecmp ("Instance", child->key) == 0)
643 status = cf_util_get_string (child, &xpath->instance);
644 else if (strcasecmp ("Values", child->key) == 0)
645 status = cx_config_add_values ("Values", xpath, child);
648 WARNING ("curl_xml plugin: Option `%s' not allowed here.", child->key);
654 } /* for (i = 0; i < ci->children_num; i++) */
658 if (xpath->type == NULL)
660 WARNING ("curl_xml plugin: `Type' missing in `xpath' block.");
665 } /* while (status == 0) */
672 if (db->tree == NULL)
673 db->tree = cx_avl_create();
679 c_avl_insert (tree, strdup(name), xpath);
682 ERROR ("curl_xml plugin: invalid key: %s", xpath->path);
688 } /* }}} int cx_config_add_xpath */
690 /* Initialize db->curl */
691 static int cx_init_curl (cx_t *db) /* {{{ */
693 db->curl = curl_easy_init ();
694 if (db->curl == NULL)
696 ERROR ("curl_xml plugin: curl_easy_init failed.");
700 curl_easy_setopt (db->curl, CURLOPT_WRITEFUNCTION, cx_curl_callback);
701 curl_easy_setopt (db->curl, CURLOPT_WRITEDATA, db);
702 curl_easy_setopt (db->curl, CURLOPT_USERAGENT,
703 PACKAGE_NAME"/"PACKAGE_VERSION);
704 curl_easy_setopt (db->curl, CURLOPT_ERRORBUFFER, db->curl_errbuf);
705 curl_easy_setopt (db->curl, CURLOPT_URL, db->url);
707 if (db->user != NULL)
709 size_t credentials_size;
711 credentials_size = strlen (db->user) + 2;
712 if (db->pass != NULL)
713 credentials_size += strlen (db->pass);
715 db->credentials = (char *) malloc (credentials_size);
716 if (db->credentials == NULL)
718 ERROR ("curl_xml plugin: malloc failed.");
722 ssnprintf (db->credentials, credentials_size, "%s:%s",
723 db->user, (db->pass == NULL) ? "" : db->pass);
724 curl_easy_setopt (db->curl, CURLOPT_USERPWD, db->credentials);
727 curl_easy_setopt (db->curl, CURLOPT_SSL_VERIFYPEER, db->verify_peer);
728 curl_easy_setopt (db->curl, CURLOPT_SSL_VERIFYHOST,
729 db->verify_host ? 2 : 0);
730 if (db->cacert != NULL)
731 curl_easy_setopt (db->curl, CURLOPT_CAINFO, db->cacert);
734 } /* }}} int cx_init_curl */
736 static int cx_config_add_url (oconfig_item_t *ci) /* {{{ */
742 if ((ci->values_num != 1)
743 || (ci->values[0].type != OCONFIG_TYPE_STRING))
745 WARNING ("curl_xml plugin: The `URL' block "
746 "needs exactly one string argument.");
750 db = (cx_t *) malloc (sizeof (*db));
753 ERROR ("curl_xml plugin: malloc failed.");
756 memset (db, 0, sizeof (*db));
758 if (strcasecmp ("URL", ci->key) == 0)
760 status = cf_util_get_string (ci, &db->url);
769 ERROR ("curl_xml plugin: cx_config: "
770 "Invalid key: %s", ci->key);
774 /* Fill the `cx_t' structure.. */
775 for (i = 0; i < ci->children_num; i++)
777 oconfig_item_t *child = ci->children + i;
779 if (strcasecmp ("Instance", child->key) == 0)
780 status = cf_util_get_string (child, &db->instance);
781 else if (strcasecmp ("Host", child->key) == 0)
782 status = cf_util_get_string (child, &db->host);
783 else if (strcasecmp ("User", child->key) == 0)
784 status = cf_util_get_string (child, &db->user);
785 else if (strcasecmp ("Password", child->key) == 0)
786 status = cf_util_get_string (child, &db->pass);
787 else if (strcasecmp ("VerifyPeer", child->key) == 0)
788 status = cf_util_get_boolean (child, &db->verify_peer);
789 else if (strcasecmp ("VerifyHost", child->key) == 0)
790 status = cf_util_get_boolean (child, &db->verify_host);
791 else if (strcasecmp ("CACert", child->key) == 0)
792 status = cf_util_get_string (child, &db->cacert);
793 else if (strcasecmp ("xpath", child->key) == 0)
794 status = cx_config_add_xpath (db, child);
797 WARNING ("curl_xml plugin: Option `%s' not allowed here.", child->key);
807 if (db->tree == NULL)
809 WARNING ("curl_xml plugin: No (valid) `Key' block "
810 "within `URL' block `%s'.", db->url);
814 status = cx_init_curl (db);
817 /* If all went well, register this database for reading */
821 char cb_name[DATA_MAX_NAME_LEN];
823 if (db->instance == NULL)
824 db->instance = strdup("default");
826 DEBUG ("curl_xml plugin: Registering new read callback: %s",
829 memset (&ud, 0, sizeof (ud));
830 ud.data = (void *) db;
831 ud.free_func = cx_free;
833 ssnprintf (cb_name, sizeof (cb_name), "curl_xml-%s-%s",
834 db->instance, db->url);
836 plugin_register_complex_read (cb_name, cx_read,
837 /* interval = */ NULL, &ud);
846 } /* }}} int cx_config_add_url */
848 /* }}} End of configuration handling functions */
850 static int cx_config (oconfig_item_t *ci) /* {{{ */
860 for (i = 0; i < ci->children_num; i++)
862 oconfig_item_t *child = ci->children + i;
864 if (strcasecmp ("URL", child->key) == 0)
866 status = cx_config_add_url (child);
874 WARNING ("curl_xml plugin: Option `%s' not allowed here.", child->key);
879 if ((success == 0) && (errors > 0))
881 ERROR ("curl_xml plugin: All statements failed.");
886 } /* }}} int cx_config */
888 void module_register (void)
890 plugin_register_complex_config ("curl_xml", cx_config);
891 } /* void module_register */
893 /* vim: set sw=2 sts=2 et fdm=marker : */