1 #define NETDATA_HEALTH_INTERNALS
4 // ----------------------------------------------------------------------------
7 inline int rrdvar_fix_name(char *variable) {
10 if (!isalnum(*variable) && *variable != '.' && *variable != '_') {
21 int rrdvar_compare(void* a, void* b) {
22 if(((RRDVAR *)a)->hash < ((RRDVAR *)b)->hash) return -1;
23 else if(((RRDVAR *)a)->hash > ((RRDVAR *)b)->hash) return 1;
24 else return strcmp(((RRDVAR *)a)->name, ((RRDVAR *)b)->name);
27 static inline RRDVAR *rrdvar_index_add(avl_tree_lock *tree, RRDVAR *rv) {
28 RRDVAR *ret = (RRDVAR *)avl_insert_lock(tree, (avl *)(rv));
30 debug(D_VARIABLES, "Request to insert RRDVAR '%s' into index failed. Already exists.", rv->name);
35 static inline RRDVAR *rrdvar_index_del(avl_tree_lock *tree, RRDVAR *rv) {
36 RRDVAR *ret = (RRDVAR *)avl_remove_lock(tree, (avl *)(rv));
38 error("Request to remove RRDVAR '%s' from index failed. Not Found.", rv->name);
43 static inline RRDVAR *rrdvar_index_find(avl_tree_lock *tree, const char *name, uint32_t hash) {
45 tmp.name = (char *)name;
46 tmp.hash = (hash)?hash:simple_hash(tmp.name);
48 return (RRDVAR *)avl_search_lock(tree, (avl *)&tmp);
51 inline void rrdvar_free(RRDHOST *host, avl_tree_lock *tree, RRDVAR *rv) {
57 debug(D_VARIABLES, "Deleting variable '%s'", rv->name);
58 if(unlikely(!rrdvar_index_del(tree, rv)))
59 error("Attempted to delete variable '%s' from host '%s', but it is not found.", rv->name, host->hostname);
66 inline RRDVAR *rrdvar_create_and_index(const char *scope, avl_tree_lock *tree, const char *name, int type, void *value) {
67 char *variable = strdupz(name);
68 rrdvar_fix_name(variable);
69 uint32_t hash = simple_hash(variable);
71 RRDVAR *rv = rrdvar_index_find(tree, variable, hash);
73 debug(D_VARIABLES, "Variable '%s' not found in scope '%s'. Creating a new one.", variable, scope);
75 rv = callocz(1, sizeof(RRDVAR));
81 RRDVAR *ret = rrdvar_index_add(tree, rv);
82 if(unlikely(ret != rv)) {
83 debug(D_VARIABLES, "Variable '%s' in scope '%s' already exists", variable, scope);
84 rrdvar_free(NULL, NULL, rv);
88 debug(D_VARIABLES, "Variable '%s' created in scope '%s'", variable, scope);
91 debug(D_VARIABLES, "Variable '%s' is already found in scope '%s'.", variable, scope);
97 // it must return NULL - not the existing variable - or double-free will happen
104 // ----------------------------------------------------------------------------
107 RRDVAR *rrdvar_custom_host_variable_create(RRDHOST *host, const char *name) {
108 calculated_number *v = callocz(1, sizeof(calculated_number));
110 RRDVAR *rv = rrdvar_create_and_index("host", &host->variables_root_index, name, RRDVAR_TYPE_CALCULATED_ALLOCATED, v);
113 error("Requested variable '%s' already exists - possibly 2 plugins will be updating it at the same time", name);
115 char *variable = strdupz(name);
116 rrdvar_fix_name(variable);
117 uint32_t hash = simple_hash(variable);
119 rv = rrdvar_index_find(&host->variables_root_index, variable, hash);
125 void rrdvar_custom_host_variable_destroy(RRDHOST *host, const char *name) {
126 char *variable = strdupz(name);
127 rrdvar_fix_name(variable);
128 uint32_t hash = simple_hash(variable);
130 RRDVAR *rv = rrdvar_index_find(&host->variables_root_index, variable, hash);
134 error("Attempted to remove variable '%s' from host '%s', but it does not exist.", name, host->hostname);
138 if(rv->type != RRDVAR_TYPE_CALCULATED_ALLOCATED) {
139 error("Attempted to remove variable '%s' from host '%s', but it does not a custom allocated variable.", name, host->hostname);
143 if(!rrdvar_index_del(&host->variables_root_index, rv)) {
144 error("Attempted to remove variable '%s' from host '%s', but it cannot be found.", name, host->hostname);
153 void rrdvar_custom_host_variable_set(RRDVAR *rv, calculated_number value) {
154 if(rv->type != RRDVAR_TYPE_CALCULATED_ALLOCATED)
155 error("requested to set variable '%s' to value " CALCULATED_NUMBER_FORMAT " but the variable is not a custom one.", rv->name, value);
157 calculated_number *v = rv->value;
162 // ----------------------------------------------------------------------------
165 static calculated_number rrdvar2number(RRDVAR *rv) {
167 case RRDVAR_TYPE_CALCULATED_ALLOCATED:
168 case RRDVAR_TYPE_CALCULATED: {
169 calculated_number *n = (calculated_number *)rv->value;
173 case RRDVAR_TYPE_TIME_T: {
174 time_t *n = (time_t *)rv->value;
178 case RRDVAR_TYPE_COLLECTED: {
179 collected_number *n = (collected_number *)rv->value;
183 case RRDVAR_TYPE_TOTAL: {
184 total_number *n = (total_number *)rv->value;
188 case RRDVAR_TYPE_INT: {
189 int *n = (int *)rv->value;
194 error("I don't know how to convert RRDVAR type %d to calculated_number", rv->type);
199 int health_variable_lookup(const char *variable, uint32_t hash, RRDCALC *rc, calculated_number *result) {
200 RRDSET *st = rc->rrdset;
205 rv = rrdvar_index_find(&st->variables_root_index, variable, hash);
207 *result = rrdvar2number(rv);
211 rv = rrdvar_index_find(&st->rrdfamily->variables_root_index, variable, hash);
213 *result = rrdvar2number(rv);
217 rv = rrdvar_index_find(&st->rrdhost->variables_root_index, variable, hash);
219 *result = rrdvar2number(rv);
226 // ----------------------------------------------------------------------------
229 struct variable2json_helper {
234 static int single_variable2json(void *entry, void *data) {
235 struct variable2json_helper *helper = (struct variable2json_helper *)data;
236 RRDVAR *rv = (RRDVAR *)entry;
237 calculated_number value = rrdvar2number(rv);
239 if(unlikely(isnan(value) || isinf(value)))
240 buffer_sprintf(helper->buf, "%s\n\t\t\"%s\": null", helper->counter?",":"", rv->name);
242 buffer_sprintf(helper->buf, "%s\n\t\t\"%s\": %0.5Lf", helper->counter?",":"", rv->name, (long double)value);
249 void health_api_v1_chart_variables2json(RRDSET *st, BUFFER *buf) {
250 struct variable2json_helper helper = {
255 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);
256 avl_traverse_lock(&st->variables_root_index, single_variable2json, (void *)&helper);
257 buffer_sprintf(buf, "\n\t},\n\t\"family\": \"%s\",\n\t\"family_variables\": {", st->family);
259 avl_traverse_lock(&st->rrdfamily->variables_root_index, single_variable2json, (void *)&helper);
260 buffer_sprintf(buf, "\n\t},\n\t\"host\": \"%s\",\n\t\"host_variables\": {", st->rrdhost->hostname);
262 avl_traverse_lock(&st->rrdhost->variables_root_index, single_variable2json, (void *)&helper);
263 buffer_strcat(buf, "\n\t}\n}\n");