1 /**
2 * collectd - src/curl_xml.c
3 * Copyright (C) 2009,2010 Amit Gupta
4 *
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.
8 *
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.
13 *
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
17 *
18 * Authors:
19 * Amit Gupta <amit.gupta221 at gmail.com>
20 **/
22 #include "collectd.h"
23 #include "common.h"
24 #include "plugin.h"
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 /*
39 * Private data structures
40 */
41 struct cx_values_s /* {{{ */
42 {
43 char path[DATA_MAX_NAME_LEN];
44 size_t path_len;
45 };
46 typedef struct cx_values_s cx_values_t;
47 /* }}} */
49 struct cx_xpath_s /* {{{ */
50 {
51 char *path;
52 char *type;
53 cx_values_t *values;
54 int values_len;
55 char *instance_prefix;
56 char *instance;
57 int is_table;
58 unsigned long magic;
59 };
60 typedef struct cx_xpath_s cx_xpath_t;
61 /* }}} */
63 struct cx_s /* {{{ */
64 {
65 char *instance;
66 char *host;
68 char *url;
69 char *user;
70 char *pass;
71 char *credentials;
72 _Bool verify_peer;
73 _Bool verify_host;
74 char *cacert;
76 CURL *curl;
77 char curl_errbuf[CURL_ERROR_SIZE];
78 char *buffer;
79 size_t buffer_size;
80 size_t buffer_fill;
82 c_avl_tree_t *tree; /* tree of xpath blocks */
83 };
84 typedef struct cx_s cx_t; /* }}} */
86 /*
87 * Private functions
88 */
89 static size_t cx_curl_callback (void *buf, /* {{{ */
90 size_t size, size_t nmemb, void *user_data)
91 {
92 size_t len = size * nmemb;
93 cx_t *db;
95 db = user_data;
96 if (db == NULL)
97 {
98 ERROR ("curl_xml plugin: cx_curl_callback: "
99 "user_data pointer is NULL.");
100 return (0);
101 }
103 if (len <= 0)
104 return (len);
106 if ((db->buffer_fill + len) >= db->buffer_size)
107 {
108 char *temp;
110 temp = (char *) realloc (db->buffer,
111 db->buffer_fill + len + 1);
112 if (temp == NULL)
113 {
114 ERROR ("curl_xml plugin: realloc failed.");
115 return (0);
116 }
117 db->buffer = temp;
118 db->buffer_size = db->buffer_fill + len + 1;
119 }
121 memcpy (db->buffer + db->buffer_fill, (char *) buf, len);
122 db->buffer_fill += len;
123 db->buffer[db->buffer_fill] = 0;
125 return (len);
126 } /* }}} size_t cx_curl_callback */
128 static void cx_xpath_free (cx_xpath_t *xpath) /* {{{ */
129 {
130 if (xpath == NULL)
131 return;
133 sfree (xpath->path);
134 sfree (xpath->type);
135 sfree (xpath->instance_prefix);
136 sfree (xpath->instance);
137 sfree (xpath->values);
138 sfree (xpath);
139 } /* }}} void cx_xpath_free */
141 static void cx_tree_free (c_avl_tree_t *tree) /* {{{ */
142 {
143 char *name;
144 void *value;
146 while (c_avl_pick (tree, (void *) &name, (void *) &value) == 0)
147 {
148 cx_xpath_t *key = (cx_xpath_t *)value;
150 if (CX_IS_KEY(key))
151 cx_xpath_free (key);
152 else
153 cx_tree_free ((c_avl_tree_t *)value);
155 sfree (name);
156 }
158 c_avl_destroy (tree);
159 } /* }}} void cx_tree_free */
161 static void cx_free (void *arg) /* {{{ */
162 {
163 cx_t *db;
165 DEBUG ("curl_xml plugin: cx_free (arg = %p);", arg);
167 db = (cx_t *) arg;
169 if (db == NULL)
170 return;
172 if (db->curl != NULL)
173 curl_easy_cleanup (db->curl);
174 db->curl = NULL;
176 if (db->tree != NULL)
177 cx_tree_free (db->tree);
178 db->tree = NULL;
180 sfree (db->buffer);
181 sfree (db->instance);
182 sfree (db->host);
184 sfree (db->url);
185 sfree (db->user);
186 sfree (db->pass);
187 sfree (db->credentials);
188 sfree (db->cacert);
190 sfree (db);
191 } /* }}} void cx_free */
193 static int cx_check_type (cx_xpath_t *xpath) /* {{{ */
194 {
195 const data_set_t *ds;
197 ds = plugin_get_ds (xpath->type);
198 if (!ds)
199 {
200 WARNING ("curl_xml plugin: DataSet `%s' not defined.", xpath->type);
201 return (-1);
202 }
204 if (ds->ds_num != xpath->values_len)
205 {
206 WARNING ("curl_xml plugin: DataSet `%s' requires %i values, but config talks about %i",
207 xpath->type, ds->ds_num, xpath->values_len);
208 return (-1);
209 }
211 return (0);
212 } /* }}} cx_check_type */
214 static xmlXPathObjectPtr cx_evaluate_xpath (xmlXPathContextPtr xpath_ctx, /* {{{ */
215 xmlChar *expr)
216 {
217 xmlXPathObjectPtr xpath_obj;
219 /* XXX: When to free this? */
220 xpath_obj = xmlXPathEvalExpression(BAD_CAST expr, xpath_ctx);
221 if (xpath_obj == NULL)
222 {
223 WARNING ("curl_xml plugin: "
224 "Error unable to evaluate xpath expression \"%s\". Skipping...", expr);
225 return NULL;
226 }
228 return xpath_obj;
229 } /* }}} cx_evaluate_xpath */
231 static int cx_if_not_text_node (xmlNodePtr node) /* {{{ */
232 {
233 if (node->type == XML_TEXT_NODE || node->type == XML_ATTRIBUTE_NODE)
234 return (0);
236 WARNING ("curl_xml plugin: "
237 "Node \"%s\" doesn't seem to be a text node. Skipping...", node->name);
238 return -1;
239 } /* }}} cx_if_not_text_node */
241 static int cx_handle_single_value_xpath (xmlXPathContextPtr xpath_ctx, /* {{{ */
242 cx_xpath_t *xpath,
243 const data_set_t *ds, value_list_t *vl, int index)
244 {
245 xmlXPathObjectPtr values_node_obj;
246 xmlNodeSetPtr values_node;
247 int tmp_size;
248 char *node_value;
250 values_node_obj = cx_evaluate_xpath (xpath_ctx, BAD_CAST xpath->values[index].path);
251 if (values_node_obj == NULL)
252 return (-1); /* Error already logged. */
254 values_node = values_node_obj->nodesetval;
255 tmp_size = (values_node) ? values_node->nodeNr : 0;
257 if (tmp_size == 0)
258 {
259 WARNING ("curl_xml plugin: "
260 "relative xpath expression \"%s\" doesn't match any of the nodes. "
261 "Skipping...", xpath->values[index].path);
262 xmlXPathFreeObject (values_node_obj);
263 return (-1);
264 }
266 if (tmp_size > 1)
267 {
268 WARNING ("curl_xml plugin: "
269 "relative xpath expression \"%s\" is expected to return "
270 "only one node. Skipping...", xpath->values[index].path);
271 xmlXPathFreeObject (values_node_obj);
272 return (-1);
273 }
275 /* ignoring the element if other than textnode/attribute*/
276 if (cx_if_not_text_node(values_node->nodeTab[0]))
277 {
278 WARNING ("curl_xml plugin: "
279 "relative xpath expression \"%s\" is expected to return "
280 "only text/attribute node which is not the case. Skipping...",
281 xpath->values[index].path);
282 xmlXPathFreeObject (values_node_obj);
283 return (-1);
284 }
286 node_value = (char *) xmlNodeGetContent(values_node->nodeTab[0]);
287 switch (ds->ds[index].type)
288 {
289 case DS_TYPE_COUNTER:
290 vl->values[index].counter = (counter_t) strtoull (node_value,
291 /* endptr = */ NULL, /* base = */ 0);
292 break;
293 case DS_TYPE_DERIVE:
294 vl->values[index].derive = (derive_t) strtoll (node_value,
295 /* endptr = */ NULL, /* base = */ 0);
296 break;
297 case DS_TYPE_ABSOLUTE:
298 vl->values[index].absolute = (absolute_t) strtoull (node_value,
299 /* endptr = */ NULL, /* base = */ 0);
300 break;
301 case DS_TYPE_GAUGE:
302 vl->values[index].gauge = (gauge_t) strtod (node_value,
303 /* endptr = */ NULL);
304 }
306 /* free up object */
307 xmlXPathFreeObject (values_node_obj);
309 /* We have reached here which means that
310 * we have got something to work */
311 return (0);
312 } /* }}} int cx_handle_single_value_xpath */
314 static int cx_handle_all_value_xpaths (xmlXPathContextPtr xpath_ctx, /* {{{ */
315 cx_xpath_t *xpath,
316 const data_set_t *ds, value_list_t *vl)
317 {
318 value_t values[xpath->values_len];
319 int status;
320 int i;
322 assert (xpath->values_len > 0);
323 assert (xpath->values_len == vl->values_len);
324 assert (xpath->values_len == ds->ds_num);
325 vl->values = values;
327 for (i = 0; i < xpath->values_len; i++)
328 {
329 status = cx_handle_single_value_xpath (xpath_ctx, xpath, ds, vl, i);
330 if (status != 0)
331 return (-1); /* An error has been printed. */
332 } /* for (i = 0; i < xpath->values_len; i++) */
334 plugin_dispatch_values (vl);
335 vl->values = NULL;
337 return (0);
338 } /* }}} int cx_handle_all_value_xpaths */
340 static int cx_handle_instance_xpath (xmlXPathContextPtr xpath_ctx, /* {{{ */
341 cx_xpath_t *xpath, value_list_t *vl,
342 _Bool is_table)
343 {
344 xmlXPathObjectPtr instance_node_obj = NULL;
345 xmlNodeSetPtr instance_node = NULL;
347 memset (vl->type_instance, 0, sizeof (vl->type_instance));
349 if (is_table && (vl->type_instance == NULL))
350 {
351 WARNING ("curl_xml plugin: "
352 "Base-XPath %s is a table, but no instance-XPath has been defined.",
353 xpath->path);
354 return (-1);
355 }
357 /* instance has to be an xpath expression */
358 if (xpath->instance != NULL)
359 {
360 int tmp_size;
362 instance_node_obj = cx_evaluate_xpath (xpath_ctx, BAD_CAST xpath->instance);
363 if (instance_node_obj == NULL)
364 return (-1); /* error is logged already */
366 instance_node = instance_node_obj->nodesetval;
367 tmp_size = (instance_node) ? instance_node->nodeNr : 0;
369 if ( (tmp_size == 0) && (is_table) )
370 {
371 WARNING ("curl_xml plugin: "
372 "relative xpath expression for 'Instance' \"%s\" doesn't match "
373 "any of the nodes. Skipping the node.", xpath->instance);
374 xmlXPathFreeObject (instance_node_obj);
375 return (-1);
376 }
378 if (tmp_size > 1)
379 {
380 WARNING ("curl_xml plugin: "
381 "relative xpath expression for 'Instance' \"%s\" is expected "
382 "to return only one text node. Skipping the node.", xpath->instance);
383 xmlXPathFreeObject (instance_node_obj);
384 return (-1);
385 }
387 /* ignoring the element if other than textnode/attribute */
388 if (cx_if_not_text_node(instance_node->nodeTab[0]))
389 {
390 WARNING ("curl_xml plugin: "
391 "relative xpath expression \"%s\" is expected to return only text node "
392 "which is not the case. Skipping the node.", xpath->instance);
393 xmlXPathFreeObject (instance_node_obj);
394 return (-1);
395 }
396 } /* if (xpath->instance != NULL) */
398 if (xpath->instance_prefix != NULL)
399 {
400 if (instance_node != NULL)
401 ssnprintf (vl->type_instance, sizeof (vl->type_instance),"%s-%s",
402 xpath->instance_prefix, (char *) xmlNodeGetContent(instance_node->nodeTab[0]));
403 else
404 sstrncpy (vl->type_instance, xpath->instance_prefix,
405 sizeof (vl->type_instance));
406 }
407 else
408 {
409 /* If instance_prefix and instance_node are NULL, then
410 * don't set the type_instance */
411 if (instance_node != NULL)
412 sstrncpy (vl->type_instance, (char *) xmlNodeGetContent(instance_node->nodeTab[0]),
413 sizeof (vl->type_instance));
414 }
416 /* Free `instance_node_obj' this late, because `instance_node' points to
417 * somewhere inside this structure. */
418 xmlXPathFreeObject (instance_node_obj);
420 return (0);
421 } /* }}} int cx_handle_instance_xpath */
423 static int cx_handle_base_xpath (char *plugin_instance, /* {{{ */
424 xmlXPathContextPtr xpath_ctx,
425 char *base_xpath, cx_xpath_t *xpath)
426 {
427 int total_nodes;
428 int i;
430 xmlXPathObjectPtr base_node_obj = NULL;
431 xmlNodeSetPtr base_nodes = NULL;
433 value_list_t vl = VALUE_LIST_INIT;
434 const data_set_t *ds;
436 base_node_obj = cx_evaluate_xpath (xpath_ctx, BAD_CAST base_xpath);
437 if (base_node_obj == NULL)
438 return -1; /* error is logged already */
440 base_nodes = base_node_obj->nodesetval;
441 total_nodes = (base_nodes) ? base_nodes->nodeNr : 0;
443 if (total_nodes == 0)
444 {
445 ERROR ("curl_xml plugin: "
446 "xpath expression \"%s\" doesn't match any of the node. Skipping...", base_xpath);
447 xmlXPathFreeObject (base_node_obj);
448 return -1;
449 }
451 /* If base_xpath returned multiple results, then */
452 /* Instance in the xpath block is required */
453 if (total_nodes > 1 && xpath->instance == NULL)
454 {
455 ERROR ("curl_xml plugin: "
456 "Instance is must in xpath block since the base xpath expression \"%s\" "
457 "returned multiple results. Skipping the xpath block...", base_xpath);
458 return -1;
459 }
461 /* set the values for the value_list */
462 ds = plugin_get_ds (xpath->type);
463 vl.values_len = ds->ds_num;
464 sstrncpy (vl.type, xpath->type, sizeof (vl.type));
465 sstrncpy (vl.plugin, "curl_xml", sizeof (vl.plugin));
466 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
467 if (plugin_instance != NULL)
468 sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
470 for (i = 0; i < total_nodes; i++)
471 {
472 int status;
474 xpath_ctx->node = base_nodes->nodeTab[i];
476 status = cx_handle_instance_xpath (xpath_ctx, xpath, &vl,
477 /* is_table = */ (total_nodes > 1)); /* FIXME */
478 if (status != 0)
479 continue; /* An error has already been reported. */
481 status = cx_handle_all_value_xpaths (xpath_ctx, xpath, ds, &vl);
482 if (status != 0)
483 continue; /* An error has been logged. */
484 } /* for (i = 0; i < total_nodes; i++) */
486 /* free up the allocated memory */
487 xmlXPathFreeObject (base_node_obj);
489 return (0);
490 } /* }}} cx_handle_base_xpath */
492 static int cx_handle_parsed_xml(xmlDocPtr doc, /* {{{ */
493 xmlXPathContextPtr xpath_ctx, cx_t *db)
494 {
495 c_avl_iterator_t *iter;
496 char *key;
497 cx_xpath_t *value;
498 int status=-1;
500 iter = c_avl_get_iterator (db->tree);
501 while (c_avl_iterator_next (iter, (void *) &key, (void *) &value) == 0)
502 {
503 if (cx_check_type(value) == -1)
504 continue;
506 if (cx_handle_base_xpath(db->instance, xpath_ctx, key, value) == 0)
507 status = 0; /* we got atleast one success */
508 } /* while (c_avl_iterator_next) */
510 return status;
511 } /* }}} cx_handle_parsed_xml */
513 static int cx_parse_stats_xml(xmlChar* xml, cx_t *db) /* {{{ */
514 {
515 int status;
516 xmlDocPtr doc;
517 xmlXPathContextPtr xpath_ctx;
519 /* Load the XML */
520 doc = xmlParseDoc(xml);
521 if (doc == NULL)
522 {
523 ERROR ("curl_xml plugin: Failed to parse the xml document - %s", xml);
524 return (-1);
525 }
527 xpath_ctx = xmlXPathNewContext(doc);
528 if(xpath_ctx == NULL)
529 {
530 ERROR ("curl_xml plugin: Failed to create the xml context");
531 xmlFreeDoc(doc);
532 return (-1);
533 }
535 status = cx_handle_parsed_xml (doc, xpath_ctx, db);
536 /* Cleanup */
537 xmlXPathFreeContext(xpath_ctx);
538 xmlFreeDoc(doc);
539 return status;
540 } /* }}} cx_parse_stats_xml */
542 static int cx_curl_perform (cx_t *db, CURL *curl) /* {{{ */
543 {
544 int status;
545 long rc;
546 char *ptr;
547 char *url;
549 db->buffer_fill = 0;
550 status = curl_easy_perform (curl);
552 curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &url);
553 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &rc);
555 if (rc != 200)
556 {
557 ERROR ("curl_xml plugin: curl_easy_perform failed with response code %ld (%s)",
558 rc, url);
559 return (-1);
560 }
562 if (status != 0)
563 {
564 ERROR ("curl_xml plugin: curl_easy_perform failed with status %i: %s (%s)",
565 status, db->curl_errbuf, url);
566 return (-1);
567 }
569 ptr = db->buffer;
571 status = cx_parse_stats_xml(BAD_CAST ptr, db);
572 db->buffer_fill = 0;
574 return status;
575 } /* }}} int cx_curl_perform */
577 static int cx_read (user_data_t *ud) /* {{{ */
578 {
579 cx_t *db;
581 if ((ud == NULL) || (ud->data == NULL))
582 {
583 ERROR ("curl_xml plugin: cx_read: Invalid user data.");
584 return (-1);
585 }
587 db = (cx_t *) ud->data;
589 return cx_curl_perform (db, db->curl);
590 } /* }}} int cx_read */
592 /* Configuration handling functions {{{ */
594 static int cx_config_add_values (const char *name, cx_xpath_t *xpath, /* {{{ */
595 oconfig_item_t *ci)
596 {
597 int i;
599 if (ci->values_num < 1)
600 {
601 WARNING ("curl_xml plugin: `Values' needs at least one argument.");
602 return (-1);
603 }
605 for (i = 0; i < ci->values_num; i++)
606 if (ci->values[i].type != OCONFIG_TYPE_STRING)
607 {
608 WARNING ("curl_xml plugin: `Values' needs only string argument.");
609 return (-1);
610 }
612 sfree (xpath->values);
614 xpath->values_len = 0;
615 xpath->values = (cx_values_t *) malloc (sizeof (cx_values_t) * ci->values_num);
616 if (xpath->values == NULL)
617 return (-1);
618 xpath->values_len = ci->values_num;
620 /* populate cx_values_t structure */
621 for (i = 0; i < ci->values_num; i++)
622 {
623 xpath->values[i].path_len = sizeof (ci->values[i].value.string);
624 sstrncpy (xpath->values[i].path, ci->values[i].value.string, sizeof (xpath->values[i].path));
625 }
627 return (0);
628 } /* }}} cx_config_add_values */
630 static c_avl_tree_t *cx_avl_create(void) /* {{{ */
631 {
632 return c_avl_create ((int (*) (const void *, const void *)) strcmp);
633 } /* }}} cx_avl_create */
635 static int cx_config_add_xpath (cx_t *db, /* {{{ */
636 oconfig_item_t *ci)
637 {
638 cx_xpath_t *xpath;
639 int status;
640 int i;
642 if ((ci->values_num != 1)
643 || (ci->values[0].type != OCONFIG_TYPE_STRING))
644 {
645 WARNING ("curl_xml plugin: The `xpath' block "
646 "needs exactly one string argument.");
647 return (-1);
648 }
650 xpath = (cx_xpath_t *) malloc (sizeof (*xpath));
651 if (xpath == NULL)
652 {
653 ERROR ("curl_xml plugin: malloc failed.");
654 return (-1);
655 }
656 memset (xpath, 0, sizeof (*xpath));
657 xpath->magic = CX_KEY_MAGIC;
659 if (strcasecmp ("xpath", ci->key) == 0)
660 {
661 status = cf_util_get_string (ci, &xpath->path);
662 if (status != 0)
663 {
664 sfree (xpath);
665 return (status);
666 }
667 }
668 else
669 {
670 ERROR ("curl_xml plugin: cx_config: "
671 "Invalid key: %s", ci->key);
672 return (-1);
673 }
675 status = 0;
676 for (i = 0; i < ci->children_num; i++)
677 {
678 oconfig_item_t *child = ci->children + i;
680 if (strcasecmp ("Type", child->key) == 0)
681 status = cf_util_get_string (child, &xpath->type);
682 else if (strcasecmp ("InstancePrefix", child->key) == 0)
683 status = cf_util_get_string (child, &xpath->instance_prefix);
684 else if (strcasecmp ("Instance", child->key) == 0)
685 status = cf_util_get_string (child, &xpath->instance);
686 else if (strcasecmp ("Values", child->key) == 0)
687 status = cx_config_add_values ("Values", xpath, child);
688 else
689 {
690 WARNING ("curl_xml plugin: Option `%s' not allowed here.", child->key);
691 status = -1;
692 }
694 if (status != 0)
695 break;
696 } /* for (i = 0; i < ci->children_num; i++) */
698 while (status == 0)
699 {
700 if (xpath->type == NULL)
701 {
702 WARNING ("curl_xml plugin: `Type' missing in `xpath' block.");
703 status = -1;
704 }
706 break;
707 } /* while (status == 0) */
709 if (status == 0)
710 {
711 char *name;
712 c_avl_tree_t *tree;
714 if (db->tree == NULL)
715 db->tree = cx_avl_create();
717 tree = db->tree;
718 name = xpath->path;
720 if (*name)
721 c_avl_insert (tree, strdup(name), xpath);
722 else
723 {
724 ERROR ("curl_xml plugin: invalid key: %s", xpath->path);
725 status = -1;
726 }
727 }
729 return (status);
730 } /* }}} int cx_config_add_xpath */
732 /* Initialize db->curl */
733 static int cx_init_curl (cx_t *db) /* {{{ */
734 {
735 db->curl = curl_easy_init ();
736 if (db->curl == NULL)
737 {
738 ERROR ("curl_xml plugin: curl_easy_init failed.");
739 return (-1);
740 }
742 curl_easy_setopt (db->curl, CURLOPT_WRITEFUNCTION, cx_curl_callback);
743 curl_easy_setopt (db->curl, CURLOPT_WRITEDATA, db);
744 curl_easy_setopt (db->curl, CURLOPT_USERAGENT,
745 PACKAGE_NAME"/"PACKAGE_VERSION);
746 curl_easy_setopt (db->curl, CURLOPT_ERRORBUFFER, db->curl_errbuf);
747 curl_easy_setopt (db->curl, CURLOPT_URL, db->url);
749 if (db->user != NULL)
750 {
751 size_t credentials_size;
753 credentials_size = strlen (db->user) + 2;
754 if (db->pass != NULL)
755 credentials_size += strlen (db->pass);
757 db->credentials = (char *) malloc (credentials_size);
758 if (db->credentials == NULL)
759 {
760 ERROR ("curl_xml plugin: malloc failed.");
761 return (-1);
762 }
764 ssnprintf (db->credentials, credentials_size, "%s:%s",
765 db->user, (db->pass == NULL) ? "" : db->pass);
766 curl_easy_setopt (db->curl, CURLOPT_USERPWD, db->credentials);
767 }
769 curl_easy_setopt (db->curl, CURLOPT_SSL_VERIFYPEER, db->verify_peer);
770 curl_easy_setopt (db->curl, CURLOPT_SSL_VERIFYHOST,
771 db->verify_host ? 2 : 0);
772 if (db->cacert != NULL)
773 curl_easy_setopt (db->curl, CURLOPT_CAINFO, db->cacert);
775 return (0);
776 } /* }}} int cx_init_curl */
778 static int cx_config_add_url (oconfig_item_t *ci) /* {{{ */
779 {
780 cx_t *db;
781 int status = 0;
782 int i;
784 if ((ci->values_num != 1)
785 || (ci->values[0].type != OCONFIG_TYPE_STRING))
786 {
787 WARNING ("curl_xml plugin: The `URL' block "
788 "needs exactly one string argument.");
789 return (-1);
790 }
792 db = (cx_t *) malloc (sizeof (*db));
793 if (db == NULL)
794 {
795 ERROR ("curl_xml plugin: malloc failed.");
796 return (-1);
797 }
798 memset (db, 0, sizeof (*db));
800 if (strcasecmp ("URL", ci->key) == 0)
801 {
802 status = cf_util_get_string (ci, &db->url);
803 if (status != 0)
804 {
805 sfree (db);
806 return (status);
807 }
808 }
809 else
810 {
811 ERROR ("curl_xml plugin: cx_config: "
812 "Invalid key: %s", ci->key);
813 return (-1);
814 }
816 /* Fill the `cx_t' structure.. */
817 for (i = 0; i < ci->children_num; i++)
818 {
819 oconfig_item_t *child = ci->children + i;
821 if (strcasecmp ("Instance", child->key) == 0)
822 status = cf_util_get_string (child, &db->instance);
823 else if (strcasecmp ("Host", child->key) == 0)
824 status = cf_util_get_string (child, &db->host);
825 else if (strcasecmp ("User", child->key) == 0)
826 status = cf_util_get_string (child, &db->user);
827 else if (strcasecmp ("Password", child->key) == 0)
828 status = cf_util_get_string (child, &db->pass);
829 else if (strcasecmp ("VerifyPeer", child->key) == 0)
830 status = cf_util_get_boolean (child, &db->verify_peer);
831 else if (strcasecmp ("VerifyHost", child->key) == 0)
832 status = cf_util_get_boolean (child, &db->verify_host);
833 else if (strcasecmp ("CACert", child->key) == 0)
834 status = cf_util_get_string (child, &db->cacert);
835 else if (strcasecmp ("xpath", child->key) == 0)
836 status = cx_config_add_xpath (db, child);
837 else
838 {
839 WARNING ("curl_xml plugin: Option `%s' not allowed here.", child->key);
840 status = -1;
841 }
843 if (status != 0)
844 break;
845 }
847 if (status == 0)
848 {
849 if (db->tree == NULL)
850 {
851 WARNING ("curl_xml plugin: No (valid) `Key' block "
852 "within `URL' block `%s'.", db->url);
853 status = -1;
854 }
855 if (status == 0)
856 status = cx_init_curl (db);
857 }
859 /* If all went well, register this database for reading */
860 if (status == 0)
861 {
862 user_data_t ud;
863 char cb_name[DATA_MAX_NAME_LEN];
865 if (db->instance == NULL)
866 db->instance = strdup("default");
868 DEBUG ("curl_xml plugin: Registering new read callback: %s",
869 db->instance);
871 memset (&ud, 0, sizeof (ud));
872 ud.data = (void *) db;
873 ud.free_func = cx_free;
875 ssnprintf (cb_name, sizeof (cb_name), "curl_xml-%s-%s",
876 db->instance, db->url);
878 plugin_register_complex_read (cb_name, cx_read,
879 /* interval = */ NULL, &ud);
880 }
881 else
882 {
883 cx_free (db);
884 return (-1);
885 }
887 return (0);
888 } /* }}} int cx_config_add_url */
890 /* }}} End of configuration handling functions */
892 static int cx_config (oconfig_item_t *ci) /* {{{ */
893 {
894 int success;
895 int errors;
896 int status;
897 int i;
899 success = 0;
900 errors = 0;
902 for (i = 0; i < ci->children_num; i++)
903 {
904 oconfig_item_t *child = ci->children + i;
906 if (strcasecmp ("URL", child->key) == 0)
907 {
908 status = cx_config_add_url (child);
909 if (status == 0)
910 success++;
911 else
912 errors++;
913 }
914 else
915 {
916 WARNING ("curl_xml plugin: Option `%s' not allowed here.", child->key);
917 errors++;
918 }
919 }
921 if ((success == 0) && (errors > 0))
922 {
923 ERROR ("curl_xml plugin: All statements failed.");
924 return (-1);
925 }
927 return (0);
928 } /* }}} int cx_config */
930 void module_register (void)
931 {
932 plugin_register_complex_config ("curl_xml", cx_config);
933 } /* void module_register */
935 /* vim: set sw=2 sts=2 et fdm=marker : */