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_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"
36 /*
37 * Private data structures
38 */
39 struct cx_values_s /* {{{ */
40 {
41 char path[DATA_MAX_NAME_LEN];
42 size_t path_len;
43 };
44 typedef struct cx_values_s cx_values_t;
45 /* }}} */
47 struct cx_xpath_s /* {{{ */
48 {
49 char *path;
50 char *type;
51 cx_values_t *values;
52 int values_len;
53 char *instance_prefix;
54 char *instance;
55 int is_table;
56 unsigned long magic;
57 };
58 typedef struct cx_xpath_s cx_xpath_t;
59 /* }}} */
61 struct cx_s /* {{{ */
62 {
63 char *instance;
64 char *host;
66 char *url;
67 char *user;
68 char *pass;
69 char *credentials;
70 _Bool verify_peer;
71 _Bool verify_host;
72 char *cacert;
74 CURL *curl;
75 char curl_errbuf[CURL_ERROR_SIZE];
76 char *buffer;
77 size_t buffer_size;
78 size_t buffer_fill;
80 llist_t *list; /* list of xpath blocks */
81 };
82 typedef struct cx_s cx_t; /* }}} */
84 /*
85 * Private functions
86 */
87 static size_t cx_curl_callback (void *buf, /* {{{ */
88 size_t size, size_t nmemb, void *user_data)
89 {
90 size_t len = size * nmemb;
91 cx_t *db;
93 db = user_data;
94 if (db == NULL)
95 {
96 ERROR ("curl_xml plugin: cx_curl_callback: "
97 "user_data pointer is NULL.");
98 return (0);
99 }
101 if (len <= 0)
102 return (len);
104 if ((db->buffer_fill + len) >= db->buffer_size)
105 {
106 char *temp;
108 temp = (char *) realloc (db->buffer,
109 db->buffer_fill + len + 1);
110 if (temp == NULL)
111 {
112 ERROR ("curl_xml plugin: realloc failed.");
113 return (0);
114 }
115 db->buffer = temp;
116 db->buffer_size = db->buffer_fill + len + 1;
117 }
119 memcpy (db->buffer + db->buffer_fill, (char *) buf, len);
120 db->buffer_fill += len;
121 db->buffer[db->buffer_fill] = 0;
123 return (len);
124 } /* }}} size_t cx_curl_callback */
126 static void cx_xpath_free (cx_xpath_t *xpath) /* {{{ */
127 {
128 if (xpath == NULL)
129 return;
131 sfree (xpath->path);
132 sfree (xpath->type);
133 sfree (xpath->instance_prefix);
134 sfree (xpath->instance);
135 sfree (xpath->values);
136 sfree (xpath);
137 } /* }}} void cx_xpath_free */
139 static void cx_list_free (llist_t *list) /* {{{ */
140 {
141 llentry_t *le;
143 le = llist_head (list);
144 while (le != NULL)
145 {
146 llentry_t *le_next;
148 le_next = le->next;
150 sfree (le->key);
151 cx_xpath_free (le->value);
153 le = le_next;
154 }
156 llist_destroy (list);
157 list = NULL;
158 } /* }}} void cx_list_free */
160 static void cx_free (void *arg) /* {{{ */
161 {
162 cx_t *db;
164 DEBUG ("curl_xml plugin: cx_free (arg = %p);", arg);
166 db = (cx_t *) arg;
168 if (db == NULL)
169 return;
171 if (db->curl != NULL)
172 curl_easy_cleanup (db->curl);
173 db->curl = NULL;
175 if (db->list != NULL)
176 cx_list_free (db->list);
178 sfree (db->buffer);
179 sfree (db->instance);
180 sfree (db->host);
182 sfree (db->url);
183 sfree (db->user);
184 sfree (db->pass);
185 sfree (db->credentials);
186 sfree (db->cacert);
188 sfree (db);
189 } /* }}} void cx_free */
191 static int cx_check_type (const data_set_t *ds, cx_xpath_t *xpath) /* {{{ */
192 {
193 if (!ds)
194 {
195 WARNING ("curl_xml plugin: DataSet `%s' not defined.", xpath->type);
196 return (-1);
197 }
199 if (ds->ds_num != xpath->values_len)
200 {
201 WARNING ("curl_xml plugin: DataSet `%s' requires %i values, but config talks about %i",
202 xpath->type, ds->ds_num, xpath->values_len);
203 return (-1);
204 }
206 return (0);
207 } /* }}} cx_check_type */
209 static xmlXPathObjectPtr cx_evaluate_xpath (xmlXPathContextPtr xpath_ctx, /* {{{ */
210 xmlChar *expr)
211 {
212 xmlXPathObjectPtr xpath_obj;
214 /* XXX: When to free this? */
215 xpath_obj = xmlXPathEvalExpression(BAD_CAST expr, xpath_ctx);
216 if (xpath_obj == NULL)
217 {
218 WARNING ("curl_xml plugin: "
219 "Error unable to evaluate xpath expression \"%s\". Skipping...", expr);
220 return NULL;
221 }
223 return xpath_obj;
224 } /* }}} cx_evaluate_xpath */
226 static int cx_if_not_text_node (xmlNodePtr node) /* {{{ */
227 {
228 if (node->type == XML_TEXT_NODE || node->type == XML_ATTRIBUTE_NODE)
229 return (0);
231 WARNING ("curl_xml plugin: "
232 "Node \"%s\" doesn't seem to be a text node. Skipping...", node->name);
233 return -1;
234 } /* }}} cx_if_not_text_node */
236 static int cx_handle_single_value_xpath (xmlXPathContextPtr xpath_ctx, /* {{{ */
237 cx_xpath_t *xpath,
238 const data_set_t *ds, value_list_t *vl, int index)
239 {
240 xmlXPathObjectPtr values_node_obj;
241 xmlNodeSetPtr values_node;
242 int tmp_size;
243 char *node_value;
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;
252 if (tmp_size == 0)
253 {
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);
258 return (-1);
259 }
261 if (tmp_size > 1)
262 {
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);
267 return (-1);
268 }
270 /* ignoring the element if other than textnode/attribute*/
271 if (cx_if_not_text_node(values_node->nodeTab[0]))
272 {
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);
278 return (-1);
279 }
281 node_value = (char *) xmlNodeGetContent(values_node->nodeTab[0]);
282 switch (ds->ds[index].type)
283 {
284 case DS_TYPE_COUNTER:
285 vl->values[index].counter = (counter_t) strtoull (node_value,
286 /* endptr = */ NULL, /* base = */ 0);
287 break;
288 case DS_TYPE_DERIVE:
289 vl->values[index].derive = (derive_t) strtoll (node_value,
290 /* endptr = */ NULL, /* base = */ 0);
291 break;
292 case DS_TYPE_ABSOLUTE:
293 vl->values[index].absolute = (absolute_t) strtoull (node_value,
294 /* endptr = */ NULL, /* base = */ 0);
295 break;
296 case DS_TYPE_GAUGE:
297 vl->values[index].gauge = (gauge_t) strtod (node_value,
298 /* endptr = */ NULL);
299 }
301 /* free up object */
302 xmlXPathFreeObject (values_node_obj);
304 /* We have reached here which means that
305 * we have got something to work */
306 return (0);
307 } /* }}} int cx_handle_single_value_xpath */
309 static int cx_handle_all_value_xpaths (xmlXPathContextPtr xpath_ctx, /* {{{ */
310 cx_xpath_t *xpath,
311 const data_set_t *ds, value_list_t *vl)
312 {
313 value_t values[xpath->values_len];
314 int status;
315 int i;
317 assert (xpath->values_len > 0);
318 assert (xpath->values_len == vl->values_len);
319 assert (xpath->values_len == ds->ds_num);
320 vl->values = values;
322 for (i = 0; i < xpath->values_len; i++)
323 {
324 status = cx_handle_single_value_xpath (xpath_ctx, xpath, ds, vl, i);
325 if (status != 0)
326 return (-1); /* An error has been printed. */
327 } /* for (i = 0; i < xpath->values_len; i++) */
329 plugin_dispatch_values (vl);
330 vl->values = NULL;
332 return (0);
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,
337 _Bool is_table)
338 {
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))
348 {
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.",
352 xpath->path);
353 return (-1);
354 }
356 /* instance has to be an xpath expression */
357 if (xpath->instance != NULL)
358 {
359 int tmp_size;
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;
368 if ( (tmp_size == 0) && (is_table) )
369 {
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);
374 return (-1);
375 }
377 if (tmp_size > 1)
378 {
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);
383 return (-1);
384 }
386 /* ignoring the element if other than textnode/attribute */
387 if (cx_if_not_text_node(instance_node->nodeTab[0]))
388 {
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);
393 return (-1);
394 }
395 } /* if (xpath->instance != NULL) */
397 if (xpath->instance_prefix != NULL)
398 {
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]));
402 else
403 sstrncpy (vl->type_instance, xpath->instance_prefix,
404 sizeof (vl->type_instance));
405 }
406 else
407 {
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));
413 }
415 /* Free `instance_node_obj' this late, because `instance_node' points to
416 * somewhere inside this structure. */
417 xmlXPathFreeObject (instance_node_obj);
419 return (0);
420 } /* }}} int cx_handle_instance_xpath */
422 static int cx_handle_base_xpath (char *plugin_instance, /* {{{ */
423 xmlXPathContextPtr xpath_ctx, const data_set_t *ds,
424 char *base_xpath, cx_xpath_t *xpath)
425 {
426 int total_nodes;
427 int i;
429 xmlXPathObjectPtr base_node_obj = NULL;
430 xmlNodeSetPtr base_nodes = NULL;
432 value_list_t vl = VALUE_LIST_INIT;
434 base_node_obj = cx_evaluate_xpath (xpath_ctx, BAD_CAST base_xpath);
435 if (base_node_obj == NULL)
436 return -1; /* error is logged already */
438 base_nodes = base_node_obj->nodesetval;
439 total_nodes = (base_nodes) ? base_nodes->nodeNr : 0;
441 if (total_nodes == 0)
442 {
443 ERROR ("curl_xml plugin: "
444 "xpath expression \"%s\" doesn't match any of the nodes. "
445 "Skipping the xpath block...", base_xpath);
446 xmlXPathFreeObject (base_node_obj);
447 return -1;
448 }
450 /* If base_xpath returned multiple results, then */
451 /* Instance in the xpath block is required */
452 if (total_nodes > 1 && xpath->instance == NULL)
453 {
454 ERROR ("curl_xml plugin: "
455 "InstanceFrom is must in xpath block since the base xpath expression \"%s\" "
456 "returned multiple results. Skipping the xpath block...", base_xpath);
457 return -1;
458 }
460 /* set the values for the value_list */
461 vl.values_len = ds->ds_num;
462 sstrncpy (vl.type, xpath->type, sizeof (vl.type));
463 sstrncpy (vl.plugin, "curl_xml", sizeof (vl.plugin));
464 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
465 if (plugin_instance != NULL)
466 sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
468 for (i = 0; i < total_nodes; i++)
469 {
470 int status;
472 xpath_ctx->node = base_nodes->nodeTab[i];
474 status = cx_handle_instance_xpath (xpath_ctx, xpath, &vl,
475 /* is_table = */ (total_nodes > 1));
476 if (status != 0)
477 continue; /* An error has already been reported. */
479 status = cx_handle_all_value_xpaths (xpath_ctx, xpath, ds, &vl);
480 if (status != 0)
481 continue; /* An error has been logged. */
482 } /* for (i = 0; i < total_nodes; i++) */
484 /* free up the allocated memory */
485 xmlXPathFreeObject (base_node_obj);
487 return (0);
488 } /* }}} cx_handle_base_xpath */
490 static int cx_handle_parsed_xml(xmlDocPtr doc, /* {{{ */
491 xmlXPathContextPtr xpath_ctx, cx_t *db)
492 {
493 llentry_t *le;
494 const data_set_t *ds;
495 cx_xpath_t *xpath;
496 int status=-1;
499 le = llist_head (db->list);
500 while (le != NULL)
501 {
502 /* get the ds */
503 xpath = (cx_xpath_t *) le->value;
504 ds = plugin_get_ds (xpath->type);
506 if ( (cx_check_type(ds, xpath) == 0) &&
507 (cx_handle_base_xpath(db->instance, xpath_ctx, ds, le->key, xpath) == 0) )
508 status = 0; /* we got atleast one success */
510 le = le->next;
511 } /* while (le != NULL) */
513 return status;
514 } /* }}} cx_handle_parsed_xml */
516 static int cx_parse_stats_xml(xmlChar* xml, cx_t *db) /* {{{ */
517 {
518 int status;
519 xmlDocPtr doc;
520 xmlXPathContextPtr xpath_ctx;
522 /* Load the XML */
523 doc = xmlParseDoc(xml);
524 if (doc == NULL)
525 {
526 ERROR ("curl_xml plugin: Failed to parse the xml document - %s", xml);
527 return (-1);
528 }
530 xpath_ctx = xmlXPathNewContext(doc);
531 if(xpath_ctx == NULL)
532 {
533 ERROR ("curl_xml plugin: Failed to create the xml context");
534 xmlFreeDoc(doc);
535 return (-1);
536 }
538 status = cx_handle_parsed_xml (doc, xpath_ctx, db);
539 /* Cleanup */
540 xmlXPathFreeContext(xpath_ctx);
541 xmlFreeDoc(doc);
542 return status;
543 } /* }}} cx_parse_stats_xml */
545 static int cx_curl_perform (cx_t *db, CURL *curl) /* {{{ */
546 {
547 int status;
548 long rc;
549 char *ptr;
550 char *url;
552 db->buffer_fill = 0;
553 status = curl_easy_perform (curl);
555 curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &url);
556 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &rc);
558 if (rc != 200)
559 {
560 ERROR ("curl_xml plugin: curl_easy_perform failed with response code %ld (%s)",
561 rc, url);
562 return (-1);
563 }
565 if (status != 0)
566 {
567 ERROR ("curl_xml plugin: curl_easy_perform failed with status %i: %s (%s)",
568 status, db->curl_errbuf, url);
569 return (-1);
570 }
572 ptr = db->buffer;
574 status = cx_parse_stats_xml(BAD_CAST ptr, db);
575 db->buffer_fill = 0;
577 return status;
578 } /* }}} int cx_curl_perform */
580 static int cx_read (user_data_t *ud) /* {{{ */
581 {
582 cx_t *db;
584 if ((ud == NULL) || (ud->data == NULL))
585 {
586 ERROR ("curl_xml plugin: cx_read: Invalid user data.");
587 return (-1);
588 }
590 db = (cx_t *) ud->data;
592 return cx_curl_perform (db, db->curl);
593 } /* }}} int cx_read */
595 /* Configuration handling functions {{{ */
597 static int cx_config_add_values (const char *name, cx_xpath_t *xpath, /* {{{ */
598 oconfig_item_t *ci)
599 {
600 int i;
602 if (ci->values_num < 1)
603 {
604 WARNING ("curl_xml plugin: `ValuesFrom' needs at least one argument.");
605 return (-1);
606 }
608 for (i = 0; i < ci->values_num; i++)
609 if (ci->values[i].type != OCONFIG_TYPE_STRING)
610 {
611 WARNING ("curl_xml plugin: `ValuesFrom' needs only string argument.");
612 return (-1);
613 }
615 sfree (xpath->values);
617 xpath->values_len = 0;
618 xpath->values = (cx_values_t *) malloc (sizeof (cx_values_t) * ci->values_num);
619 if (xpath->values == NULL)
620 return (-1);
621 xpath->values_len = ci->values_num;
623 /* populate cx_values_t structure */
624 for (i = 0; i < ci->values_num; i++)
625 {
626 xpath->values[i].path_len = sizeof (ci->values[i].value.string);
627 sstrncpy (xpath->values[i].path, ci->values[i].value.string, sizeof (xpath->values[i].path));
628 }
630 return (0);
631 } /* }}} cx_config_add_values */
633 static int cx_config_add_xpath (cx_t *db, /* {{{ */
634 oconfig_item_t *ci)
635 {
636 cx_xpath_t *xpath;
637 int status;
638 int i;
640 xpath = (cx_xpath_t *) malloc (sizeof (*xpath));
641 if (xpath == NULL)
642 {
643 ERROR ("curl_xml plugin: malloc failed.");
644 return (-1);
645 }
646 memset (xpath, 0, sizeof (*xpath));
648 status = cf_util_get_string (ci, &xpath->path);
649 if (status != 0)
650 {
651 sfree (xpath);
652 return (status);
653 }
655 /* error out if xpath->path is an empty string */
656 if (*xpath->path == 0)
657 {
658 ERROR ("curl_xml plugin: invalid xpath. "
659 "xpath value can't be an empty string");
660 return (-1);
661 }
663 status = 0;
664 for (i = 0; i < ci->children_num; i++)
665 {
666 oconfig_item_t *child = ci->children + i;
668 if (strcasecmp ("Type", child->key) == 0)
669 status = cf_util_get_string (child, &xpath->type);
670 else if (strcasecmp ("InstancePrefix", child->key) == 0)
671 status = cf_util_get_string (child, &xpath->instance_prefix);
672 else if (strcasecmp ("InstanceFrom", child->key) == 0)
673 status = cf_util_get_string (child, &xpath->instance);
674 else if (strcasecmp ("ValuesFrom", child->key) == 0)
675 status = cx_config_add_values ("ValuesFrom", xpath, child);
676 else
677 {
678 WARNING ("curl_xml plugin: Option `%s' not allowed here.", child->key);
679 status = -1;
680 }
682 if (status != 0)
683 break;
684 } /* for (i = 0; i < ci->children_num; i++) */
686 if (status == 0 && xpath->type == NULL)
687 {
688 WARNING ("curl_xml plugin: `Type' missing in `xpath' block.");
689 status = -1;
690 }
692 if (status == 0)
693 {
694 char *name;
695 llentry_t *le;
697 if (db->list == NULL)
698 {
699 db->list = llist_create();
700 if (db->list == NULL)
701 {
702 ERROR ("curl_xml plugin: list creation failed.");
703 return (-1);
704 }
705 }
707 name = strdup(xpath->path);
708 if (name == NULL)
709 {
710 ERROR ("curl_xml plugin: strdup failed.");
711 return (-1);
712 }
714 le = llentry_create (name, xpath);
715 if (le == NULL)
716 {
717 ERROR ("curl_xml plugin: llentry_create failed.");
718 return (-1);
719 }
721 llist_append (db->list, le);
722 }
724 return (status);
725 } /* }}} int cx_config_add_xpath */
727 /* Initialize db->curl */
728 static int cx_init_curl (cx_t *db) /* {{{ */
729 {
730 db->curl = curl_easy_init ();
731 if (db->curl == NULL)
732 {
733 ERROR ("curl_xml plugin: curl_easy_init failed.");
734 return (-1);
735 }
737 curl_easy_setopt (db->curl, CURLOPT_WRITEFUNCTION, cx_curl_callback);
738 curl_easy_setopt (db->curl, CURLOPT_WRITEDATA, db);
739 curl_easy_setopt (db->curl, CURLOPT_USERAGENT,
740 PACKAGE_NAME"/"PACKAGE_VERSION);
741 curl_easy_setopt (db->curl, CURLOPT_ERRORBUFFER, db->curl_errbuf);
742 curl_easy_setopt (db->curl, CURLOPT_URL, db->url);
744 if (db->user != NULL)
745 {
746 size_t credentials_size;
748 credentials_size = strlen (db->user) + 2;
749 if (db->pass != NULL)
750 credentials_size += strlen (db->pass);
752 db->credentials = (char *) malloc (credentials_size);
753 if (db->credentials == NULL)
754 {
755 ERROR ("curl_xml plugin: malloc failed.");
756 return (-1);
757 }
759 ssnprintf (db->credentials, credentials_size, "%s:%s",
760 db->user, (db->pass == NULL) ? "" : db->pass);
761 curl_easy_setopt (db->curl, CURLOPT_USERPWD, db->credentials);
762 }
764 curl_easy_setopt (db->curl, CURLOPT_SSL_VERIFYPEER, db->verify_peer ? 1L : 0L);
765 curl_easy_setopt (db->curl, CURLOPT_SSL_VERIFYHOST,
766 db->verify_host ? 2L : 0L);
767 if (db->cacert != NULL)
768 curl_easy_setopt (db->curl, CURLOPT_CAINFO, db->cacert);
770 return (0);
771 } /* }}} int cx_init_curl */
773 static int cx_config_add_url (oconfig_item_t *ci) /* {{{ */
774 {
775 cx_t *db;
776 int status = 0;
777 int i;
779 if ((ci->values_num != 1)
780 || (ci->values[0].type != OCONFIG_TYPE_STRING))
781 {
782 WARNING ("curl_xml plugin: The `URL' block "
783 "needs exactly one string argument.");
784 return (-1);
785 }
787 db = (cx_t *) malloc (sizeof (*db));
788 if (db == NULL)
789 {
790 ERROR ("curl_xml plugin: malloc failed.");
791 return (-1);
792 }
793 memset (db, 0, sizeof (*db));
795 if (strcasecmp ("URL", ci->key) == 0)
796 {
797 status = cf_util_get_string (ci, &db->url);
798 if (status != 0)
799 {
800 sfree (db);
801 return (status);
802 }
803 }
804 else
805 {
806 ERROR ("curl_xml plugin: cx_config: "
807 "Invalid key: %s", ci->key);
808 return (-1);
809 }
811 /* Fill the `cx_t' structure.. */
812 for (i = 0; i < ci->children_num; i++)
813 {
814 oconfig_item_t *child = ci->children + i;
816 if (strcasecmp ("Instance", child->key) == 0)
817 status = cf_util_get_string (child, &db->instance);
818 else if (strcasecmp ("Host", child->key) == 0)
819 status = cf_util_get_string (child, &db->host);
820 else if (strcasecmp ("User", child->key) == 0)
821 status = cf_util_get_string (child, &db->user);
822 else if (strcasecmp ("Password", child->key) == 0)
823 status = cf_util_get_string (child, &db->pass);
824 else if (strcasecmp ("VerifyPeer", child->key) == 0)
825 status = cf_util_get_boolean (child, &db->verify_peer);
826 else if (strcasecmp ("VerifyHost", child->key) == 0)
827 status = cf_util_get_boolean (child, &db->verify_host);
828 else if (strcasecmp ("CACert", child->key) == 0)
829 status = cf_util_get_string (child, &db->cacert);
830 else if (strcasecmp ("xpath", child->key) == 0)
831 status = cx_config_add_xpath (db, child);
832 else
833 {
834 WARNING ("curl_xml plugin: Option `%s' not allowed here.", child->key);
835 status = -1;
836 }
838 if (status != 0)
839 break;
840 }
842 if (status == 0)
843 {
844 if (db->list == NULL)
845 {
846 WARNING ("curl_xml plugin: No (valid) `Key' block "
847 "within `URL' block `%s'.", db->url);
848 status = -1;
849 }
850 if (status == 0)
851 status = cx_init_curl (db);
852 }
854 /* If all went well, register this database for reading */
855 if (status == 0)
856 {
857 user_data_t ud;
858 char cb_name[DATA_MAX_NAME_LEN];
860 if (db->instance == NULL)
861 db->instance = strdup("default");
863 DEBUG ("curl_xml plugin: Registering new read callback: %s",
864 db->instance);
866 memset (&ud, 0, sizeof (ud));
867 ud.data = (void *) db;
868 ud.free_func = cx_free;
870 ssnprintf (cb_name, sizeof (cb_name), "curl_xml-%s-%s",
871 db->instance, db->url);
873 plugin_register_complex_read (/* group = */ NULL, cb_name, cx_read,
874 /* interval = */ NULL, &ud);
875 }
876 else
877 {
878 cx_free (db);
879 return (-1);
880 }
882 return (0);
883 } /* }}} int cx_config_add_url */
885 /* }}} End of configuration handling functions */
887 static int cx_config (oconfig_item_t *ci) /* {{{ */
888 {
889 int success;
890 int errors;
891 int status;
892 int i;
894 success = 0;
895 errors = 0;
897 for (i = 0; i < ci->children_num; i++)
898 {
899 oconfig_item_t *child = ci->children + i;
901 if (strcasecmp ("URL", child->key) == 0)
902 {
903 status = cx_config_add_url (child);
904 if (status == 0)
905 success++;
906 else
907 errors++;
908 }
909 else
910 {
911 WARNING ("curl_xml plugin: Option `%s' not allowed here.", child->key);
912 errors++;
913 }
914 }
916 if ((success == 0) && (errors > 0))
917 {
918 ERROR ("curl_xml plugin: All statements failed.");
919 return (-1);
920 }
922 return (0);
923 } /* }}} int cx_config */
925 void module_register (void)
926 {
927 plugin_register_complex_config ("curl_xml", cx_config);
928 } /* void module_register */
930 /* vim: set sw=2 sts=2 et fdm=marker : */