db5cd61167cbd18a8395964fb5566662a286a3c3
1 /**
2 * collectd - src/curl_xml.c
3 * Copyright (C) 2009,2010 Amit Gupta
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the License is applicable.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17 *
18 * Authors:
19 * Amit Gupta <amit.gupta221 at gmail.com>
20 **/
22 #include "collectd.h"
23 #include "common.h"
24 #include "plugin.h"
25 #include "configfile.h"
26 #include "utils_llist.h"
28 #include <libxml/parser.h>
29 #include <libxml/tree.h>
30 #include <libxml/xpath.h>
31 #include <libxml/xpathInternals.h>
33 #include <curl/curl.h>
35 #define CX_DEFAULT_HOST "localhost"
37 /*
38 * Private data structures
39 */
40 struct cx_values_s /* {{{ */
41 {
42 char path[DATA_MAX_NAME_LEN];
43 size_t path_len;
44 };
45 typedef struct cx_values_s cx_values_t;
46 /* }}} */
48 struct cx_xpath_s /* {{{ */
49 {
50 char *path;
51 char *type;
52 cx_values_t *values;
53 int values_len;
54 char *instance_prefix;
55 char *instance;
56 int is_table;
57 unsigned long magic;
58 };
59 typedef struct cx_xpath_s cx_xpath_t;
60 /* }}} */
62 struct cx_s /* {{{ */
63 {
64 char *instance;
65 char *host;
67 char *url;
68 char *user;
69 char *pass;
70 char *credentials;
71 _Bool verify_peer;
72 _Bool verify_host;
73 char *cacert;
74 char *post_body;
75 struct curl_slist *headers;
76 char *namespaces;
78 CURL *curl;
79 char curl_errbuf[CURL_ERROR_SIZE];
80 char *buffer;
81 size_t buffer_size;
82 size_t buffer_fill;
84 llist_t *list; /* list of xpath blocks */
85 };
86 typedef struct cx_s cx_t; /* }}} */
88 /*
89 * Private functions
90 */
91 static size_t cx_curl_callback (void *buf, /* {{{ */
92 size_t size, size_t nmemb, void *user_data)
93 {
94 size_t len = size * nmemb;
95 cx_t *db;
97 db = user_data;
98 if (db == NULL)
99 {
100 ERROR ("curl_xml plugin: cx_curl_callback: "
101 "user_data pointer is NULL.");
102 return (0);
103 }
105 if (len <= 0)
106 return (len);
108 if ((db->buffer_fill + len) >= db->buffer_size)
109 {
110 char *temp;
112 temp = (char *) realloc (db->buffer,
113 db->buffer_fill + len + 1);
114 if (temp == NULL)
115 {
116 ERROR ("curl_xml plugin: realloc failed.");
117 return (0);
118 }
119 db->buffer = temp;
120 db->buffer_size = db->buffer_fill + len + 1;
121 }
123 memcpy (db->buffer + db->buffer_fill, (char *) buf, len);
124 db->buffer_fill += len;
125 db->buffer[db->buffer_fill] = 0;
127 return (len);
128 } /* }}} size_t cx_curl_callback */
130 static void cx_xpath_free (cx_xpath_t *xpath) /* {{{ */
131 {
132 if (xpath == NULL)
133 return;
135 sfree (xpath->path);
136 sfree (xpath->type);
137 sfree (xpath->instance_prefix);
138 sfree (xpath->instance);
139 sfree (xpath->values);
140 sfree (xpath);
141 } /* }}} void cx_xpath_free */
143 static void cx_list_free (llist_t *list) /* {{{ */
144 {
145 llentry_t *le;
147 le = llist_head (list);
148 while (le != NULL)
149 {
150 llentry_t *le_next;
152 le_next = le->next;
154 sfree (le->key);
155 cx_xpath_free (le->value);
157 le = le_next;
158 }
160 llist_destroy (list);
161 list = NULL;
162 } /* }}} void cx_list_free */
164 static void cx_free (void *arg) /* {{{ */
165 {
166 cx_t *db;
168 DEBUG ("curl_xml plugin: cx_free (arg = %p);", arg);
170 db = (cx_t *) arg;
172 if (db == NULL)
173 return;
175 if (db->curl != NULL)
176 curl_easy_cleanup (db->curl);
177 db->curl = NULL;
179 if (db->list != NULL)
180 cx_list_free (db->list);
182 sfree (db->buffer);
183 sfree (db->instance);
184 sfree (db->host);
186 sfree (db->url);
187 sfree (db->user);
188 sfree (db->pass);
189 sfree (db->credentials);
190 sfree (db->cacert);
191 sfree (db->post_body);
192 curl_slist_free_all (db->headers);
193 sfree (db->namespaces);
195 sfree (db);
196 } /* }}} void cx_free */
198 static int cx_config_append_string (const char *name, struct curl_slist **dest, /* {{{ */
199 oconfig_item_t *ci)
200 {
201 if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING))
202 {
203 WARNING ("curl_xml plugin: `%s' needs exactly one string argument.", name);
204 return (-1);
205 }
207 *dest = curl_slist_append(*dest, ci->values[0].value.string);
208 if (*dest == NULL)
209 return (-1);
211 return (0);
212 } /* }}} int cx_config_append_string */
214 static int cx_check_type (const data_set_t *ds, cx_xpath_t *xpath) /* {{{ */
215 {
216 if (!ds)
217 {
218 WARNING ("curl_xml plugin: DataSet `%s' not defined.", xpath->type);
219 return (-1);
220 }
222 if (ds->ds_num != xpath->values_len)
223 {
224 WARNING ("curl_xml plugin: DataSet `%s' requires %i values, but config talks about %i",
225 xpath->type, ds->ds_num, xpath->values_len);
226 return (-1);
227 }
229 return (0);
230 } /* }}} cx_check_type */
232 static xmlXPathObjectPtr cx_evaluate_xpath (xmlXPathContextPtr xpath_ctx, /* {{{ */
233 xmlChar *expr)
234 {
235 xmlXPathObjectPtr xpath_obj;
237 /* XXX: When to free this? */
238 xpath_obj = xmlXPathEvalExpression(BAD_CAST expr, xpath_ctx);
239 if (xpath_obj == NULL)
240 {
241 WARNING ("curl_xml plugin: "
242 "Error unable to evaluate xpath expression \"%s\". Skipping...", expr);
243 return NULL;
244 }
246 return xpath_obj;
247 } /* }}} cx_evaluate_xpath */
249 static int cx_if_not_text_node (xmlNodePtr node) /* {{{ */
250 {
251 if (node->type == XML_TEXT_NODE || node->type == XML_ATTRIBUTE_NODE ||
252 node->type == XML_ELEMENT_NODE)
253 return (0);
255 WARNING ("curl_xml plugin: "
256 "Node \"%s\" doesn't seem to be a text node. Skipping...", node->name);
257 return -1;
258 } /* }}} cx_if_not_text_node */
260 /**
261 * cx_register_namespaces:
262 * @xpath_ctx: the pointer to an XPath context.
263 * @nslist: the list of known namespaces in
264 * "<prefix1>=<href1> <prefix2>=href2> ..." format.
265 *
266 * Registers namespaces from @nslist in @xpath_ctx.
267 *
268 * Returns 0 on success and a negative value otherwise.
269 *
270 * author: Aleksey Sanin
271 *
272 * The following license statement applies to the function
273 * cx_register_namespaces only:
274 *
275 * Permission is hereby granted, free of charge, to any person obtaining a copy
276 * of this software and associated documentation files (the "Software"), to deal
277 * in the Software without restriction, including without limitation the rights
278 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
279 * copies of the Software, and to permit persons to whom the Software is fur-
280 * nished to do so, subject to the following conditions:
281 *
282 * The above copyright notice and this permission notice shall be included in
283 * all copies or substantial portions of the Software.
284 *
285 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
286 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FIT-
287 * NESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
288 * DANIEL VEILLARD BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
289 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CON-
290 * NECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
291 *
292 * Except as contained in this notice, the name of Daniel Veillard shall not
293 * be used in advertising or otherwise to promote the sale, use or other deal-
294 * ings in this Software without prior written authorization from him.
295 */
296 static int cx_register_namespaces(xmlXPathContextPtr xpath_ctx, /* {{{ */
297 const xmlChar* nslist)
298 {
299 xmlChar* nslistdup;
300 xmlChar* prefix;
301 xmlChar* href;
302 xmlChar* next;
304 assert(xpath_ctx);
305 assert(nslist);
307 nslistdup = xmlStrdup(nslist);
308 if(nslistdup == NULL) {
309 ERROR ("curl_xml plugin: "
310 "unable to strdup namespaces list");
311 return (-1);
312 }
314 next = nslistdup;
315 while(next != NULL) {
316 /* skip spaces */
317 while((*next) == ' ') next++;
318 if((*next) == '\0') break;
320 /* find prefix */
321 prefix = next;
322 next = (xmlChar*)xmlStrchr(next, '=');
323 if(next == NULL) {
324 ERROR ("curl_xml plugin: "
325 "invalid namespaces list format");
326 xmlFree(nslistdup);
327 return (-1);
328 }
329 *(next++) = '\0';
331 /* find href */
332 href = next;
333 next = (xmlChar*)xmlStrchr(next, ' ');
334 if(next != NULL) {
335 *(next++) = '\0';
336 }
338 /* do register namespace */
339 if(xmlXPathRegisterNs(xpath_ctx, prefix, href) != 0) {
340 ERROR ("curl_xml plugin: "
341 "unable to register NS with prefix=\"%s\" and href=\"%s\"\n",
342 prefix, href);
343 xmlFree(nslistdup);
344 return (-1);
345 }
346 }
348 xmlFree(nslistdup);
349 return (0);
350 } /* }}} cx_register_namespaces */
352 static int cx_handle_single_value_xpath (xmlXPathContextPtr xpath_ctx, /* {{{ */
353 cx_xpath_t *xpath,
354 const data_set_t *ds, value_list_t *vl, int index)
355 {
356 xmlXPathObjectPtr values_node_obj;
357 xmlNodeSetPtr values_node;
358 int tmp_size;
359 char *node_value;
361 values_node_obj = cx_evaluate_xpath (xpath_ctx, BAD_CAST xpath->values[index].path);
362 if (values_node_obj == NULL)
363 return (-1); /* Error already logged. */
365 values_node = values_node_obj->nodesetval;
366 tmp_size = (values_node) ? values_node->nodeNr : 0;
368 if (tmp_size == 0)
369 {
370 WARNING ("curl_xml plugin: "
371 "relative xpath expression \"%s\" doesn't match any of the nodes. "
372 "Skipping...", xpath->values[index].path);
373 xmlXPathFreeObject (values_node_obj);
374 return (-1);
375 }
377 if (tmp_size > 1)
378 {
379 WARNING ("curl_xml plugin: "
380 "relative xpath expression \"%s\" is expected to return "
381 "only one node. Skipping...", xpath->values[index].path);
382 xmlXPathFreeObject (values_node_obj);
383 return (-1);
384 }
386 /* ignoring the element if other than textnode/attribute*/
387 if (cx_if_not_text_node(values_node->nodeTab[0]))
388 {
389 WARNING ("curl_xml plugin: "
390 "relative xpath expression \"%s\" is expected to return "
391 "only text/attribute node which is not the case. Skipping...",
392 xpath->values[index].path);
393 xmlXPathFreeObject (values_node_obj);
394 return (-1);
395 }
397 node_value = (char *) xmlNodeGetContent(values_node->nodeTab[0]);
398 switch (ds->ds[index].type)
399 {
400 case DS_TYPE_COUNTER:
401 vl->values[index].counter = (counter_t) strtoull (node_value,
402 /* endptr = */ NULL, /* base = */ 0);
403 break;
404 case DS_TYPE_DERIVE:
405 vl->values[index].derive = (derive_t) strtoll (node_value,
406 /* endptr = */ NULL, /* base = */ 0);
407 break;
408 case DS_TYPE_ABSOLUTE:
409 vl->values[index].absolute = (absolute_t) strtoull (node_value,
410 /* endptr = */ NULL, /* base = */ 0);
411 break;
412 case DS_TYPE_GAUGE:
413 vl->values[index].gauge = (gauge_t) strtod (node_value,
414 /* endptr = */ NULL);
415 }
417 /* free up object */
418 xmlXPathFreeObject (values_node_obj);
420 /* We have reached here which means that
421 * we have got something to work */
422 return (0);
423 } /* }}} int cx_handle_single_value_xpath */
425 static int cx_handle_all_value_xpaths (xmlXPathContextPtr xpath_ctx, /* {{{ */
426 cx_xpath_t *xpath,
427 const data_set_t *ds, value_list_t *vl)
428 {
429 value_t values[xpath->values_len];
430 int status;
431 int i;
433 assert (xpath->values_len > 0);
434 assert (xpath->values_len == vl->values_len);
435 assert (xpath->values_len == ds->ds_num);
436 vl->values = values;
438 for (i = 0; i < xpath->values_len; i++)
439 {
440 status = cx_handle_single_value_xpath (xpath_ctx, xpath, ds, vl, i);
441 if (status != 0)
442 return (-1); /* An error has been printed. */
443 } /* for (i = 0; i < xpath->values_len; i++) */
445 plugin_dispatch_values (vl);
446 vl->values = NULL;
448 return (0);
449 } /* }}} int cx_handle_all_value_xpaths */
451 static int cx_handle_instance_xpath (xmlXPathContextPtr xpath_ctx, /* {{{ */
452 cx_xpath_t *xpath, value_list_t *vl,
453 _Bool is_table)
454 {
455 xmlXPathObjectPtr instance_node_obj = NULL;
456 xmlNodeSetPtr instance_node = NULL;
458 memset (vl->type_instance, 0, sizeof (vl->type_instance));
460 /* If the base xpath returns more than one block, the result is assumed to be
461 * a table. The `Instance' option is not optional in this case. Check for the
462 * condition and inform the user. */
463 if (is_table && (vl->type_instance == NULL))
464 {
465 WARNING ("curl_xml plugin: "
466 "Base-XPath %s is a table (more than one result was returned), "
467 "but no instance-XPath has been defined.",
468 xpath->path);
469 return (-1);
470 }
472 /* instance has to be an xpath expression */
473 if (xpath->instance != NULL)
474 {
475 int tmp_size;
477 instance_node_obj = cx_evaluate_xpath (xpath_ctx, BAD_CAST xpath->instance);
478 if (instance_node_obj == NULL)
479 return (-1); /* error is logged already */
481 instance_node = instance_node_obj->nodesetval;
482 tmp_size = (instance_node) ? instance_node->nodeNr : 0;
484 if (tmp_size <= 0)
485 {
486 WARNING ("curl_xml plugin: "
487 "relative xpath expression for 'InstanceFrom' \"%s\" doesn't match "
488 "any of the nodes. Skipping the node.", xpath->instance);
489 xmlXPathFreeObject (instance_node_obj);
490 return (-1);
491 }
493 if (tmp_size > 1)
494 {
495 WARNING ("curl_xml plugin: "
496 "relative xpath expression for 'InstanceFrom' \"%s\" is expected "
497 "to return only one text node. Skipping the node.", xpath->instance);
498 xmlXPathFreeObject (instance_node_obj);
499 return (-1);
500 }
502 /* ignoring the element if other than textnode/attribute */
503 if (cx_if_not_text_node(instance_node->nodeTab[0]))
504 {
505 WARNING ("curl_xml plugin: "
506 "relative xpath expression \"%s\" is expected to return only text node "
507 "which is not the case. Skipping the node.", xpath->instance);
508 xmlXPathFreeObject (instance_node_obj);
509 return (-1);
510 }
511 } /* if (xpath->instance != NULL) */
513 if (xpath->instance_prefix != NULL)
514 {
515 if (instance_node != NULL)
516 ssnprintf (vl->type_instance, sizeof (vl->type_instance),"%s%s",
517 xpath->instance_prefix, (char *) xmlNodeGetContent(instance_node->nodeTab[0]));
518 else
519 sstrncpy (vl->type_instance, xpath->instance_prefix,
520 sizeof (vl->type_instance));
521 }
522 else
523 {
524 /* If instance_prefix and instance_node are NULL, then
525 * don't set the type_instance */
526 if (instance_node != NULL)
527 sstrncpy (vl->type_instance, (char *) xmlNodeGetContent(instance_node->nodeTab[0]),
528 sizeof (vl->type_instance));
529 }
531 /* Free `instance_node_obj' this late, because `instance_node' points to
532 * somewhere inside this structure. */
533 xmlXPathFreeObject (instance_node_obj);
535 return (0);
536 } /* }}} int cx_handle_instance_xpath */
538 static int cx_handle_base_xpath (char const *plugin_instance, /* {{{ */
539 char const *host,
540 xmlXPathContextPtr xpath_ctx, const data_set_t *ds,
541 char *base_xpath, cx_xpath_t *xpath)
542 {
543 int total_nodes;
544 int i;
546 xmlXPathObjectPtr base_node_obj = NULL;
547 xmlNodeSetPtr base_nodes = NULL;
549 value_list_t vl = VALUE_LIST_INIT;
551 base_node_obj = cx_evaluate_xpath (xpath_ctx, BAD_CAST base_xpath);
552 if (base_node_obj == NULL)
553 return -1; /* error is logged already */
555 base_nodes = base_node_obj->nodesetval;
556 total_nodes = (base_nodes) ? base_nodes->nodeNr : 0;
558 if (total_nodes == 0)
559 {
560 ERROR ("curl_xml plugin: "
561 "xpath expression \"%s\" doesn't match any of the nodes. "
562 "Skipping the xpath block...", base_xpath);
563 xmlXPathFreeObject (base_node_obj);
564 return -1;
565 }
567 /* If base_xpath returned multiple results, then */
568 /* Instance in the xpath block is required */
569 if (total_nodes > 1 && xpath->instance == NULL)
570 {
571 ERROR ("curl_xml plugin: "
572 "InstanceFrom is must in xpath block since the base xpath expression \"%s\" "
573 "returned multiple results. Skipping the xpath block...", base_xpath);
574 return -1;
575 }
577 /* set the values for the value_list */
578 vl.values_len = ds->ds_num;
579 sstrncpy (vl.type, xpath->type, sizeof (vl.type));
580 sstrncpy (vl.plugin, "curl_xml", sizeof (vl.plugin));
581 sstrncpy (vl.host, (host != NULL) ? host : hostname_g, sizeof (vl.host));
582 if (plugin_instance != NULL)
583 sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
585 for (i = 0; i < total_nodes; i++)
586 {
587 int status;
589 xpath_ctx->node = base_nodes->nodeTab[i];
591 status = cx_handle_instance_xpath (xpath_ctx, xpath, &vl,
592 /* is_table = */ (total_nodes > 1));
593 if (status != 0)
594 continue; /* An error has already been reported. */
596 status = cx_handle_all_value_xpaths (xpath_ctx, xpath, ds, &vl);
597 if (status != 0)
598 continue; /* An error has been logged. */
599 } /* for (i = 0; i < total_nodes; i++) */
601 /* free up the allocated memory */
602 xmlXPathFreeObject (base_node_obj);
604 return (0);
605 } /* }}} cx_handle_base_xpath */
607 static int cx_handle_parsed_xml(xmlDocPtr doc, /* {{{ */
608 xmlXPathContextPtr xpath_ctx, cx_t *db)
609 {
610 llentry_t *le;
611 const data_set_t *ds;
612 cx_xpath_t *xpath;
613 int status=-1;
616 le = llist_head (db->list);
617 while (le != NULL)
618 {
619 /* get the ds */
620 xpath = (cx_xpath_t *) le->value;
621 ds = plugin_get_ds (xpath->type);
623 if ( (cx_check_type(ds, xpath) == 0) &&
624 (cx_handle_base_xpath(db->instance, db->host,
625 xpath_ctx, ds, le->key, xpath) == 0) )
626 status = 0; /* we got atleast one success */
628 le = le->next;
629 } /* while (le != NULL) */
631 return status;
632 } /* }}} cx_handle_parsed_xml */
634 static int cx_parse_stats_xml(xmlChar* xml, cx_t *db) /* {{{ */
635 {
636 int status;
637 xmlDocPtr doc;
638 xmlXPathContextPtr xpath_ctx;
640 /* Load the XML */
641 doc = xmlParseDoc(xml);
642 if (doc == NULL)
643 {
644 ERROR ("curl_xml plugin: Failed to parse the xml document - %s", xml);
645 return (-1);
646 }
648 xpath_ctx = xmlXPathNewContext(doc);
649 if(xpath_ctx == NULL)
650 {
651 ERROR ("curl_xml plugin: Failed to create the xml context");
652 xmlFreeDoc(doc);
653 return (-1);
654 }
656 if((db->namespaces != NULL) &&
657 (cx_register_namespaces(xpath_ctx, BAD_CAST db->namespaces) < 0))
658 {
659 xmlFreeDoc(doc);
660 return (-1);
661 }
663 status = cx_handle_parsed_xml (doc, xpath_ctx, db);
664 /* Cleanup */
665 xmlXPathFreeContext(xpath_ctx);
666 xmlFreeDoc(doc);
667 return status;
668 } /* }}} cx_parse_stats_xml */
670 static int cx_curl_perform (cx_t *db, CURL *curl) /* {{{ */
671 {
672 int status;
673 long rc;
674 char *ptr;
675 char *url;
677 db->buffer_fill = 0;
678 status = curl_easy_perform (curl);
679 if (status != CURLE_OK)
680 {
681 ERROR ("curl_xml plugin: curl_easy_perform failed with status %i: %s (%s)",
682 status, db->curl_errbuf, url);
683 return (-1);
684 }
686 curl_easy_getinfo(curl, CURLINFO_EFFECTIVE_URL, &url);
687 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &rc);
689 /* The response code is zero if a non-HTTP transport was used. */
690 if ((rc != 0) && (rc != 200))
691 {
692 ERROR ("curl_xml plugin: curl_easy_perform failed with response code %ld (%s)",
693 rc, url);
694 return (-1);
695 }
697 ptr = db->buffer;
699 status = cx_parse_stats_xml(BAD_CAST ptr, db);
700 db->buffer_fill = 0;
702 return status;
703 } /* }}} int cx_curl_perform */
705 static int cx_read (user_data_t *ud) /* {{{ */
706 {
707 cx_t *db;
709 if ((ud == NULL) || (ud->data == NULL))
710 {
711 ERROR ("curl_xml plugin: cx_read: Invalid user data.");
712 return (-1);
713 }
715 db = (cx_t *) ud->data;
717 return cx_curl_perform (db, db->curl);
718 } /* }}} int cx_read */
720 /* Configuration handling functions {{{ */
722 static int cx_config_add_values (const char *name, cx_xpath_t *xpath, /* {{{ */
723 oconfig_item_t *ci)
724 {
725 int i;
727 if (ci->values_num < 1)
728 {
729 WARNING ("curl_xml plugin: `ValuesFrom' needs at least one argument.");
730 return (-1);
731 }
733 for (i = 0; i < ci->values_num; i++)
734 if (ci->values[i].type != OCONFIG_TYPE_STRING)
735 {
736 WARNING ("curl_xml plugin: `ValuesFrom' needs only string argument.");
737 return (-1);
738 }
740 sfree (xpath->values);
742 xpath->values_len = 0;
743 xpath->values = (cx_values_t *) malloc (sizeof (cx_values_t) * ci->values_num);
744 if (xpath->values == NULL)
745 return (-1);
746 xpath->values_len = ci->values_num;
748 /* populate cx_values_t structure */
749 for (i = 0; i < ci->values_num; i++)
750 {
751 xpath->values[i].path_len = sizeof (ci->values[i].value.string);
752 sstrncpy (xpath->values[i].path, ci->values[i].value.string, sizeof (xpath->values[i].path));
753 }
755 return (0);
756 } /* }}} cx_config_add_values */
758 static int cx_config_add_xpath (cx_t *db, /* {{{ */
759 oconfig_item_t *ci)
760 {
761 cx_xpath_t *xpath;
762 int status;
763 int i;
765 xpath = (cx_xpath_t *) malloc (sizeof (*xpath));
766 if (xpath == NULL)
767 {
768 ERROR ("curl_xml plugin: malloc failed.");
769 return (-1);
770 }
771 memset (xpath, 0, sizeof (*xpath));
773 status = cf_util_get_string (ci, &xpath->path);
774 if (status != 0)
775 {
776 sfree (xpath);
777 return (status);
778 }
780 /* error out if xpath->path is an empty string */
781 if (*xpath->path == 0)
782 {
783 ERROR ("curl_xml plugin: invalid xpath. "
784 "xpath value can't be an empty string");
785 return (-1);
786 }
788 status = 0;
789 for (i = 0; i < ci->children_num; i++)
790 {
791 oconfig_item_t *child = ci->children + i;
793 if (strcasecmp ("Type", child->key) == 0)
794 status = cf_util_get_string (child, &xpath->type);
795 else if (strcasecmp ("InstancePrefix", child->key) == 0)
796 status = cf_util_get_string (child, &xpath->instance_prefix);
797 else if (strcasecmp ("InstanceFrom", child->key) == 0)
798 status = cf_util_get_string (child, &xpath->instance);
799 else if (strcasecmp ("ValuesFrom", child->key) == 0)
800 status = cx_config_add_values ("ValuesFrom", xpath, child);
801 else
802 {
803 WARNING ("curl_xml plugin: Option `%s' not allowed here.", child->key);
804 status = -1;
805 }
807 if (status != 0)
808 break;
809 } /* for (i = 0; i < ci->children_num; i++) */
811 if (status == 0 && xpath->type == NULL)
812 {
813 WARNING ("curl_xml plugin: `Type' missing in `xpath' block.");
814 status = -1;
815 }
817 if (status == 0)
818 {
819 char *name;
820 llentry_t *le;
822 if (db->list == NULL)
823 {
824 db->list = llist_create();
825 if (db->list == NULL)
826 {
827 ERROR ("curl_xml plugin: list creation failed.");
828 return (-1);
829 }
830 }
832 name = strdup(xpath->path);
833 if (name == NULL)
834 {
835 ERROR ("curl_xml plugin: strdup failed.");
836 return (-1);
837 }
839 le = llentry_create (name, xpath);
840 if (le == NULL)
841 {
842 ERROR ("curl_xml plugin: llentry_create failed.");
843 return (-1);
844 }
846 llist_append (db->list, le);
847 }
849 return (status);
850 } /* }}} int cx_config_add_xpath */
852 /* Initialize db->curl */
853 static int cx_init_curl (cx_t *db) /* {{{ */
854 {
855 db->curl = curl_easy_init ();
856 if (db->curl == NULL)
857 {
858 ERROR ("curl_xml plugin: curl_easy_init failed.");
859 return (-1);
860 }
862 curl_easy_setopt (db->curl, CURLOPT_NOSIGNAL, 1L);
863 curl_easy_setopt (db->curl, CURLOPT_WRITEFUNCTION, cx_curl_callback);
864 curl_easy_setopt (db->curl, CURLOPT_WRITEDATA, db);
865 curl_easy_setopt (db->curl, CURLOPT_USERAGENT,
866 PACKAGE_NAME"/"PACKAGE_VERSION);
867 curl_easy_setopt (db->curl, CURLOPT_ERRORBUFFER, db->curl_errbuf);
868 curl_easy_setopt (db->curl, CURLOPT_URL, db->url);
870 if (db->user != NULL)
871 {
872 size_t credentials_size;
874 credentials_size = strlen (db->user) + 2;
875 if (db->pass != NULL)
876 credentials_size += strlen (db->pass);
878 db->credentials = (char *) malloc (credentials_size);
879 if (db->credentials == NULL)
880 {
881 ERROR ("curl_xml plugin: malloc failed.");
882 return (-1);
883 }
885 ssnprintf (db->credentials, credentials_size, "%s:%s",
886 db->user, (db->pass == NULL) ? "" : db->pass);
887 curl_easy_setopt (db->curl, CURLOPT_USERPWD, db->credentials);
888 }
890 curl_easy_setopt (db->curl, CURLOPT_SSL_VERIFYPEER, db->verify_peer ? 1L : 0L);
891 curl_easy_setopt (db->curl, CURLOPT_SSL_VERIFYHOST,
892 db->verify_host ? 2L : 0L);
893 if (db->cacert != NULL)
894 curl_easy_setopt (db->curl, CURLOPT_CAINFO, db->cacert);
895 if (db->headers != NULL)
896 curl_easy_setopt (db->curl, CURLOPT_HTTPHEADER, db->headers);
897 if (db->post_body != NULL)
898 curl_easy_setopt (db->curl, CURLOPT_POSTFIELDS, db->post_body);
900 return (0);
901 } /* }}} int cx_init_curl */
903 static int cx_config_add_url (oconfig_item_t *ci) /* {{{ */
904 {
905 cx_t *db;
906 int status = 0;
907 int i;
909 if ((ci->values_num != 1)
910 || (ci->values[0].type != OCONFIG_TYPE_STRING))
911 {
912 WARNING ("curl_xml plugin: The `URL' block "
913 "needs exactly one string argument.");
914 return (-1);
915 }
917 db = (cx_t *) malloc (sizeof (*db));
918 if (db == NULL)
919 {
920 ERROR ("curl_xml plugin: malloc failed.");
921 return (-1);
922 }
923 memset (db, 0, sizeof (*db));
925 if (strcasecmp ("URL", ci->key) == 0)
926 {
927 status = cf_util_get_string (ci, &db->url);
928 if (status != 0)
929 {
930 sfree (db);
931 return (status);
932 }
933 }
934 else
935 {
936 ERROR ("curl_xml plugin: cx_config: "
937 "Invalid key: %s", ci->key);
938 return (-1);
939 }
941 /* Fill the `cx_t' structure.. */
942 for (i = 0; i < ci->children_num; i++)
943 {
944 oconfig_item_t *child = ci->children + i;
946 if (strcasecmp ("Instance", child->key) == 0)
947 status = cf_util_get_string (child, &db->instance);
948 else if (strcasecmp ("Host", child->key) == 0)
949 status = cf_util_get_string (child, &db->host);
950 else if (strcasecmp ("User", child->key) == 0)
951 status = cf_util_get_string (child, &db->user);
952 else if (strcasecmp ("Password", child->key) == 0)
953 status = cf_util_get_string (child, &db->pass);
954 else if (strcasecmp ("VerifyPeer", child->key) == 0)
955 status = cf_util_get_boolean (child, &db->verify_peer);
956 else if (strcasecmp ("VerifyHost", child->key) == 0)
957 status = cf_util_get_boolean (child, &db->verify_host);
958 else if (strcasecmp ("CACert", child->key) == 0)
959 status = cf_util_get_string (child, &db->cacert);
960 else if (strcasecmp ("xpath", child->key) == 0)
961 status = cx_config_add_xpath (db, child);
962 else if (strcasecmp ("Header", child->key) == 0)
963 status = cx_config_append_string ("Header", &db->headers, child);
964 else if (strcasecmp ("Post", child->key) == 0)
965 status = cf_util_get_string (child, &db->post_body);
966 else if (strcasecmp ("Namespaces", child->key) == 0)
967 status = cf_util_get_string (child, &db->namespaces);
968 else
969 {
970 WARNING ("curl_xml plugin: Option `%s' not allowed here.", child->key);
971 status = -1;
972 }
974 if (status != 0)
975 break;
976 }
978 if (status == 0)
979 {
980 if (db->list == NULL)
981 {
982 WARNING ("curl_xml plugin: No (valid) `Key' block "
983 "within `URL' block `%s'.", db->url);
984 status = -1;
985 }
986 if (status == 0)
987 status = cx_init_curl (db);
988 }
990 /* If all went well, register this database for reading */
991 if (status == 0)
992 {
993 user_data_t ud;
994 char cb_name[DATA_MAX_NAME_LEN];
996 if (db->instance == NULL)
997 db->instance = strdup("default");
999 DEBUG ("curl_xml plugin: Registering new read callback: %s",
1000 db->instance);
1002 memset (&ud, 0, sizeof (ud));
1003 ud.data = (void *) db;
1004 ud.free_func = cx_free;
1006 ssnprintf (cb_name, sizeof (cb_name), "curl_xml-%s-%s",
1007 db->instance, db->url);
1009 plugin_register_complex_read (/* group = */ NULL, cb_name, cx_read,
1010 /* interval = */ NULL, &ud);
1011 }
1012 else
1013 {
1014 cx_free (db);
1015 return (-1);
1016 }
1018 return (0);
1019 } /* }}} int cx_config_add_url */
1021 /* }}} End of configuration handling functions */
1023 static int cx_config (oconfig_item_t *ci) /* {{{ */
1024 {
1025 int success;
1026 int errors;
1027 int status;
1028 int i;
1030 success = 0;
1031 errors = 0;
1033 for (i = 0; i < ci->children_num; i++)
1034 {
1035 oconfig_item_t *child = ci->children + i;
1037 if (strcasecmp ("URL", child->key) == 0)
1038 {
1039 status = cx_config_add_url (child);
1040 if (status == 0)
1041 success++;
1042 else
1043 errors++;
1044 }
1045 else
1046 {
1047 WARNING ("curl_xml plugin: Option `%s' not allowed here.", child->key);
1048 errors++;
1049 }
1050 }
1052 if ((success == 0) && (errors > 0))
1053 {
1054 ERROR ("curl_xml plugin: All statements failed.");
1055 return (-1);
1056 }
1058 return (0);
1059 } /* }}} int cx_config */
1061 void module_register (void)
1062 {
1063 plugin_register_complex_config ("curl_xml", cx_config);
1064 } /* void module_register */
1066 /* vim: set sw=2 sts=2 et fdm=marker : */