1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
|
// 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
|