mirror of
https://github.com/netdata/netdata.git
synced 2025-04-24 13:14:11 +00:00

* configure.ac: Add support for GCC's __attribute__((unused)) When compiling under GCC with -Wextra (along with -Wall) there are a lot of compiler warnings such as collectors/cgroups.plugin/cgroup-network.c:89:45: warning: unused parameter ‘scope’ [-Wunused-parameter] 89 | struct iface *read_proc_net_dev(const char *scope, const char *prefix) { | ~~~~~~~~~~~~^~~~~ Some arguments may be able to be got rid off, others won't. GCC (and at least clang[0]) provide an __attribute__((unused)) annotation that can be used on function parameters (also on variables, functions, labels, enums, structs etc) to inform the compiler of such and will squash warnings of the above nature. A check is added to configure.ac for the use of GCC (I believe $GCC will be set to 'yes' for clang also) and if found it creates __always_unused & __maybe_unused #define's set to __attribute__((unused)) otherwise it just sets them empty. If other compilers have a similar feature this check can be adjusted to accommodate them. The reason for the two defines is that some variables may always be unused in a function, others may or may not depending on #ifdef's for example. So we are able to document both cases. Subsequent commits will start making use of these to squash such compiler warnings. [0]: https://clang.llvm.org/docs/AttributeReference.html#maybe-unused-unused Signed-off-by: Andrew Clayton <andrew@zeta.digital-domain.net> * collectors/statsd.plugin: Mark a function argument as __maybe_unused In collectors/statsd.plugin/statsd.c the app function argument to STATSD_APP_CHART_DIM() might be unused if NETDATA_INTERNAL_CHECKS is not defined, then the debug() macro that it's used in from libnetdata/log/log.h will be defined to a dummy function where none of the arguments are used. This fixes a compiler warning [-Wunused-parameter] when compiling with -Wextra *and* -Wall. Signed-off-by: Andrew Clayton <andrew@zeta.digital-domain.net> * collectors/apps.plugin: Mark a function argument as __maybe_unused In collectors/apps.plugin/apps_plugin.c the function debug_print_process_tree() takes an argument 'msg' that might be unused if NETDATA_INTERNAL_CHECKS is not defined, then debug_log() will be set to a dummy function that takes no arguments. This fixes a compiler warning [-Wunused-parameter] when compiling with -Wextra *and* -Wall. Signed-off-by: Andrew Clayton <andrew@zeta.digital-domain.net> * libnetdata/locks/locks: Mark function arguments as __maybe_unused In libnetdata/locks/locks.c there a number of functions that take arguments 'file', 'function' & 'line' that might be unused if NETDATA_INTERNAL_CHECKS is not defined, then the debug() macro that it's used in from libnetdata/log/log.h will be defined to a dummy function where none of the arguments are used. This fixes compiler warnings [-Wunused-parameter] when compiling with -Wextra *and* -Wall. Signed-off-by: Andrew Clayton <andrew@zeta.digital-domain.net> * libnetdata/socket/security: Mark a function argument as __maybe_unused In libnetdata/socket/security.c the function security_info_callback() takes an argument 'ret' that might be unused if NETDATA_INTERNAL_CHECKS is not defined, then the debug() macro that it's used in from libnetdata/log/log.h will be defined to a dummy function where none of the arguments are used. This fixes a compiler warning [-Wunused-parameter] when compiling with -Wextra *and* -Wall. Signed-off-by: Andrew Clayton <andrew@zeta.digital-domain.net> * collectors/cgroups.plugin: Mark a function argument as __maybe_unused In collectors/cgroups.plugin/cgroup-network.c the function read_proc_net_dev() takes an argument 'scope' that might be unused if the NETDATA_INTERNAL_CHECKS is not defined. This fixes a compiler warning [-Wunused-parameter] when compiling with -Wextra *and* -Wall". Signed-off-by: Andrew Clayton <andrew@zeta.digital-domain.net> * database/rrddim: Mark function arguments as __maybe_unused In database/rrddim.c there a couple of functions that take a 'st' argument that might be unused if NETDATA_INTERNAL_CHECKS is not defined, then the debug() macro that it's used in from libnetdata/log/log.h will be defined to a dummy function where none of the arguments are used. This fixes compiler warnings [-Wunused-parameter] when compiling with -Wextra *and* -Wall. Signed-off-by: Andrew Clayton <andrew@zeta.digital-domain.net> * database/rrdvar: Mark a function argument as __maybe_unused In database/rrdvar.c the function rrdvar_create_and_index() take an argument 'scope' that might be unused if NETDATA_INTERNAL_CHECKS is not defined, then the debug() macro that it's used in from libnetdata/log/log.h will be defined to a dummy function where none of the arguments are used. This fixes a compiler warning [-Wunused-parameter] when compiling with -Wextra *and* -Wall. Signed-off-by: Andrew Clayton <andrew@zeta.digital-domain.net>
286 lines
9.1 KiB
C
286 lines
9.1 KiB
C
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
#define NETDATA_HEALTH_INTERNALS
|
|
#include "rrd.h"
|
|
|
|
// ----------------------------------------------------------------------------
|
|
// RRDVAR management
|
|
|
|
inline int rrdvar_fix_name(char *variable) {
|
|
int fixed = 0;
|
|
while(*variable) {
|
|
if (!isalnum(*variable) && *variable != '.' && *variable != '_') {
|
|
*variable++ = '_';
|
|
fixed++;
|
|
}
|
|
else
|
|
variable++;
|
|
}
|
|
|
|
return fixed;
|
|
}
|
|
|
|
int rrdvar_compare(void* a, void* b) {
|
|
if(((RRDVAR *)a)->hash < ((RRDVAR *)b)->hash) return -1;
|
|
else if(((RRDVAR *)a)->hash > ((RRDVAR *)b)->hash) return 1;
|
|
else return strcmp(((RRDVAR *)a)->name, ((RRDVAR *)b)->name);
|
|
}
|
|
|
|
static inline RRDVAR *rrdvar_index_add(avl_tree_lock *tree, RRDVAR *rv) {
|
|
RRDVAR *ret = (RRDVAR *)avl_insert_lock(tree, (avl *)(rv));
|
|
if(ret != rv)
|
|
debug(D_VARIABLES, "Request to insert RRDVAR '%s' into index failed. Already exists.", rv->name);
|
|
|
|
return ret;
|
|
}
|
|
|
|
static inline RRDVAR *rrdvar_index_del(avl_tree_lock *tree, RRDVAR *rv) {
|
|
RRDVAR *ret = (RRDVAR *)avl_remove_lock(tree, (avl *)(rv));
|
|
if(!ret)
|
|
error("Request to remove RRDVAR '%s' from index failed. Not Found.", rv->name);
|
|
|
|
return ret;
|
|
}
|
|
|
|
static inline RRDVAR *rrdvar_index_find(avl_tree_lock *tree, const char *name, uint32_t hash) {
|
|
RRDVAR tmp;
|
|
tmp.name = (char *)name;
|
|
tmp.hash = (hash)?hash:simple_hash(tmp.name);
|
|
|
|
return (RRDVAR *)avl_search_lock(tree, (avl *)&tmp);
|
|
}
|
|
|
|
inline void rrdvar_free(RRDHOST *host, avl_tree_lock *tree, RRDVAR *rv) {
|
|
(void)host;
|
|
|
|
if(!rv) return;
|
|
|
|
if(tree) {
|
|
debug(D_VARIABLES, "Deleting variable '%s'", rv->name);
|
|
if(unlikely(!rrdvar_index_del(tree, rv)))
|
|
error("RRDVAR: Attempted to delete variable '%s' from host '%s', but it is not found.", rv->name, host->hostname);
|
|
}
|
|
|
|
if(rv->options & RRDVAR_OPTION_ALLOCATED)
|
|
freez(rv->value);
|
|
|
|
freez(rv->name);
|
|
freez(rv);
|
|
}
|
|
|
|
inline RRDVAR *rrdvar_create_and_index(const char *scope __maybe_unused, avl_tree_lock *tree, const char *name,
|
|
RRDVAR_TYPE type, RRDVAR_OPTIONS options, void *value) {
|
|
char *variable = strdupz(name);
|
|
rrdvar_fix_name(variable);
|
|
uint32_t hash = simple_hash(variable);
|
|
|
|
RRDVAR *rv = rrdvar_index_find(tree, variable, hash);
|
|
if(unlikely(!rv)) {
|
|
debug(D_VARIABLES, "Variable '%s' not found in scope '%s'. Creating a new one.", variable, scope);
|
|
|
|
rv = callocz(1, sizeof(RRDVAR));
|
|
rv->name = variable;
|
|
rv->hash = hash;
|
|
rv->type = type;
|
|
rv->options = options;
|
|
rv->value = value;
|
|
rv->last_updated = now_realtime_sec();
|
|
|
|
RRDVAR *ret = rrdvar_index_add(tree, rv);
|
|
if(unlikely(ret != rv)) {
|
|
debug(D_VARIABLES, "Variable '%s' in scope '%s' already exists", variable, scope);
|
|
freez(rv);
|
|
freez(variable);
|
|
rv = NULL;
|
|
}
|
|
else
|
|
debug(D_VARIABLES, "Variable '%s' created in scope '%s'", variable, scope);
|
|
}
|
|
else {
|
|
debug(D_VARIABLES, "Variable '%s' is already found in scope '%s'.", variable, scope);
|
|
|
|
// already exists
|
|
freez(variable);
|
|
|
|
// this is important
|
|
// it must return NULL - not the existing variable - or double-free will happen
|
|
rv = NULL;
|
|
}
|
|
|
|
return rv;
|
|
}
|
|
|
|
void rrdvar_free_remaining_variables(RRDHOST *host, avl_tree_lock *tree_lock) {
|
|
// This is not bullet proof - avl should support some means to destroy it
|
|
// with a callback for each item already in the index
|
|
|
|
RRDVAR *rv, *last = NULL;
|
|
while((rv = (RRDVAR *)tree_lock->avl_tree.root)) {
|
|
if(unlikely(rv == last)) {
|
|
error("RRDVAR: INTERNAL ERROR: Cannot cleanup tree of RRDVARs");
|
|
break;
|
|
}
|
|
last = rv;
|
|
rrdvar_free(host, tree_lock, rv);
|
|
}
|
|
}
|
|
|
|
// ----------------------------------------------------------------------------
|
|
// CUSTOM HOST VARIABLES
|
|
|
|
inline int rrdvar_callback_for_all_host_variables(RRDHOST *host, int (*callback)(void * /*rrdvar*/, void * /*data*/), void *data) {
|
|
return avl_traverse_lock(&host->rrdvar_root_index, callback, data);
|
|
}
|
|
|
|
static RRDVAR *rrdvar_custom_variable_create(const char *scope, avl_tree_lock *tree_lock, const char *name) {
|
|
calculated_number *v = callocz(1, sizeof(calculated_number));
|
|
*v = NAN;
|
|
|
|
RRDVAR *rv = rrdvar_create_and_index(scope, tree_lock, name, RRDVAR_TYPE_CALCULATED, RRDVAR_OPTION_CUSTOM_HOST_VAR|RRDVAR_OPTION_ALLOCATED, v);
|
|
if(unlikely(!rv)) {
|
|
freez(v);
|
|
debug(D_VARIABLES, "Requested variable '%s' already exists - possibly 2 plugins are updating it at the same time.", name);
|
|
|
|
char *variable = strdupz(name);
|
|
rrdvar_fix_name(variable);
|
|
uint32_t hash = simple_hash(variable);
|
|
|
|
// find the existing one to return it
|
|
rv = rrdvar_index_find(tree_lock, variable, hash);
|
|
|
|
freez(variable);
|
|
}
|
|
|
|
return rv;
|
|
}
|
|
|
|
RRDVAR *rrdvar_custom_host_variable_create(RRDHOST *host, const char *name) {
|
|
return rrdvar_custom_variable_create("host", &host->rrdvar_root_index, name);
|
|
}
|
|
|
|
void rrdvar_custom_host_variable_set(RRDHOST *host, RRDVAR *rv, calculated_number value) {
|
|
if(rv->type != RRDVAR_TYPE_CALCULATED || !(rv->options & RRDVAR_OPTION_CUSTOM_HOST_VAR) || !(rv->options & RRDVAR_OPTION_ALLOCATED))
|
|
error("requested to set variable '%s' to value " CALCULATED_NUMBER_FORMAT " but the variable is not a custom one.", rv->name, value);
|
|
else {
|
|
calculated_number *v = rv->value;
|
|
if(*v != value) {
|
|
*v = value;
|
|
|
|
rv->last_updated = now_realtime_sec();
|
|
|
|
// if the host is streaming, send this variable upstream immediately
|
|
rrdpush_sender_send_this_host_variable_now(host, rv);
|
|
}
|
|
}
|
|
}
|
|
|
|
int foreach_host_variable_callback(RRDHOST *host, int (*callback)(RRDVAR * /*rv*/, void * /*data*/), void *data) {
|
|
return avl_traverse_lock(&host->rrdvar_root_index, (int (*)(void *, void *))callback, data);
|
|
}
|
|
|
|
// ----------------------------------------------------------------------------
|
|
// RRDVAR lookup
|
|
|
|
calculated_number rrdvar2number(RRDVAR *rv) {
|
|
switch(rv->type) {
|
|
case RRDVAR_TYPE_CALCULATED: {
|
|
calculated_number *n = (calculated_number *)rv->value;
|
|
return *n;
|
|
}
|
|
|
|
case RRDVAR_TYPE_TIME_T: {
|
|
time_t *n = (time_t *)rv->value;
|
|
return *n;
|
|
}
|
|
|
|
case RRDVAR_TYPE_COLLECTED: {
|
|
collected_number *n = (collected_number *)rv->value;
|
|
return *n;
|
|
}
|
|
|
|
case RRDVAR_TYPE_TOTAL: {
|
|
total_number *n = (total_number *)rv->value;
|
|
return *n;
|
|
}
|
|
|
|
case RRDVAR_TYPE_INT: {
|
|
int *n = (int *)rv->value;
|
|
return *n;
|
|
}
|
|
|
|
default:
|
|
error("I don't know how to convert RRDVAR type %u to calculated_number", rv->type);
|
|
return NAN;
|
|
}
|
|
}
|
|
|
|
int health_variable_lookup(const char *variable, uint32_t hash, RRDCALC *rc, calculated_number *result) {
|
|
RRDSET *st = rc->rrdset;
|
|
if(!st) return 0;
|
|
|
|
RRDHOST *host = st->rrdhost;
|
|
RRDVAR *rv;
|
|
|
|
rv = rrdvar_index_find(&st->rrdvar_root_index, variable, hash);
|
|
if(rv) {
|
|
*result = rrdvar2number(rv);
|
|
return 1;
|
|
}
|
|
|
|
rv = rrdvar_index_find(&st->rrdfamily->rrdvar_root_index, variable, hash);
|
|
if(rv) {
|
|
*result = rrdvar2number(rv);
|
|
return 1;
|
|
}
|
|
|
|
rv = rrdvar_index_find(&host->rrdvar_root_index, variable, hash);
|
|
if(rv) {
|
|
*result = rrdvar2number(rv);
|
|
return 1;
|
|
}
|
|
|
|
return 0;
|
|
}
|
|
|
|
// ----------------------------------------------------------------------------
|
|
// RRDVAR to JSON
|
|
|
|
struct variable2json_helper {
|
|
BUFFER *buf;
|
|
size_t counter;
|
|
};
|
|
|
|
static int single_variable2json(void *entry, void *data) {
|
|
struct variable2json_helper *helper = (struct variable2json_helper *)data;
|
|
RRDVAR *rv = (RRDVAR *)entry;
|
|
calculated_number value = rrdvar2number(rv);
|
|
|
|
if(unlikely(isnan(value) || isinf(value)))
|
|
buffer_sprintf(helper->buf, "%s\n\t\t\"%s\": null", helper->counter?",":"", rv->name);
|
|
else
|
|
buffer_sprintf(helper->buf, "%s\n\t\t\"%s\": %0.5" LONG_DOUBLE_MODIFIER, helper->counter?",":"", rv->name, (LONG_DOUBLE)value);
|
|
|
|
helper->counter++;
|
|
|
|
return 0;
|
|
}
|
|
|
|
void health_api_v1_chart_variables2json(RRDSET *st, BUFFER *buf) {
|
|
RRDHOST *host = st->rrdhost;
|
|
|
|
struct variable2json_helper helper = {
|
|
.buf = buf,
|
|
.counter = 0
|
|
};
|
|
|
|
buffer_sprintf(buf, "{\n\t\"chart\": \"%s\",\n\t\"chart_name\": \"%s\",\n\t\"chart_context\": \"%s\",\n\t\"chart_variables\": {", st->id, st->name, st->context);
|
|
avl_traverse_lock(&st->rrdvar_root_index, single_variable2json, (void *)&helper);
|
|
buffer_sprintf(buf, "\n\t},\n\t\"family\": \"%s\",\n\t\"family_variables\": {", st->family);
|
|
helper.counter = 0;
|
|
avl_traverse_lock(&st->rrdfamily->rrdvar_root_index, single_variable2json, (void *)&helper);
|
|
buffer_sprintf(buf, "\n\t},\n\t\"host\": \"%s\",\n\t\"host_variables\": {", host->hostname);
|
|
helper.counter = 0;
|
|
avl_traverse_lock(&host->rrdvar_root_index, single_variable2json, (void *)&helper);
|
|
buffer_strcat(buf, "\n\t}\n}\n");
|
|
}
|
|
|