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