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;
75 char curl_errbuf[CURL_ERROR_SIZE];
80 llist_t *list; /* list of xpath blocks */
82 typedef struct cx_s cx_t; /* }}} */
87 static size_t cx_curl_callback (void *buf, /* {{{ */
88 size_t size, size_t nmemb, void *user_data)
90 size_t len = size * nmemb;
96 ERROR ("curl_xml plugin: cx_curl_callback: "
97 "user_data pointer is NULL.");
104 if ((db->buffer_fill + len) >= db->buffer_size)
108 temp = (char *) realloc (db->buffer,
109 db->buffer_fill + len + 1);
112 ERROR ("curl_xml plugin: realloc failed.");
116 db->buffer_size = db->buffer_fill + len + 1;
119 memcpy (db->buffer + db->buffer_fill, (char *) buf, len);
120 db->buffer_fill += len;
121 db->buffer[db->buffer_fill] = 0;
124 } /* }}} size_t cx_curl_callback */
126 static void cx_xpath_free (cx_xpath_t *xpath) /* {{{ */
133 sfree (xpath->instance_prefix);
134 sfree (xpath->instance);
135 sfree (xpath->values);
137 } /* }}} void cx_xpath_free */
139 static void cx_list_free (llist_t *list) /* {{{ */
143 le = llist_head (list);
151 cx_xpath_free (le->value);
156 llist_destroy (list);
158 } /* }}} void cx_list_free */
160 static void cx_free (void *arg) /* {{{ */
164 DEBUG ("curl_xml plugin: cx_free (arg = %p);", arg);
171 if (db->curl != NULL)
172 curl_easy_cleanup (db->curl);
175 if (db->list != NULL)
176 cx_list_free (db->list);
179 sfree (db->instance);
185 sfree (db->credentials);
189 } /* }}} void cx_free */
191 static int cx_check_type (const data_set_t *ds, cx_xpath_t *xpath) /* {{{ */
195 WARNING ("curl_xml plugin: DataSet `%s' not defined.", xpath->type);
199 if (ds->ds_num != xpath->values_len)
201 WARNING ("curl_xml plugin: DataSet `%s' requires %i values, but config talks about %i",
202 xpath->type, ds->ds_num, xpath->values_len);
207 } /* }}} cx_check_type */
209 static xmlXPathObjectPtr cx_evaluate_xpath (xmlXPathContextPtr xpath_ctx, /* {{{ */
212 xmlXPathObjectPtr xpath_obj;
214 /* XXX: When to free this? */
215 xpath_obj = xmlXPathEvalExpression(BAD_CAST expr, xpath_ctx);
216 if (xpath_obj == NULL)
218 WARNING ("curl_xml plugin: "
219 "Error unable to evaluate xpath expression \"%s\". Skipping...", expr);
224 } /* }}} cx_evaluate_xpath */
226 static int cx_if_not_text_node (xmlNodePtr node) /* {{{ */
228 if (node->type == XML_TEXT_NODE || node->type == XML_ATTRIBUTE_NODE)
231 WARNING ("curl_xml plugin: "
232 "Node \"%s\" doesn't seem to be a text node. Skipping...", node->name);
234 } /* }}} cx_if_not_text_node */
236 static int cx_handle_single_value_xpath (xmlXPathContextPtr xpath_ctx, /* {{{ */
238 const data_set_t *ds, value_list_t *vl, int index)
240 xmlXPathObjectPtr values_node_obj;
241 xmlNodeSetPtr values_node;
245 values_node_obj = cx_evaluate_xpath (xpath_ctx, BAD_CAST xpath->values[index].path);
246 if (values_node_obj == NULL)
247 return (-1); /* Error already logged. */
249 values_node = values_node_obj->nodesetval;
250 tmp_size = (values_node) ? values_node->nodeNr : 0;
254 WARNING ("curl_xml plugin: "
255 "relative xpath expression \"%s\" doesn't match any of the nodes. "
256 "Skipping...", xpath->values[index].path);
257 xmlXPathFreeObject (values_node_obj);
263 WARNING ("curl_xml plugin: "
264 "relative xpath expression \"%s\" is expected to return "
265 "only one node. Skipping...", xpath->values[index].path);
266 xmlXPathFreeObject (values_node_obj);
270 /* ignoring the element if other than textnode/attribute*/
271 if (cx_if_not_text_node(values_node->nodeTab[0]))
273 WARNING ("curl_xml plugin: "
274 "relative xpath expression \"%s\" is expected to return "
275 "only text/attribute node which is not the case. Skipping...",
276 xpath->values[index].path);
277 xmlXPathFreeObject (values_node_obj);
281 node_value = (char *) xmlNodeGetContent(values_node->nodeTab[0]);
282 switch (ds->ds[index].type)
284 case DS_TYPE_COUNTER:
285 vl->values[index].counter = (counter_t) strtoull (node_value,
286 /* endptr = */ NULL, /* base = */ 0);
289 vl->values[index].derive = (derive_t) strtoll (node_value,
290 /* endptr = */ NULL, /* base = */ 0);
292 case DS_TYPE_ABSOLUTE:
293 vl->values[index].absolute = (absolute_t) strtoull (node_value,
294 /* endptr = */ NULL, /* base = */ 0);
297 vl->values[index].gauge = (gauge_t) strtod (node_value,
298 /* endptr = */ NULL);
302 xmlXPathFreeObject (values_node_obj);
304 /* We have reached here which means that
305 * we have got something to work */
307 } /* }}} int cx_handle_single_value_xpath */
309 static int cx_handle_all_value_xpaths (xmlXPathContextPtr xpath_ctx, /* {{{ */
311 const data_set_t *ds, value_list_t *vl)
313 value_t values[xpath->values_len];
317 assert (xpath->values_len > 0);
318 assert (xpath->values_len == vl->values_len);
319 assert (xpath->values_len == ds->ds_num);
322 for (i = 0; i < xpath->values_len; i++)
324 status = cx_handle_single_value_xpath (xpath_ctx, xpath, ds, vl, i);
326 return (-1); /* An error has been printed. */
327 } /* for (i = 0; i < xpath->values_len; i++) */
329 plugin_dispatch_values (vl);
333 } /* }}} int cx_handle_all_value_xpaths */
335 static int cx_handle_instance_xpath (xmlXPathContextPtr xpath_ctx, /* {{{ */
336 cx_xpath_t *xpath, value_list_t *vl,
339 xmlXPathObjectPtr instance_node_obj = NULL;
340 xmlNodeSetPtr instance_node = NULL;
342 memset (vl->type_instance, 0, sizeof (vl->type_instance));
344 /* If the base xpath returns more than one block, the result is assumed to be
345 * a table. The `Instnce' option is not optional in this case. Check for the
346 * condition and inform the user. */
347 if (is_table && (vl->type_instance == NULL))
349 WARNING ("curl_xml plugin: "
350 "Base-XPath %s is a table (more than one result was returned), "
351 "but no instance-XPath has been defined.",
356 /* instance has to be an xpath expression */
357 if (xpath->instance != NULL)
361 instance_node_obj = cx_evaluate_xpath (xpath_ctx, BAD_CAST xpath->instance);
362 if (instance_node_obj == NULL)
363 return (-1); /* error is logged already */
365 instance_node = instance_node_obj->nodesetval;
366 tmp_size = (instance_node) ? instance_node->nodeNr : 0;
370 WARNING ("curl_xml plugin: "
371 "relative xpath expression for 'InstanceFrom' \"%s\" doesn't match "
372 "any of the nodes. Skipping the node.", xpath->instance);
373 xmlXPathFreeObject (instance_node_obj);
379 WARNING ("curl_xml plugin: "
380 "relative xpath expression for 'InstanceFrom' \"%s\" is expected "
381 "to return only one text node. Skipping the node.", xpath->instance);
382 xmlXPathFreeObject (instance_node_obj);
386 /* ignoring the element if other than textnode/attribute */
387 if (cx_if_not_text_node(instance_node->nodeTab[0]))
389 WARNING ("curl_xml plugin: "
390 "relative xpath expression \"%s\" is expected to return only text node "
391 "which is not the case. Skipping the node.", xpath->instance);
392 xmlXPathFreeObject (instance_node_obj);
395 } /* if (xpath->instance != NULL) */
397 if (xpath->instance_prefix != NULL)
399 if (instance_node != NULL)
400 ssnprintf (vl->type_instance, sizeof (vl->type_instance),"%s%s",
401 xpath->instance_prefix, (char *) xmlNodeGetContent(instance_node->nodeTab[0]));
403 sstrncpy (vl->type_instance, xpath->instance_prefix,
404 sizeof (vl->type_instance));
408 /* If instance_prefix and instance_node are NULL, then
409 * don't set the type_instance */
410 if (instance_node != NULL)
411 sstrncpy (vl->type_instance, (char *) xmlNodeGetContent(instance_node->nodeTab[0]),
412 sizeof (vl->type_instance));
415 /* Free `instance_node_obj' this late, because `instance_node' points to
416 * somewhere inside this structure. */
417 xmlXPathFreeObject (instance_node_obj);
420 } /* }}} int cx_handle_instance_xpath */
422 static int cx_handle_base_xpath (char const *plugin_instance, /* {{{ */
424 xmlXPathContextPtr xpath_ctx, const data_set_t *ds,
425 char *base_xpath, cx_xpath_t *xpath)
430 xmlXPathObjectPtr base_node_obj = NULL;
431 xmlNodeSetPtr base_nodes = NULL;
433 value_list_t vl = VALUE_LIST_INIT;
435 base_node_obj = cx_evaluate_xpath (xpath_ctx, BAD_CAST base_xpath);
436 if (base_node_obj == NULL)
437 return -1; /* error is logged already */
439 base_nodes = base_node_obj->nodesetval;
440 total_nodes = (base_nodes) ? base_nodes->nodeNr : 0;
442 if (total_nodes == 0)
444 ERROR ("curl_xml plugin: "
445 "xpath expression \"%s\" doesn't match any of the nodes. "
446 "Skipping the xpath block...", base_xpath);
447 xmlXPathFreeObject (base_node_obj);
451 /* If base_xpath returned multiple results, then */
452 /* Instance in the xpath block is required */
453 if (total_nodes > 1 && xpath->instance == NULL)
455 ERROR ("curl_xml plugin: "
456 "InstanceFrom is must in xpath block since the base xpath expression \"%s\" "
457 "returned multiple results. Skipping the xpath block...", base_xpath);
461 /* set the values for the value_list */
462 vl.values_len = ds->ds_num;
463 sstrncpy (vl.type, xpath->type, sizeof (vl.type));
464 sstrncpy (vl.plugin, "curl_xml", sizeof (vl.plugin));
465 sstrncpy (vl.host, (host != NULL) ? host : hostname_g, sizeof (vl.host));
466 if (plugin_instance != NULL)
467 sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
469 for (i = 0; i < total_nodes; i++)
473 xpath_ctx->node = base_nodes->nodeTab[i];
475 status = cx_handle_instance_xpath (xpath_ctx, xpath, &vl,
476 /* is_table = */ (total_nodes > 1));
478 continue; /* An error has already been reported. */
480 status = cx_handle_all_value_xpaths (xpath_ctx, xpath, ds, &vl);
482 continue; /* An error has been logged. */
483 } /* for (i = 0; i < total_nodes; i++) */
485 /* free up the allocated memory */
486 xmlXPathFreeObject (base_node_obj);
489 } /* }}} cx_handle_base_xpath */
491 static int cx_handle_parsed_xml(xmlDocPtr doc, /* {{{ */
492 xmlXPathContextPtr xpath_ctx, cx_t *db)
495 const data_set_t *ds;
500 le = llist_head (db->list);
504 xpath = (cx_xpath_t *) le->value;
505 ds = plugin_get_ds (xpath->type);
507 if ( (cx_check_type(ds, xpath) == 0) &&
508 (cx_handle_base_xpath(db->instance, db->host,
509 xpath_ctx, ds, le->key, xpath) == 0) )
510 status = 0; /* we got atleast one success */
513 } /* while (le != NULL) */
516 } /* }}} cx_handle_parsed_xml */
518 static int cx_parse_stats_xml(xmlChar* xml, cx_t *db) /* {{{ */
522 xmlXPathContextPtr xpath_ctx;
525 doc = xmlParseDoc(xml);
528 ERROR ("curl_xml plugin: Failed to parse the xml document - %s", xml);
532 xpath_ctx = xmlXPathNewContext(doc);
533 if(xpath_ctx == NULL)
535 ERROR ("curl_xml plugin: Failed to create the xml context");
540 status = cx_handle_parsed_xml (doc, xpath_ctx, db);
542 xmlXPathFreeContext(xpath_ctx);
545 } /* }}} cx_parse_stats_xml */
547 static int cx_curl_perform (cx_t *db, CURL *curl) /* {{{ */
555 status = curl_easy_perform (curl);
556 if (status != CURLE_OK)
558 ERROR ("curl_xml plugin: curl_easy_perform failed with status %i: %s",
559 status, db->curl_errbuf);
563 curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &url);
564 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &rc);
566 /* The response code is zero if a non-HTTP transport was used. */
567 if ((rc != 0) && (rc != 200))
569 ERROR ("curl_xml plugin: curl_easy_perform failed with response code %ld (%s)",
576 status = cx_parse_stats_xml(BAD_CAST ptr, db);
580 } /* }}} int cx_curl_perform */
582 static int cx_read (user_data_t *ud) /* {{{ */
586 if ((ud == NULL) || (ud->data == NULL))
588 ERROR ("curl_xml plugin: cx_read: Invalid user data.");
592 db = (cx_t *) ud->data;
594 return cx_curl_perform (db, db->curl);
595 } /* }}} int cx_read */
597 /* Configuration handling functions {{{ */
599 static int cx_config_add_values (const char *name, cx_xpath_t *xpath, /* {{{ */
604 if (ci->values_num < 1)
606 WARNING ("curl_xml plugin: `ValuesFrom' needs at least one argument.");
610 for (i = 0; i < ci->values_num; i++)
611 if (ci->values[i].type != OCONFIG_TYPE_STRING)
613 WARNING ("curl_xml plugin: `ValuesFrom' needs only string argument.");
617 sfree (xpath->values);
619 xpath->values_len = 0;
620 xpath->values = (cx_values_t *) malloc (sizeof (cx_values_t) * ci->values_num);
621 if (xpath->values == NULL)
623 xpath->values_len = ci->values_num;
625 /* populate cx_values_t structure */
626 for (i = 0; i < ci->values_num; i++)
628 xpath->values[i].path_len = sizeof (ci->values[i].value.string);
629 sstrncpy (xpath->values[i].path, ci->values[i].value.string, sizeof (xpath->values[i].path));
633 } /* }}} cx_config_add_values */
635 static int cx_config_add_xpath (cx_t *db, /* {{{ */
642 xpath = (cx_xpath_t *) malloc (sizeof (*xpath));
645 ERROR ("curl_xml plugin: malloc failed.");
648 memset (xpath, 0, sizeof (*xpath));
650 status = cf_util_get_string (ci, &xpath->path);
657 /* error out if xpath->path is an empty string */
658 if (*xpath->path == 0)
660 ERROR ("curl_xml plugin: invalid xpath. "
661 "xpath value can't be an empty string");
666 for (i = 0; i < ci->children_num; i++)
668 oconfig_item_t *child = ci->children + i;
670 if (strcasecmp ("Type", child->key) == 0)
671 status = cf_util_get_string (child, &xpath->type);
672 else if (strcasecmp ("InstancePrefix", child->key) == 0)
673 status = cf_util_get_string (child, &xpath->instance_prefix);
674 else if (strcasecmp ("InstanceFrom", child->key) == 0)
675 status = cf_util_get_string (child, &xpath->instance);
676 else if (strcasecmp ("ValuesFrom", child->key) == 0)
677 status = cx_config_add_values ("ValuesFrom", xpath, child);
680 WARNING ("curl_xml plugin: Option `%s' not allowed here.", child->key);
686 } /* for (i = 0; i < ci->children_num; i++) */
688 if (status == 0 && xpath->type == NULL)
690 WARNING ("curl_xml plugin: `Type' missing in `xpath' block.");
699 if (db->list == NULL)
701 db->list = llist_create();
702 if (db->list == NULL)
704 ERROR ("curl_xml plugin: list creation failed.");
709 name = strdup(xpath->path);
712 ERROR ("curl_xml plugin: strdup failed.");
716 le = llentry_create (name, xpath);
719 ERROR ("curl_xml plugin: llentry_create failed.");
723 llist_append (db->list, le);
727 } /* }}} int cx_config_add_xpath */
729 /* Initialize db->curl */
730 static int cx_init_curl (cx_t *db) /* {{{ */
732 db->curl = curl_easy_init ();
733 if (db->curl == NULL)
735 ERROR ("curl_xml plugin: curl_easy_init failed.");
739 curl_easy_setopt (db->curl, CURLOPT_NOSIGNAL, 1L);
740 curl_easy_setopt (db->curl, CURLOPT_WRITEFUNCTION, cx_curl_callback);
741 curl_easy_setopt (db->curl, CURLOPT_WRITEDATA, db);
742 curl_easy_setopt (db->curl, CURLOPT_USERAGENT,
743 PACKAGE_NAME"/"PACKAGE_VERSION);
744 curl_easy_setopt (db->curl, CURLOPT_ERRORBUFFER, db->curl_errbuf);
745 curl_easy_setopt (db->curl, CURLOPT_URL, db->url);
747 if (db->user != NULL)
749 size_t credentials_size;
751 credentials_size = strlen (db->user) + 2;
752 if (db->pass != NULL)
753 credentials_size += strlen (db->pass);
755 db->credentials = (char *) malloc (credentials_size);
756 if (db->credentials == NULL)
758 ERROR ("curl_xml plugin: malloc failed.");
762 ssnprintf (db->credentials, credentials_size, "%s:%s",
763 db->user, (db->pass == NULL) ? "" : db->pass);
764 curl_easy_setopt (db->curl, CURLOPT_USERPWD, db->credentials);
767 curl_easy_setopt (db->curl, CURLOPT_SSL_VERIFYPEER, db->verify_peer ? 1L : 0L);
768 curl_easy_setopt (db->curl, CURLOPT_SSL_VERIFYHOST,
769 db->verify_host ? 2L : 0L);
770 if (db->cacert != NULL)
771 curl_easy_setopt (db->curl, CURLOPT_CAINFO, db->cacert);
774 } /* }}} int cx_init_curl */
776 static int cx_config_add_url (oconfig_item_t *ci) /* {{{ */
782 if ((ci->values_num != 1)
783 || (ci->values[0].type != OCONFIG_TYPE_STRING))
785 WARNING ("curl_xml plugin: The `URL' block "
786 "needs exactly one string argument.");
790 db = (cx_t *) malloc (sizeof (*db));
793 ERROR ("curl_xml plugin: malloc failed.");
796 memset (db, 0, sizeof (*db));
798 if (strcasecmp ("URL", ci->key) == 0)
800 status = cf_util_get_string (ci, &db->url);
809 ERROR ("curl_xml plugin: cx_config: "
810 "Invalid key: %s", ci->key);
814 /* Fill the `cx_t' structure.. */
815 for (i = 0; i < ci->children_num; i++)
817 oconfig_item_t *child = ci->children + i;
819 if (strcasecmp ("Instance", child->key) == 0)
820 status = cf_util_get_string (child, &db->instance);
821 else if (strcasecmp ("Host", child->key) == 0)
822 status = cf_util_get_string (child, &db->host);
823 else if (strcasecmp ("User", child->key) == 0)
824 status = cf_util_get_string (child, &db->user);
825 else if (strcasecmp ("Password", child->key) == 0)
826 status = cf_util_get_string (child, &db->pass);
827 else if (strcasecmp ("VerifyPeer", child->key) == 0)
828 status = cf_util_get_boolean (child, &db->verify_peer);
829 else if (strcasecmp ("VerifyHost", child->key) == 0)
830 status = cf_util_get_boolean (child, &db->verify_host);
831 else if (strcasecmp ("CACert", child->key) == 0)
832 status = cf_util_get_string (child, &db->cacert);
833 else if (strcasecmp ("xpath", child->key) == 0)
834 status = cx_config_add_xpath (db, child);
837 WARNING ("curl_xml plugin: Option `%s' not allowed here.", child->key);
847 if (db->list == NULL)
849 WARNING ("curl_xml plugin: No (valid) `Key' block "
850 "within `URL' block `%s'.", db->url);
854 status = cx_init_curl (db);
857 /* If all went well, register this database for reading */
861 char cb_name[DATA_MAX_NAME_LEN];
863 if (db->instance == NULL)
864 db->instance = strdup("default");
866 DEBUG ("curl_xml plugin: Registering new read callback: %s",
869 memset (&ud, 0, sizeof (ud));
870 ud.data = (void *) db;
871 ud.free_func = cx_free;
873 ssnprintf (cb_name, sizeof (cb_name), "curl_xml-%s-%s",
874 db->instance, db->url);
876 plugin_register_complex_read (/* group = */ NULL, cb_name, cx_read,
877 /* interval = */ NULL, &ud);
886 } /* }}} int cx_config_add_url */
888 /* }}} End of configuration handling functions */
890 static int cx_config (oconfig_item_t *ci) /* {{{ */
900 for (i = 0; i < ci->children_num; i++)
902 oconfig_item_t *child = ci->children + i;
904 if (strcasecmp ("URL", child->key) == 0)
906 status = cx_config_add_url (child);
914 WARNING ("curl_xml plugin: Option `%s' not allowed here.", child->key);
919 if ((success == 0) && (errors > 0))
921 ERROR ("curl_xml plugin: All statements failed.");
926 } /* }}} int cx_config */
928 void module_register (void)
930 plugin_register_complex_config ("curl_xml", cx_config);
931 } /* void module_register */
933 /* vim: set sw=2 sts=2 et fdm=marker : */