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)
251 WARNING ("curl_xml plugin: "
252 "Node \"%s\" doesn't seem to be a text node. Skipping...", node->name);
254 } /* }}} cx_if_not_text_node */
256 static int cx_handle_single_value_xpath (xmlXPathContextPtr xpath_ctx, /* {{{ */
258 const data_set_t *ds, value_list_t *vl, int index)
260 xmlXPathObjectPtr values_node_obj;
261 xmlNodeSetPtr values_node;
265 values_node_obj = cx_evaluate_xpath (xpath_ctx, BAD_CAST xpath->values[index].path);
266 if (values_node_obj == NULL)
267 return (-1); /* Error already logged. */
269 values_node = values_node_obj->nodesetval;
270 tmp_size = (values_node) ? values_node->nodeNr : 0;
274 WARNING ("curl_xml plugin: "
275 "relative xpath expression \"%s\" doesn't match any of the nodes. "
276 "Skipping...", xpath->values[index].path);
277 xmlXPathFreeObject (values_node_obj);
283 WARNING ("curl_xml plugin: "
284 "relative xpath expression \"%s\" is expected to return "
285 "only one node. Skipping...", xpath->values[index].path);
286 xmlXPathFreeObject (values_node_obj);
290 /* ignoring the element if other than textnode/attribute*/
291 if (cx_if_not_text_node(values_node->nodeTab[0]))
293 WARNING ("curl_xml plugin: "
294 "relative xpath expression \"%s\" is expected to return "
295 "only text/attribute node which is not the case. Skipping...",
296 xpath->values[index].path);
297 xmlXPathFreeObject (values_node_obj);
301 node_value = (char *) xmlNodeGetContent(values_node->nodeTab[0]);
302 switch (ds->ds[index].type)
304 case DS_TYPE_COUNTER:
305 vl->values[index].counter = (counter_t) strtoull (node_value,
306 /* endptr = */ NULL, /* base = */ 0);
309 vl->values[index].derive = (derive_t) strtoll (node_value,
310 /* endptr = */ NULL, /* base = */ 0);
312 case DS_TYPE_ABSOLUTE:
313 vl->values[index].absolute = (absolute_t) strtoull (node_value,
314 /* endptr = */ NULL, /* base = */ 0);
317 vl->values[index].gauge = (gauge_t) strtod (node_value,
318 /* endptr = */ NULL);
322 xmlXPathFreeObject (values_node_obj);
324 /* We have reached here which means that
325 * we have got something to work */
327 } /* }}} int cx_handle_single_value_xpath */
329 static int cx_handle_all_value_xpaths (xmlXPathContextPtr xpath_ctx, /* {{{ */
331 const data_set_t *ds, value_list_t *vl)
333 value_t values[xpath->values_len];
337 assert (xpath->values_len > 0);
338 assert (xpath->values_len == vl->values_len);
339 assert (xpath->values_len == ds->ds_num);
342 for (i = 0; i < xpath->values_len; i++)
344 status = cx_handle_single_value_xpath (xpath_ctx, xpath, ds, vl, i);
346 return (-1); /* An error has been printed. */
347 } /* for (i = 0; i < xpath->values_len; i++) */
349 plugin_dispatch_values (vl);
353 } /* }}} int cx_handle_all_value_xpaths */
355 static int cx_handle_instance_xpath (xmlXPathContextPtr xpath_ctx, /* {{{ */
356 cx_xpath_t *xpath, value_list_t *vl,
359 xmlXPathObjectPtr instance_node_obj = NULL;
360 xmlNodeSetPtr instance_node = NULL;
362 memset (vl->type_instance, 0, sizeof (vl->type_instance));
364 /* If the base xpath returns more than one block, the result is assumed to be
365 * a table. The `Instnce' option is not optional in this case. Check for the
366 * condition and inform the user. */
367 if (is_table && (vl->type_instance == NULL))
369 WARNING ("curl_xml plugin: "
370 "Base-XPath %s is a table (more than one result was returned), "
371 "but no instance-XPath has been defined.",
376 /* instance has to be an xpath expression */
377 if (xpath->instance != NULL)
381 instance_node_obj = cx_evaluate_xpath (xpath_ctx, BAD_CAST xpath->instance);
382 if (instance_node_obj == NULL)
383 return (-1); /* error is logged already */
385 instance_node = instance_node_obj->nodesetval;
386 tmp_size = (instance_node) ? instance_node->nodeNr : 0;
390 WARNING ("curl_xml plugin: "
391 "relative xpath expression for 'InstanceFrom' \"%s\" doesn't match "
392 "any of the nodes. Skipping the node.", xpath->instance);
393 xmlXPathFreeObject (instance_node_obj);
399 WARNING ("curl_xml plugin: "
400 "relative xpath expression for 'InstanceFrom' \"%s\" is expected "
401 "to return only one text node. Skipping the node.", xpath->instance);
402 xmlXPathFreeObject (instance_node_obj);
406 /* ignoring the element if other than textnode/attribute */
407 if (cx_if_not_text_node(instance_node->nodeTab[0]))
409 WARNING ("curl_xml plugin: "
410 "relative xpath expression \"%s\" is expected to return only text node "
411 "which is not the case. Skipping the node.", xpath->instance);
412 xmlXPathFreeObject (instance_node_obj);
415 } /* if (xpath->instance != NULL) */
417 if (xpath->instance_prefix != NULL)
419 if (instance_node != NULL)
420 ssnprintf (vl->type_instance, sizeof (vl->type_instance),"%s%s",
421 xpath->instance_prefix, (char *) xmlNodeGetContent(instance_node->nodeTab[0]));
423 sstrncpy (vl->type_instance, xpath->instance_prefix,
424 sizeof (vl->type_instance));
428 /* If instance_prefix and instance_node are NULL, then
429 * don't set the type_instance */
430 if (instance_node != NULL)
431 sstrncpy (vl->type_instance, (char *) xmlNodeGetContent(instance_node->nodeTab[0]),
432 sizeof (vl->type_instance));
435 /* Free `instance_node_obj' this late, because `instance_node' points to
436 * somewhere inside this structure. */
437 xmlXPathFreeObject (instance_node_obj);
440 } /* }}} int cx_handle_instance_xpath */
442 static int cx_handle_base_xpath (char const *plugin_instance, /* {{{ */
444 xmlXPathContextPtr xpath_ctx, const data_set_t *ds,
445 char *base_xpath, cx_xpath_t *xpath)
450 xmlXPathObjectPtr base_node_obj = NULL;
451 xmlNodeSetPtr base_nodes = NULL;
453 value_list_t vl = VALUE_LIST_INIT;
455 base_node_obj = cx_evaluate_xpath (xpath_ctx, BAD_CAST base_xpath);
456 if (base_node_obj == NULL)
457 return -1; /* error is logged already */
459 base_nodes = base_node_obj->nodesetval;
460 total_nodes = (base_nodes) ? base_nodes->nodeNr : 0;
462 if (total_nodes == 0)
464 ERROR ("curl_xml plugin: "
465 "xpath expression \"%s\" doesn't match any of the nodes. "
466 "Skipping the xpath block...", base_xpath);
467 xmlXPathFreeObject (base_node_obj);
471 /* If base_xpath returned multiple results, then */
472 /* Instance in the xpath block is required */
473 if (total_nodes > 1 && xpath->instance == NULL)
475 ERROR ("curl_xml plugin: "
476 "InstanceFrom is must in xpath block since the base xpath expression \"%s\" "
477 "returned multiple results. Skipping the xpath block...", base_xpath);
481 /* set the values for the value_list */
482 vl.values_len = ds->ds_num;
483 sstrncpy (vl.type, xpath->type, sizeof (vl.type));
484 sstrncpy (vl.plugin, "curl_xml", sizeof (vl.plugin));
485 sstrncpy (vl.host, (host != NULL) ? host : hostname_g, sizeof (vl.host));
486 if (plugin_instance != NULL)
487 sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
489 for (i = 0; i < total_nodes; i++)
493 xpath_ctx->node = base_nodes->nodeTab[i];
495 status = cx_handle_instance_xpath (xpath_ctx, xpath, &vl,
496 /* is_table = */ (total_nodes > 1));
498 continue; /* An error has already been reported. */
500 status = cx_handle_all_value_xpaths (xpath_ctx, xpath, ds, &vl);
502 continue; /* An error has been logged. */
503 } /* for (i = 0; i < total_nodes; i++) */
505 /* free up the allocated memory */
506 xmlXPathFreeObject (base_node_obj);
509 } /* }}} cx_handle_base_xpath */
511 static int cx_handle_parsed_xml(xmlDocPtr doc, /* {{{ */
512 xmlXPathContextPtr xpath_ctx, cx_t *db)
515 const data_set_t *ds;
520 le = llist_head (db->list);
524 xpath = (cx_xpath_t *) le->value;
525 ds = plugin_get_ds (xpath->type);
527 if ( (cx_check_type(ds, xpath) == 0) &&
528 (cx_handle_base_xpath(db->instance, db->host,
529 xpath_ctx, ds, le->key, xpath) == 0) )
530 status = 0; /* we got atleast one success */
533 } /* while (le != NULL) */
536 } /* }}} cx_handle_parsed_xml */
538 static int cx_parse_stats_xml(xmlChar* xml, cx_t *db) /* {{{ */
542 xmlXPathContextPtr xpath_ctx;
545 doc = xmlParseDoc(xml);
548 ERROR ("curl_xml plugin: Failed to parse the xml document - %s", xml);
552 xpath_ctx = xmlXPathNewContext(doc);
553 if(xpath_ctx == NULL)
555 ERROR ("curl_xml plugin: Failed to create the xml context");
560 status = cx_handle_parsed_xml (doc, xpath_ctx, db);
562 xmlXPathFreeContext(xpath_ctx);
565 } /* }}} cx_parse_stats_xml */
567 static int cx_curl_perform (cx_t *db, CURL *curl) /* {{{ */
575 status = curl_easy_perform (curl);
576 if (status != CURLE_OK)
578 ERROR ("curl_xml plugin: curl_easy_perform failed with status %i: %s (%s)",
579 status, db->curl_errbuf, url);
583 curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &url);
584 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &rc);
586 /* The response code is zero if a non-HTTP transport was used. */
587 if ((rc != 0) && (rc != 200))
589 ERROR ("curl_xml plugin: curl_easy_perform failed with response code %ld (%s)",
596 status = cx_parse_stats_xml(BAD_CAST ptr, db);
600 } /* }}} int cx_curl_perform */
602 static int cx_read (user_data_t *ud) /* {{{ */
606 if ((ud == NULL) || (ud->data == NULL))
608 ERROR ("curl_xml plugin: cx_read: Invalid user data.");
612 db = (cx_t *) ud->data;
614 return cx_curl_perform (db, db->curl);
615 } /* }}} int cx_read */
617 /* Configuration handling functions {{{ */
619 static int cx_config_add_values (const char *name, cx_xpath_t *xpath, /* {{{ */
624 if (ci->values_num < 1)
626 WARNING ("curl_xml plugin: `ValuesFrom' needs at least one argument.");
630 for (i = 0; i < ci->values_num; i++)
631 if (ci->values[i].type != OCONFIG_TYPE_STRING)
633 WARNING ("curl_xml plugin: `ValuesFrom' needs only string argument.");
637 sfree (xpath->values);
639 xpath->values_len = 0;
640 xpath->values = (cx_values_t *) malloc (sizeof (cx_values_t) * ci->values_num);
641 if (xpath->values == NULL)
643 xpath->values_len = ci->values_num;
645 /* populate cx_values_t structure */
646 for (i = 0; i < ci->values_num; i++)
648 xpath->values[i].path_len = sizeof (ci->values[i].value.string);
649 sstrncpy (xpath->values[i].path, ci->values[i].value.string, sizeof (xpath->values[i].path));
653 } /* }}} cx_config_add_values */
655 static int cx_config_add_xpath (cx_t *db, /* {{{ */
662 xpath = (cx_xpath_t *) malloc (sizeof (*xpath));
665 ERROR ("curl_xml plugin: malloc failed.");
668 memset (xpath, 0, sizeof (*xpath));
670 status = cf_util_get_string (ci, &xpath->path);
677 /* error out if xpath->path is an empty string */
678 if (*xpath->path == 0)
680 ERROR ("curl_xml plugin: invalid xpath. "
681 "xpath value can't be an empty string");
686 for (i = 0; i < ci->children_num; i++)
688 oconfig_item_t *child = ci->children + i;
690 if (strcasecmp ("Type", child->key) == 0)
691 status = cf_util_get_string (child, &xpath->type);
692 else if (strcasecmp ("InstancePrefix", child->key) == 0)
693 status = cf_util_get_string (child, &xpath->instance_prefix);
694 else if (strcasecmp ("InstanceFrom", child->key) == 0)
695 status = cf_util_get_string (child, &xpath->instance);
696 else if (strcasecmp ("ValuesFrom", child->key) == 0)
697 status = cx_config_add_values ("ValuesFrom", xpath, child);
700 WARNING ("curl_xml plugin: Option `%s' not allowed here.", child->key);
706 } /* for (i = 0; i < ci->children_num; i++) */
708 if (status == 0 && xpath->type == NULL)
710 WARNING ("curl_xml plugin: `Type' missing in `xpath' block.");
719 if (db->list == NULL)
721 db->list = llist_create();
722 if (db->list == NULL)
724 ERROR ("curl_xml plugin: list creation failed.");
729 name = strdup(xpath->path);
732 ERROR ("curl_xml plugin: strdup failed.");
736 le = llentry_create (name, xpath);
739 ERROR ("curl_xml plugin: llentry_create failed.");
743 llist_append (db->list, le);
747 } /* }}} int cx_config_add_xpath */
749 /* Initialize db->curl */
750 static int cx_init_curl (cx_t *db) /* {{{ */
752 db->curl = curl_easy_init ();
753 if (db->curl == NULL)
755 ERROR ("curl_xml plugin: curl_easy_init failed.");
759 curl_easy_setopt (db->curl, CURLOPT_NOSIGNAL, 1L);
760 curl_easy_setopt (db->curl, CURLOPT_WRITEFUNCTION, cx_curl_callback);
761 curl_easy_setopt (db->curl, CURLOPT_WRITEDATA, db);
762 curl_easy_setopt (db->curl, CURLOPT_USERAGENT,
763 PACKAGE_NAME"/"PACKAGE_VERSION);
764 curl_easy_setopt (db->curl, CURLOPT_ERRORBUFFER, db->curl_errbuf);
765 curl_easy_setopt (db->curl, CURLOPT_URL, db->url);
767 if (db->user != NULL)
769 size_t credentials_size;
771 credentials_size = strlen (db->user) + 2;
772 if (db->pass != NULL)
773 credentials_size += strlen (db->pass);
775 db->credentials = (char *) malloc (credentials_size);
776 if (db->credentials == NULL)
778 ERROR ("curl_xml plugin: malloc failed.");
782 ssnprintf (db->credentials, credentials_size, "%s:%s",
783 db->user, (db->pass == NULL) ? "" : db->pass);
784 curl_easy_setopt (db->curl, CURLOPT_USERPWD, db->credentials);
787 curl_easy_setopt (db->curl, CURLOPT_SSL_VERIFYPEER, db->verify_peer ? 1L : 0L);
788 curl_easy_setopt (db->curl, CURLOPT_SSL_VERIFYHOST,
789 db->verify_host ? 2L : 0L);
790 if (db->cacert != NULL)
791 curl_easy_setopt (db->curl, CURLOPT_CAINFO, db->cacert);
792 if (db->headers != NULL)
793 curl_easy_setopt (db->curl, CURLOPT_HTTPHEADER, db->headers);
794 if (db->post_body != NULL)
795 curl_easy_setopt (db->curl, CURLOPT_POSTFIELDS, db->post_body);
798 } /* }}} int cx_init_curl */
800 static int cx_config_add_url (oconfig_item_t *ci) /* {{{ */
806 if ((ci->values_num != 1)
807 || (ci->values[0].type != OCONFIG_TYPE_STRING))
809 WARNING ("curl_xml plugin: The `URL' block "
810 "needs exactly one string argument.");
814 db = (cx_t *) malloc (sizeof (*db));
817 ERROR ("curl_xml plugin: malloc failed.");
820 memset (db, 0, sizeof (*db));
822 if (strcasecmp ("URL", ci->key) == 0)
824 status = cf_util_get_string (ci, &db->url);
833 ERROR ("curl_xml plugin: cx_config: "
834 "Invalid key: %s", ci->key);
838 /* Fill the `cx_t' structure.. */
839 for (i = 0; i < ci->children_num; i++)
841 oconfig_item_t *child = ci->children + i;
843 if (strcasecmp ("Instance", child->key) == 0)
844 status = cf_util_get_string (child, &db->instance);
845 else if (strcasecmp ("Host", child->key) == 0)
846 status = cf_util_get_string (child, &db->host);
847 else if (strcasecmp ("User", child->key) == 0)
848 status = cf_util_get_string (child, &db->user);
849 else if (strcasecmp ("Password", child->key) == 0)
850 status = cf_util_get_string (child, &db->pass);
851 else if (strcasecmp ("VerifyPeer", child->key) == 0)
852 status = cf_util_get_boolean (child, &db->verify_peer);
853 else if (strcasecmp ("VerifyHost", child->key) == 0)
854 status = cf_util_get_boolean (child, &db->verify_host);
855 else if (strcasecmp ("CACert", child->key) == 0)
856 status = cf_util_get_string (child, &db->cacert);
857 else if (strcasecmp ("xpath", child->key) == 0)
858 status = cx_config_add_xpath (db, child);
859 else if (strcasecmp ("Header", child->key) == 0)
860 status = cx_config_append_string ("Header", &db->headers, child);
861 else if (strcasecmp ("Post", child->key) == 0)
862 status = cf_util_get_string (child, &db->post_body);
865 WARNING ("curl_xml plugin: Option `%s' not allowed here.", child->key);
875 if (db->list == NULL)
877 WARNING ("curl_xml plugin: No (valid) `Key' block "
878 "within `URL' block `%s'.", db->url);
882 status = cx_init_curl (db);
885 /* If all went well, register this database for reading */
889 char cb_name[DATA_MAX_NAME_LEN];
891 if (db->instance == NULL)
892 db->instance = strdup("default");
894 DEBUG ("curl_xml plugin: Registering new read callback: %s",
897 memset (&ud, 0, sizeof (ud));
898 ud.data = (void *) db;
899 ud.free_func = cx_free;
901 ssnprintf (cb_name, sizeof (cb_name), "curl_xml-%s-%s",
902 db->instance, db->url);
904 plugin_register_complex_read (/* group = */ NULL, cb_name, cx_read,
905 /* interval = */ NULL, &ud);
914 } /* }}} int cx_config_add_url */
916 /* }}} End of configuration handling functions */
918 static int cx_config (oconfig_item_t *ci) /* {{{ */
928 for (i = 0; i < ci->children_num; i++)
930 oconfig_item_t *child = ci->children + i;
932 if (strcasecmp ("URL", child->key) == 0)
934 status = cx_config_add_url (child);
942 WARNING ("curl_xml plugin: Option `%s' not allowed here.", child->key);
947 if ((success == 0) && (errors > 0))
949 ERROR ("curl_xml plugin: All statements failed.");
954 } /* }}} int cx_config */
956 void module_register (void)
958 plugin_register_complex_config ("curl_xml", cx_config);
959 } /* void module_register */
961 /* vim: set sw=2 sts=2 et fdm=marker : */