X-Git-Url: https://git.octo.it/?p=collectd.git;a=blobdiff_plain;f=src%2Fbind.c;h=370ebc58dc0710afcc874c483a474b3be97aa3a8;hp=38761775fa70dac5badbd53e8c859e8594de02ad;hb=ec51ddee94fa2ba1e01fe0e336ccc9c190a198ff;hpb=9717b1a55d60d992c16e66e2ae5bdfb42f80aca8 diff --git a/src/bind.c b/src/bind.c index 38761775..370ebc58 100644 --- a/src/bind.c +++ b/src/bind.c @@ -35,11 +35,19 @@ #endif #endif /* STRPTIME_NEEDS_STANDARDS */ +#if TIMEGM_NEEDS_BSD +#ifndef _BSD_SOURCE +#define _BSD_SOURCE 1 +#endif +#endif /* TIMEGM_NEEDS_BSD */ + #include "collectd.h" #include "common.h" #include "plugin.h" +#include + /* Some versions of libcurl don't include this themselves and then don't have * fd_set available. */ #if HAVE_SYS_SELECT_H @@ -96,25 +104,25 @@ typedef struct list_info_ptr_s list_info_ptr_t; /* FIXME: Enabled by default for backwards compatibility. */ /* TODO: Remove time parsing code. */ -static _Bool config_parse_time = 1; +static bool config_parse_time = true; -static char *url = NULL; +static char *url; static int global_opcodes = 1; static int global_qtypes = 1; static int global_server_stats = 1; static int global_zone_maint_stats = 1; -static int global_resolver_stats = 0; +static int global_resolver_stats; static int global_memory_stats = 1; static int timeout = -1; -static cb_view_t *views = NULL; -static size_t views_num = 0; +static cb_view_t *views; +static size_t views_num; -static CURL *curl = NULL; +static CURL *curl; -static char *bind_buffer = NULL; -static size_t bind_buffer_size = 0; -static size_t bind_buffer_fill = 0; +static char *bind_buffer; +static size_t bind_buffer_size; +static size_t bind_buffer_fill; static char bind_curl_error[CURL_ERROR_SIZE]; /* Translation table for the `nsstats' values. */ @@ -263,15 +271,13 @@ static size_t bind_curl_callback(void *buf, size_t size, /* {{{ */ size_t len = size * nmemb; if (len == 0) - return (len); + return len; if ((bind_buffer_fill + len) >= bind_buffer_size) { - char *temp; - - temp = realloc(bind_buffer, bind_buffer_fill + len + 1); + char *temp = realloc(bind_buffer, bind_buffer_fill + len + 1); if (temp == NULL) { ERROR("bind plugin: realloc failed."); - return (0); + return 0; } bind_buffer = temp; bind_buffer_size = bind_buffer_fill + len + 1; @@ -281,7 +287,7 @@ static size_t bind_curl_callback(void *buf, size_t size, /* {{{ */ bind_buffer_fill += len; bind_buffer[bind_buffer_fill] = 0; - return (len); + return len; } /* }}} size_t bind_curl_callback */ /* @@ -293,7 +299,7 @@ static int bind_xml_table_callback(const char *name, value_t value, /* {{{ */ translation_table_ptr_t *table = (translation_table_ptr_t *)user_data; if (table == NULL) - return (-1); + return -1; for (size_t i = 0; i < table->table_length; i++) { if (strcmp(table->table[i].xml_name, name) != 0) @@ -304,7 +310,7 @@ static int bind_xml_table_callback(const char *name, value_t value, /* {{{ */ break; } - return (0); + return 0; } /* }}} int bind_xml_table_callback */ /* @@ -317,52 +323,48 @@ static int bind_xml_list_callback(const char *name, /* {{{ */ list_info_ptr_t *list_info = (list_info_ptr_t *)user_data; if (list_info == NULL) - return (-1); + return -1; submit(current_time, list_info->plugin_instance, list_info->type, /* type instance = */ name, value); - return (0); + return 0; } /* }}} int bind_xml_list_callback */ static int bind_xml_read_derive(xmlDoc *doc, xmlNode *node, /* {{{ */ derive_t *ret_value) { - char *str_ptr; - value_t value; - int status; - - str_ptr = (char *)xmlNodeListGetString(doc, node->xmlChildrenNode, 1); + char *str_ptr = (char *)xmlNodeListGetString(doc, node->xmlChildrenNode, 1); if (str_ptr == NULL) { ERROR("bind plugin: bind_xml_read_derive: xmlNodeListGetString failed."); - return (-1); + return -1; } - status = parse_value(str_ptr, &value, DS_TYPE_DERIVE); + value_t value; + + int status = parse_value(str_ptr, &value, DS_TYPE_DERIVE); if (status != 0) { ERROR("bind plugin: Parsing string \"%s\" to derive value failed.", str_ptr); xmlFree(str_ptr); - return (-1); + return -1; } xmlFree(str_ptr); *ret_value = value.derive; - return (0); + return 0; } /* }}} int bind_xml_read_derive */ static int bind_xml_read_gauge(xmlDoc *doc, xmlNode *node, /* {{{ */ gauge_t *ret_value) { - char *str_ptr, *end_ptr; - double value; - - str_ptr = (char *)xmlNodeListGetString(doc, node->xmlChildrenNode, 1); + char *str_ptr = (char *)xmlNodeListGetString(doc, node->xmlChildrenNode, 1); if (str_ptr == NULL) { ERROR("bind plugin: bind_xml_read_gauge: xmlNodeListGetString failed."); - return (-1); + return -1; } + char *end_ptr; errno = 0; - value = strtod(str_ptr, &end_ptr); + double value = strtod(str_ptr, &end_ptr); xmlFree(str_ptr); if (str_ptr == end_ptr || errno) { if (errno && (value < 0)) @@ -371,32 +373,27 @@ static int bind_xml_read_gauge(xmlDoc *doc, xmlNode *node, /* {{{ */ ERROR("bind plugin: bind_xml_read_gauge: strtod failed with overflow."); else ERROR("bind plugin: bind_xml_read_gauge: strtod failed."); - return (-1); + return -1; } *ret_value = (gauge_t)value; - return (0); + return 0; } /* }}} int bind_xml_read_gauge */ static int bind_xml_read_timestamp(const char *xpath_expression, /* {{{ */ xmlDoc *doc, xmlXPathContext *xpathCtx, time_t *ret_value) { - xmlXPathObject *xpathObj = NULL; - xmlNode *node; - char *str_ptr; - char *tmp; - struct tm tm = {0}; - - xpathObj = xmlXPathEvalExpression(BAD_CAST xpath_expression, xpathCtx); + xmlXPathObject *xpathObj = + xmlXPathEvalExpression(BAD_CAST xpath_expression, xpathCtx); if (xpathObj == NULL) { ERROR("bind plugin: Unable to evaluate XPath expression `%s'.", xpath_expression); - return (-1); + return -1; } if ((xpathObj->nodesetval == NULL) || (xpathObj->nodesetval->nodeNr < 1)) { xmlXPathFreeObject(xpathObj); - return (-1); + return -1; } if (xpathObj->nodesetval->nodeNr != 1) { @@ -405,34 +402,52 @@ static int bind_xml_read_timestamp(const char *xpath_expression, /* {{{ */ xpath_expression, xpathObj->nodesetval->nodeNr); } - node = xpathObj->nodesetval->nodeTab[0]; + xmlNode *node = xpathObj->nodesetval->nodeTab[0]; if (node->xmlChildrenNode == NULL) { ERROR("bind plugin: bind_xml_read_timestamp: " "node->xmlChildrenNode == NULL"); xmlXPathFreeObject(xpathObj); - return (-1); + return -1; } - str_ptr = (char *)xmlNodeListGetString(doc, node->xmlChildrenNode, 1); + char *str_ptr = (char *)xmlNodeListGetString(doc, node->xmlChildrenNode, 1); if (str_ptr == NULL) { ERROR("bind plugin: bind_xml_read_timestamp: xmlNodeListGetString failed."); xmlXPathFreeObject(xpathObj); - return (-1); + return -1; } - tmp = strptime(str_ptr, "%Y-%m-%dT%T", &tm); + struct tm tm = {0}; + char *tmp = strptime(str_ptr, "%Y-%m-%dT%T", &tm); xmlFree(str_ptr); if (tmp == NULL) { ERROR("bind plugin: bind_xml_read_timestamp: strptime failed."); xmlXPathFreeObject(xpathObj); - return (-1); + return -1; } - *ret_value = mktime(&tm); +#if HAVE_TIMEGM + time_t t = timegm(&tm); + if (t == ((time_t)-1)) { + ERROR("bind plugin: timegm() failed: %s", STRERRNO); + return -1; + } + *ret_value = t; +#else + time_t t = mktime(&tm); + if (t == ((time_t)-1)) { + ERROR("bind plugin: mktime() failed: %s", STRERRNO); + return -1; + } + /* mktime assumes that tm is local time. Luckily, it also sets timezone to + * the offset used for the conversion, and we undo the conversion to convert + * back to UTC. */ + *ret_value = t - timezone; +#endif xmlXPathFreeObject(xpathObj); - return (0); + return 0; } /* }}} int bind_xml_read_timestamp */ /* @@ -449,25 +464,23 @@ static int bind_parse_generic_name_value(const char *xpath_expression, /* {{{ */ void *user_data, xmlDoc *doc, xmlXPathContext *xpathCtx, time_t current_time, int ds_type) { - xmlXPathObject *xpathObj = NULL; - int num_entries; - - xpathObj = xmlXPathEvalExpression(BAD_CAST xpath_expression, xpathCtx); + xmlXPathObject *xpathObj = + xmlXPathEvalExpression(BAD_CAST xpath_expression, xpathCtx); if (xpathObj == NULL) { ERROR("bind plugin: Unable to evaluate XPath expression `%s'.", xpath_expression); - return (-1); + return -1; } - num_entries = 0; + int num_entries = 0; /* Iterate over all matching nodes. */ for (int i = 0; xpathObj->nodesetval && (i < xpathObj->nodesetval->nodeNr); i++) { + xmlNode *name_node = NULL; xmlNode *counter = NULL; - xmlNode *parent; - parent = xpathObj->nodesetval->nodeTab[i]; + xmlNode *parent = xpathObj->nodesetval->nodeTab[i]; DEBUG("bind plugin: bind_parse_generic_name_value: parent->name = %s;", (char *)parent->name); @@ -493,8 +506,10 @@ static int bind_parse_generic_name_value(const char *xpath_expression, /* {{{ */ status = bind_xml_read_gauge(doc, counter, &value.gauge); else status = bind_xml_read_derive(doc, counter, &value.derive); - if (status != 0) + if (status != 0) { + xmlFree(name); continue; + } status = (*list_callback)(name, value, current_time, user_data); if (status == 0) @@ -509,7 +524,7 @@ static int bind_parse_generic_name_value(const char *xpath_expression, /* {{{ */ xmlXPathFreeObject(xpathObj); - return (0); + return 0; } /* }}} int bind_parse_generic_name_value */ /* @@ -528,32 +543,29 @@ static int bind_parse_generic_value_list(const char *xpath_expression, /* {{{ */ void *user_data, xmlDoc *doc, xmlXPathContext *xpathCtx, time_t current_time, int ds_type) { - xmlXPathObject *xpathObj = NULL; - int num_entries; - - xpathObj = xmlXPathEvalExpression(BAD_CAST xpath_expression, xpathCtx); + xmlXPathObject *xpathObj = + xmlXPathEvalExpression(BAD_CAST xpath_expression, xpathCtx); if (xpathObj == NULL) { ERROR("bind plugin: Unable to evaluate XPath expression `%s'.", xpath_expression); - return (-1); + return -1; } - num_entries = 0; + int num_entries = 0; /* Iterate over all matching nodes. */ for (int i = 0; xpathObj->nodesetval && (i < xpathObj->nodesetval->nodeNr); i++) { /* Iterate over all child nodes. */ for (xmlNode *child = xpathObj->nodesetval->nodeTab[i]->xmlChildrenNode; child != NULL; child = child->next) { - char *node_name; - value_t value; - int status; if (child->type != XML_ELEMENT_NODE) continue; - node_name = (char *)child->name; + char *node_name = (char *)child->name; + value_t value; + int status; if (ds_type == DS_TYPE_GAUGE) status = bind_xml_read_gauge(doc, child, &value.gauge); else @@ -572,7 +584,7 @@ static int bind_parse_generic_value_list(const char *xpath_expression, /* {{{ */ xmlXPathFreeObject(xpathObj); - return (0); + return 0; } /* }}} int bind_parse_generic_value_list */ /* @@ -590,17 +602,16 @@ static int bind_parse_generic_name_attr_value_list( const char *xpath_expression, /* {{{ */ list_callback_t list_callback, void *user_data, xmlDoc *doc, xmlXPathContext *xpathCtx, time_t current_time, int ds_type) { - xmlXPathObject *xpathObj = NULL; - int num_entries; - xpathObj = xmlXPathEvalExpression(BAD_CAST xpath_expression, xpathCtx); + xmlXPathObject *xpathObj = + xmlXPathEvalExpression(BAD_CAST xpath_expression, xpathCtx); if (xpathObj == NULL) { ERROR("bind plugin: Unable to evaluate XPath expression `%s'.", xpath_expression); - return (-1); + return -1; } - num_entries = 0; + int num_entries = 0; /* Iterate over all matching nodes. */ for (int i = 0; xpathObj->nodesetval && (i < xpathObj->nodesetval->nodeNr); i++) { @@ -613,25 +624,29 @@ static int bind_parse_generic_name_attr_value_list( if (strncmp("counter", (char *)child->name, strlen("counter")) != 0) continue; - char *attr_name; - value_t value; - int status; - - attr_name = (char *)xmlGetProp(child, BAD_CAST "name"); + char *attr_name = (char *)xmlGetProp(child, BAD_CAST "name"); if (attr_name == NULL) { DEBUG("bind plugin: found without name."); continue; } + + value_t value; + int status; + if (ds_type == DS_TYPE_GAUGE) status = bind_xml_read_gauge(doc, child, &value.gauge); else status = bind_xml_read_derive(doc, child, &value.derive); - if (status != 0) + if (status != 0) { + xmlFree(attr_name); continue; + } status = (*list_callback)(attr_name, value, current_time, user_data); if (status == 0) num_entries++; + + xmlFree(attr_name); } } @@ -640,15 +655,13 @@ static int bind_parse_generic_name_attr_value_list( xmlXPathFreeObject(xpathObj); - return (0); + return 0; } /* }}} int bind_parse_generic_name_attr_value_list */ static int bind_xml_stats_handle_zone(int version, xmlDoc *doc, /* {{{ */ xmlXPathContext *path_ctx, xmlNode *node, cb_view_t *view, time_t current_time) { - xmlXPathObject *path_obj; char *zone_name = NULL; - size_t j; if (version >= 3) { char *n = (char *)xmlGetProp(node, BAD_CAST "name"); @@ -660,10 +673,11 @@ static int bind_xml_stats_handle_zone(int version, xmlDoc *doc, /* {{{ */ xmlFree(n); xmlFree(c); } else { - path_obj = xmlXPathEvalExpression(BAD_CAST "name", path_ctx); + xmlXPathObject *path_obj = + xmlXPathEvalExpression(BAD_CAST "name", path_ctx); if (path_obj == NULL) { ERROR("bind plugin: xmlXPathEvalExpression failed."); - return (-1); + return -1; } for (int i = 0; path_obj->nodesetval && (i < path_obj->nodesetval->nodeNr); @@ -678,19 +692,19 @@ static int bind_xml_stats_handle_zone(int version, xmlDoc *doc, /* {{{ */ if (zone_name == NULL) { ERROR("bind plugin: Could not determine zone name."); - return (-1); + return -1; } + size_t j; for (j = 0; j < view->zones_num; j++) { if (strcasecmp(zone_name, view->zones[j]) == 0) break; } xmlFree(zone_name); - zone_name = NULL; if (j >= view->zones_num) - return (0); + return 0; zone_name = view->zones[j]; @@ -702,8 +716,8 @@ static int bind_xml_stats_handle_zone(int version, xmlDoc *doc, /* {{{ */ nsstats_translation_table_length, plugin_instance}; - ssnprintf(plugin_instance, sizeof(plugin_instance), "%s-zone-%s", - view->name, zone_name); + snprintf(plugin_instance, sizeof(plugin_instance), "%s-zone-%s", view->name, + zone_name); if (version == 3) { list_info_ptr_t list_info = {plugin_instance, @@ -726,26 +740,24 @@ static int bind_xml_stats_handle_zone(int version, xmlDoc *doc, /* {{{ */ } } /* }}} */ - return (0); + return 0; } /* }}} int bind_xml_stats_handle_zone */ static int bind_xml_stats_search_zones(int version, xmlDoc *doc, /* {{{ */ xmlXPathContext *path_ctx, xmlNode *node, cb_view_t *view, time_t current_time) { - xmlXPathObject *zone_nodes = NULL; - xmlXPathContext *zone_path_context; - - zone_path_context = xmlXPathNewContext(doc); + xmlXPathContext *zone_path_context = xmlXPathNewContext(doc); if (zone_path_context == NULL) { ERROR("bind plugin: xmlXPathNewContext failed."); - return (-1); + return -1; } - zone_nodes = xmlXPathEvalExpression(BAD_CAST "zones/zone", path_ctx); + xmlXPathObject *zone_nodes = + xmlXPathEvalExpression(BAD_CAST "zones/zone", path_ctx); if (zone_nodes == NULL) { ERROR("bind plugin: Cannot find any tags."); xmlXPathFreeContext(zone_path_context); - return (-1); + return -1; } for (int i = 0; i < zone_nodes->nodesetval->nodeNr; i++) { @@ -760,7 +772,7 @@ static int bind_xml_stats_search_zones(int version, xmlDoc *doc, /* {{{ */ xmlXPathFreeObject(zone_nodes); xmlXPathFreeContext(zone_path_context); - return (0); + return 0; } /* }}} int bind_xml_stats_search_zones */ static int bind_xml_stats_handle_view(int version, xmlDoc *doc, /* {{{ */ @@ -775,7 +787,7 @@ static int bind_xml_stats_handle_view(int version, xmlDoc *doc, /* {{{ */ if (view_name == NULL) { ERROR("bind plugin: Could not determine view name."); - return (-1); + return -1; } for (j = 0; j < views_num; j++) { @@ -786,11 +798,11 @@ static int bind_xml_stats_handle_view(int version, xmlDoc *doc, /* {{{ */ xmlFree(view_name); view_name = NULL; } else { - xmlXPathObject *path_obj; - path_obj = xmlXPathEvalExpression(BAD_CAST "name", path_ctx); + xmlXPathObject *path_obj = + xmlXPathEvalExpression(BAD_CAST "name", path_ctx); if (path_obj == NULL) { ERROR("bind plugin: xmlXPathEvalExpression failed."); - return (-1); + return -1; } for (int i = 0; path_obj->nodesetval && (i < path_obj->nodesetval->nodeNr); @@ -804,7 +816,7 @@ static int bind_xml_stats_handle_view(int version, xmlDoc *doc, /* {{{ */ if (view_name == NULL) { ERROR("bind plugin: Could not determine view name."); xmlXPathFreeObject(path_obj); - return (-1); + return -1; } for (j = 0; j < views_num; j++) { @@ -820,7 +832,7 @@ static int bind_xml_stats_handle_view(int version, xmlDoc *doc, /* {{{ */ } if (j >= views_num) - return (0); + return 0; view = views + j; @@ -833,8 +845,7 @@ static int bind_xml_stats_handle_view(int version, xmlDoc *doc, /* {{{ */ list_info_ptr_t list_info = {plugin_instance, /* type = */ "dns_qtype"}; - ssnprintf(plugin_instance, sizeof(plugin_instance), "%s-qtypes", - view->name); + snprintf(plugin_instance, sizeof(plugin_instance), "%s-qtypes", view->name); if (version == 3) { bind_parse_generic_name_attr_value_list( /* xpath = */ "counters[@type='resqtype']", @@ -856,8 +867,8 @@ static int bind_xml_stats_handle_view(int version, xmlDoc *doc, /* {{{ */ resstats_translation_table_length, plugin_instance}; - ssnprintf(plugin_instance, sizeof(plugin_instance), "%s-resolver_stats", - view->name); + snprintf(plugin_instance, sizeof(plugin_instance), "%s-resolver_stats", + view->name); if (version == 3) { bind_parse_generic_name_attr_value_list( "counters[@type='resstats']", @@ -879,8 +890,8 @@ static int bind_xml_stats_handle_view(int version, xmlDoc *doc, /* {{{ */ list_info_ptr_t list_info = {plugin_instance, /* type = */ "dns_qtype_cached"}; - ssnprintf(plugin_instance, sizeof(plugin_instance), "%s-cache_rr_sets", - view->name); + snprintf(plugin_instance, sizeof(plugin_instance), "%s-cache_rr_sets", + view->name); bind_parse_generic_name_value(/* xpath = */ "cache/rrset", /* callback = */ bind_xml_list_callback, @@ -892,33 +903,29 @@ static int bind_xml_stats_handle_view(int version, xmlDoc *doc, /* {{{ */ bind_xml_stats_search_zones(version, doc, path_ctx, node, view, current_time); - return (0); + return 0; } /* }}} int bind_xml_stats_handle_view */ static int bind_xml_stats_search_views(int version, xmlDoc *doc, /* {{{ */ xmlXPathContext *xpathCtx, xmlNode *statsnode, time_t current_time) { - xmlXPathObject *view_nodes = NULL; - xmlXPathContext *view_path_context; - - view_path_context = xmlXPathNewContext(doc); + xmlXPathContext *view_path_context = xmlXPathNewContext(doc); if (view_path_context == NULL) { ERROR("bind plugin: xmlXPathNewContext failed."); - return (-1); + return -1; } - view_nodes = xmlXPathEvalExpression(BAD_CAST "views/view", xpathCtx); + xmlXPathObject *view_nodes = + xmlXPathEvalExpression(BAD_CAST "views/view", xpathCtx); if (view_nodes == NULL) { ERROR("bind plugin: Cannot find any tags."); xmlXPathFreeContext(view_path_context); - return (-1); + return -1; } for (int i = 0; i < view_nodes->nodesetval->nodeNr; i++) { - xmlNode *node; - - node = view_nodes->nodesetval->nodeTab[i]; + xmlNode *node = view_nodes->nodesetval->nodeTab[i]; assert(node != NULL); view_path_context->node = node; @@ -929,7 +936,7 @@ static int bind_xml_stats_search_views(int version, xmlDoc *doc, /* {{{ */ xmlXPathFreeObject(view_nodes); xmlXPathFreeContext(view_path_context); - return (0); + return 0; } /* }}} int bind_xml_stats_search_views */ static void bind_xml_stats_v3(xmlDoc *doc, /* {{{ */ @@ -1222,17 +1229,16 @@ static void bind_xml_stats_v1_v2(int version, xmlDoc *doc, /* {{{ */ static int bind_xml_stats(int version, xmlDoc *doc, /* {{{ */ xmlXPathContext *xpathCtx, xmlNode *statsnode) { time_t current_time = 0; - int status; xpathCtx->node = statsnode; /* TODO: Check `server/boot-time' to recognize server restarts. */ - status = bind_xml_read_timestamp("server/current-time", doc, xpathCtx, - ¤t_time); + int status = bind_xml_read_timestamp("server/current-time", doc, xpathCtx, + ¤t_time); if (status != 0) { ERROR("bind plugin: Reading `server/current-time' failed."); - return (-1); + return -1; } DEBUG("bind plugin: Current server time is %i.", (int)current_time); @@ -1273,29 +1279,27 @@ static int bind_xml_stats(int version, xmlDoc *doc, /* {{{ */ static int bind_xml(const char *data) /* {{{ */ { - xmlDoc *doc = NULL; - xmlXPathContext *xpathCtx = NULL; - xmlXPathObject *xpathObj = NULL; int ret = -1; - doc = xmlParseMemory(data, strlen(data)); + xmlDoc *doc = xmlParseMemory(data, strlen(data)); if (doc == NULL) { ERROR("bind plugin: xmlParseMemory failed."); - return (-1); + return -1; } - xpathCtx = xmlXPathNewContext(doc); + xmlXPathContext *xpathCtx = xmlXPathNewContext(doc); if (xpathCtx == NULL) { ERROR("bind plugin: xmlXPathNewContext failed."); xmlFreeDoc(doc); - return (-1); + return -1; } // // version 3.* of statistics XML (since BIND9.9) // - xpathObj = xmlXPathEvalExpression(BAD_CAST "/statistics", xpathCtx); + xmlXPathObject *xpathObj = + xmlXPathEvalExpression(BAD_CAST "/statistics", xpathCtx); if (xpathObj == NULL || xpathObj->nodesetval == NULL || xpathObj->nodesetval->nodeNr == 0) { DEBUG("bind plugin: Statistics appears not to be v3"); @@ -1340,7 +1344,7 @@ static int bind_xml(const char *data) /* {{{ */ xmlXPathFreeContext(xpathCtx); xmlFreeDoc(doc); - return (ret); + return ret; } // @@ -1352,13 +1356,13 @@ static int bind_xml(const char *data) /* {{{ */ ERROR("bind plugin: Cannot find the tag."); xmlXPathFreeContext(xpathCtx); xmlFreeDoc(doc); - return (-1); + return -1; } else if (xpathObj->nodesetval == NULL) { ERROR("bind plugin: xmlXPathEvalExpression failed."); xmlXPathFreeObject(xpathObj); xmlXPathFreeContext(xpathCtx); xmlFreeDoc(doc); - return (-1); + return -1; } for (int i = 0; i < xpathObj->nodesetval->nodeNr; i++) { @@ -1406,7 +1410,7 @@ static int bind_xml(const char *data) /* {{{ */ xmlXPathFreeContext(xpathCtx); xmlFreeDoc(doc); - return (ret); + return ret; } /* }}} int bind_xml */ static int bind_config_set_bool(const char *name, int *var, /* {{{ */ @@ -1415,7 +1419,7 @@ static int bind_config_set_bool(const char *name, int *var, /* {{{ */ WARNING("bind plugin: The `%s' option needs " "exactly one boolean argument.", name); - return (-1); + return -1; } if (ci->values[0].value.boolean) @@ -1427,44 +1431,40 @@ static int bind_config_set_bool(const char *name, int *var, /* {{{ */ static int bind_config_add_view_zone(cb_view_t *view, /* {{{ */ oconfig_item_t *ci) { - char **tmp; - if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) { WARNING("bind plugin: The `Zone' option needs " "exactly one string argument."); - return (-1); + return -1; } - tmp = realloc(view->zones, sizeof(char *) * (view->zones_num + 1)); + char **tmp = realloc(view->zones, sizeof(char *) * (view->zones_num + 1)); if (tmp == NULL) { ERROR("bind plugin: realloc failed."); - return (-1); + return -1; } view->zones = tmp; view->zones[view->zones_num] = strdup(ci->values[0].value.string); if (view->zones[view->zones_num] == NULL) { ERROR("bind plugin: strdup failed."); - return (-1); + return -1; } view->zones_num++; - return (0); + return 0; } /* }}} int bind_config_add_view_zone */ static int bind_config_add_view(oconfig_item_t *ci) /* {{{ */ { - cb_view_t *tmp; - if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) { WARNING("bind plugin: `View' blocks need exactly one string argument."); - return (-1); + return -1; } - tmp = realloc(views, sizeof(*views) * (views_num + 1)); + cb_view_t *tmp = realloc(views, sizeof(*views) * (views_num + 1)); if (tmp == NULL) { ERROR("bind plugin: realloc failed."); - return (-1); + return -1; } views = tmp; tmp = views + views_num; @@ -1480,7 +1480,7 @@ static int bind_config_add_view(oconfig_item_t *ci) /* {{{ */ if (tmp->name == NULL) { ERROR("bind plugin: strdup failed."); sfree(views); - return (-1); + return -1; } for (int i = 0; i < ci->children_num; i++) { @@ -1502,7 +1502,7 @@ static int bind_config_add_view(oconfig_item_t *ci) /* {{{ */ } /* for (i = 0; i < ci->children_num; i++) */ views_num++; - return (0); + return 0; } /* }}} int bind_config_add_view */ static int bind_config(oconfig_item_t *ci) /* {{{ */ @@ -1515,7 +1515,7 @@ static int bind_config(oconfig_item_t *ci) /* {{{ */ (child->values[0].type != OCONFIG_TYPE_STRING)) { WARNING("bind plugin: The `Url' option needs " "exactly one string argument."); - return (-1); + return -1; } sfree(url); @@ -1545,25 +1545,24 @@ static int bind_config(oconfig_item_t *ci) /* {{{ */ } } - return (0); + return 0; } /* }}} int bind_config */ static int bind_init(void) /* {{{ */ { if (curl != NULL) - return (0); + return 0; curl = curl_easy_init(); if (curl == NULL) { ERROR("bind plugin: bind_init: curl_easy_init failed."); - return (-1); + return -1; } curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1L); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, bind_curl_callback); curl_easy_setopt(curl, CURLOPT_USERAGENT, COLLECTD_USERAGENT); curl_easy_setopt(curl, CURLOPT_ERRORBUFFER, bind_curl_error); - curl_easy_setopt(curl, CURLOPT_URL, (url != NULL) ? url : BIND_DEFAULT_URL); curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); curl_easy_setopt(curl, CURLOPT_MAXREDIRS, 50L); #ifdef HAVE_CURLOPT_TIMEOUT_MS @@ -1572,29 +1571,30 @@ static int bind_init(void) /* {{{ */ plugin_get_interval())); #endif - return (0); + return 0; } /* }}} int bind_init */ static int bind_read(void) /* {{{ */ { - int status; - if (curl == NULL) { ERROR("bind plugin: I don't have a CURL object."); - return (-1); + return -1; } bind_buffer_fill = 0; + + curl_easy_setopt(curl, CURLOPT_URL, (url != NULL) ? url : BIND_DEFAULT_URL); + if (curl_easy_perform(curl) != CURLE_OK) { ERROR("bind plugin: curl_easy_perform failed: %s", bind_curl_error); - return (-1); + return -1; } - status = bind_xml(bind_buffer); + int status = bind_xml(bind_buffer); if (status != 0) - return (-1); + return -1; else - return (0); + return 0; } /* }}} int bind_read */ static int bind_shutdown(void) /* {{{ */ @@ -1604,7 +1604,7 @@ static int bind_shutdown(void) /* {{{ */ curl = NULL; } - return (0); + return 0; } /* }}} int bind_shutdown */ void module_register(void) {