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_llist.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"
37 * Private data structures
39 struct cx_values_s /* {{{ */
41 char path[DATA_MAX_NAME_LEN];
44 typedef struct cx_values_s cx_values_t;
47 struct cx_xpath_s /* {{{ */
53 char *instance_prefix;
58 typedef struct cx_xpath_s cx_xpath_t;
74 struct curl_slist *headers;
77 char curl_errbuf[CURL_ERROR_SIZE];
82 llist_t *list; /* list 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_list_free (llist_t *list) /* {{{ */
145 le = llist_head (list);
153 cx_xpath_free (le->value);
158 llist_destroy (list);
160 } /* }}} void cx_list_free */
162 static void cx_free (void *arg) /* {{{ */
166 DEBUG ("curl_xml plugin: cx_free (arg = %p);", arg);
173 if (db->curl != NULL)
174 curl_easy_cleanup (db->curl);
177 if (db->list != NULL)
178 cx_list_free (db->list);
181 sfree (db->instance);
187 sfree (db->credentials);
189 sfree (db->post_body);
190 curl_slist_free_all (db->headers);
193 } /* }}} void cx_free */
195 static int cx_config_append_string (const char *name, struct curl_slist **dest, /* {{{ */
198 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
200 WARNING ("curl_xml plugin: `%s' needs exactly one string argument.", name);
204 *dest = curl_slist_append(*dest, ci->values[0].value.string);
209 } /* }}} int cx_config_append_string */
211 static int cx_check_type (const data_set_t *ds, cx_xpath_t *xpath) /* {{{ */
215 WARNING ("curl_xml plugin: DataSet `%s' not defined.", xpath->type);
219 if (ds->ds_num != xpath->values_len)
221 WARNING ("curl_xml plugin: DataSet `%s' requires %i values, but config talks about %i",
222 xpath->type, ds->ds_num, xpath->values_len);
227 } /* }}} cx_check_type */
229 static xmlXPathObjectPtr cx_evaluate_xpath (xmlXPathContextPtr xpath_ctx, /* {{{ */
232 xmlXPathObjectPtr xpath_obj;
234 /* XXX: When to free this? */
235 xpath_obj = xmlXPathEvalExpression(BAD_CAST expr, xpath_ctx);
236 if (xpath_obj == NULL)
238 WARNING ("curl_xml plugin: "
239 "Error unable to evaluate xpath expression \"%s\". Skipping...", expr);
244 } /* }}} cx_evaluate_xpath */
246 static int cx_if_not_text_node (xmlNodePtr node) /* {{{ */
248 if (node->type == XML_TEXT_NODE || node->type == XML_ATTRIBUTE_NODE ||
249 node->type == XML_ELEMENT_NODE)
252 WARNING ("curl_xml plugin: "
253 "Node \"%s\" doesn't seem to be a text node. Skipping...", node->name);
255 } /* }}} cx_if_not_text_node */
257 static int cx_handle_single_value_xpath (xmlXPathContextPtr xpath_ctx, /* {{{ */
259 const data_set_t *ds, value_list_t *vl, int index)
261 xmlXPathObjectPtr values_node_obj;
262 xmlNodeSetPtr values_node;
266 values_node_obj = cx_evaluate_xpath (xpath_ctx, BAD_CAST xpath->values[index].path);
267 if (values_node_obj == NULL)
268 return (-1); /* Error already logged. */
270 values_node = values_node_obj->nodesetval;
271 tmp_size = (values_node) ? values_node->nodeNr : 0;
275 WARNING ("curl_xml plugin: "
276 "relative xpath expression \"%s\" doesn't match any of the nodes. "
277 "Skipping...", xpath->values[index].path);
278 xmlXPathFreeObject (values_node_obj);
284 WARNING ("curl_xml plugin: "
285 "relative xpath expression \"%s\" is expected to return "
286 "only one node. Skipping...", xpath->values[index].path);
287 xmlXPathFreeObject (values_node_obj);
291 /* ignoring the element if other than textnode/attribute*/
292 if (cx_if_not_text_node(values_node->nodeTab[0]))
294 WARNING ("curl_xml plugin: "
295 "relative xpath expression \"%s\" is expected to return "
296 "only text/attribute node which is not the case. Skipping...",
297 xpath->values[index].path);
298 xmlXPathFreeObject (values_node_obj);
302 node_value = (char *) xmlNodeGetContent(values_node->nodeTab[0]);
303 switch (ds->ds[index].type)
305 case DS_TYPE_COUNTER:
306 vl->values[index].counter = (counter_t) strtoull (node_value,
307 /* endptr = */ NULL, /* base = */ 0);
310 vl->values[index].derive = (derive_t) strtoll (node_value,
311 /* endptr = */ NULL, /* base = */ 0);
313 case DS_TYPE_ABSOLUTE:
314 vl->values[index].absolute = (absolute_t) strtoull (node_value,
315 /* endptr = */ NULL, /* base = */ 0);
318 vl->values[index].gauge = (gauge_t) strtod (node_value,
319 /* endptr = */ NULL);
323 xmlXPathFreeObject (values_node_obj);
325 /* We have reached here which means that
326 * we have got something to work */
328 } /* }}} int cx_handle_single_value_xpath */
330 static int cx_handle_all_value_xpaths (xmlXPathContextPtr xpath_ctx, /* {{{ */
332 const data_set_t *ds, value_list_t *vl)
334 value_t values[xpath->values_len];
338 assert (xpath->values_len > 0);
339 assert (xpath->values_len == vl->values_len);
340 assert (xpath->values_len == ds->ds_num);
343 for (i = 0; i < xpath->values_len; i++)
345 status = cx_handle_single_value_xpath (xpath_ctx, xpath, ds, vl, i);
347 return (-1); /* An error has been printed. */
348 } /* for (i = 0; i < xpath->values_len; i++) */
350 plugin_dispatch_values (vl);
354 } /* }}} int cx_handle_all_value_xpaths */
356 static int cx_handle_instance_xpath (xmlXPathContextPtr xpath_ctx, /* {{{ */
357 cx_xpath_t *xpath, value_list_t *vl,
360 xmlXPathObjectPtr instance_node_obj = NULL;
361 xmlNodeSetPtr instance_node = NULL;
363 memset (vl->type_instance, 0, sizeof (vl->type_instance));
365 /* If the base xpath returns more than one block, the result is assumed to be
366 * a table. The `Instnce' option is not optional in this case. Check for the
367 * condition and inform the user. */
368 if (is_table && (vl->type_instance == NULL))
370 WARNING ("curl_xml plugin: "
371 "Base-XPath %s is a table (more than one result was returned), "
372 "but no instance-XPath has been defined.",
377 /* instance has to be an xpath expression */
378 if (xpath->instance != NULL)
382 instance_node_obj = cx_evaluate_xpath (xpath_ctx, BAD_CAST xpath->instance);
383 if (instance_node_obj == NULL)
384 return (-1); /* error is logged already */
386 instance_node = instance_node_obj->nodesetval;
387 tmp_size = (instance_node) ? instance_node->nodeNr : 0;
391 WARNING ("curl_xml plugin: "
392 "relative xpath expression for 'InstanceFrom' \"%s\" doesn't match "
393 "any of the nodes. Skipping the node.", xpath->instance);
394 xmlXPathFreeObject (instance_node_obj);
400 WARNING ("curl_xml plugin: "
401 "relative xpath expression for 'InstanceFrom' \"%s\" is expected "
402 "to return only one text node. Skipping the node.", xpath->instance);
403 xmlXPathFreeObject (instance_node_obj);
407 /* ignoring the element if other than textnode/attribute */
408 if (cx_if_not_text_node(instance_node->nodeTab[0]))
410 WARNING ("curl_xml plugin: "
411 "relative xpath expression \"%s\" is expected to return only text node "
412 "which is not the case. Skipping the node.", xpath->instance);
413 xmlXPathFreeObject (instance_node_obj);
416 } /* if (xpath->instance != NULL) */
418 if (xpath->instance_prefix != NULL)
420 if (instance_node != NULL)
421 ssnprintf (vl->type_instance, sizeof (vl->type_instance),"%s%s",
422 xpath->instance_prefix, (char *) xmlNodeGetContent(instance_node->nodeTab[0]));
424 sstrncpy (vl->type_instance, xpath->instance_prefix,
425 sizeof (vl->type_instance));
429 /* If instance_prefix and instance_node are NULL, then
430 * don't set the type_instance */
431 if (instance_node != NULL)
432 sstrncpy (vl->type_instance, (char *) xmlNodeGetContent(instance_node->nodeTab[0]),
433 sizeof (vl->type_instance));
436 /* Free `instance_node_obj' this late, because `instance_node' points to
437 * somewhere inside this structure. */
438 xmlXPathFreeObject (instance_node_obj);
441 } /* }}} int cx_handle_instance_xpath */
443 static int cx_handle_base_xpath (char const *plugin_instance, /* {{{ */
445 xmlXPathContextPtr xpath_ctx, const data_set_t *ds,
446 char *base_xpath, cx_xpath_t *xpath)
451 xmlXPathObjectPtr base_node_obj = NULL;
452 xmlNodeSetPtr base_nodes = NULL;
454 value_list_t vl = VALUE_LIST_INIT;
456 base_node_obj = cx_evaluate_xpath (xpath_ctx, BAD_CAST base_xpath);
457 if (base_node_obj == NULL)
458 return -1; /* error is logged already */
460 base_nodes = base_node_obj->nodesetval;
461 total_nodes = (base_nodes) ? base_nodes->nodeNr : 0;
463 if (total_nodes == 0)
465 ERROR ("curl_xml plugin: "
466 "xpath expression \"%s\" doesn't match any of the nodes. "
467 "Skipping the xpath block...", base_xpath);
468 xmlXPathFreeObject (base_node_obj);
472 /* If base_xpath returned multiple results, then */
473 /* Instance in the xpath block is required */
474 if (total_nodes > 1 && xpath->instance == NULL)
476 ERROR ("curl_xml plugin: "
477 "InstanceFrom is must in xpath block since the base xpath expression \"%s\" "
478 "returned multiple results. Skipping the xpath block...", base_xpath);
482 /* set the values for the value_list */
483 vl.values_len = ds->ds_num;
484 sstrncpy (vl.type, xpath->type, sizeof (vl.type));
485 sstrncpy (vl.plugin, "curl_xml", sizeof (vl.plugin));
486 sstrncpy (vl.host, (host != NULL) ? host : hostname_g, sizeof (vl.host));
487 if (plugin_instance != NULL)
488 sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
490 for (i = 0; i < total_nodes; i++)
494 xpath_ctx->node = base_nodes->nodeTab[i];
496 status = cx_handle_instance_xpath (xpath_ctx, xpath, &vl,
497 /* is_table = */ (total_nodes > 1));
499 continue; /* An error has already been reported. */
501 status = cx_handle_all_value_xpaths (xpath_ctx, xpath, ds, &vl);
503 continue; /* An error has been logged. */
504 } /* for (i = 0; i < total_nodes; i++) */
506 /* free up the allocated memory */
507 xmlXPathFreeObject (base_node_obj);
510 } /* }}} cx_handle_base_xpath */
512 static int cx_handle_parsed_xml(xmlDocPtr doc, /* {{{ */
513 xmlXPathContextPtr xpath_ctx, cx_t *db)
516 const data_set_t *ds;
521 le = llist_head (db->list);
525 xpath = (cx_xpath_t *) le->value;
526 ds = plugin_get_ds (xpath->type);
528 if ( (cx_check_type(ds, xpath) == 0) &&
529 (cx_handle_base_xpath(db->instance, db->host,
530 xpath_ctx, ds, le->key, xpath) == 0) )
531 status = 0; /* we got atleast one success */
534 } /* while (le != NULL) */
537 } /* }}} cx_handle_parsed_xml */
539 static int cx_parse_stats_xml(xmlChar* xml, cx_t *db) /* {{{ */
543 xmlXPathContextPtr xpath_ctx;
546 doc = xmlParseDoc(xml);
549 ERROR ("curl_xml plugin: Failed to parse the xml document - %s", xml);
553 xpath_ctx = xmlXPathNewContext(doc);
554 if(xpath_ctx == NULL)
556 ERROR ("curl_xml plugin: Failed to create the xml context");
561 status = cx_handle_parsed_xml (doc, xpath_ctx, db);
563 xmlXPathFreeContext(xpath_ctx);
566 } /* }}} cx_parse_stats_xml */
568 static int cx_curl_perform (cx_t *db, CURL *curl) /* {{{ */
576 status = curl_easy_perform (curl);
577 if (status != CURLE_OK)
579 ERROR ("curl_xml plugin: curl_easy_perform failed with status %i: %s (%s)",
580 status, db->curl_errbuf, url);
584 curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &url);
585 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &rc);
587 /* The response code is zero if a non-HTTP transport was used. */
588 if ((rc != 0) && (rc != 200))
590 ERROR ("curl_xml plugin: curl_easy_perform failed with response code %ld (%s)",
597 status = cx_parse_stats_xml(BAD_CAST ptr, db);
601 } /* }}} int cx_curl_perform */
603 static int cx_read (user_data_t *ud) /* {{{ */
607 if ((ud == NULL) || (ud->data == NULL))
609 ERROR ("curl_xml plugin: cx_read: Invalid user data.");
613 db = (cx_t *) ud->data;
615 return cx_curl_perform (db, db->curl);
616 } /* }}} int cx_read */
618 /* Configuration handling functions {{{ */
620 static int cx_config_add_values (const char *name, cx_xpath_t *xpath, /* {{{ */
625 if (ci->values_num < 1)
627 WARNING ("curl_xml plugin: `ValuesFrom' needs at least one argument.");
631 for (i = 0; i < ci->values_num; i++)
632 if (ci->values[i].type != OCONFIG_TYPE_STRING)
634 WARNING ("curl_xml plugin: `ValuesFrom' needs only string argument.");
638 sfree (xpath->values);
640 xpath->values_len = 0;
641 xpath->values = (cx_values_t *) malloc (sizeof (cx_values_t) * ci->values_num);
642 if (xpath->values == NULL)
644 xpath->values_len = ci->values_num;
646 /* populate cx_values_t structure */
647 for (i = 0; i < ci->values_num; i++)
649 xpath->values[i].path_len = sizeof (ci->values[i].value.string);
650 sstrncpy (xpath->values[i].path, ci->values[i].value.string, sizeof (xpath->values[i].path));
654 } /* }}} cx_config_add_values */
656 static int cx_config_add_xpath (cx_t *db, /* {{{ */
663 xpath = (cx_xpath_t *) malloc (sizeof (*xpath));
666 ERROR ("curl_xml plugin: malloc failed.");
669 memset (xpath, 0, sizeof (*xpath));
671 status = cf_util_get_string (ci, &xpath->path);
678 /* error out if xpath->path is an empty string */
679 if (*xpath->path == 0)
681 ERROR ("curl_xml plugin: invalid xpath. "
682 "xpath value can't be an empty string");
687 for (i = 0; i < ci->children_num; i++)
689 oconfig_item_t *child = ci->children + i;
691 if (strcasecmp ("Type", child->key) == 0)
692 status = cf_util_get_string (child, &xpath->type);
693 else if (strcasecmp ("InstancePrefix", child->key) == 0)
694 status = cf_util_get_string (child, &xpath->instance_prefix);
695 else if (strcasecmp ("InstanceFrom", child->key) == 0)
696 status = cf_util_get_string (child, &xpath->instance);
697 else if (strcasecmp ("ValuesFrom", child->key) == 0)
698 status = cx_config_add_values ("ValuesFrom", xpath, child);
701 WARNING ("curl_xml plugin: Option `%s' not allowed here.", child->key);
707 } /* for (i = 0; i < ci->children_num; i++) */
709 if (status == 0 && xpath->type == NULL)
711 WARNING ("curl_xml plugin: `Type' missing in `xpath' block.");
720 if (db->list == NULL)
722 db->list = llist_create();
723 if (db->list == NULL)
725 ERROR ("curl_xml plugin: list creation failed.");
730 name = strdup(xpath->path);
733 ERROR ("curl_xml plugin: strdup failed.");
737 le = llentry_create (name, xpath);
740 ERROR ("curl_xml plugin: llentry_create failed.");
744 llist_append (db->list, le);
748 } /* }}} int cx_config_add_xpath */
750 /* Initialize db->curl */
751 static int cx_init_curl (cx_t *db) /* {{{ */
753 db->curl = curl_easy_init ();
754 if (db->curl == NULL)
756 ERROR ("curl_xml plugin: curl_easy_init failed.");
760 curl_easy_setopt (db->curl, CURLOPT_NOSIGNAL, 1L);
761 curl_easy_setopt (db->curl, CURLOPT_WRITEFUNCTION, cx_curl_callback);
762 curl_easy_setopt (db->curl, CURLOPT_WRITEDATA, db);
763 curl_easy_setopt (db->curl, CURLOPT_USERAGENT,
764 PACKAGE_NAME"/"PACKAGE_VERSION);
765 curl_easy_setopt (db->curl, CURLOPT_ERRORBUFFER, db->curl_errbuf);
766 curl_easy_setopt (db->curl, CURLOPT_URL, db->url);
768 if (db->user != NULL)
770 size_t credentials_size;
772 credentials_size = strlen (db->user) + 2;
773 if (db->pass != NULL)
774 credentials_size += strlen (db->pass);
776 db->credentials = (char *) malloc (credentials_size);
777 if (db->credentials == NULL)
779 ERROR ("curl_xml plugin: malloc failed.");
783 ssnprintf (db->credentials, credentials_size, "%s:%s",
784 db->user, (db->pass == NULL) ? "" : db->pass);
785 curl_easy_setopt (db->curl, CURLOPT_USERPWD, db->credentials);
788 curl_easy_setopt (db->curl, CURLOPT_SSL_VERIFYPEER, db->verify_peer ? 1L : 0L);
789 curl_easy_setopt (db->curl, CURLOPT_SSL_VERIFYHOST,
790 db->verify_host ? 2L : 0L);
791 if (db->cacert != NULL)
792 curl_easy_setopt (db->curl, CURLOPT_CAINFO, db->cacert);
793 if (db->headers != NULL)
794 curl_easy_setopt (db->curl, CURLOPT_HTTPHEADER, db->headers);
795 if (db->post_body != NULL)
796 curl_easy_setopt (db->curl, CURLOPT_POSTFIELDS, db->post_body);
799 } /* }}} int cx_init_curl */
801 static int cx_config_add_url (oconfig_item_t *ci) /* {{{ */
807 if ((ci->values_num != 1)
808 || (ci->values[0].type != OCONFIG_TYPE_STRING))
810 WARNING ("curl_xml plugin: The `URL' block "
811 "needs exactly one string argument.");
815 db = (cx_t *) malloc (sizeof (*db));
818 ERROR ("curl_xml plugin: malloc failed.");
821 memset (db, 0, sizeof (*db));
823 if (strcasecmp ("URL", ci->key) == 0)
825 status = cf_util_get_string (ci, &db->url);
834 ERROR ("curl_xml plugin: cx_config: "
835 "Invalid key: %s", ci->key);
839 /* Fill the `cx_t' structure.. */
840 for (i = 0; i < ci->children_num; i++)
842 oconfig_item_t *child = ci->children + i;
844 if (strcasecmp ("Instance", child->key) == 0)
845 status = cf_util_get_string (child, &db->instance);
846 else if (strcasecmp ("Host", child->key) == 0)
847 status = cf_util_get_string (child, &db->host);
848 else if (strcasecmp ("User", child->key) == 0)
849 status = cf_util_get_string (child, &db->user);
850 else if (strcasecmp ("Password", child->key) == 0)
851 status = cf_util_get_string (child, &db->pass);
852 else if (strcasecmp ("VerifyPeer", child->key) == 0)
853 status = cf_util_get_boolean (child, &db->verify_peer);
854 else if (strcasecmp ("VerifyHost", child->key) == 0)
855 status = cf_util_get_boolean (child, &db->verify_host);
856 else if (strcasecmp ("CACert", child->key) == 0)
857 status = cf_util_get_string (child, &db->cacert);
858 else if (strcasecmp ("xpath", child->key) == 0)
859 status = cx_config_add_xpath (db, child);
860 else if (strcasecmp ("Header", child->key) == 0)
861 status = cx_config_append_string ("Header", &db->headers, child);
862 else if (strcasecmp ("Post", child->key) == 0)
863 status = cf_util_get_string (child, &db->post_body);
866 WARNING ("curl_xml plugin: Option `%s' not allowed here.", child->key);
876 if (db->list == NULL)
878 WARNING ("curl_xml plugin: No (valid) `Key' block "
879 "within `URL' block `%s'.", db->url);
883 status = cx_init_curl (db);
886 /* If all went well, register this database for reading */
890 char cb_name[DATA_MAX_NAME_LEN];
892 if (db->instance == NULL)
893 db->instance = strdup("default");
895 DEBUG ("curl_xml plugin: Registering new read callback: %s",
898 memset (&ud, 0, sizeof (ud));
899 ud.data = (void *) db;
900 ud.free_func = cx_free;
902 ssnprintf (cb_name, sizeof (cb_name), "curl_xml-%s-%s",
903 db->instance, db->url);
905 plugin_register_complex_read (/* group = */ NULL, cb_name, cx_read,
906 /* interval = */ NULL, &ud);
915 } /* }}} int cx_config_add_url */
917 /* }}} End of configuration handling functions */
919 static int cx_config (oconfig_item_t *ci) /* {{{ */
929 for (i = 0; i < ci->children_num; i++)
931 oconfig_item_t *child = ci->children + i;
933 if (strcasecmp ("URL", child->key) == 0)
935 status = cx_config_add_url (child);
943 WARNING ("curl_xml plugin: Option `%s' not allowed here.", child->key);
948 if ((success == 0) && (errors > 0))
950 ERROR ("curl_xml plugin: All statements failed.");
955 } /* }}} int cx_config */
957 void module_register (void)
959 plugin_register_complex_config ("curl_xml", cx_config);
960 } /* void module_register */
962 /* vim: set sw=2 sts=2 et fdm=marker : */