53c6e620dd881a34d4e9c2d86ab5468bd8ae942f
1 /**
2 * collectd - src/curl_xml.c
3 * Copyright (C) 2009 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 int verify_peer;
73 int 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_submit_xpath_values (char *plugin_instance, /* {{{ */
242 xmlXPathContextPtr xpath_ctx,
243 char *base_xpath, cx_xpath_t *xpath)
244 {
245 int i;
246 int j;
247 int total_nodes;
248 int tmp_size;
249 int status=-1;
250 char *node_value;
252 xmlXPathObjectPtr base_node_obj = NULL;
253 xmlXPathObjectPtr instance_node_obj = NULL;
254 xmlXPathObjectPtr values_node_obj = NULL;
255 xmlNodeSetPtr base_nodes = NULL;
256 xmlNodeSetPtr instance_node = NULL;
257 xmlNodeSetPtr values_node = NULL;
259 value_list_t vl = VALUE_LIST_INIT;
260 const data_set_t *ds;
262 base_node_obj = cx_evaluate_xpath (xpath_ctx, BAD_CAST base_xpath);
263 if (base_node_obj == NULL)
264 return -1; /* error is logged already */
266 base_nodes = base_node_obj->nodesetval;
267 total_nodes = (base_nodes) ? base_nodes->nodeNr : 0;
269 if (total_nodes == 0)
270 {
271 ERROR ("curl_xml plugin: "
272 "xpath expression \"%s\" doesn't match any of the node. Skipping...", base_xpath);
273 xmlXPathFreeObject (base_node_obj);
274 return -1;
275 }
277 /* If base_xpath returned multiple results, then */
278 /* Instance in the xpath block is required */
279 if (total_nodes > 1 && xpath->instance == NULL)
280 {
281 ERROR ("curl_xml plugin: "
282 "Instance is must in xpath block since the base xpath expression \"%s\" "
283 "returned multiple results. Skipping the xpath block...", base_xpath);
284 return -1;
285 }
287 /* set the values for the value_list */
288 ds = plugin_get_ds (xpath->type);
289 vl.values_len = ds->ds_num;
290 sstrncpy (vl.type, xpath->type, sizeof (vl.type));
291 sstrncpy (vl.plugin, "curl_xml", sizeof (vl.plugin));
292 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
293 if (plugin_instance != NULL)
294 sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
296 for (i = 0; i < total_nodes; i++)
297 {
298 xpath_ctx->node = base_nodes->nodeTab[i];
300 /* instance has to be an xpath expression */
301 if (xpath->instance != NULL)
302 {
303 instance_node_obj = cx_evaluate_xpath (xpath_ctx, BAD_CAST xpath->instance);
304 if (instance_node_obj == NULL)
305 continue; /* error is logged already */
307 instance_node = instance_node_obj->nodesetval;
308 tmp_size = (instance_node) ? instance_node->nodeNr : 0;
310 if ( (tmp_size == 0) && (total_nodes > 1) )
311 {
312 WARNING ("curl_xml plugin: "
313 "relative xpath expression for 'Instance' \"%s\" doesn't match "
314 "any of the nodes. Skipping the node - %s",
315 xpath->instance, base_nodes->nodeTab[i]->name);
316 xmlXPathFreeObject (instance_node_obj);
317 continue;
318 }
320 if (tmp_size > 1)
321 {
322 WARNING ("curl_xml plugin: "
323 "relative xpath expression for 'Instance' \"%s\" is expected "
324 "to return only one text node. Skipping the node - %s",
325 xpath->instance, base_nodes->nodeTab[i]->name);
326 xmlXPathFreeObject (instance_node_obj);
327 continue;
328 }
330 // ignoring the element if other than textnode/attribute
331 if (cx_if_not_text_node(instance_node->nodeTab[0]))
332 {
333 WARNING ("curl_xml plugin: "
334 "relative xpath expression \"%s\" is expected to return only text node "
335 "which is not the case. Skipping the node - %s",
336 xpath->instance, base_nodes->nodeTab[i]->name);
337 xmlXPathFreeObject (instance_node_obj);
338 continue;
339 }
340 }
342 for (j = 0; j < xpath->values_len; j++)
343 {
344 values_node_obj = cx_evaluate_xpath (xpath_ctx, BAD_CAST xpath->values[j].path);
345 values_node = values_node_obj->nodesetval;
346 tmp_size = (values_node) ? values_node->nodeNr : 0;
348 if (tmp_size == 0)
349 {
350 WARNING ("curl_xml plugin: "
351 "relative xpath expression \"%s\" doesn't match any of the nodes. "
352 "Skipping...", xpath->values[j].path);
353 xmlXPathFreeObject (values_node_obj);
354 continue;
355 }
357 if (tmp_size > 1)
358 {
359 WARNING ("curl_xml plugin: "
360 "relative xpath expression \"%s\" is expected to return "
361 "only one node. Skipping...", xpath->values[j].path);
362 xmlXPathFreeObject (values_node_obj);
363 continue;
364 }
366 /* ignoring the element if other than textnode/attribute*/
367 if (cx_if_not_text_node(values_node->nodeTab[0]))
368 {
369 WARNING ("curl_xml plugin: "
370 "relative xpath expression \"%s\" is expected to return "
371 "only text/attribute node which is not the case. Skipping...",
372 xpath->values[j].path);
373 xmlXPathFreeObject (values_node_obj);
374 continue;
375 }
377 vl.values = (value_t *) malloc (sizeof (value_t) * vl.values_len);
378 if (vl.values == NULL)
379 {
380 ERROR ("curl_xml plugin: malloc failed.");
381 xmlXPathFreeObject (base_node_obj);
382 xmlXPathFreeObject (instance_node_obj);
383 xmlXPathFreeObject (values_node_obj);
384 return (-1);
385 }
387 node_value = (char *) xmlNodeGetContent(values_node->nodeTab[0]);
388 switch (ds->ds[j].type)
389 {
390 case DS_TYPE_COUNTER:
391 vl.values[j].counter = atoi(node_value);
392 break;
393 case DS_TYPE_DERIVE:
394 vl.values[j].derive = atoi(node_value);
395 break;
396 case DS_TYPE_ABSOLUTE:
397 vl.values[j].absolute = atoi(node_value);
398 break;
399 case DS_TYPE_GAUGE:
400 vl.values[j].absolute = atoi(node_value);
401 }
403 if (xpath->instance_prefix != NULL)
404 {
405 if (instance_node != NULL)
406 ssnprintf (vl.type_instance, sizeof (vl.type_instance),"%s-%s",
407 xpath->instance_prefix, (char *) xmlNodeGetContent(instance_node->nodeTab[0]));
408 else
409 sstrncpy (vl.type_instance, xpath->instance_prefix,
410 sizeof (vl.type_instance));
411 }
412 else
413 {
414 /* If instance_prefix and instance_node are NULL , then */
415 /* don't set the type_instance */
416 if (instance_node != NULL)
417 sstrncpy (vl.type_instance, (char *) xmlNodeGetContent(instance_node->nodeTab[0]),
418 sizeof (vl.type_instance));
419 }
421 /* free up object */
422 xmlXPathFreeObject (values_node_obj);
424 /* We have reached here which means that */
425 /* we have got something to work */
426 status = 0;
427 }
429 /* submit the values */
430 if (vl.values)
431 plugin_dispatch_values (&vl);
433 sfree(vl.values);
434 if (instance_node_obj != NULL)
435 xmlXPathFreeObject (instance_node_obj);
436 }
438 /* free up the allocated memory */
439 xmlXPathFreeObject (base_node_obj);
441 return status;
442 } /* }}} cx_submit_xpath_values */
444 static int cx_submit_statistics(xmlDocPtr doc, /* {{{ */
445 xmlXPathContextPtr xpath_ctx, cx_t *db)
446 {
447 c_avl_iterator_t *iter;
448 char *key;
449 cx_xpath_t *value;
450 int status=-1;
452 iter = c_avl_get_iterator (db->tree);
453 while (c_avl_iterator_next (iter, (void *) &key, (void *) &value) == 0)
454 {
455 if (cx_check_type(value) == -1)
456 continue;
458 if (cx_submit_xpath_values(db->instance, xpath_ctx, key, value) == 0)
459 status = 0; /* we got atleast one success */
460 } /* while (c_avl_iterator_next) */
462 return status;
463 } /* }}} cx_submit_statistics */
465 static int cx_parse_stats_xml(xmlChar* xml, cx_t *db) /* {{{ */
466 {
467 int status;
468 xmlDocPtr doc;
469 xmlXPathContextPtr xpath_ctx;
471 /* Load the XML */
472 doc = xmlParseDoc(xml);
473 if (doc == NULL)
474 {
475 ERROR ("curl_xml plugin: Failed to parse the xml document - %s", xml);
476 return (-1);
477 }
479 xpath_ctx = xmlXPathNewContext(doc);
480 if(xpath_ctx == NULL)
481 {
482 ERROR ("curl_xml plugin: Failed to create the xml context");
483 xmlFreeDoc(doc);
484 return (-1);
485 }
487 status = cx_submit_statistics (doc, xpath_ctx, db);
488 /* Cleanup */
489 xmlXPathFreeContext(xpath_ctx);
490 xmlFreeDoc(doc);
491 return status;
492 } /* }}} cx_parse_stats_xml */
494 static int cx_curl_perform (cx_t *db, CURL *curl) /* {{{ */
495 {
496 int status;
497 long rc;
498 char *ptr;
499 char *url;
501 db->buffer_fill = 0;
502 status = curl_easy_perform (curl);
504 curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &url);
505 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &rc);
507 if (rc != 200)
508 {
509 ERROR ("curl_xml plugin: curl_easy_perform failed with response code %ld (%s)",
510 rc, url);
511 return (-1);
512 }
514 if (status != 0)
515 {
516 ERROR ("curl_xml plugin: curl_easy_perform failed with status %i: %s (%s)",
517 status, db->curl_errbuf, url);
518 return (-1);
519 }
521 ptr = db->buffer;
523 status = cx_parse_stats_xml(BAD_CAST ptr, db);
524 db->buffer_fill = 0;
526 return status;
527 } /* }}} int cx_curl_perform */
529 static int cx_read (user_data_t *ud) /* {{{ */
530 {
531 cx_t *db;
533 if ((ud == NULL) || (ud->data == NULL))
534 {
535 ERROR ("curl_xml plugin: cx_read: Invalid user data.");
536 return (-1);
537 }
539 db = (cx_t *) ud->data;
541 return cx_curl_perform (db, db->curl);
542 } /* }}} int cx_read */
544 /* Configuration handling functions {{{ */
546 static int cx_config_add_values (const char *name, cx_xpath_t *xpath, /* {{{ */
547 oconfig_item_t *ci)
548 {
549 int i;
551 if (ci->values_num < 1)
552 {
553 WARNING ("curl_xml plugin: `Values' needs at least one argument.");
554 return (-1);
555 }
557 for (i = 0; i < ci->values_num; i++)
558 if (ci->values[i].type != OCONFIG_TYPE_STRING)
559 {
560 WARNING ("curl_xml plugin: `Values' needs only string argument.");
561 return (-1);
562 }
564 sfree (xpath->values);
566 xpath->values_len = 0;
567 xpath->values = (cx_values_t *) malloc (sizeof (cx_values_t) * ci->values_num);
568 if (xpath->values == NULL)
569 return (-1);
570 xpath->values_len = ci->values_num;
572 /* populate cx_values_t structure */
573 for (i = 0; i < ci->values_num; i++)
574 {
575 xpath->values[i].path_len = sizeof (ci->values[i].value.string);
576 sstrncpy (xpath->values[i].path, ci->values[i].value.string, sizeof (xpath->values[i].path));
577 }
579 return (0);
580 } /* }}} cx_config_add_values */
582 static int cx_config_add_string (const char *name, char **dest, /* {{{ */
583 oconfig_item_t *ci)
584 {
585 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
586 {
587 WARNING ("curl_xml plugin: `%s' needs exactly one string argument.", name);
588 return (-1);
589 }
591 sfree (*dest);
592 *dest = strdup (ci->values[0].value.string);
593 if (*dest == NULL)
594 return (-1);
596 return (0);
597 } /* }}} int cx_config_add_string */
599 static int cx_config_set_boolean (const char *name, int *dest, /* {{{ */
600 oconfig_item_t *ci)
601 {
602 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN))
603 {
604 WARNING ("curl_xml plugin: `%s' needs exactly one boolean argument.", name);
605 return (-1);
606 }
608 *dest = ci->values[0].value.boolean ? 1 : 0;
610 return (0);
611 } /* }}} int cx_config_set_boolean */
613 static c_avl_tree_t *cx_avl_create(void) /* {{{ */
614 {
615 return c_avl_create ((int (*) (const void *, const void *)) strcmp);
616 } /* }}} cx_avl_create */
618 static int cx_config_add_xpath (cx_t *db, /* {{{ */
619 oconfig_item_t *ci)
620 {
621 cx_xpath_t *xpath;
622 int status;
623 int i;
625 if ((ci->values_num != 1)
626 || (ci->values[0].type != OCONFIG_TYPE_STRING))
627 {
628 WARNING ("curl_xml plugin: The `xpath' block "
629 "needs exactly one string argument.");
630 return (-1);
631 }
633 xpath = (cx_xpath_t *) malloc (sizeof (*xpath));
634 if (xpath == NULL)
635 {
636 ERROR ("curl_xml plugin: malloc failed.");
637 return (-1);
638 }
639 memset (xpath, 0, sizeof (*xpath));
640 xpath->magic = CX_KEY_MAGIC;
642 if (strcasecmp ("xpath", ci->key) == 0)
643 {
644 status = cx_config_add_string ("xpath", &xpath->path, ci);
645 if (status != 0)
646 {
647 sfree (xpath);
648 return (status);
649 }
650 }
651 else
652 {
653 ERROR ("curl_xml plugin: cx_config: "
654 "Invalid key: %s", ci->key);
655 return (-1);
656 }
658 status = 0;
659 for (i = 0; i < ci->children_num; i++)
660 {
661 oconfig_item_t *child = ci->children + i;
663 if (strcasecmp ("Type", child->key) == 0)
664 status = cx_config_add_string ("Type", &xpath->type, child);
665 else if (strcasecmp ("InstancePrefix", child->key) == 0)
666 status = cx_config_add_string ("InstancePrefix", &xpath->instance_prefix, child);
667 else if (strcasecmp ("Instance", child->key) == 0)
668 status = cx_config_add_string ("Instance", &xpath->instance, child);
669 else if (strcasecmp ("Values", child->key) == 0)
670 status = cx_config_add_values ("Values", xpath, child);
671 else
672 {
673 WARNING ("curl_xml plugin: Option `%s' not allowed here.", child->key);
674 status = -1;
675 }
677 if (status != 0)
678 break;
679 } /* for (i = 0; i < ci->children_num; i++) */
681 while (status == 0)
682 {
683 if (xpath->type == NULL)
684 {
685 WARNING ("curl_xml plugin: `Type' missing in `xpath' block.");
686 status = -1;
687 }
689 break;
690 } /* while (status == 0) */
692 if (status == 0)
693 {
694 char *name;
695 c_avl_tree_t *tree;
697 if (db->tree == NULL)
698 db->tree = cx_avl_create();
700 tree = db->tree;
701 name = xpath->path;
703 if (*name)
704 c_avl_insert (tree, strdup(name), xpath);
705 else
706 {
707 ERROR ("curl_xml plugin: invalid key: %s", xpath->path);
708 status = -1;
709 }
710 }
712 return (status);
713 } /* }}} int cx_config_add_xpath */
715 static int cx_init_curl (cx_t *db) /* {{{ */
716 {
717 db->curl = curl_easy_init ();
718 if (db->curl == NULL)
719 {
720 ERROR ("curl_xml plugin: curl_easy_init failed.");
721 return (-1);
722 }
724 curl_easy_setopt (db->curl, CURLOPT_WRITEFUNCTION, cx_curl_callback);
725 curl_easy_setopt (db->curl, CURLOPT_WRITEDATA, db);
726 curl_easy_setopt (db->curl, CURLOPT_USERAGENT,
727 PACKAGE_NAME"/"PACKAGE_VERSION);
728 curl_easy_setopt (db->curl, CURLOPT_ERRORBUFFER, db->curl_errbuf);
729 curl_easy_setopt (db->curl, CURLOPT_URL, db->url);
731 if (db->user != NULL)
732 {
733 size_t credentials_size;
735 credentials_size = strlen (db->user) + 2;
736 if (db->pass != NULL)
737 credentials_size += strlen (db->pass);
739 db->credentials = (char *) malloc (credentials_size);
740 if (db->credentials == NULL)
741 {
742 ERROR ("curl_xml plugin: malloc failed.");
743 return (-1);
744 }
746 ssnprintf (db->credentials, credentials_size, "%s:%s",
747 db->user, (db->pass == NULL) ? "" : db->pass);
748 curl_easy_setopt (db->curl, CURLOPT_USERPWD, db->credentials);
749 }
751 curl_easy_setopt (db->curl, CURLOPT_SSL_VERIFYPEER, db->verify_peer);
752 curl_easy_setopt (db->curl, CURLOPT_SSL_VERIFYHOST,
753 db->verify_host ? 2 : 0);
754 if (db->cacert != NULL)
755 curl_easy_setopt (db->curl, CURLOPT_CAINFO, db->cacert);
757 return (0);
758 } /* }}} int cx_init_curl */
760 static int cx_config_add_url (oconfig_item_t *ci) /* {{{ */
761 {
762 cx_t *db;
763 int status = 0;
764 int i;
766 if ((ci->values_num != 1)
767 || (ci->values[0].type != OCONFIG_TYPE_STRING))
768 {
769 WARNING ("curl_xml plugin: The `URL' block "
770 "needs exactly one string argument.");
771 return (-1);
772 }
774 db = (cx_t *) malloc (sizeof (*db));
775 if (db == NULL)
776 {
777 ERROR ("curl_xml plugin: malloc failed.");
778 return (-1);
779 }
780 memset (db, 0, sizeof (*db));
782 if (strcasecmp ("URL", ci->key) == 0)
783 {
784 status = cx_config_add_string ("URL", &db->url, ci);
785 if (status != 0)
786 {
787 sfree (db);
788 return (status);
789 }
790 }
791 else
792 {
793 ERROR ("curl_xml plugin: cx_config: "
794 "Invalid key: %s", ci->key);
795 return (-1);
796 }
798 /* Fill the `cx_t' structure.. */
799 for (i = 0; i < ci->children_num; i++)
800 {
801 oconfig_item_t *child = ci->children + i;
803 if (strcasecmp ("Instance", child->key) == 0)
804 status = cx_config_add_string ("Instance", &db->instance, child);
805 else if (strcasecmp ("Host", child->key) == 0)
806 status = cx_config_add_string ("Host", &db->host, child);
807 else if (strcasecmp ("User", child->key) == 0)
808 status = cx_config_add_string ("User", &db->user, child);
809 else if (strcasecmp ("Password", child->key) == 0)
810 status = cx_config_add_string ("Password", &db->pass, child);
811 else if (strcasecmp ("VerifyPeer", child->key) == 0)
812 status = cx_config_set_boolean ("VerifyPeer", &db->verify_peer, child);
813 else if (strcasecmp ("VerifyHost", child->key) == 0)
814 status = cx_config_set_boolean ("VerifyHost", &db->verify_host, child);
815 else if (strcasecmp ("CACert", child->key) == 0)
816 status = cx_config_add_string ("CACert", &db->cacert, child);
817 else if (strcasecmp ("xpath", child->key) == 0)
818 status = cx_config_add_xpath (db, child);
819 else
820 {
821 WARNING ("curl_xml plugin: Option `%s' not allowed here.", child->key);
822 status = -1;
823 }
825 if (status != 0)
826 break;
827 }
829 if (status == 0)
830 {
831 if (db->tree == NULL)
832 {
833 WARNING ("curl_xml plugin: No (valid) `Key' block "
834 "within `URL' block `%s'.", db->url);
835 status = -1;
836 }
837 if (status == 0)
838 status = cx_init_curl (db);
839 }
841 /* If all went well, register this database for reading */
842 if (status == 0)
843 {
844 user_data_t ud;
845 char cb_name[DATA_MAX_NAME_LEN];
847 if (db->instance == NULL)
848 db->instance = strdup("default");
850 DEBUG ("curl_xml plugin: Registering new read callback: %s",
851 db->instance);
853 memset (&ud, 0, sizeof (ud));
854 ud.data = (void *) db;
855 ud.free_func = cx_free;
857 ssnprintf (cb_name, sizeof (cb_name), "curl_xml-%s-%s",
858 db->instance, db->url);
860 plugin_register_complex_read (cb_name, cx_read,
861 /* interval = */ NULL, &ud);
862 }
863 else
864 {
865 cx_free (db);
866 return (-1);
867 }
869 return (0);
870 } /* }}} int cx_config_add_url */
872 /* }}} End of configuration handling functions */
874 static int cx_config (oconfig_item_t *ci) /* {{{ */
875 {
876 int success;
877 int errors;
878 int status;
879 int i;
881 success = 0;
882 errors = 0;
884 for (i = 0; i < ci->children_num; i++)
885 {
886 oconfig_item_t *child = ci->children + i;
888 if (strcasecmp ("URL", child->key) == 0)
889 {
890 status = cx_config_add_url (child);
891 if (status == 0)
892 success++;
893 else
894 errors++;
895 }
896 else
897 {
898 WARNING ("curl_xml plugin: Option `%s' not allowed here.", child->key);
899 errors++;
900 }
901 }
903 if ((success == 0) && (errors > 0))
904 {
905 ERROR ("curl_xml plugin: All statements failed.");
906 return (-1);
907 }
909 return (0);
910 } /* }}} int cx_config */
912 void module_register (void)
913 {
914 plugin_register_complex_config ("curl_xml", cx_config);
915 } /* void module_register */
917 /* vim: set sw=2 sts=2 et fdm=marker : */