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