diff options
Diffstat (limited to '')
-rw-r--r-- | database/rrdvar.c (renamed from src/rrdvar.c) | 28 |
1 files changed, 19 insertions, 9 deletions
diff --git a/src/rrdvar.c b/database/rrdvar.c index 6936c36f1..951a38cac 100644 --- a/src/rrdvar.c +++ b/database/rrdvar.c @@ -1,5 +1,7 @@ +// SPDX-License-Identifier: GPL-3.0-or-later + #define NETDATA_HEALTH_INTERNALS -#include "common.h" +#include "rrd.h" // ---------------------------------------------------------------------------- // RRDVAR management @@ -59,14 +61,14 @@ inline void rrdvar_free(RRDHOST *host, avl_tree_lock *tree, RRDVAR *rv) { error("RRDVAR: Attempted to delete variable '%s' from host '%s', but it is not found.", rv->name, host->hostname); } - if(rv->type == RRDVAR_TYPE_CALCULATED_ALLOCATED) + if(rv->options & RRDVAR_OPTION_ALLOCATED) freez(rv->value); freez(rv->name); freez(rv); } -inline RRDVAR *rrdvar_create_and_index(const char *scope, avl_tree_lock *tree, const char *name, RRDVAR_TYPE type, void *value) { +inline RRDVAR *rrdvar_create_and_index(const char *scope, 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); @@ -79,7 +81,9 @@ inline RRDVAR *rrdvar_create_and_index(const char *scope, avl_tree_lock *tree, c 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)) { @@ -106,7 +110,7 @@ inline RRDVAR *rrdvar_create_and_index(const char *scope, avl_tree_lock *tree, c } void rrdvar_free_remaining_variables(RRDHOST *host, avl_tree_lock *tree_lock) { - // FIXME: this is not bullet proof - avl should support some means to destroy it + // 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; @@ -123,7 +127,7 @@ void rrdvar_free_remaining_variables(RRDHOST *host, avl_tree_lock *tree_lock) { // ---------------------------------------------------------------------------- // CUSTOM HOST VARIABLES -inline int rrdvar_callback_for_all_host_variables(RRDHOST *host, int (*callback)(void *rrdvar, void *data), void *data) { +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); } @@ -131,7 +135,7 @@ static RRDVAR *rrdvar_custom_variable_create(const char *scope, avl_tree_lock *t calculated_number *v = callocz(1, sizeof(calculated_number)); *v = NAN; - RRDVAR *rv = rrdvar_create_and_index(scope, tree_lock, name, RRDVAR_TYPE_CALCULATED_ALLOCATED, v); + 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)) { free(v); debug(D_VARIABLES, "Requested variable '%s' already exists - possibly 2 plugins are updating it at the same time.", name); @@ -140,6 +144,7 @@ static RRDVAR *rrdvar_custom_variable_create(const char *scope, avl_tree_lock *t 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); @@ -153,25 +158,30 @@ RRDVAR *rrdvar_custom_host_variable_create(RRDHOST *host, const char *name) { } void rrdvar_custom_host_variable_set(RRDHOST *host, RRDVAR *rv, calculated_number value) { - if(rv->type != RRDVAR_TYPE_CALCULATED_ALLOCATED) + 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 -static calculated_number rrdvar2number(RRDVAR *rv) { +calculated_number rrdvar2number(RRDVAR *rv) { switch(rv->type) { - case RRDVAR_TYPE_CALCULATED_ALLOCATED: case RRDVAR_TYPE_CALCULATED: { calculated_number *n = (calculated_number *)rv->value; return *n; |