curl_xml plugin: Documented the type-instance behavior a bit better.
[collectd.git] / src / curl_xml.c
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  **/
21
22 #include "collectd.h"
23 #include "common.h"
24 #include "plugin.h"
25 #include "configfile.h"
26 #include "utils_avltree.h"
27
28 #include <libxml/parser.h>
29 #include <libxml/tree.h>
30 #include <libxml/xpath.h>
31
32 #include <curl/curl.h>
33
34 #define CX_DEFAULT_HOST "localhost"
35 #define CX_KEY_MAGIC 0x43484b59UL /* CHKY */
36 #define CX_IS_KEY(key) (key)->magic == CX_KEY_MAGIC
37
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 /* }}} */
48
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 /* }}} */
62
63 struct cx_s /* {{{ */
64 {
65   char *instance;
66   char *host;
67
68   char *url;
69   char *user;
70   char *pass;
71   char *credentials;
72   _Bool verify_peer;
73   _Bool verify_host;
74   char *cacert;
75
76   CURL *curl;
77   char curl_errbuf[CURL_ERROR_SIZE];
78   char *buffer;
79   size_t buffer_size;
80   size_t buffer_fill;
81
82   c_avl_tree_t *tree; /* tree of xpath blocks */
83 };
84 typedef struct cx_s cx_t; /* }}} */
85
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;
94
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   }
102
103    if (len <= 0)
104     return (len);
105
106   if ((db->buffer_fill + len) >= db->buffer_size)
107   {
108     char *temp;
109
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   }
120
121   memcpy (db->buffer + db->buffer_fill, (char *) buf, len);
122   db->buffer_fill += len;
123   db->buffer[db->buffer_fill] = 0;
124
125   return (len);
126 } /* }}} size_t cx_curl_callback */
127
128 static void cx_xpath_free (cx_xpath_t *xpath) /* {{{ */
129 {
130   if (xpath == NULL)
131     return;
132
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 */
140
141 static void cx_tree_free (c_avl_tree_t *tree) /* {{{ */
142 {
143   char *name;
144   void *value;
145
146   while (c_avl_pick (tree, (void *) &name, (void *) &value) == 0)
147   {
148     cx_xpath_t *key = (cx_xpath_t *)value;
149
150     if (CX_IS_KEY(key))
151       cx_xpath_free (key);
152     else
153       cx_tree_free ((c_avl_tree_t *)value);
154
155     sfree (name);
156   }
157
158   c_avl_destroy (tree);
159 } /* }}} void cx_tree_free */
160
161 static void cx_free (void *arg) /* {{{ */
162 {
163   cx_t *db;
164
165   DEBUG ("curl_xml plugin: cx_free (arg = %p);", arg);
166
167   db = (cx_t *) arg;
168
169   if (db == NULL)
170     return;
171
172   if (db->curl != NULL)
173     curl_easy_cleanup (db->curl);
174   db->curl = NULL;
175
176   if (db->tree != NULL)
177     cx_tree_free (db->tree);
178   db->tree = NULL;
179
180   sfree (db->buffer);
181   sfree (db->instance);
182   sfree (db->host);
183
184   sfree (db->url);
185   sfree (db->user);
186   sfree (db->pass);
187   sfree (db->credentials);
188   sfree (db->cacert);
189
190   sfree (db);
191 } /* }}} void cx_free */
192
193 static int cx_check_type (cx_xpath_t *xpath) /* {{{ */
194 {
195   const data_set_t *ds;
196   
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   }
203
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   }
210
211   return (0);
212 } /* }}} cx_check_type */
213
214 static xmlXPathObjectPtr cx_evaluate_xpath (xmlXPathContextPtr xpath_ctx, /* {{{ */ 
215            xmlChar *expr)
216 {
217   xmlXPathObjectPtr xpath_obj;
218
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   }
227
228   return xpath_obj;
229 } /* }}} cx_evaluate_xpath */
230
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);
235
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 */
240
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;
249
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. */
253
254   values_node = values_node_obj->nodesetval;
255   tmp_size = (values_node) ? values_node->nodeNr : 0;
256
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   }
265
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   }
274
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   }
285
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   }
305
306   /* free up object */
307   xmlXPathFreeObject (values_node_obj);
308
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 */
313
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;
321
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;
326
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++) */
333
334   plugin_dispatch_values (vl);
335   vl->values = NULL;
336
337   return (0);
338 } /* }}} int cx_handle_all_value_xpaths */
339
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;
346
347   memset (vl->type_instance, 0, sizeof (vl->type_instance));
348
349   /* If the base xpath returns more than one block, the result is assumed to be
350    * a table. The `Instnce' option is not optional in this case. Check for the
351    * condition and inform the user. */
352   if (is_table && (vl->type_instance == NULL))
353   {
354     WARNING ("curl_xml plugin: "
355         "Base-XPath %s is a table (more than one result was returned), "
356         "but no instance-XPath has been defined.",
357         xpath->path);
358     return (-1);
359   }
360
361   /* instance has to be an xpath expression */
362   if (xpath->instance != NULL)
363   {
364     int tmp_size;
365
366     instance_node_obj = cx_evaluate_xpath (xpath_ctx, BAD_CAST xpath->instance);
367     if (instance_node_obj == NULL)
368       return (-1); /* error is logged already */
369
370     instance_node = instance_node_obj->nodesetval;
371     tmp_size = (instance_node) ? instance_node->nodeNr : 0;
372
373     if ( (tmp_size == 0) && (is_table) )
374     {
375       WARNING ("curl_xml plugin: "
376           "relative xpath expression for 'Instance' \"%s\" doesn't match "
377           "any of the nodes. Skipping the node.", xpath->instance);
378       xmlXPathFreeObject (instance_node_obj);
379       return (-1);
380     }
381
382     if (tmp_size > 1)
383     {
384       WARNING ("curl_xml plugin: "
385           "relative xpath expression for 'Instance' \"%s\" is expected "
386           "to return only one text node. Skipping the node.", xpath->instance);
387       xmlXPathFreeObject (instance_node_obj);
388       return (-1);
389     }
390
391     /* ignoring the element if other than textnode/attribute */
392     if (cx_if_not_text_node(instance_node->nodeTab[0]))
393     {
394       WARNING ("curl_xml plugin: "
395           "relative xpath expression \"%s\" is expected to return only text node "
396           "which is not the case. Skipping the node.", xpath->instance);
397       xmlXPathFreeObject (instance_node_obj);
398       return (-1);
399     }
400   } /* if (xpath->instance != NULL) */
401
402   if (xpath->instance_prefix != NULL)
403   {
404     if (instance_node != NULL)
405       ssnprintf (vl->type_instance, sizeof (vl->type_instance),"%s-%s",
406           xpath->instance_prefix, (char *) xmlNodeGetContent(instance_node->nodeTab[0]));
407     else
408       sstrncpy (vl->type_instance, xpath->instance_prefix,
409           sizeof (vl->type_instance));
410   }
411   else
412   {
413     /* If instance_prefix and instance_node are NULL, then
414      * don't set the type_instance */
415     if (instance_node != NULL)
416       sstrncpy (vl->type_instance, (char *) xmlNodeGetContent(instance_node->nodeTab[0]),
417           sizeof (vl->type_instance));
418   }
419
420   /* Free `instance_node_obj' this late, because `instance_node' points to
421    * somewhere inside this structure. */
422   xmlXPathFreeObject (instance_node_obj);
423
424   return (0);
425 } /* }}} int cx_handle_instance_xpath */
426
427 static int  cx_handle_base_xpath (char *plugin_instance, /* {{{ */
428     xmlXPathContextPtr xpath_ctx, 
429     char *base_xpath, cx_xpath_t *xpath)
430 {
431   int total_nodes;
432   int i;
433
434   xmlXPathObjectPtr base_node_obj = NULL;
435   xmlNodeSetPtr base_nodes = NULL;
436
437   value_list_t vl = VALUE_LIST_INIT;
438   const data_set_t *ds;
439
440   base_node_obj = cx_evaluate_xpath (xpath_ctx, BAD_CAST base_xpath); 
441   if (base_node_obj == NULL)
442     return -1; /* error is logged already */
443
444   base_nodes = base_node_obj->nodesetval;
445   total_nodes = (base_nodes) ? base_nodes->nodeNr : 0;
446
447   if (total_nodes == 0)
448   {
449      ERROR ("curl_xml plugin: "
450               "xpath expression \"%s\" doesn't match any of the node. Skipping...", base_xpath);
451      xmlXPathFreeObject (base_node_obj);
452      return -1;
453   }
454
455   /* If base_xpath returned multiple results, then */
456   /* Instance in the xpath block is required */ 
457   if (total_nodes > 1 && xpath->instance == NULL)
458   {
459     ERROR ("curl_xml plugin: "
460              "Instance is must in xpath block since the base xpath expression \"%s\" "
461              "returned multiple results. Skipping the xpath block...", base_xpath);
462     return -1;
463   }
464
465   /* set the values for the value_list */
466   ds = plugin_get_ds (xpath->type);
467   vl.values_len = ds->ds_num;
468   sstrncpy (vl.type, xpath->type, sizeof (vl.type));
469   sstrncpy (vl.plugin, "curl_xml", sizeof (vl.plugin));
470   sstrncpy (vl.host, hostname_g, sizeof (vl.host));
471   if (plugin_instance != NULL)
472     sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance)); 
473
474   for (i = 0; i < total_nodes; i++)
475   {
476     int status;
477
478     xpath_ctx->node = base_nodes->nodeTab[i];
479
480     status = cx_handle_instance_xpath (xpath_ctx, xpath, &vl,
481         /* is_table = */ (total_nodes > 1));
482     if (status != 0)
483       continue; /* An error has already been reported. */
484
485     status = cx_handle_all_value_xpaths (xpath_ctx, xpath, ds, &vl);
486     if (status != 0)
487       continue; /* An error has been logged. */
488   } /* for (i = 0; i < total_nodes; i++) */
489
490   /* free up the allocated memory */
491   xmlXPathFreeObject (base_node_obj); 
492
493   return (0); 
494 } /* }}} cx_handle_base_xpath */
495
496 static int cx_handle_parsed_xml(xmlDocPtr doc, /* {{{ */ 
497                        xmlXPathContextPtr xpath_ctx, cx_t *db)
498 {
499   c_avl_iterator_t *iter;
500   char *key;
501   cx_xpath_t *value;
502   int status=-1;
503   
504   iter = c_avl_get_iterator (db->tree);
505   while (c_avl_iterator_next (iter, (void *) &key, (void *) &value) == 0)
506   {
507     if (cx_check_type(value) == -1)
508       continue;
509
510     if (cx_handle_base_xpath(db->instance, xpath_ctx, key, value) == 0)
511       status = 0; /* we got atleast one success */
512   } /* while (c_avl_iterator_next) */
513
514   return status;
515 } /* }}} cx_handle_parsed_xml */
516
517 static int cx_parse_stats_xml(xmlChar* xml, cx_t *db) /* {{{ */
518 {
519   int status;
520   xmlDocPtr doc;
521   xmlXPathContextPtr xpath_ctx;
522
523   /* Load the XML */
524   doc = xmlParseDoc(xml);
525   if (doc == NULL)
526   {
527     ERROR ("curl_xml plugin: Failed to parse the xml document  - %s", xml);
528     return (-1);
529   }
530
531   xpath_ctx = xmlXPathNewContext(doc);
532   if(xpath_ctx == NULL)
533   {
534     ERROR ("curl_xml plugin: Failed to create the xml context");
535     xmlFreeDoc(doc);
536     return (-1);
537   }
538
539   status = cx_handle_parsed_xml (doc, xpath_ctx, db);
540   /* Cleanup */
541   xmlXPathFreeContext(xpath_ctx);
542   xmlFreeDoc(doc);
543   return status;
544 } /* }}} cx_parse_stats_xml */
545
546 static int cx_curl_perform (cx_t *db, CURL *curl) /* {{{ */
547 {
548   int status;
549   long rc;
550   char *ptr;
551   char *url;
552
553   db->buffer_fill = 0; 
554   status = curl_easy_perform (curl);
555
556   curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &url);
557   curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &rc);
558
559   if (rc != 200)
560   {
561     ERROR ("curl_xml plugin: curl_easy_perform failed with response code %ld (%s)",
562            rc, url);
563     return (-1);
564   }
565
566   if (status != 0)
567   {
568     ERROR ("curl_xml plugin: curl_easy_perform failed with status %i: %s (%s)",
569            status, db->curl_errbuf, url);
570     return (-1);
571   }
572
573   ptr = db->buffer;
574
575   status = cx_parse_stats_xml(BAD_CAST ptr, db);
576   db->buffer_fill = 0;
577
578   return status;
579 } /* }}} int cx_curl_perform */
580
581 static int cx_read (user_data_t *ud) /* {{{ */
582 {
583   cx_t *db;
584
585   if ((ud == NULL) || (ud->data == NULL))
586   {
587     ERROR ("curl_xml plugin: cx_read: Invalid user data.");
588     return (-1);
589   }
590
591   db = (cx_t *) ud->data;
592
593   return cx_curl_perform (db, db->curl);
594 } /* }}} int cx_read */
595
596 /* Configuration handling functions {{{ */
597
598 static int cx_config_add_values (const char *name, cx_xpath_t *xpath, /* {{{ */
599                                       oconfig_item_t *ci)
600 {
601   int i;
602
603   if (ci->values_num < 1)
604   {
605     WARNING ("curl_xml plugin: `Values' needs at least one argument.");
606     return (-1);
607   }
608
609   for (i = 0; i < ci->values_num; i++)
610     if (ci->values[i].type != OCONFIG_TYPE_STRING)
611     {
612       WARNING ("curl_xml plugin: `Values' needs only string argument.");
613       return (-1);
614     }
615
616   sfree (xpath->values);
617
618   xpath->values_len = 0;
619   xpath->values = (cx_values_t *) malloc (sizeof (cx_values_t) * ci->values_num);
620   if (xpath->values == NULL)
621     return (-1);
622   xpath->values_len = ci->values_num;
623
624   /* populate cx_values_t structure */
625   for (i = 0; i < ci->values_num; i++)
626   {
627     xpath->values[i].path_len = sizeof (ci->values[i].value.string);
628     sstrncpy (xpath->values[i].path, ci->values[i].value.string, sizeof (xpath->values[i].path));
629   }
630
631   return (0); 
632 } /* }}} cx_config_add_values */
633
634 static c_avl_tree_t *cx_avl_create(void) /* {{{ */
635 {
636   return c_avl_create ((int (*) (const void *, const void *)) strcmp);
637 } /* }}} cx_avl_create */
638
639 static int cx_config_add_xpath (cx_t *db, /* {{{ */
640                                    oconfig_item_t *ci)
641 {
642   cx_xpath_t *xpath;
643   int status;
644   int i;
645
646   if ((ci->values_num != 1)
647       || (ci->values[0].type != OCONFIG_TYPE_STRING))
648   {
649     WARNING ("curl_xml plugin: The `xpath' block "
650              "needs exactly one string argument.");
651     return (-1);
652   }
653
654   xpath = (cx_xpath_t *) malloc (sizeof (*xpath));
655   if (xpath == NULL)
656   {
657     ERROR ("curl_xml plugin: malloc failed.");
658     return (-1);
659   }
660   memset (xpath, 0, sizeof (*xpath));
661   xpath->magic = CX_KEY_MAGIC;
662
663   if (strcasecmp ("xpath", ci->key) == 0)
664   {
665     status = cf_util_get_string (ci, &xpath->path);
666     if (status != 0)
667     {
668       sfree (xpath);
669       return (status);
670     }
671   }
672   else
673   {
674     ERROR ("curl_xml plugin: cx_config: "
675            "Invalid key: %s", ci->key);
676     return (-1);
677   }
678
679   status = 0;
680   for (i = 0; i < ci->children_num; i++)
681   {
682     oconfig_item_t *child = ci->children + i;
683
684     if (strcasecmp ("Type", child->key) == 0)
685       status = cf_util_get_string (child, &xpath->type);
686     else if (strcasecmp ("InstancePrefix", child->key) == 0)
687       status = cf_util_get_string (child, &xpath->instance_prefix);
688     else if (strcasecmp ("Instance", child->key) == 0)
689       status = cf_util_get_string (child, &xpath->instance);
690     else if (strcasecmp ("Values", child->key) == 0)
691       status = cx_config_add_values ("Values", xpath, child);
692     else
693     {
694       WARNING ("curl_xml plugin: Option `%s' not allowed here.", child->key);
695       status = -1;
696     }
697
698     if (status != 0)
699       break;
700   } /* for (i = 0; i < ci->children_num; i++) */
701
702   while (status == 0)
703   {
704     if (xpath->type == NULL)
705     {
706       WARNING ("curl_xml plugin: `Type' missing in `xpath' block.");
707       status = -1;
708     }
709
710     break;
711   } /* while (status == 0) */
712
713   if (status == 0)
714   {
715     char *name;
716     c_avl_tree_t *tree;
717
718     if (db->tree == NULL)
719       db->tree = cx_avl_create();
720
721     tree = db->tree;
722     name = xpath->path;
723
724     if (*name)
725       c_avl_insert (tree, strdup(name), xpath);
726     else
727     {
728       ERROR ("curl_xml plugin: invalid key: %s", xpath->path);
729       status = -1;
730     }
731   }
732
733   return (status);
734 } /* }}} int cx_config_add_xpath */
735
736 /* Initialize db->curl */
737 static int cx_init_curl (cx_t *db) /* {{{ */
738 {
739   db->curl = curl_easy_init ();
740   if (db->curl == NULL)
741   {
742     ERROR ("curl_xml plugin: curl_easy_init failed.");
743     return (-1);
744   }
745
746   curl_easy_setopt (db->curl, CURLOPT_WRITEFUNCTION, cx_curl_callback);
747   curl_easy_setopt (db->curl, CURLOPT_WRITEDATA, db);
748   curl_easy_setopt (db->curl, CURLOPT_USERAGENT,
749                     PACKAGE_NAME"/"PACKAGE_VERSION);
750   curl_easy_setopt (db->curl, CURLOPT_ERRORBUFFER, db->curl_errbuf);
751   curl_easy_setopt (db->curl, CURLOPT_URL, db->url);
752
753   if (db->user != NULL)
754   {
755     size_t credentials_size;
756
757     credentials_size = strlen (db->user) + 2;
758     if (db->pass != NULL)
759       credentials_size += strlen (db->pass);
760
761     db->credentials = (char *) malloc (credentials_size);
762     if (db->credentials == NULL)
763     {
764       ERROR ("curl_xml plugin: malloc failed.");
765       return (-1);
766     }
767
768     ssnprintf (db->credentials, credentials_size, "%s:%s",
769                db->user, (db->pass == NULL) ? "" : db->pass);
770     curl_easy_setopt (db->curl, CURLOPT_USERPWD, db->credentials);
771   }
772
773   curl_easy_setopt (db->curl, CURLOPT_SSL_VERIFYPEER, db->verify_peer);
774   curl_easy_setopt (db->curl, CURLOPT_SSL_VERIFYHOST,
775                     db->verify_host ? 2 : 0);
776   if (db->cacert != NULL)
777     curl_easy_setopt (db->curl, CURLOPT_CAINFO, db->cacert);
778
779   return (0);
780 } /* }}} int cx_init_curl */
781
782 static int cx_config_add_url (oconfig_item_t *ci) /* {{{ */
783 {
784   cx_t *db;
785   int status = 0;
786   int i;
787
788   if ((ci->values_num != 1)
789       || (ci->values[0].type != OCONFIG_TYPE_STRING))
790   {
791     WARNING ("curl_xml plugin: The `URL' block "
792              "needs exactly one string argument.");
793     return (-1);
794   }
795
796   db = (cx_t *) malloc (sizeof (*db));
797   if (db == NULL)
798   {
799     ERROR ("curl_xml plugin: malloc failed.");
800     return (-1);
801   }
802   memset (db, 0, sizeof (*db));
803
804   if (strcasecmp ("URL", ci->key) == 0)
805   {
806     status = cf_util_get_string (ci, &db->url);
807     if (status != 0)
808     {
809       sfree (db);
810       return (status);
811     }
812   }
813   else
814   {
815     ERROR ("curl_xml plugin: cx_config: "
816            "Invalid key: %s", ci->key);
817     return (-1);
818   }
819
820   /* Fill the `cx_t' structure.. */
821   for (i = 0; i < ci->children_num; i++)
822   {
823     oconfig_item_t *child = ci->children + i;
824
825     if (strcasecmp ("Instance", child->key) == 0)
826       status = cf_util_get_string (child, &db->instance);
827     else if (strcasecmp ("Host", child->key) == 0)
828       status = cf_util_get_string (child, &db->host);
829     else if (strcasecmp ("User", child->key) == 0)
830       status = cf_util_get_string (child, &db->user);
831     else if (strcasecmp ("Password", child->key) == 0)
832       status = cf_util_get_string (child, &db->pass);
833     else if (strcasecmp ("VerifyPeer", child->key) == 0)
834       status = cf_util_get_boolean (child, &db->verify_peer);
835     else if (strcasecmp ("VerifyHost", child->key) == 0)
836       status = cf_util_get_boolean (child, &db->verify_host);
837     else if (strcasecmp ("CACert", child->key) == 0)
838       status = cf_util_get_string (child, &db->cacert);
839     else if (strcasecmp ("xpath", child->key) == 0)
840       status = cx_config_add_xpath (db, child);
841     else
842     {
843       WARNING ("curl_xml plugin: Option `%s' not allowed here.", child->key);
844       status = -1;
845     }
846
847     if (status != 0)
848       break;
849   }
850
851   if (status == 0)
852   {
853     if (db->tree == NULL)
854     {
855       WARNING ("curl_xml plugin: No (valid) `Key' block "
856                "within `URL' block `%s'.", db->url);
857       status = -1;
858     }
859     if (status == 0)
860       status = cx_init_curl (db);
861   }
862
863   /* If all went well, register this database for reading */
864   if (status == 0)
865   {
866     user_data_t ud;
867     char cb_name[DATA_MAX_NAME_LEN];
868
869     if (db->instance == NULL)
870       db->instance = strdup("default");
871
872     DEBUG ("curl_xml plugin: Registering new read callback: %s",
873            db->instance);
874
875     memset (&ud, 0, sizeof (ud));
876     ud.data = (void *) db;
877     ud.free_func = cx_free;
878
879     ssnprintf (cb_name, sizeof (cb_name), "curl_xml-%s-%s",
880                db->instance, db->url);
881
882     plugin_register_complex_read (cb_name, cx_read,
883                                   /* interval = */ NULL, &ud);
884   }
885   else
886   {
887     cx_free (db);
888     return (-1);
889   }
890
891   return (0);
892 } /* }}} int cx_config_add_url */
893
894 /* }}} End of configuration handling functions */
895
896 static int cx_config (oconfig_item_t *ci) /* {{{ */
897 {
898   int success;
899   int errors;
900   int status;
901   int i;
902
903   success = 0;
904   errors = 0;
905
906   for (i = 0; i < ci->children_num; i++)
907   {
908     oconfig_item_t *child = ci->children + i;
909
910     if (strcasecmp ("URL", child->key) == 0)
911     {
912       status = cx_config_add_url (child);
913       if (status == 0)
914         success++;
915       else
916         errors++;
917     }
918     else
919     {
920       WARNING ("curl_xml plugin: Option `%s' not allowed here.", child->key);
921       errors++;
922     }
923   }
924
925   if ((success == 0) && (errors > 0))
926   {
927     ERROR ("curl_xml plugin: All statements failed.");
928     return (-1);
929   }
930
931   return (0);
932 } /* }}} int cx_config */
933
934 void module_register (void)
935 {
936   plugin_register_complex_config ("curl_xml", cx_config);
937 } /* void module_register */
938
939 /* vim: set sw=2 sts=2 et fdm=marker : */