From: Pavel Rochnyak Date: Fri, 29 Jun 2018 12:56:59 +0000 (+0700) Subject: Merge pull request #2844 from elfiesmelfie/fix_snmp_agent X-Git-Url: https://git.octo.it/?p=collectd.git;a=commitdiff_plain;h=b626c5956e7289cb3d7fceb230baba0182f36d68;hp=ef917009d077a9da2528ac35d2e4049d10163bdf Merge pull request #2844 from elfiesmelfie/fix_snmp_agent SNMP Agent plugin: Fix coverity scan issues #2814 --- diff --git a/src/bind.c b/src/bind.c index 3a5e3c31..fe3480d0 100644 --- a/src/bind.c +++ b/src/bind.c @@ -73,9 +73,9 @@ typedef int (*list_callback_t)(const char *name, value_t value, struct cb_view_s { char *name; - int qtypes; - int resolver_stats; - int cacherrsets; + _Bool qtypes; + _Bool resolver_stats; + _Bool cacherrsets; char **zones; size_t zones_num; @@ -107,12 +107,12 @@ typedef struct list_info_ptr_s list_info_ptr_t; static bool config_parse_time = true; 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; -static int global_memory_stats = 1; +static _Bool global_opcodes = 1; +static _Bool global_qtypes = 1; +static _Bool global_server_stats = 1; +static _Bool global_zone_maint_stats = 1; +static _Bool global_resolver_stats; +static _Bool global_memory_stats = 1; static int timeout = -1; static cb_view_t *views; @@ -343,8 +343,6 @@ static int bind_xml_read_derive(xmlDoc *doc, xmlNode *node, /* {{{ */ 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; } @@ -1410,22 +1408,6 @@ static int bind_xml(const char *data) /* {{{ */ return ret; } /* }}} int bind_xml */ -static int bind_config_set_bool(const char *name, int *var, /* {{{ */ - oconfig_item_t *ci) { - if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN)) { - WARNING("bind plugin: The `%s' option needs " - "exactly one boolean argument.", - name); - return -1; - } - - if (ci->values[0].value.boolean) - *var = 1; - else - *var = 0; - return 0; -} /* }}} int bind_config_set_bool */ - static int bind_config_add_view_zone(cb_view_t *view, /* {{{ */ oconfig_item_t *ci) { if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) { @@ -1484,11 +1466,11 @@ static int bind_config_add_view(oconfig_item_t *ci) /* {{{ */ oconfig_item_t *child = ci->children + i; if (strcasecmp("QTypes", child->key) == 0) - bind_config_set_bool("QTypes", &tmp->qtypes, child); + cf_util_get_boolean(child, &tmp->qtypes); else if (strcasecmp("ResolverStats", child->key) == 0) - bind_config_set_bool("ResolverStats", &tmp->resolver_stats, child); + cf_util_get_boolean(child, &tmp->resolver_stats); else if (strcasecmp("CacheRRSets", child->key) == 0) - bind_config_set_bool("CacheRRSets", &tmp->cacherrsets, child); + cf_util_get_boolean(child, &tmp->cacherrsets); else if (strcasecmp("Zone", child->key) == 0) bind_config_add_view_zone(tmp, child); else { @@ -1508,27 +1490,19 @@ static int bind_config(oconfig_item_t *ci) /* {{{ */ oconfig_item_t *child = ci->children + i; if (strcasecmp("Url", child->key) == 0) { - if ((child->values_num != 1) || - (child->values[0].type != OCONFIG_TYPE_STRING)) { - WARNING("bind plugin: The `Url' option needs " - "exactly one string argument."); - return -1; - } - - sfree(url); - url = strdup(child->values[0].value.string); + cf_util_get_string(child, &url); } else if (strcasecmp("OpCodes", child->key) == 0) - bind_config_set_bool("OpCodes", &global_opcodes, child); + cf_util_get_boolean(child, &global_opcodes); else if (strcasecmp("QTypes", child->key) == 0) - bind_config_set_bool("QTypes", &global_qtypes, child); + cf_util_get_boolean(child, &global_qtypes); else if (strcasecmp("ServerStats", child->key) == 0) - bind_config_set_bool("ServerStats", &global_server_stats, child); + cf_util_get_boolean(child, &global_server_stats); else if (strcasecmp("ZoneMaintStats", child->key) == 0) - bind_config_set_bool("ZoneMaintStats", &global_zone_maint_stats, child); + cf_util_get_boolean(child, &global_zone_maint_stats); else if (strcasecmp("ResolverStats", child->key) == 0) - bind_config_set_bool("ResolverStats", &global_resolver_stats, child); + cf_util_get_boolean(child, &global_resolver_stats); else if (strcasecmp("MemoryStats", child->key) == 0) - bind_config_set_bool("MemoryStats", &global_memory_stats, child); + cf_util_get_boolean(child, &global_memory_stats); else if (strcasecmp("View", child->key) == 0) bind_config_add_view(child); else if (strcasecmp("ParseTime", child->key) == 0) diff --git a/src/collectd-tg.c b/src/collectd-tg.c index 4669c655..2d83bbfe 100644 --- a/src/collectd-tg.c +++ b/src/collectd-tg.c @@ -105,7 +105,7 @@ static double dtime(void) /* {{{ */ { struct timespec ts = {0}; - if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0) + if (clock_gettime(CLOCK_REALTIME, &ts) != 0) perror("clock_gettime"); return (double)ts.tv_sec + (double)ts.tv_nsec / 1e9; diff --git a/src/collectd.conf.pod b/src/collectd.conf.pod index 75b956fe..ccc69491 100644 --- a/src/collectd.conf.pod +++ b/src/collectd.conf.pod @@ -5438,6 +5438,12 @@ behavior is to let the kernel choose the appropriate interface. Be warned that the manual selection of an interface for unicast traffic is only necessary in rare cases. +=item B I + +Set the outgoing IP address for IP packets. This option can be used instead of +the I option to explicitly define the IP address which will be used +to send Packets to the remote server. + =item B I Sets the interval at which to re-resolve the DNS for the I. This is diff --git a/src/curl_json.c b/src/curl_json.c index f0badc99..5d96cbd5 100644 --- a/src/curl_json.c +++ b/src/curl_json.c @@ -256,7 +256,6 @@ static int cj_cb_number(void *ctx, const char *number, yajl_len_t number_len) { value_t vt; int status = parse_value(buffer, &vt, type); if (status != 0) { - NOTICE("curl_json plugin: Unable to parse number: \"%s\"", buffer); cj_advance_array(ctx); return CJ_CB_CONTINUE; } diff --git a/src/daemon/common.c b/src/daemon/common.c index 2251d2e0..76c7036d 100644 --- a/src/daemon/common.c +++ b/src/daemon/common.c @@ -417,7 +417,7 @@ int strunescape(char *buf, size_t buf_len) { continue; if (((i + 1) >= buf_len) || (buf[i + 1] == 0)) { - ERROR("string unescape: backslash found at end of string."); + P_ERROR("string unescape: backslash found at end of string."); /* Ensure null-byte at the end of the buffer. */ buf[i] = 0; return -1; @@ -606,9 +606,9 @@ int check_create_dir(const char *file_orig) { * behavior. */ if (fields[i][0] == '.') { - ERROR("Cowardly refusing to create a directory that " - "begins with a `.' (dot): `%s'", - file_orig); + P_ERROR("Cowardly refusing to create a directory that " + "begins with a `.' (dot): `%s'", + file_orig); return -2; } @@ -619,7 +619,7 @@ int check_create_dir(const char *file_orig) { if (strjoin(dir + path_is_absolute, (size_t)(sizeof(dir) - path_is_absolute), fields, (size_t)(i + 1), "/") < 0) { - ERROR("strjoin failed: `%s', component #%i", file_orig, i); + P_ERROR("strjoin failed: `%s', component #%i", file_orig, i); return -1; } @@ -635,16 +635,16 @@ int check_create_dir(const char *file_orig) { if (EEXIST == errno) continue; - ERROR("check_create_dir: mkdir (%s): %s", dir, STRERRNO); + P_ERROR("check_create_dir: mkdir (%s): %s", dir, STRERRNO); return -1; } else { - ERROR("check_create_dir: stat (%s): %s", dir, STRERRNO); + P_ERROR("check_create_dir: stat (%s): %s", dir, STRERRNO); return -1; } } else if (!S_ISDIR(statbuf.st_mode)) { - ERROR("check_create_dir: `%s' exists but is not " - "a directory!", - dir); + P_ERROR("check_create_dir: `%s' exists but is not " + "a directory!", + dir); return -1; } break; @@ -667,12 +667,12 @@ int get_kstat(kstat_t **ksp_ptr, char *module, int instance, char *name) { *ksp_ptr = kstat_lookup(kc, module, instance, name); if (*ksp_ptr == NULL) { - ERROR("get_kstat: Cound not find kstat %s", ident); + P_ERROR("get_kstat: Cound not find kstat %s", ident); return -1; } if ((*ksp_ptr)->ks_type != KSTAT_TYPE_NAMED) { - ERROR("get_kstat: kstat %s has wrong type", ident); + P_ERROR("get_kstat: kstat %s has wrong type", ident); *ksp_ptr = NULL; return -1; } @@ -683,12 +683,12 @@ int get_kstat(kstat_t **ksp_ptr, char *module, int instance, char *name) { #endif if (kstat_read(kc, *ksp_ptr, NULL) == -1) { - ERROR("get_kstat: kstat %s could not be read", ident); + P_ERROR("get_kstat: kstat %s could not be read", ident); return -1; } if ((*ksp_ptr)->ks_type != KSTAT_TYPE_NAMED) { - ERROR("get_kstat: kstat %s has wrong type", ident); + P_ERROR("get_kstat: kstat %s has wrong type", ident); return -1; } @@ -700,12 +700,12 @@ long long get_kstat_value(kstat_t *ksp, char *name) { long long retval = -1LL; if (ksp == NULL) { - ERROR("get_kstat_value (\"%s\"): ksp is NULL.", name); + P_ERROR("get_kstat_value (\"%s\"): ksp is NULL.", name); return -1LL; } else if (ksp->ks_type != KSTAT_TYPE_NAMED) { - ERROR("get_kstat_value (\"%s\"): ksp->ks_type (%#x) " - "is not KSTAT_TYPE_NAMED (%#x).", - name, (unsigned int)ksp->ks_type, (unsigned int)KSTAT_TYPE_NAMED); + P_ERROR("get_kstat_value (\"%s\"): ksp->ks_type (%#x) " + "is not KSTAT_TYPE_NAMED (%#x).", + name, (unsigned int)ksp->ks_type, (unsigned int)KSTAT_TYPE_NAMED); return -1LL; } @@ -723,7 +723,7 @@ long long get_kstat_value(kstat_t *ksp, char *name) { else if (kn->data_type == KSTAT_DATA_UINT64) retval = (long long)kn->value.ui64; /* XXX: Might overflow! */ else - WARNING("get_kstat_value: Not a numeric value: %s", name); + P_WARNING("get_kstat_value: Not a numeric value: %s", name); return retval; } @@ -1035,19 +1035,19 @@ int parse_value(const char *value_orig, value_t *ret_value, int ds_type) { default: sfree(value); - ERROR("parse_value: Invalid data source type: %i.", ds_type); + P_ERROR("parse_value: Invalid data source type: %i.", ds_type); return -1; } if (value == endptr) { - ERROR("parse_value: Failed to parse string as %s: \"%s\".", - DS_TYPE_TO_STRING(ds_type), value); + P_ERROR("parse_value: Failed to parse string as %s: \"%s\".", + DS_TYPE_TO_STRING(ds_type), value); sfree(value); return -1; } else if ((NULL != endptr) && ('\0' != *endptr)) - INFO("parse_value: Ignoring trailing garbage \"%s\" after %s value. " - "Input string was \"%s\".", - endptr, DS_TYPE_TO_STRING(ds_type), value_orig); + P_INFO("parse_value: Ignoring trailing garbage \"%s\" after %s value. " + "Input string was \"%s\".", + endptr, DS_TYPE_TO_STRING(ds_type), value_orig); sfree(value); return 0; @@ -1212,7 +1212,7 @@ int walk_directory(const char *dir, dirwalk_callback_f callback, failure = 0; if ((dh = opendir(dir)) == NULL) { - ERROR("walk_directory: Cannot open '%s': %s", dir, STRERRNO); + P_ERROR("walk_directory: Cannot open '%s': %s", dir, STRERRNO); return -1; } @@ -1252,7 +1252,7 @@ ssize_t read_file_contents(const char *filename, char *buf, size_t bufsize) { ret = (ssize_t)fread(buf, 1, bufsize, fh); if ((ret == 0) && (ferror(fh) != 0)) { - ERROR("read_file_contents: Reading file \"%s\" failed.", filename); + P_ERROR("read_file_contents: Reading file \"%s\" failed.", filename); ret = -1; } @@ -1411,8 +1411,8 @@ int service_name_to_port_number(const char *service_name) { status = getaddrinfo(/* node = */ NULL, service_name, &ai_hints, &ai_list); if (status != 0) { - ERROR("service_name_to_port_number: getaddrinfo failed: %s", - gai_strerror(status)); + P_ERROR("service_name_to_port_number: getaddrinfo failed: %s", + gai_strerror(status)); return -1; } @@ -1450,7 +1450,7 @@ void set_sock_opts(int sockfd) /* {{{ */ status = getsockopt(sockfd, SOL_SOCKET, SO_TYPE, &socktype, &(socklen_t){sizeof(socktype)}); if (status != 0) { - WARNING("set_sock_opts: failed to determine socket type"); + P_WARNING("set_sock_opts: failed to determine socket type"); return; } @@ -1458,14 +1458,14 @@ void set_sock_opts(int sockfd) /* {{{ */ status = setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, &(int){1}, sizeof(int)); if (status != 0) - WARNING("set_sock_opts: failed to set socket keepalive flag"); + P_WARNING("set_sock_opts: failed to set socket keepalive flag"); #ifdef TCP_KEEPIDLE int tcp_keepidle = ((CDTIME_T_TO_MS(plugin_get_interval()) - 1) / 100 + 1); status = setsockopt(sockfd, IPPROTO_TCP, TCP_KEEPIDLE, &tcp_keepidle, sizeof(tcp_keepidle)); if (status != 0) - WARNING("set_sock_opts: failed to set socket tcp keepalive time"); + P_WARNING("set_sock_opts: failed to set socket tcp keepalive time"); #endif #ifdef TCP_KEEPINTVL @@ -1474,7 +1474,7 @@ void set_sock_opts(int sockfd) /* {{{ */ status = setsockopt(sockfd, IPPROTO_TCP, TCP_KEEPINTVL, &tcp_keepintvl, sizeof(tcp_keepintvl)); if (status != 0) - WARNING("set_sock_opts: failed to set socket tcp keepalive interval"); + P_WARNING("set_sock_opts: failed to set socket tcp keepalive interval"); #endif } } /* }}} void set_sock_opts */ @@ -1558,12 +1558,12 @@ int check_capability(int arg) /* {{{ */ return -1; if (!(cap = cap_get_proc())) { - ERROR("check_capability: cap_get_proc failed."); + P_ERROR("check_capability: cap_get_proc failed."); return -1; } if (cap_get_flag(cap, cap_value, CAP_EFFECTIVE, &cap_flag_value) < 0) { - ERROR("check_capability: cap_get_flag failed."); + P_ERROR("check_capability: cap_get_flag failed."); cap_free(cap); return -1; } @@ -1574,8 +1574,8 @@ int check_capability(int arg) /* {{{ */ #else int check_capability(__attribute__((unused)) int arg) /* {{{ */ { - WARNING("check_capability: unsupported capability implementation. " - "Some plugin(s) may require elevated privileges to work properly."); + P_WARNING("check_capability: unsupported capability implementation. " + "Some plugin(s) may require elevated privileges to work properly."); return 0; } /* }}} int check_capability */ #endif /* HAVE_CAPABILITY */ diff --git a/src/daemon/configfile.c b/src/daemon/configfile.c index f88d234f..d9038002 100644 --- a/src/daemon/configfile.c +++ b/src/daemon/configfile.c @@ -251,11 +251,7 @@ static int dispatch_value_plugindir(oconfig_item_t *ci) { } static int dispatch_loadplugin(oconfig_item_t *ci) { - const char *name; bool global = false; - plugin_ctx_t ctx = {0}; - plugin_ctx_t old_ctx; - int ret_val; assert(strcasecmp(ci->key, "LoadPlugin") == 0); @@ -265,14 +261,16 @@ static int dispatch_loadplugin(oconfig_item_t *ci) { return -1; } - name = ci->values[0].value.string; + const char *name = ci->values[0].value.string; if (strcmp("libvirt", name) == 0) name = "virt"; /* default to the global interval set before loading this plugin */ - ctx.interval = cf_get_default_interval(); - ctx.flush_interval = 0; - ctx.flush_timeout = 0; + plugin_ctx_t ctx = { + .interval = cf_get_default_interval(), .name = strdup(name), + }; + if (ctx.name == NULL) + return ENOMEM; for (int i = 0; i < ci->children_num; ++i) { oconfig_item_t *child = ci->children + i; @@ -288,12 +286,12 @@ static int dispatch_loadplugin(oconfig_item_t *ci) { else { WARNING("Ignoring unknown LoadPlugin option \"%s\" " "for plugin \"%s\"", - child->key, ci->values[0].value.string); + child->key, name); } } - old_ctx = plugin_set_ctx(ctx); - ret_val = plugin_load(name, global); + plugin_ctx_t old_ctx = plugin_set_ctx(ctx); + int ret_val = plugin_load(name, global); /* reset to the "global" context */ plugin_set_ctx(old_ctx); @@ -1048,16 +1046,12 @@ int cf_read(const char *filename) { * success. */ int cf_util_get_string(const oconfig_item_t *ci, char **ret_string) /* {{{ */ { - char *string; - if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) { - ERROR("cf_util_get_string: The %s option requires " - "exactly one string argument.", - ci->key); + P_ERROR("The `%s' option requires exactly one string argument.", ci->key); return -1; } - string = strdup(ci->values[0].value.string); + char *string = strdup(ci->values[0].value.string); if (string == NULL) return -1; @@ -1076,9 +1070,7 @@ int cf_util_get_string_buffer(const oconfig_item_t *ci, char *buffer, /* {{{ */ return EINVAL; if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) { - ERROR("cf_util_get_string_buffer: The %s option requires " - "exactly one string argument.", - ci->key); + P_ERROR("The `%s' option requires exactly one string argument.", ci->key); return -1; } @@ -1095,9 +1087,7 @@ int cf_util_get_int(const oconfig_item_t *ci, int *ret_value) /* {{{ */ return EINVAL; if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER)) { - ERROR("cf_util_get_int: The %s option requires " - "exactly one numeric argument.", - ci->key); + P_ERROR("The `%s' option requires exactly one numeric argument.", ci->key); return -1; } @@ -1112,9 +1102,7 @@ int cf_util_get_double(const oconfig_item_t *ci, double *ret_value) /* {{{ */ return EINVAL; if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER)) { - ERROR("cf_util_get_double: The %s option requires " - "exactly one numeric argument.", - ci->key); + P_ERROR("The `%s' option requires exactly one numeric argument.", ci->key); return -1; } @@ -1130,9 +1118,7 @@ int cf_util_get_boolean(const oconfig_item_t *ci, bool *ret_bool) /* {{{ */ if ((ci->values_num != 1) || ((ci->values[0].type != OCONFIG_TYPE_BOOLEAN) && (ci->values[0].type != OCONFIG_TYPE_STRING))) { - ERROR("cf_util_get_boolean: The %s option requires " - "exactly one boolean argument.", - ci->key); + P_ERROR("The `%s' option requires exactly one boolean argument.", ci->key); return -1; } @@ -1141,19 +1127,19 @@ int cf_util_get_boolean(const oconfig_item_t *ci, bool *ret_bool) /* {{{ */ *ret_bool = ci->values[0].value.boolean ? true : false; break; case OCONFIG_TYPE_STRING: - WARNING("cf_util_get_boolean: Using string value `%s' for boolean option " - "`%s' is deprecated and will be removed in future releases. " - "Use unquoted true or false instead.", - ci->values[0].value.string, ci->key); + P_WARNING("Using string value `%s' for boolean option `%s' is deprecated " + "and will be removed in future releases. Use unquoted true or " + "false instead.", + ci->values[0].value.string, ci->key); if (IS_TRUE(ci->values[0].value.string)) *ret_bool = true; else if (IS_FALSE(ci->values[0].value.string)) *ret_bool = false; else { - ERROR("cf_util_get_boolean: Cannot parse string value `%s' of the `%s' " - "option as a boolean value.", - ci->values[0].value.string, ci->key); + P_ERROR("Cannot parse string value `%s' of the `%s' option as a boolean " + "value.", + ci->values[0].value.string, ci->key); return -1; } break; @@ -1194,9 +1180,7 @@ int cf_util_get_port_number(const oconfig_item_t *ci) /* {{{ */ if ((ci->values_num != 1) || ((ci->values[0].type != OCONFIG_TYPE_STRING) && (ci->values[0].type != OCONFIG_TYPE_NUMBER))) { - ERROR("cf_util_get_port_number: The \"%s\" option requires " - "exactly one string argument.", - ci->key); + P_ERROR("The `%s' option requires exactly one string argument.", ci->key); return -1; } @@ -1206,11 +1190,9 @@ int cf_util_get_port_number(const oconfig_item_t *ci) /* {{{ */ assert(ci->values[0].type == OCONFIG_TYPE_NUMBER); tmp = (int)(ci->values[0].value.number + 0.5); if ((tmp < 1) || (tmp > 65535)) { - ERROR("cf_util_get_port_number: The \"%s\" option requires " - "a service name or a port number. The number " - "you specified, %i, is not in the valid " - "range of 1-65535.", - ci->key, tmp); + P_ERROR("The `%s' option requires a service name or a port number. The " + "number you specified, %i, is not in the valid range of 1-65535.", + ci->key, tmp); return -1; } @@ -1224,18 +1206,15 @@ int cf_util_get_service(const oconfig_item_t *ci, char **ret_string) /* {{{ */ int status; if (ci->values_num != 1) { - ERROR("cf_util_get_service: The %s option requires exactly " - "one argument.", - ci->key); + P_ERROR("The `%s` option requires exactly one argument.", ci->key); return -1; } if (ci->values[0].type == OCONFIG_TYPE_STRING) return cf_util_get_string(ci, ret_string); if (ci->values[0].type != OCONFIG_TYPE_NUMBER) { - ERROR("cf_util_get_service: The %s option requires " - "exactly one string or numeric argument.", - ci->key); + P_ERROR("The `%s` option requires exactly one string or numeric argument.", + ci->key); } port = 0; @@ -1243,16 +1222,14 @@ int cf_util_get_service(const oconfig_item_t *ci, char **ret_string) /* {{{ */ if (status != 0) return status; else if ((port < 1) || (port > 65535)) { - ERROR("cf_util_get_service: The port number given " - "for the %s option is out of " - "range (%i).", - ci->key, port); + P_ERROR("The port number given for the `%s` option is out of range (%i).", + ci->key, port); return -1; } service = malloc(6); if (service == NULL) { - ERROR("cf_util_get_service: Out of memory."); + P_ERROR("cf_util_get_service: Out of memory."); return -1; } snprintf(service, 6, "%i", port); @@ -1269,16 +1246,13 @@ int cf_util_get_cdtime(const oconfig_item_t *ci, cdtime_t *ret_value) /* {{{ */ return EINVAL; if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER)) { - ERROR("cf_util_get_cdtime: The %s option requires " - "exactly one numeric argument.", - ci->key); + P_ERROR("The `%s' option requires exactly one numeric argument.", ci->key); return -1; } if (ci->values[0].value.number < 0.0) { - ERROR("cf_util_get_cdtime: The numeric argument of the %s " - "option must not be negative.", - ci->key); + P_ERROR("The numeric argument of the `%s' option must not be negative.", + ci->key); return -1; } diff --git a/src/daemon/plugin.c b/src/daemon/plugin.c index 427a8134..92e1ab26 100644 --- a/src/daemon/plugin.c +++ b/src/daemon/plugin.c @@ -346,19 +346,22 @@ static void log_list_callbacks(llist_t **list, /* {{{ */ static int create_register_callback(llist_t **list, /* {{{ */ const char *name, void *callback, user_data_t const *ud) { - callback_func_t *cf; - cf = calloc(1, sizeof(*cf)); + if (name == NULL || callback == NULL) + return EINVAL; + + callback_func_t *cf = calloc(1, sizeof(*cf)); if (cf == NULL) { free_userdata(ud); ERROR("plugin: create_register_callback: calloc failed."); - return -1; + return ENOMEM; } cf->cf_callback = callback; if (ud == NULL) { - cf->cf_udata.data = NULL; - cf->cf_udata.free_func = NULL; + cf->cf_udata = (user_data_t){ + .data = NULL, .free_func = NULL, + }; } else { cf->cf_udata = *ud; } @@ -1714,8 +1717,12 @@ int plugin_write(const char *plugin, /* {{{ */ callback_func_t *cf = le->value; plugin_write_cb callback; - /* do not switch plugin context; rather keep the context (interval) - * information of the calling read plugin */ + /* Keep the read plugin's interval and flush information but update the + * plugin name. */ + plugin_ctx_t old_ctx = plugin_get_ctx(); + plugin_ctx_t ctx = old_ctx; + ctx.name = cf->cf_ctx.name; + plugin_set_ctx(ctx); DEBUG("plugin: plugin_write: Writing values via %s.", le->key); callback = cf->cf_callback; @@ -1725,6 +1732,7 @@ int plugin_write(const char *plugin, /* {{{ */ else success++; + plugin_set_ctx(old_ctx); le = le->next; } @@ -2236,6 +2244,21 @@ void plugin_log(int level, const char *format, ...) { } } /* void plugin_log */ +void daemon_log(int level, const char *format, ...) { + char msg[1024] = ""; // Size inherits from plugin_log() + + char const *name = plugin_get_ctx().name; + if (name == NULL) + name = "UNKNOWN"; + + va_list ap; + va_start(ap, format); + vsnprintf(msg, sizeof(msg), format, ap); + va_end(ap); + + plugin_log(level, "%s plugin: %s", name, msg); +} /* void daemon_log */ + int parse_log_severity(const char *severity) { int log_level = -1; diff --git a/src/daemon/plugin.h b/src/daemon/plugin.h index 03690678..871eccdc 100644 --- a/src/daemon/plugin.h +++ b/src/daemon/plugin.h @@ -171,6 +171,7 @@ struct user_data_s { typedef struct user_data_s user_data_t; struct plugin_ctx_s { + char *name; cdtime_t interval; cdtime_t flush_interval; cdtime_t flush_timeout; @@ -398,6 +399,15 @@ int parse_notif_severity(const char *severity); #define DEBUG(...) /* noop */ #endif /* ! COLLECT_DEBUG */ +/* This will log messages, prefixed by plugin name */ +void daemon_log(int level, const char *format, ...) + __attribute__((format(printf, 2, 3))); + +#define P_ERROR(...) daemon_log(LOG_ERR, __VA_ARGS__) +#define P_WARNING(...) daemon_log(LOG_WARNING, __VA_ARGS__) +#define P_NOTICE(...) daemon_log(LOG_NOTICE, __VA_ARGS__) +#define P_INFO(...) daemon_log(LOG_INFO, __VA_ARGS__) + const data_set_t *plugin_get_ds(const char *name); int plugin_notification_meta_add_string(notification_t *n, const char *name, diff --git a/src/daemon/plugin_mock.c b/src/daemon/plugin_mock.c index a11d4f54..1624f0ea 100644 --- a/src/daemon/plugin_mock.c +++ b/src/daemon/plugin_mock.c @@ -170,6 +170,17 @@ void plugin_log(int level, char const *format, ...) { printf("plugin_log (%i, \"%s\");\n", level, buffer); } +void daemon_log(int level, char const *format, ...) { + char buffer[1024]; + va_list ap; + + va_start(ap, format); + vsnprintf(buffer, sizeof(buffer), format, ap); + va_end(ap); + + printf("daemon_log (%i, \"%s\");\n", level, buffer); +} + void plugin_init_ctx(void) { /* nop */ } diff --git a/src/filecount.c b/src/filecount.c index ef1a1387..9091ff55 100644 --- a/src/filecount.c +++ b/src/filecount.c @@ -154,26 +154,6 @@ static int fc_config_add_dir_instance(fc_directory_conf_t *dir, return fc_config_set_instance(dir, ci->values[0].value.string); } /* int fc_config_add_dir_instance */ -static int fc_config_add_dir_name(fc_directory_conf_t *dir, - oconfig_item_t *ci) { - if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) { - WARNING("filecount plugin: The `Name' config option needs exactly one " - "string argument."); - return -1; - } - - char *temp = strdup(ci->values[0].value.string); - if (temp == NULL) { - ERROR("filecount plugin: strdup failed."); - return -1; - } - - sfree(dir->name); - dir->name = temp; - - return 0; -} /* int fc_config_add_dir_name */ - static int fc_config_add_dir_mtime(fc_directory_conf_t *dir, oconfig_item_t *ci) { if ((ci->values_num != 1) || ((ci->values[0].type != OCONFIG_TYPE_STRING) && @@ -369,7 +349,7 @@ static int fc_config_add_dir(oconfig_item_t *ci) { else if (strcasecmp("Instance", option->key) == 0) status = fc_config_add_dir_instance(dir, option); else if (strcasecmp("Name", option->key) == 0) - status = fc_config_add_dir_name(dir, option); + status = cf_util_get_string(option, &dir->name); else if (strcasecmp("MTime", option->key) == 0) status = fc_config_add_dir_mtime(dir, option); else if (strcasecmp("Size", option->key) == 0) diff --git a/src/gmond.c b/src/gmond.c index ca654194..2bca05ad 100644 --- a/src/gmond.c +++ b/src/gmond.c @@ -833,28 +833,6 @@ static int mc_receive_thread_stop(void) /* {{{ */ * * */ -static int gmond_config_set_string(oconfig_item_t *ci, char **str) /* {{{ */ -{ - char *tmp; - - if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) { - WARNING("gmond plugin: The `%s' option needs " - "exactly one string argument.", - ci->key); - return -1; - } - - tmp = strdup(ci->values[0].value.string); - if (tmp == NULL) { - ERROR("gmond plugin: strdup failed."); - return -1; - } - - sfree(*str); - *str = tmp; - return 0; -} /* }}} int gmond_config_set_string */ - static int gmond_config_add_metric(oconfig_item_t *ci) /* {{{ */ { metric_map_t *map; @@ -889,11 +867,11 @@ static int gmond_config_add_metric(oconfig_item_t *ci) /* {{{ */ for (int i = 0; i < ci->children_num; i++) { oconfig_item_t *child = ci->children + i; if (strcasecmp("Type", child->key) == 0) - gmond_config_set_string(child, &map->type); + cf_util_get_string(child, &map->type); else if (strcasecmp("TypeInstance", child->key) == 0) - gmond_config_set_string(child, &map->type_instance); + cf_util_get_string(child, &map->type_instance); else if (strcasecmp("DataSource", child->key) == 0) - gmond_config_set_string(child, &map->ds_name); + cf_util_get_string(child, &map->ds_name); else { WARNING("gmond plugin: Unknown configuration option `%s' ignored.", child->key); diff --git a/src/memcachec.c b/src/memcachec.c index 13e388e8..f293aa1b 100644 --- a/src/memcachec.c +++ b/src/memcachec.c @@ -123,21 +123,6 @@ static int cmc_page_init_memc(web_page_t *wp) /* {{{ */ return 0; } /* }}} int cmc_page_init_memc */ -static int cmc_config_add_string(const char *name, char **dest, /* {{{ */ - oconfig_item_t *ci) { - if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) { - WARNING("memcachec plugin: `%s' needs exactly one string argument.", name); - return -1; - } - - sfree(*dest); - *dest = strdup(ci->values[0].value.string); - if (*dest == NULL) - return -1; - - return 0; -} /* }}} int cmc_config_add_string */ - static int cmc_config_add_match_dstype(int *dstype_ret, /* {{{ */ oconfig_item_t *ci) { int dstype; @@ -204,16 +189,15 @@ static int cmc_config_add_match(web_page_t *page, /* {{{ */ oconfig_item_t *child = ci->children + i; if (strcasecmp("Regex", child->key) == 0) - status = cmc_config_add_string("Regex", &match->regex, child); + status = cf_util_get_string(child, &match->regex); else if (strcasecmp("ExcludeRegex", child->key) == 0) - status = - cmc_config_add_string("ExcludeRegex", &match->exclude_regex, child); + status = cf_util_get_string(child, &match->exclude_regex); else if (strcasecmp("DSType", child->key) == 0) status = cmc_config_add_match_dstype(&match->dstype, child); else if (strcasecmp("Type", child->key) == 0) - status = cmc_config_add_string("Type", &match->type, child); + status = cf_util_get_string(child, &match->type); else if (strcasecmp("Instance", child->key) == 0) - status = cmc_config_add_string("Instance", &match->instance, child); + status = cf_util_get_string(child, &match->instance); else { WARNING("memcachec plugin: Option `%s' not allowed here.", child->key); status = -1; @@ -301,11 +285,11 @@ static int cmc_config_add_page(oconfig_item_t *ci) /* {{{ */ oconfig_item_t *child = ci->children + i; if (strcasecmp("Server", child->key) == 0) - status = cmc_config_add_string("Server", &page->server, child); + status = cf_util_get_string(child, &page->server); else if (strcasecmp("Key", child->key) == 0) - status = cmc_config_add_string("Key", &page->key, child); + status = cf_util_get_string(child, &page->key); else if (strcasecmp("Plugin", child->key) == 0) - status = cmc_config_add_string("Plugin", &page->plugin_name, child); + status = cf_util_get_string(child, &page->plugin_name); else if (strcasecmp("Match", child->key) == 0) /* Be liberal with failing matches => don't set `status'. */ cmc_config_add_match(page, child); diff --git a/src/netapp.c b/src/netapp.c index 281764c7..62600ca2 100644 --- a/src/netapp.c +++ b/src/netapp.c @@ -2229,42 +2229,6 @@ static int cna_query_system(host_config_t *host) /* {{{ */ /* * Configuration handling */ -/* Sets a given flag if the boolean argument is true and unsets the flag if it - * is false. On error, the flag-field is not changed. */ -static int cna_config_bool_to_flag(const oconfig_item_t *ci, /* {{{ */ - uint32_t *flags, uint32_t flag) { - if ((ci == NULL) || (flags == NULL)) - return EINVAL; - - if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_BOOLEAN)) { - WARNING("netapp plugin: The %s option needs exactly one boolean argument.", - ci->key); - return -1; - } - - if (ci->values[0].value.boolean) - *flags |= flag; - else - *flags &= ~flag; - - return 0; -} /* }}} int cna_config_bool_to_flag */ - -/* Handling of the "Interval" option which is allowed in every block. */ -static int cna_config_get_interval(const oconfig_item_t *ci, /* {{{ */ - cna_interval_t *out_interval) { - cdtime_t tmp = 0; - int status; - - status = cf_util_get_cdtime(ci, &tmp); - if (status != 0) - return status; - - out_interval->interval = tmp; - out_interval->last_read = 0; - - return 0; -} /* }}} int cna_config_get_interval */ /* Handling of the "GetIO", "GetOps" and "GetLatency" options within a * block. */ @@ -2385,7 +2349,7 @@ static int cna_config_volume_performance(host_config_t *host, /* {{{ */ /* if (!item || !item->key || !*item->key) continue; */ if (strcasecmp(item->key, "Interval") == 0) - cna_config_get_interval(item, &cfg_volume_perf->interval); + cf_util_get_cdtime(item, &cfg_volume_perf->interval.interval); else if (!strcasecmp(item->key, "GetIO")) cna_config_volume_perf_option(cfg_volume_perf, item); else if (!strcasecmp(item->key, "GetOps")) @@ -2481,7 +2445,7 @@ static int cna_config_quota(host_config_t *host, oconfig_item_t *ci) /* {{{ */ oconfig_item_t *item = ci->children + i; if (strcasecmp(item->key, "Interval") == 0) - cna_config_get_interval(item, &cfg_quota->interval); + cf_util_get_cdtime(item, &cfg_quota->interval.interval); else WARNING("netapp plugin: The option %s is not allowed within " "`Quota' blocks.", @@ -2517,9 +2481,9 @@ static int cna_config_disk(host_config_t *host, oconfig_item_t *ci) { /* {{{ */ /* if (!item || !item->key || !*item->key) continue; */ if (strcasecmp(item->key, "Interval") == 0) - cna_config_get_interval(item, &cfg_disk->interval); + cf_util_get_cdtime(item, &cfg_disk->interval.interval); else if (strcasecmp(item->key, "GetBusy") == 0) - cna_config_bool_to_flag(item, &cfg_disk->flags, CFG_DISK_BUSIEST); + cf_util_get_flag(item, &cfg_disk->flags, CFG_DISK_BUSIEST); } if ((cfg_disk->flags & CFG_DISK_ALL) == 0) { @@ -2556,15 +2520,15 @@ static int cna_config_wafl(host_config_t *host, oconfig_item_t *ci) /* {{{ */ oconfig_item_t *item = ci->children + i; if (strcasecmp(item->key, "Interval") == 0) - cna_config_get_interval(item, &cfg_wafl->interval); + cf_util_get_cdtime(item, &cfg_wafl->interval.interval); else if (!strcasecmp(item->key, "GetNameCache")) - cna_config_bool_to_flag(item, &cfg_wafl->flags, CFG_WAFL_NAME_CACHE); + cf_util_get_flag(item, &cfg_wafl->flags, CFG_WAFL_NAME_CACHE); else if (!strcasecmp(item->key, "GetDirCache")) - cna_config_bool_to_flag(item, &cfg_wafl->flags, CFG_WAFL_DIR_CACHE); + cf_util_get_flag(item, &cfg_wafl->flags, CFG_WAFL_DIR_CACHE); else if (!strcasecmp(item->key, "GetBufferCache")) - cna_config_bool_to_flag(item, &cfg_wafl->flags, CFG_WAFL_BUF_CACHE); + cf_util_get_flag(item, &cfg_wafl->flags, CFG_WAFL_BUF_CACHE); else if (!strcasecmp(item->key, "GetInodeCache")) - cna_config_bool_to_flag(item, &cfg_wafl->flags, CFG_WAFL_INODE_CACHE); + cf_util_get_flag(item, &cfg_wafl->flags, CFG_WAFL_INODE_CACHE); else WARNING("netapp plugin: The %s config option is not allowed within " "`WAFL' blocks.", @@ -2636,7 +2600,7 @@ static int cna_config_volume_usage(host_config_t *host, /* {{{ */ /* if (!item || !item->key || !*item->key) continue; */ if (strcasecmp(item->key, "Interval") == 0) - cna_config_get_interval(item, &cfg_volume_usage->interval); + cf_util_get_cdtime(item, &cfg_volume_usage->interval.interval); else if (!strcasecmp(item->key, "GetCapacity")) cna_config_volume_usage_option(cfg_volume_usage, item); else if (!strcasecmp(item->key, "GetSnapshot")) @@ -2677,7 +2641,7 @@ static int cna_config_snapvault(host_config_t *host, /* {{{ */ oconfig_item_t *item = ci->children + i; if (strcasecmp(item->key, "Interval") == 0) - cna_config_get_interval(item, &cfg_snapvault->interval); + cf_util_get_cdtime(item, &cfg_snapvault->interval.interval); else WARNING("netapp plugin: The option %s is not allowed within " "`SnapVault' blocks.", @@ -2712,15 +2676,15 @@ static int cna_config_system(host_config_t *host, /* {{{ */ oconfig_item_t *item = ci->children + i; if (strcasecmp(item->key, "Interval") == 0) { - cna_config_get_interval(item, &cfg_system->interval); + cf_util_get_cdtime(item, &cfg_system->interval.interval); } else if (!strcasecmp(item->key, "GetCPULoad")) { - cna_config_bool_to_flag(item, &cfg_system->flags, CFG_SYSTEM_CPU); + cf_util_get_flag(item, &cfg_system->flags, CFG_SYSTEM_CPU); } else if (!strcasecmp(item->key, "GetInterfaces")) { - cna_config_bool_to_flag(item, &cfg_system->flags, CFG_SYSTEM_NET); + cf_util_get_flag(item, &cfg_system->flags, CFG_SYSTEM_NET); } else if (!strcasecmp(item->key, "GetDiskOps")) { - cna_config_bool_to_flag(item, &cfg_system->flags, CFG_SYSTEM_OPS); + cf_util_get_flag(item, &cfg_system->flags, CFG_SYSTEM_OPS); } else if (!strcasecmp(item->key, "GetDiskIO")) { - cna_config_bool_to_flag(item, &cfg_system->flags, CFG_SYSTEM_DISK); + cf_util_get_flag(item, &cfg_system->flags, CFG_SYSTEM_DISK); } else { WARNING("netapp plugin: The %s config option is not allowed within " "`System' blocks.", diff --git a/src/network.c b/src/network.c index 84ba00b5..d602dfc8 100644 --- a/src/network.c +++ b/src/network.c @@ -113,6 +113,7 @@ struct sockent_client { #endif cdtime_t next_resolve_reconnect; cdtime_t resolve_interval; + struct sockaddr_storage *bind_addr; }; struct sockent_server { @@ -1501,6 +1502,7 @@ static void free_sockent_client(struct sockent_client *sec) /* {{{ */ sec->fd = -1; } sfree(sec->addr); + sfree(sec->bind_addr); #if HAVE_GCRYPT_H sfree(sec->username); sfree(sec->password); @@ -1683,6 +1685,42 @@ static int network_set_interface(const sockent_t *se, return 0; } /* }}} network_set_interface */ +static int network_bind_socket_to_addr(sockent_t *se, + const struct addrinfo *ai) { + + if (se->data.client.bind_addr == NULL) + return 0; + + DEBUG("network_plugin: fd %i: bind socket to address", se->data.client.fd); + char pbuffer[64]; + + if (ai->ai_family == AF_INET) { + struct sockaddr_in *addr = + (struct sockaddr_in *)(se->data.client.bind_addr); + inet_ntop(AF_INET, &(addr->sin_addr), pbuffer, 64); + DEBUG("network_plugin: binding client socket to ipv4 address: %s", pbuffer); + if (bind(se->data.client.fd, (struct sockaddr *)addr, sizeof(*addr)) == + -1) { + ERROR("network plugin: failed to bind client socket (ipv4) to %s: %s", + pbuffer, STRERRNO); + return -1; + } + } else if (ai->ai_family == AF_INET6) { + struct sockaddr_in6 *addr = + (struct sockaddr_in6 *)(se->data.client.bind_addr); + inet_ntop(AF_INET6, &(addr->sin6_addr), pbuffer, 64); + DEBUG("network_plugin: binding client socket to ipv6 address: %s", pbuffer); + if (bind(se->data.client.fd, (struct sockaddr *)addr, sizeof(*addr)) == + -1) { + ERROR("network plugin: failed to bind client socket (ipv6) to %s: %s", + pbuffer, STRERRNO); + return -1; + } + } + + return 0; +} /* int network_bind_socket_to_addr */ + static int network_bind_socket(int fd, const struct addrinfo *ai, const int interface_idx) { #if KERNEL_SOLARIS @@ -1834,6 +1872,7 @@ static sockent_t *sockent_create(int type) /* {{{ */ } else { se->data.client.fd = -1; se->data.client.addr = NULL; + se->data.client.bind_addr = NULL; se->data.client.resolve_interval = 0; se->data.client.next_resolve_reconnect = 0; #if HAVE_GCRYPT_H @@ -1989,6 +2028,7 @@ static int sockent_client_connect(sockent_t *se) /* {{{ */ network_set_ttl(se, ai_ptr); network_set_interface(se, ai_ptr); + network_bind_socket_to_addr(se, ai_ptr); /* We don't open more than one write-socket per * node/service pair.. */ @@ -2684,6 +2724,57 @@ static int network_config_set_interface(const oconfig_item_t *ci, /* {{{ */ return 0; } /* }}} int network_config_set_interface */ +static int +network_config_set_bind_address(const oconfig_item_t *ci, + struct sockaddr_storage **bind_address) { + if ((*bind_address) != NULL) { + ERROR("network_plugin: only a single bind address is allowed"); + return -1; + } + + char addr_text[256]; + + if (cf_util_get_string_buffer(ci, addr_text, sizeof(addr_text)) != 0) + return -1; + + int ret; + struct addrinfo *res = NULL; + struct addrinfo ai_hints = {.ai_family = AF_UNSPEC, + .ai_flags = AI_NUMERICHOST, + .ai_protocol = IPPROTO_UDP, + .ai_socktype = SOCK_DGRAM}; + + ret = getaddrinfo(addr_text, NULL, &ai_hints, &res); + if (ret) { + ERROR("network plugin: Bind address option has invalid address set: %s", + gai_strerror(ret)); + return -1; + } + + *bind_address = malloc(sizeof(**bind_address)); + if (*bind_address == NULL) { + ERROR("network plugin: network_config_set_bind_address: malloc failed."); + return -1; + } + (*bind_address)->ss_family = res->ai_family; + if (res->ai_family == AF_INET) { + struct sockaddr_in *addr = (struct sockaddr_in *)(*bind_address); + inet_pton(AF_INET, addr_text, &(addr->sin_addr)); + } else if (res->ai_family == AF_INET6) { + struct sockaddr_in6 *addr = (struct sockaddr_in6 *)(*bind_address); + inet_pton(AF_INET6, addr_text, &(addr->sin6_addr)); + } else { + ERROR("network plugin: %s is an unknown address format %d\n", addr_text, + res->ai_family); + sfree(*bind_address); + freeaddrinfo(res); + return -1; + } + + freeaddrinfo(res); + return 0; +} /* int network_config_set_bind_address */ + static int network_config_set_buffer_size(const oconfig_item_t *ci) /* {{{ */ { int tmp = 0; @@ -2843,6 +2934,8 @@ static int network_config_add_server(const oconfig_item_t *ci) /* {{{ */ #endif /* HAVE_GCRYPT_H */ if (strcasecmp("Interface", child->key) == 0) network_config_set_interface(child, &se->interface); + else if (strcasecmp("BindAddress", child->key) == 0) + network_config_set_bind_address(child, &se->data.client.bind_addr); else if (strcasecmp("ResolveInterval", child->key) == 0) cf_util_get_cdtime(child, &se->data.client.resolve_interval); else { diff --git a/src/notify_email.c b/src/notify_email.c index 6b32ad9a..bb36ff27 100644 --- a/src/notify_email.c +++ b/src/notify_email.c @@ -211,8 +211,8 @@ static int notify_email_notification(const notification_t *n, char subject[MAXSTRING]; char buf[4096] = ""; + char *buf_ptr = buf; int buf_len = sizeof(buf); - int i; snprintf(severity, sizeof(severity), "%s", (n->severity == NOTIF_FAILURE) @@ -231,15 +231,36 @@ static int notify_email_notification(const notification_t *n, timestamp_str[sizeof(timestamp_str) - 1] = '\0'; /* Let's make RFC822 message text with \r\n EOLs */ - snprintf(buf, buf_len, "MIME-Version: 1.0\r\n" - "Content-Type: text/plain; charset=\"US-ASCII\"\r\n" - "Content-Transfer-Encoding: 8bit\r\n" - "Subject: %s\r\n" - "\r\n" - "%s - %s@%s\r\n" - "\r\n" - "Message: %s", - subject, timestamp_str, severity, n->host, n->message); + int status = snprintf(buf, buf_len, + "MIME-Version: 1.0\r\n" + "Content-Type: text/plain; charset=\"US-ASCII\"\r\n" + "Content-Transfer-Encoding: 8bit\r\n" + "Subject: %s\r\n" + "\r\n" + "%s - %s@%s\r\n" + "\r\n", + subject, timestamp_str, severity, n->host); + + if (status > 0) { + buf_ptr += status; + buf_len -= status; + } + +#define APPEND(format, value) \ + if ((buf_len > 0) && (strlen(value) > 0)) { \ + status = snprintf(buf_ptr, buf_len, format "\r\n", value); \ + if (status > 0) { \ + buf_ptr += status; \ + buf_len -= status; \ + } \ + } + + APPEND("Host: %s", n->host); + APPEND("Plugin: %s", n->plugin); + APPEND("Plugin instance: %s", n->plugin_instance); + APPEND("Type: %s", n->type); + APPEND("Type instance: %s", n->type_instance); + APPEND("\r\nMessage: %s", n->message); pthread_mutex_lock(&session_lock); @@ -258,7 +279,7 @@ static int notify_email_notification(const notification_t *n, smtp_set_header(message, "To", NULL, NULL); smtp_set_message_str(message, buf); - for (i = 0; i < recipients_len; i++) + for (int i = 0; i < recipients_len; i++) smtp_add_recipient(message, recipients[i]); /* Initiate a connection to the SMTP server and transfer the message. */ diff --git a/src/powerdns.c b/src/powerdns.c index eb3ec537..7a2fbfd6 100644 --- a/src/powerdns.c +++ b/src/powerdns.c @@ -359,9 +359,6 @@ static void submit(const char *plugin_instance, /* {{{ */ } if (0 != parse_value(value_str, &value, ds->ds[0].type)) { - ERROR("powerdns plugin: Cannot convert `%s' " - "to a number.", - value_str); return; } diff --git a/src/protocols.c b/src/protocols.c index 59eb49a7..36b1d83b 100644 --- a/src/protocols.c +++ b/src/protocols.c @@ -58,7 +58,6 @@ static void submit(const char *protocol_name, const char *str_key, status = parse_value(str_value, &value, DS_TYPE_DERIVE); if (status != 0) { - ERROR("protocols plugin: Parsing string as integer failed: %s", str_value); return; } diff --git a/src/redis.c b/src/redis.c index 36f57b09..7edd329c 100644 --- a/src/redis.c +++ b/src/redis.c @@ -59,6 +59,12 @@ struct redis_query_s { redis_query_t *next; }; +struct prev_s { + derive_t keyspace_hits; + derive_t keyspace_misses; +}; +typedef struct prev_s prev_t; + struct redis_node_s; typedef struct redis_node_s redis_node_t; struct redis_node_s { @@ -71,6 +77,7 @@ struct redis_node_s { bool report_cpu_usage; redisContext *redisContext; redis_query_t *queries; + prev_t prev; redis_node_t *next; }; @@ -535,6 +542,50 @@ static void redis_cpu_usage(const char *node, char const *info_line) { } } /* void redis_cpu_usage */ +static gauge_t calculate_ratio_percent(derive_t part1, derive_t part2, + derive_t *prev1, derive_t *prev2) { + if ((*prev1 == 0) || (*prev2 == 0) || (part1 < *prev1) || (part2 < *prev2)) { + *prev1 = part1; + *prev2 = part2; + return NAN; + } + + derive_t num = part1 - *prev1; + derive_t denom = part2 - *prev2 + num; + + *prev1 = part1; + *prev2 = part2; + + if (denom == 0) + return NAN; + + if (num == 0) + return 0; + + return 100.0 * (gauge_t)num / (gauge_t)denom; +} /* gauge_t calculate_ratio_percent */ + +static void redis_keyspace_usage(redis_node_t *rn, char const *info_line) { + value_t hits, misses; + + if (redis_get_info_value(info_line, "keyspace_hits", DS_TYPE_DERIVE, &hits) != + 0) + return; + + if (redis_get_info_value(info_line, "keyspace_misses", DS_TYPE_DERIVE, + &misses) != 0) + return; + + redis_submit(rn->name, "cache_result", "hits", hits); + redis_submit(rn->name, "cache_result", "misses", misses); + + prev_t *prev = &rn->prev; + gauge_t ratio = calculate_ratio_percent( + hits.derive, misses.derive, &prev->keyspace_hits, &prev->keyspace_misses); + redis_submit(rn->name, "percent", "hitratio", (value_t){.gauge = ratio}); + +} /* void redis_keyspace_usage */ + static void redis_check_connection(redis_node_t *rn) { if (rn->redisContext) return; @@ -615,15 +666,13 @@ static void redis_read_server_info(redis_node_t *rn) { DS_TYPE_GAUGE); redis_handle_info(rn->name, rr->str, "current_connections", "slaves", "connected_slaves", DS_TYPE_GAUGE); - redis_handle_info(rn->name, rr->str, "cache_result", "hits", "keyspace_hits", - DS_TYPE_DERIVE); - redis_handle_info(rn->name, rr->str, "cache_result", "misses", - "keyspace_misses", DS_TYPE_DERIVE); redis_handle_info(rn->name, rr->str, "total_bytes", "input", "total_net_input_bytes", DS_TYPE_DERIVE); redis_handle_info(rn->name, rr->str, "total_bytes", "output", "total_net_output_bytes", DS_TYPE_DERIVE); + redis_keyspace_usage(rn, rr->str); + redis_db_stats(rn->name, rr->str); if (rn->report_cpu_usage) diff --git a/src/snmp_agent_test.c b/src/snmp_agent_test.c index bd500fce..581f33d3 100644 --- a/src/snmp_agent_test.c +++ b/src/snmp_agent_test.c @@ -145,9 +145,9 @@ DEF_TEST(format_name_regex_index) { snmp_varlist_add_variable(&index_list_tmp, NULL, 0, ASN_OCTET_STR, (const u_char *)plugin_inst, strlen(plugin_inst)); snmp_varlist_add_variable(&index_list_tmp, NULL, 0, ASN_INTEGER, - (const u_char *)&vcpu, 1); + (const u_char *)&vcpu, sizeof(vcpu)); snmp_varlist_add_variable(&index_list_tmp, NULL, 0, ASN_INTEGER, - (const u_char *)&cpu, 1); + (const u_char *)&cpu, sizeof(cpu)); build_oid_noalloc(index_oid.oid, sizeof(index_oid.oid), &index_oid.oid_len, NULL, 0, index_list_tmp); diff --git a/src/table.c b/src/table.c index 189d6057..492bea61 100644 --- a/src/table.c +++ b/src/table.c @@ -132,18 +132,6 @@ static size_t tables_num; /* * configuration handling */ - -static int tbl_config_set_s(char *name, char **var, oconfig_item_t *ci) { - if (ci->values_num != 1 || ci->values[0].type != OCONFIG_TYPE_STRING) { - log_err("\"%s\" expects a single string argument.", name); - return 1; - } - - sfree(*var); - *var = sstrdup(ci->values[0].value.string); - return 0; -} /* tbl_config_set_separator */ - static int tbl_config_append_array_i(char *name, size_t **var, size_t *len, oconfig_item_t *ci) { if (ci->values_num < 1) { @@ -196,9 +184,9 @@ static int tbl_config_result(tbl_t *tbl, oconfig_item_t *ci) { oconfig_item_t *c = ci->children + i; if (strcasecmp(c->key, "Type") == 0) - tbl_config_set_s(c->key, &res->type, c); + cf_util_get_string(c, &res->type); else if (strcasecmp(c->key, "InstancePrefix") == 0) - tbl_config_set_s(c->key, &res->instance_prefix, c); + cf_util_get_string(c, &res->instance_prefix); else if (strcasecmp(c->key, "InstancesFrom") == 0) tbl_config_append_array_i(c->key, &res->instances, &res->instances_num, c); @@ -253,11 +241,11 @@ static int tbl_config_table(oconfig_item_t *ci) { oconfig_item_t *c = ci->children + i; if (strcasecmp(c->key, "Separator") == 0) - tbl_config_set_s(c->key, &tbl->sep, c); + cf_util_get_string(c, &tbl->sep); else if (strcasecmp(c->key, "Plugin") == 0) - tbl_config_set_s(c->key, &tbl->plugin_name, c); + cf_util_get_string(c, &tbl->plugin_name); else if (strcasecmp(c->key, "Instance") == 0) - tbl_config_set_s(c->key, &tbl->instance, c); + cf_util_get_string(c, &tbl->instance); else if (strcasecmp(c->key, "Result") == 0) tbl_config_result(tbl, c); else diff --git a/src/threshold.c b/src/threshold.c index 79001334..79300f14 100644 --- a/src/threshold.c +++ b/src/threshold.c @@ -109,90 +109,6 @@ static int ut_threshold_add(const threshold_t *th) { /* {{{ */ * The following approximately two hundred functions are used to handle the * configuration and fill the threshold list. * {{{ */ -static int ut_config_type_datasource(threshold_t *th, oconfig_item_t *ci) { - if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) { - WARNING("threshold values: The `DataSource' option needs exactly one " - "string argument."); - return -1; - } - - sstrncpy(th->data_source, ci->values[0].value.string, - sizeof(th->data_source)); - - return 0; -} /* int ut_config_type_datasource */ - -static int ut_config_type_instance(threshold_t *th, oconfig_item_t *ci) { - if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) { - WARNING("threshold values: The `Instance' option needs exactly one " - "string argument."); - return -1; - } - - sstrncpy(th->type_instance, ci->values[0].value.string, - sizeof(th->type_instance)); - - return 0; -} /* int ut_config_type_instance */ - -static int ut_config_type_max(threshold_t *th, oconfig_item_t *ci) { - if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER)) { - WARNING("threshold values: The `%s' option needs exactly one " - "number argument.", - ci->key); - return -1; - } - - if (strcasecmp(ci->key, "WarningMax") == 0) - th->warning_max = ci->values[0].value.number; - else - th->failure_max = ci->values[0].value.number; - - return 0; -} /* int ut_config_type_max */ - -static int ut_config_type_min(threshold_t *th, oconfig_item_t *ci) { - if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER)) { - WARNING("threshold values: The `%s' option needs exactly one " - "number argument.", - ci->key); - return -1; - } - - if (strcasecmp(ci->key, "WarningMin") == 0) - th->warning_min = ci->values[0].value.number; - else - th->failure_min = ci->values[0].value.number; - - return 0; -} /* int ut_config_type_min */ - -static int ut_config_type_hits(threshold_t *th, oconfig_item_t *ci) { - if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER)) { - WARNING("threshold values: The `%s' option needs exactly one " - "number argument.", - ci->key); - return -1; - } - - th->hits = ci->values[0].value.number; - - return 0; -} /* int ut_config_type_hits */ - -static int ut_config_type_hysteresis(threshold_t *th, oconfig_item_t *ci) { - if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER)) { - WARNING("threshold values: The `%s' option needs exactly one " - "number argument.", - ci->key); - return -1; - } - - th->hysteresis = ci->values[0].value.number; - - return 0; -} /* int ut_config_type_hysteresis */ - static int ut_config_type(const threshold_t *th_orig, oconfig_item_t *ci) { threshold_t th; int status = 0; @@ -223,15 +139,19 @@ static int ut_config_type(const threshold_t *th_orig, oconfig_item_t *ci) { oconfig_item_t *option = ci->children + i; if (strcasecmp("Instance", option->key) == 0) - status = ut_config_type_instance(&th, option); + status = cf_util_get_string_buffer(option, th.type_instance, + sizeof(th.type_instance)); else if (strcasecmp("DataSource", option->key) == 0) - status = ut_config_type_datasource(&th, option); - else if ((strcasecmp("WarningMax", option->key) == 0) || - (strcasecmp("FailureMax", option->key) == 0)) - status = ut_config_type_max(&th, option); - else if ((strcasecmp("WarningMin", option->key) == 0) || - (strcasecmp("FailureMin", option->key) == 0)) - status = ut_config_type_min(&th, option); + status = cf_util_get_string_buffer(option, th.data_source, + sizeof(th.data_source)); + else if (strcasecmp("WarningMax", option->key) == 0) + status = cf_util_get_double(option, &th.warning_max); + else if (strcasecmp("FailureMax", option->key) == 0) + status = cf_util_get_double(option, &th.failure_max); + else if (strcasecmp("WarningMin", option->key) == 0) + status = cf_util_get_double(option, &th.warning_min); + else if (strcasecmp("FailureMin", option->key) == 0) + status = cf_util_get_double(option, &th.failure_min); else if (strcasecmp("Interesting", option->key) == 0) status = cf_util_get_flag(option, &th.flags, UT_FLAG_INTERESTING); else if (strcasecmp("Invert", option->key) == 0) @@ -243,9 +163,9 @@ static int ut_config_type(const threshold_t *th_orig, oconfig_item_t *ci) { else if (strcasecmp("Percentage", option->key) == 0) status = cf_util_get_flag(option, &th.flags, UT_FLAG_PERCENTAGE); else if (strcasecmp("Hits", option->key) == 0) - status = ut_config_type_hits(&th, option); + status = cf_util_get_int(option, &th.hits); else if (strcasecmp("Hysteresis", option->key) == 0) - status = ut_config_type_hysteresis(&th, option); + status = cf_util_get_double(option, &th.hysteresis); else { WARNING("threshold values: Option `%s' not allowed inside a `Type' " "block.", @@ -264,19 +184,6 @@ static int ut_config_type(const threshold_t *th_orig, oconfig_item_t *ci) { return status; } /* int ut_config_type */ -static int ut_config_plugin_instance(threshold_t *th, oconfig_item_t *ci) { - if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) { - WARNING("threshold values: The `Instance' option needs exactly one " - "string argument."); - return -1; - } - - sstrncpy(th->plugin_instance, ci->values[0].value.string, - sizeof(th->plugin_instance)); - - return 0; -} /* int ut_config_plugin_instance */ - static int ut_config_plugin(const threshold_t *th_orig, oconfig_item_t *ci) { threshold_t th; int status = 0; @@ -302,7 +209,8 @@ static int ut_config_plugin(const threshold_t *th_orig, oconfig_item_t *ci) { if (strcasecmp("Type", option->key) == 0) status = ut_config_type(&th, option); else if (strcasecmp("Instance", option->key) == 0) - status = ut_config_plugin_instance(&th, option); + status = cf_util_get_string_buffer(option, th.plugin_instance, + sizeof(th.plugin_instance)); else { WARNING("threshold values: Option `%s' not allowed inside a `Plugin' " "block.", diff --git a/src/utils_db_query.c b/src/utils_db_query.c index 61fe2e41..54ddb046 100644 --- a/src/utils_db_query.c +++ b/src/utils_db_query.c @@ -92,47 +92,23 @@ struct udb_query_preparation_area_s /* {{{ */ /* * Config Private functions */ -static int udb_config_set_string(char **ret_string, /* {{{ */ - oconfig_item_t *ci) { - char *string; - - if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) { - WARNING("db query utils: The `%s' config option " - "needs exactly one string argument.", - ci->key); - return -1; - } - - string = strdup(ci->values[0].value.string); - if (string == NULL) { - ERROR("db query utils: strdup failed."); - return -1; - } - - if (*ret_string != NULL) - free(*ret_string); - *ret_string = string; - - return 0; -} /* }}} int udb_config_set_string */ - static int udb_config_add_string(char ***ret_array, /* {{{ */ size_t *ret_array_len, oconfig_item_t *ci) { char **array; size_t array_len; if (ci->values_num < 1) { - WARNING("db query utils: The `%s' config option " - "needs at least one argument.", - ci->key); + P_WARNING("The `%s' config option " + "needs at least one argument.", + ci->key); return -1; } for (int i = 0; i < ci->values_num; i++) { if (ci->values[i].type != OCONFIG_TYPE_STRING) { - WARNING("db query utils: Argument %i to the `%s' option " - "is not a string.", - i + 1, ci->key); + P_WARNING("Argument %i to the `%s' option " + "is not a string.", + i + 1, ci->key); return -1; } } @@ -140,7 +116,7 @@ static int udb_config_add_string(char ***ret_array, /* {{{ */ array_len = *ret_array_len; array = realloc(*ret_array, sizeof(char *) * (array_len + ci->values_num)); if (array == NULL) { - ERROR("db query utils: realloc failed."); + P_ERROR("udb_config_add_string: realloc failed."); return -1; } *ret_array = array; @@ -148,7 +124,7 @@ static int udb_config_add_string(char ***ret_array, /* {{{ */ for (int i = 0; i < ci->values_num; i++) { array[array_len] = strdup(ci->values[i].value.string); if (array[array_len] == NULL) { - ERROR("db query utils: strdup failed."); + P_ERROR("udb_config_add_string: strdup failed."); *ret_array_len = array_len; return -1; } @@ -161,18 +137,19 @@ static int udb_config_add_string(char ***ret_array, /* {{{ */ static int udb_config_set_uint(unsigned int *ret_value, /* {{{ */ oconfig_item_t *ci) { - double tmp; if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_NUMBER)) { - WARNING("db query utils: The `%s' config option " - "needs exactly one numeric argument.", - ci->key); + P_WARNING("The `%s' config option " + "needs exactly one numeric argument.", + ci->key); return -1; } - tmp = ci->values[0].value.number; - if ((tmp < 0.0) || (tmp > ((double)UINT_MAX))) + double tmp = ci->values[0].value.number; + if ((tmp < 0.0) || (tmp > ((double)UINT_MAX))) { + P_WARNING("The value given for the `%s` option is out of range.", ci->key); return -ERANGE; + } *ret_value = (unsigned int)(tmp + .5); return 0; @@ -194,7 +171,7 @@ static int udb_result_submit(udb_result_t *r, /* {{{ */ vl.values = calloc(r->values_num, sizeof(*vl.values)); if (vl.values == NULL) { - ERROR("db query utils: calloc failed."); + P_ERROR("udb_result_submit: calloc failed."); return -1; } vl.values_len = r_area->ds->ds_num; @@ -203,8 +180,8 @@ static int udb_result_submit(udb_result_t *r, /* {{{ */ char *value_str = r_area->values_buffer[i]; if (0 != parse_value(value_str, &vl.values[i], r_area->ds->ds[i].type)) { - ERROR("db query utils: udb_result_submit: Parsing `%s' as %s failed.", - value_str, DS_TYPE_TO_STRING(r_area->ds->ds[i].type)); + P_ERROR("udb_result_submit: Parsing `%s' as %s failed.", value_str, + DS_TYPE_TO_STRING(r_area->ds->ds[i].type)); errno = EINVAL; free(vl.values); return -1; @@ -238,7 +215,7 @@ static int udb_result_submit(udb_result_t *r, /* {{{ */ int status = strjoin(vl.type_instance, sizeof(vl.type_instance), r_area->instances_buffer, r->instances_num, "-"); if (status < 0) { - ERROR( + P_ERROR( "udb_result_submit: creating type_instance failed with status %d.", status); return status; @@ -249,7 +226,7 @@ static int udb_result_submit(udb_result_t *r, /* {{{ */ int status = strjoin(tmp, sizeof(tmp), r_area->instances_buffer, r->instances_num, "-"); if (status < 0) { - ERROR( + P_ERROR( "udb_result_submit: creating type_instance failed with status %d.", status); return status; @@ -267,7 +244,7 @@ static int udb_result_submit(udb_result_t *r, /* {{{ */ if (r->metadata_num > 0) { vl.meta = meta_data_create(); if (vl.meta == NULL) { - ERROR("db query utils:: meta_data_create failed."); + P_ERROR("udb_result_submit: meta_data_create failed."); free(vl.values); return -ENOMEM; } @@ -276,7 +253,7 @@ static int udb_result_submit(udb_result_t *r, /* {{{ */ int status = meta_data_add_string(vl.meta, r->metadata[i], r_area->metadata_buffer[i]); if (status != 0) { - ERROR("db query utils:: meta_data_add_string failed."); + P_ERROR("udb_result_submit: meta_data_add_string failed."); meta_data_destroy(vl.meta); vl.meta = NULL; free(vl.values); @@ -355,18 +332,18 @@ static int udb_result_prepare_result(udb_result_t const *r, /* {{{ */ /* Read `ds' and check number of values {{{ */ prep_area->ds = plugin_get_ds(r->type); if (prep_area->ds == NULL) { - ERROR("db query utils: udb_result_prepare_result: Type `%s' is not " - "known by the daemon. See types.db(5) for details.", - r->type); + P_ERROR("udb_result_prepare_result: Type `%s' is not " + "known by the daemon. See types.db(5) for details.", + r->type); BAIL_OUT(-1); } if (prep_area->ds->ds_num != r->values_num) { - ERROR("db query utils: udb_result_prepare_result: The type `%s' " - "requires exactly %" PRIsz - " value%s, but the configuration specifies %" PRIsz ".", - r->type, prep_area->ds->ds_num, - (prep_area->ds->ds_num == 1) ? "" : "s", r->values_num); + P_ERROR("udb_result_prepare_result: The type `%s' " + "requires exactly %" PRIsz + " value%s, but the configuration specifies %" PRIsz ".", + r->type, prep_area->ds->ds_num, + (prep_area->ds->ds_num == 1) ? "" : "s", r->values_num); BAIL_OUT(-1); } /* }}} */ @@ -377,39 +354,39 @@ static int udb_result_prepare_result(udb_result_t const *r, /* {{{ */ prep_area->instances_pos = (size_t *)calloc(r->instances_num, sizeof(size_t)); if (prep_area->instances_pos == NULL) { - ERROR("db query utils: udb_result_prepare_result: calloc failed."); + P_ERROR("udb_result_prepare_result: calloc failed."); BAIL_OUT(-ENOMEM); } prep_area->instances_buffer = (char **)calloc(r->instances_num, sizeof(char *)); if (prep_area->instances_buffer == NULL) { - ERROR("db query utils: udb_result_prepare_result: calloc failed."); + P_ERROR("udb_result_prepare_result: calloc failed."); BAIL_OUT(-ENOMEM); } } /* if (r->instances_num > 0) */ prep_area->values_pos = (size_t *)calloc(r->values_num, sizeof(size_t)); if (prep_area->values_pos == NULL) { - ERROR("db query utils: udb_result_prepare_result: calloc failed."); + P_ERROR("udb_result_prepare_result: calloc failed."); BAIL_OUT(-ENOMEM); } prep_area->values_buffer = (char **)calloc(r->values_num, sizeof(char *)); if (prep_area->values_buffer == NULL) { - ERROR("db query utils: udb_result_prepare_result: calloc failed."); + P_ERROR("udb_result_prepare_result: calloc failed."); BAIL_OUT(-ENOMEM); } prep_area->metadata_pos = (size_t *)calloc(r->metadata_num, sizeof(size_t)); if (prep_area->metadata_pos == NULL) { - ERROR("db query utils: udb_result_prepare_result: calloc failed."); + P_ERROR("udb_result_prepare_result: calloc failed."); BAIL_OUT(-ENOMEM); } prep_area->metadata_buffer = (char **)calloc(r->metadata_num, sizeof(char *)); if (prep_area->metadata_buffer == NULL) { - ERROR("db query utils: udb_result_prepare_result: calloc failed."); + P_ERROR("udb_result_prepare_result: calloc failed."); BAIL_OUT(-ENOMEM); } @@ -427,9 +404,9 @@ static int udb_result_prepare_result(udb_result_t const *r, /* {{{ */ } if (j >= column_num) { - ERROR("db query utils: udb_result_prepare_result: " - "Column `%s' could not be found.", - r->instances[i]); + P_ERROR("udb_result_prepare_result: " + "Column `%s' could not be found.", + r->instances[i]); BAIL_OUT(-ENOENT); } } /* }}} for (i = 0; i < r->instances_num; i++) */ @@ -446,9 +423,9 @@ static int udb_result_prepare_result(udb_result_t const *r, /* {{{ */ } if (j >= column_num) { - ERROR("db query utils: udb_result_prepare_result: " - "Column `%s' could not be found.", - r->values[i]); + P_ERROR("udb_result_prepare_result: " + "Column `%s' could not be found.", + r->values[i]); BAIL_OUT(-ENOENT); } } /* }}} for (i = 0; i < r->values_num; i++) */ @@ -465,9 +442,9 @@ static int udb_result_prepare_result(udb_result_t const *r, /* {{{ */ } if (j >= column_num) { - ERROR("db query utils: udb_result_prepare_result: " - "Metadata column `%s' could not be found.", - r->values[i]); + P_ERROR("udb_result_prepare_result: " + "Metadata column `%s' could not be found.", + r->values[i]); BAIL_OUT(-ENOENT); } } /* }}} for (i = 0; i < r->metadata_num; i++) */ @@ -507,14 +484,14 @@ static int udb_result_create(const char *query_name, /* {{{ */ int status; if (ci->values_num != 0) { - WARNING("db query utils: The `Result' block doesn't accept " - "any arguments. Ignoring %i argument%s.", - ci->values_num, (ci->values_num == 1) ? "" : "s"); + P_WARNING("The `Result' block doesn't accept " + "any arguments. Ignoring %i argument%s.", + ci->values_num, (ci->values_num == 1) ? "" : "s"); } r = calloc(1, sizeof(*r)); if (r == NULL) { - ERROR("db query utils: calloc failed."); + P_ERROR("udb_result_create: calloc failed."); return -1; } r->type = NULL; @@ -530,9 +507,9 @@ static int udb_result_create(const char *query_name, /* {{{ */ oconfig_item_t *child = ci->children + i; if (strcasecmp("Type", child->key) == 0) - status = udb_config_set_string(&r->type, child); + status = cf_util_get_string(child, &r->type); else if (strcasecmp("InstancePrefix", child->key) == 0) - status = udb_config_set_string(&r->instance_prefix, child); + status = cf_util_get_string(child, &r->instance_prefix); else if (strcasecmp("InstancesFrom", child->key) == 0) status = udb_config_add_string(&r->instances, &r->instances_num, child); else if (strcasecmp("ValuesFrom", child->key) == 0) @@ -540,8 +517,8 @@ static int udb_result_create(const char *query_name, /* {{{ */ else if (strcasecmp("MetadataFrom", child->key) == 0) status = udb_config_add_string(&r->metadata, &r->metadata_num, child); else { - WARNING("db query utils: Query `%s': Option `%s' not allowed here.", - query_name, child->key); + P_WARNING("Query `%s': Option `%s' not allowed here.", query_name, + child->key); status = -1; } @@ -552,15 +529,15 @@ static int udb_result_create(const char *query_name, /* {{{ */ /* Check that all necessary options have been given. */ while (status == 0) { if (r->type == NULL) { - WARNING("db query utils: `Type' not given for " - "result in query `%s'", - query_name); + P_WARNING("udb_result_create: `Type' not given for " + "result in query `%s'", + query_name); status = -1; } if (r->values == NULL) { - WARNING("db query utils: `ValuesFrom' not given for " - "result in query `%s'", - query_name); + P_WARNING("udb_result_create: `ValuesFrom' not given for " + "result in query `%s'", + query_name); status = -1; } @@ -623,14 +600,14 @@ int udb_query_create(udb_query_t ***ret_query_list, /* {{{ */ query_list_len = *ret_query_list_len; if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) { - WARNING("db query utils: The `Query' block " - "needs exactly one string argument."); + P_WARNING("udb_result_create: The `Query' block " + "needs exactly one string argument."); return -1; } q = calloc(1, sizeof(*q)); if (q == NULL) { - ERROR("db query utils: calloc failed."); + P_ERROR("udb_query_create: calloc failed."); return -1; } q->min_version = 0; @@ -639,7 +616,7 @@ int udb_query_create(udb_query_t ***ret_query_list, /* {{{ */ q->results = NULL; q->plugin_instance_from = NULL; - status = udb_config_set_string(&q->name, ci); + status = cf_util_get_string(ci, &q->name); if (status != 0) { sfree(q); return status; @@ -650,7 +627,7 @@ int udb_query_create(udb_query_t ***ret_query_list, /* {{{ */ oconfig_item_t *child = ci->children + i; if (strcasecmp("Statement", child->key) == 0) - status = udb_config_set_string(&q->statement, child); + status = cf_util_get_string(child, &q->statement); else if (strcasecmp("Result", child->key) == 0) status = udb_result_create(q->name, &q->results, child); else if (strcasecmp("MinVersion", child->key) == 0) @@ -658,19 +635,19 @@ int udb_query_create(udb_query_t ***ret_query_list, /* {{{ */ else if (strcasecmp("MaxVersion", child->key) == 0) status = udb_config_set_uint(&q->max_version, child); else if (strcasecmp("PluginInstanceFrom", child->key) == 0) - status = udb_config_set_string(&q->plugin_instance_from, child); + status = cf_util_get_string(child, &q->plugin_instance_from); /* Call custom callbacks */ else if (cb != NULL) { status = (*cb)(q, child); if (status != 0) { - WARNING("db query utils: The configuration callback failed " - "to handle `%s'.", - child->key); + P_WARNING("The configuration callback failed " + "to handle `%s'.", + child->key); } } else { - WARNING("db query utils: Query `%s': Option `%s' not allowed here.", - q->name, child->key); + P_WARNING("Query `%s': Option `%s' not allowed here.", q->name, + child->key); status = -1; } @@ -681,12 +658,11 @@ int udb_query_create(udb_query_t ***ret_query_list, /* {{{ */ /* Check that all necessary options have been given. */ if (status == 0) { if (q->statement == NULL) { - WARNING("db query utils: Query `%s': No `Statement' given.", q->name); + P_WARNING("Query `%s': No `Statement' given.", q->name); status = -1; } if (q->results == NULL) { - WARNING("db query utils: Query `%s': No (valid) `Result' block given.", - q->name); + P_WARNING("Query `%s': No (valid) `Result' block given.", q->name); status = -1; } } /* if (status == 0) */ @@ -698,7 +674,7 @@ int udb_query_create(udb_query_t ***ret_query_list, /* {{{ */ temp = realloc(query_list, sizeof(*query_list) * (query_list_len + 1)); if (temp == NULL) { - ERROR("db query utils: realloc failed"); + P_ERROR("udb_query_create: realloc failed"); status = -1; } else { query_list = temp; @@ -738,8 +714,8 @@ int udb_query_pick_from_list_by_name(const char *name, /* {{{ */ if ((name == NULL) || (src_list == NULL) || (dst_list == NULL) || (dst_list_len == NULL)) { - ERROR("db query utils: udb_query_pick_from_list_by_name: " - "Invalid argument."); + P_ERROR("udb_query_pick_from_list_by_name: " + "Invalid argument."); return -EINVAL; } @@ -754,7 +730,7 @@ int udb_query_pick_from_list_by_name(const char *name, /* {{{ */ tmp_list_len = *dst_list_len; tmp_list = realloc(*dst_list, (tmp_list_len + 1) * sizeof(udb_query_t *)); if (tmp_list == NULL) { - ERROR("db query utils: realloc failed."); + P_ERROR("udb_query_pick_from_list_by_name: realloc failed."); return -ENOMEM; } @@ -768,12 +744,12 @@ int udb_query_pick_from_list_by_name(const char *name, /* {{{ */ } /* for (i = 0; i < src_list_len; i++) */ if (num_added <= 0) { - ERROR("db query utils: Cannot find query `%s'. Make sure the " - "block is above the database definition!", - name); + P_ERROR("Cannot find query `%s'. Make sure the " + "block is above the database definition!", + name); return -ENOENT; } else { - DEBUG("db query utils: Added %i versions of query `%s'.", num_added, name); + DEBUG("Added %i versions of query `%s'.", num_added, name); } return 0; @@ -786,15 +762,15 @@ int udb_query_pick_from_list(oconfig_item_t *ci, /* {{{ */ if ((ci == NULL) || (src_list == NULL) || (dst_list == NULL) || (dst_list_len == NULL)) { - ERROR("db query utils: udb_query_pick_from_list: " - "Invalid argument."); + P_ERROR("udb_query_pick_from_list: " + "Invalid argument."); return -EINVAL; } if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) { - ERROR("db query utils: The `%s' config option " - "needs exactly one string argument.", - ci->key); + P_ERROR("The `%s' config option " + "needs exactly one string argument.", + ci->key); return -1; } name = ci->values[0].value.string; @@ -883,16 +859,16 @@ int udb_query_handle_result(udb_query_t const *q, /* {{{ */ if ((prep_area->column_num < 1) || (prep_area->host == NULL) || (prep_area->plugin == NULL) || (prep_area->db_name == NULL)) { - ERROR("db query utils: Query `%s': Query is not prepared; " - "can't handle result.", - q->name); + P_ERROR("Query `%s': Query is not prepared; " + "can't handle result.", + q->name); return -EINVAL; } #if defined(COLLECT_DEBUG) && COLLECT_DEBUG /* {{{ */ do { for (size_t i = 0; i < prep_area->column_num; i++) { - DEBUG("db query utils: udb_query_handle_result (%s, %s): " + DEBUG("udb_query_handle_result (%s, %s): " "column[%" PRIsz "] = %s;", prep_area->db_name, q->name, i, column_values[i]); } @@ -908,9 +884,9 @@ int udb_query_handle_result(udb_query_t const *q, /* {{{ */ } if (success == 0) { - ERROR("db query utils: udb_query_handle_result (%s, %s): " - "All results failed.", - prep_area->db_name, q->name); + P_ERROR("udb_query_handle_result (%s, %s): " + "All results failed.", + prep_area->db_name, q->name); return -1; } @@ -946,8 +922,7 @@ int udb_query_prepare_result(udb_query_t const *q, /* {{{ */ if ((prep_area->host == NULL) || (prep_area->plugin == NULL) || (prep_area->db_name == NULL)) { - ERROR("db query utils: Query `%s': Prepare failed: Out of memory.", - q->name); + P_ERROR("Query `%s': Prepare failed: Out of memory.", q->name); udb_query_finish_result(q, prep_area); return -ENOMEM; } @@ -955,7 +930,7 @@ int udb_query_prepare_result(udb_query_t const *q, /* {{{ */ #if defined(COLLECT_DEBUG) && COLLECT_DEBUG do { for (size_t i = 0; i < column_num; i++) { - DEBUG("db query utils: udb_query_prepare_result: " + DEBUG("udb_query_prepare_result: " "query = %s; column[%" PRIsz "] = %s;", q->name, i, column_names[i]); } @@ -974,9 +949,9 @@ int udb_query_prepare_result(udb_query_t const *q, /* {{{ */ } if (i >= column_num) { - ERROR("db query utils: udb_query_prepare_result: " - "Column `%s' from `PluginInstanceFrom' could not be found.", - q->plugin_instance_from); + P_ERROR("udb_query_prepare_result: " + "Column `%s' from `PluginInstanceFrom' could not be found.", + q->plugin_instance_from); udb_query_finish_result(q, prep_area); return -ENOENT; } @@ -986,9 +961,9 @@ int udb_query_prepare_result(udb_query_t const *q, /* {{{ */ for (r = q->results, r_area = prep_area->result_prep_areas; r != NULL; r = r->next, r_area = r_area->next) { if (!r_area) { - ERROR("db query utils: Query `%s': Invalid number of result " - "preparation areas.", - q->name); + P_ERROR("Query `%s': Invalid number of result " + "preparation areas.", + q->name); udb_query_finish_result(q, prep_area); return -EINVAL; } diff --git a/src/utils_format_graphite.c b/src/utils_format_graphite.c index 0bc802b6..44700b54 100644 --- a/src/utils_format_graphite.c +++ b/src/utils_format_graphite.c @@ -66,8 +66,8 @@ static int gr_format_values(char *ret, size_t ret_len, int ds_num, else if (ds->ds[ds_num].type == DS_TYPE_ABSOLUTE) BUFFER_ADD("%" PRIu64, vl->values[ds_num].absolute); else { - ERROR("gr_format_values plugin: Unknown data source type: %i", - ds->ds[ds_num].type); + P_ERROR("gr_format_values: Unknown data source type: %i", + ds->ds[ds_num].type); return -1; } @@ -183,7 +183,7 @@ int format_graphite(char *buffer, size_t buffer_size, data_set_t const *ds, if (flags & GRAPHITE_STORE_RATES) { rates = uc_get_rate(ds, vl); if (rates == NULL) { - ERROR("format_graphite: error with uc_get_rate"); + P_ERROR("format_graphite: error with uc_get_rate"); return -1; } } @@ -202,7 +202,7 @@ int format_graphite(char *buffer, size_t buffer_size, data_set_t const *ds, status = gr_format_name(key, sizeof(key), vl, ds_name, prefix, postfix, escape_char, flags); if (status != 0) { - ERROR("format_graphite: error with gr_format_name"); + P_ERROR("format_graphite: error with gr_format_name"); sfree(rates); return status; } @@ -212,7 +212,7 @@ int format_graphite(char *buffer, size_t buffer_size, data_set_t const *ds, * `values'. */ status = gr_format_values(values, sizeof(values), i, ds, vl, rates); if (status != 0) { - ERROR("format_graphite: error with gr_format_values"); + P_ERROR("format_graphite: error with gr_format_values"); sfree(rates); return status; } @@ -222,16 +222,16 @@ int format_graphite(char *buffer, size_t buffer_size, data_set_t const *ds, (size_t)snprintf(message, sizeof(message), "%s %s %u\r\n", key, values, (unsigned int)CDTIME_T_TO_TIME_T(vl->time)); if (message_len >= sizeof(message)) { - ERROR("format_graphite: message buffer too small: " - "Need %" PRIsz " bytes.", - message_len + 1); + P_ERROR("format_graphite: message buffer too small: " + "Need %" PRIsz " bytes.", + message_len + 1); sfree(rates); return -ENOMEM; } /* Append it in case we got multiple data set */ if ((buffer_pos + message_len) >= buffer_size) { - ERROR("format_graphite: target buffer too small"); + P_ERROR("format_graphite: target buffer too small"); sfree(rates); return -ENOMEM; } diff --git a/src/utils_rrdcreate.c b/src/utils_rrdcreate.c index ce7838d5..8f92cfd3 100644 --- a/src/utils_rrdcreate.c +++ b/src/utils_rrdcreate.c @@ -96,7 +96,7 @@ static srrd_create_args_t *srrd_create_args_create(const char *filename, args = calloc(1, sizeof(*args)); if (args == NULL) { - ERROR("srrd_create_args_create: calloc failed."); + P_ERROR("srrd_create_args_create: calloc failed."); return NULL; } args->filename = NULL; @@ -106,14 +106,14 @@ static srrd_create_args_t *srrd_create_args_create(const char *filename, args->filename = strdup(filename); if (args->filename == NULL) { - ERROR("srrd_create_args_create: strdup failed."); + P_ERROR("srrd_create_args_create: strdup failed."); srrd_create_args_destroy(args); return NULL; } args->argv = calloc((size_t)(argc + 1), sizeof(*args->argv)); if (args->argv == NULL) { - ERROR("srrd_create_args_create: calloc failed."); + P_ERROR("srrd_create_args_create: calloc failed."); srrd_create_args_destroy(args); return NULL; } @@ -121,7 +121,7 @@ static srrd_create_args_t *srrd_create_args_create(const char *filename, for (args->argc = 0; args->argc < argc; args->argc++) { args->argv[args->argc] = strdup(argv[args->argc]); if (args->argv[args->argc] == NULL) { - ERROR("srrd_create_args_create: strdup failed."); + P_ERROR("srrd_create_args_create: strdup failed."); srrd_create_args_destroy(args); return NULL; } @@ -212,7 +212,7 @@ static int rra_get(char ***ret, const value_list_t *vl, /* {{{ */ rra_types[j], cfg->xff, cdp_len, cdp_num); if ((status < 0) || ((size_t)status >= sizeof(buffer))) { - ERROR("rra_get: Buffer would have been truncated."); + P_ERROR("rra_get: Buffer would have been truncated."); continue; } @@ -251,7 +251,7 @@ static int ds_get(char ***ret, /* {{{ */ ds_def = calloc(ds->ds_num, sizeof(*ds_def)); if (ds_def == NULL) { - ERROR("rrdtool plugin: calloc failed: %s", STRERRNO); + P_ERROR("ds_get: calloc failed: %s", STRERRNO); return -1; } @@ -271,7 +271,7 @@ static int ds_get(char ***ret, /* {{{ */ else if (d->type == DS_TYPE_ABSOLUTE) type = "ABSOLUTE"; else { - ERROR("rrdtool plugin: Unknown DS type: %i", d->type); + P_ERROR("ds_get: Unknown DS type: %i", d->type); break; } @@ -335,8 +335,8 @@ static int srrd_create(const char *filename, /* {{{ */ status = rrd_create_r(filename_copy, pdp_step, last_up, argc, (void *)argv); if (status != 0) { - WARNING("rrdtool plugin: rrd_create_r (%s) failed: %s", filename, - rrd_get_error()); + P_WARNING("srrd_create: rrd_create_r (%s) failed: %s", filename, + rrd_get_error()); } sfree(filename_copy); @@ -360,7 +360,7 @@ static int srrd_create(const char *filename, /* {{{ */ new_argc = 6 + argc; new_argv = malloc((new_argc + 1) * sizeof(*new_argv)); if (new_argv == NULL) { - ERROR("rrdtool plugin: malloc failed."); + P_ERROR("srrd_create: malloc failed."); return -1; } @@ -388,8 +388,8 @@ static int srrd_create(const char *filename, /* {{{ */ pthread_mutex_unlock(&librrd_lock); if (status != 0) { - WARNING("rrdtool plugin: rrd_create (%s) failed: %s", filename, - rrd_get_error()); + P_WARNING("srrd_create: rrd_create (%s) failed: %s", filename, + rrd_get_error()); } sfree(new_argv); @@ -487,10 +487,11 @@ static void *srrd_create_thread(void *targs) /* {{{ */ status = lock_file(args->filename); if (status != 0) { if (status == EEXIST) - NOTICE("srrd_create_thread: File \"%s\" is already being created.", - args->filename); + P_NOTICE("srrd_create_thread: File \"%s\" is already being created.", + args->filename); else - ERROR("srrd_create_thread: Unable to lock file \"%s\".", args->filename); + P_ERROR("srrd_create_thread: Unable to lock file \"%s\".", + args->filename); srrd_create_args_destroy(args); return 0; } @@ -500,8 +501,8 @@ static void *srrd_create_thread(void *targs) /* {{{ */ status = srrd_create(tmpfile, args->pdp_step, args->last_up, args->argc, (void *)args->argv); if (status != 0) { - WARNING("srrd_create_thread: srrd_create (%s) returned status %i.", - args->filename, status); + P_WARNING("srrd_create_thread: srrd_create (%s) returned status %i.", + args->filename, status); unlink(tmpfile); unlock_file(args->filename); srrd_create_args_destroy(args); @@ -510,8 +511,8 @@ static void *srrd_create_thread(void *targs) /* {{{ */ status = rename(tmpfile, args->filename); if (status != 0) { - ERROR("srrd_create_thread: rename (\"%s\", \"%s\") failed: %s", tmpfile, - args->filename, STRERRNO); + P_ERROR("srrd_create_thread: rename (\"%s\", \"%s\") failed: %s", tmpfile, + args->filename, STRERRNO); unlink(tmpfile); unlock_file(args->filename); srrd_create_args_destroy(args); @@ -556,7 +557,7 @@ static int srrd_create_async(const char *filename, /* {{{ */ status = pthread_create(&thread, &attr, srrd_create_thread, args); if (status != 0) { - ERROR("srrd_create_async: pthread_create failed: %s", STRERROR(status)); + P_ERROR("srrd_create_async: pthread_create failed: %s", STRERROR(status)); pthread_attr_destroy(&attr); srrd_create_args_destroy(args); return status; @@ -587,12 +588,12 @@ int cu_rrd_create_file(const char *filename, /* {{{ */ return -1; if ((rra_num = rra_get(&rra_def, vl, cfg)) < 1) { - ERROR("cu_rrd_create_file failed: Could not calculate RRAs"); + P_ERROR("cu_rrd_create_file failed: Could not calculate RRAs"); return -1; } if ((ds_num = ds_get(&ds_def, ds, vl, cfg)) < 1) { - ERROR("cu_rrd_create_file failed: Could not calculate DSes"); + P_ERROR("cu_rrd_create_file failed: Could not calculate DSes"); rra_free(rra_num, rra_def); return -1; } @@ -600,7 +601,7 @@ int cu_rrd_create_file(const char *filename, /* {{{ */ argc = ds_num + rra_num; if ((argv = malloc(sizeof(*argv) * (argc + 1))) == NULL) { - ERROR("cu_rrd_create_file failed: %s", STRERRNO); + P_ERROR("cu_rrd_create_file failed: %s", STRERRNO); rra_free(rra_num, rra_def); ds_free(ds_num, ds_def); return -1; @@ -624,25 +625,25 @@ int cu_rrd_create_file(const char *filename, /* {{{ */ status = srrd_create_async(filename, stepsize, last_up, argc, (const char **)argv); if (status != 0) - WARNING("cu_rrd_create_file: srrd_create_async (%s) " - "returned status %i.", - filename, status); + P_WARNING("cu_rrd_create_file: srrd_create_async (%s) " + "returned status %i.", + filename, status); } else /* synchronous */ { status = lock_file(filename); if (status != 0) { if (status == EEXIST) - NOTICE("cu_rrd_create_file: File \"%s\" is already being created.", - filename); + P_NOTICE("cu_rrd_create_file: File \"%s\" is already being created.", + filename); else - ERROR("cu_rrd_create_file: Unable to lock file \"%s\".", filename); + P_ERROR("cu_rrd_create_file: Unable to lock file \"%s\".", filename); } else { status = srrd_create(filename, stepsize, last_up, argc, (const char **)argv); if (status != 0) { - WARNING("cu_rrd_create_file: srrd_create (%s) returned status %i.", - filename, status); + P_WARNING("cu_rrd_create_file: srrd_create (%s) returned status %i.", + filename, status); } else { DEBUG("cu_rrd_create_file: Successfully created RRD file \"%s\".", filename); diff --git a/src/write_riemann.c b/src/write_riemann.c index 1578e1c1..b35d10ee 100644 --- a/src/write_riemann.c +++ b/src/write_riemann.c @@ -699,21 +699,13 @@ static int wrr_config_node(oconfig_item_t *ci) /* {{{ */ } else if (strcasecmp("Port", child->key) == 0) { host->port = cf_util_get_port_number(child); if (host->port == -1) { - ERROR("write_riemann plugin: Invalid argument " - "configured for the \"Port\" " - "option."); break; } } else if (strcasecmp("Protocol", child->key) == 0) { char tmp[16]; status = cf_util_get_string_buffer(child, tmp, sizeof(tmp)); - if (status != 0) { - ERROR("write_riemann plugin: cf_util_get_" - "string_buffer failed with " - "status %i.", - status); + if (status != 0) break; - } if (strcasecmp("UDP", tmp) == 0) host->client_type = RIEMANN_CLIENT_UDP; @@ -729,31 +721,16 @@ static int wrr_config_node(oconfig_item_t *ci) /* {{{ */ tmp); } else if (strcasecmp("TLSCAFile", child->key) == 0) { status = cf_util_get_string(child, &host->tls_ca_file); - if (status != 0) { - ERROR("write_riemann plugin: cf_util_get_" - "string_buffer failed with " - "status %i.", - status); + if (status != 0) break; - } } else if (strcasecmp("TLSCertFile", child->key) == 0) { status = cf_util_get_string(child, &host->tls_cert_file); - if (status != 0) { - ERROR("write_riemann plugin: cf_util_get_" - "string_buffer failed with " - "status %i.", - status); + if (status != 0) break; - } } else if (strcasecmp("TLSKeyFile", child->key) == 0) { status = cf_util_get_string(child, &host->tls_key_file); - if (status != 0) { - ERROR("write_riemann plugin: cf_util_get_" - "string_buffer failed with " - "status %i.", - status); + if (status != 0) break; - } } else if (strcasecmp("StoreRates", child->key) == 0) { status = cf_util_get_boolean(child, &host->store_rates); if (status != 0) diff --git a/src/write_sensu.c b/src/write_sensu.c index 4c9f42ba..6ea8106c 100644 --- a/src/write_sensu.c +++ b/src/write_sensu.c @@ -1084,12 +1084,8 @@ static int sensu_config_node(oconfig_item_t *ci) /* {{{ */ break; } else if (strcasecmp("Port", child->key) == 0) { status = cf_util_get_service(child, &host->service); - if (status != 0) { - ERROR("write_sensu plugin: Invalid argument " - "configured for the \"Port\" " - "option."); + if (status != 0) break; - } } else if (strcasecmp("StoreRates", child->key) == 0) { status = cf_util_get_boolean(child, &host->store_rates); if (status != 0)