added VARIABLE to plugins and streaming
Costa Tsaousis (ktsaou) committed
Oct 15, 2017 at 15:50 UTC
24ef19e2c814a563ad58bc7d847311efe9628c27
5 files changed
+65
-12
src/health.h
+3
-1
@@ -11,7 +11,7 @@ typedef enum rrdvar_type {
11
RRDVAR_TYPE_COLLECTED = 3,
12
RRDVAR_TYPE_TOTAL = 4,
13
RRDVAR_TYPE_INT = 5,
14
- RRDVAR_TYPE_CALCULATED_ALLOCATED = 6
14
+ RRDVAR_TYPE_CALCULATED_ALLOCATED = 6 // a custom variable, allocate on purpose (ie. not inherited from charts)
15
} RRDVAR_TYPE;
16
17
// the variables as stored in the variables indexes
@@ -408,6 +408,8 @@ extern void rrdcalc_unlink_and_free(RRDHOST *host, RRDCALC *rc);
408
extern void rrdcalctemplate_free(RRDCALCTEMPLATE *rt);
409
extern void rrdcalctemplate_unlink_and_free(RRDHOST *host, RRDCALCTEMPLATE *rt);
410
411
+extern int rrdvar_callback_for_all_variables(RRDHOST *host, int (*callback)(void *rrdvar, void *data), void *data);
412
+
413
#ifdef NETDATA_HEALTH_INTERNALS
414
#define RRDVAR_MAX_LENGTH 1024
415
src/plugins_d.c
+26
-4
@@ -101,6 +101,7 @@ inline size_t pluginsd_process(RRDHOST *host, struct plugind *cd, FILE *fp, int
101
uint32_t CHART_HASH = simple_hash(PLUGINSD_KEYWORD_CHART);
102
uint32_t DIMENSION_HASH = simple_hash(PLUGINSD_KEYWORD_DIMENSION);
103
uint32_t DISABLE_HASH = simple_hash(PLUGINSD_KEYWORD_DISABLE);
104
+ uint32_t VARIABLE_HASH = simple_hash(PLUGINSD_KEYWORD_VARIABLE);
105
106
RRDSET *st = NULL;
107
uint32_t hash;
@@ -212,10 +213,6 @@ inline size_t pluginsd_process(RRDHOST *host, struct plugind *cd, FILE *fp, int
213
214
count++;
215
}
215
- else if(likely(hash == FLUSH_HASH && !strcmp(s, PLUGINSD_KEYWORD_FLUSH))) {
216
- debug(D_PLUGINSD, "PLUGINSD: '%s' is requesting a FLUSH", cd->fullfilename);
217
- st = NULL;
218
- }
216
else if(likely(hash == CHART_HASH && !strcmp(s, PLUGINSD_KEYWORD_CHART))) {
217
st = NULL;
218
@@ -375,6 +372,31 @@ inline size_t pluginsd_process(RRDHOST *host, struct plugind *cd, FILE *fp, int
372
if(strstr(options, "nooverflow") != NULL) rrddim_flag_set(rd, RRDDIM_FLAG_DONT_DETECT_RESETS_OR_OVERFLOWS);
373
}
374
}
375
+ else if(likely(hash == VARIABLE_HASH && !strcmp(s, PLUGINSD_KEYWORD_VARIABLE))) {
376
+ char *name = words[1];
377
+ char *value = words[2];
378
+
379
+ if(unlikely(!name || !*name)) {
380
+ error("PLUGINSD: '%s' is requesting a VARIABLE on host '%s', without a variable name. Disabling it.", cd->fullfilename, host->hostname);
381
+ enabled = 0;
382
+ break;
383
+ }
384
+
385
+ if(unlikely(!value || !*value)) value = NULL;
386
+
387
+ if(value) {
388
+ calculated_number v = (calculated_number)str2ld(value, NULL);
389
+ RRDVAR *rv = rrdvar_custom_host_variable_create(host, name);
390
+ if(rv)
391
+ rrdvar_custom_host_variable_set(rv, v);
392
+ else
393
+ error("PLUGINSD: '%s': cannot find/create VARIABLE '%s' on host '%s'", cd->fullfilename, name, host->hostname);
394
+ }
395
+ }
396
+ else if(likely(hash == FLUSH_HASH && !strcmp(s, PLUGINSD_KEYWORD_FLUSH))) {
397
+ debug(D_PLUGINSD, "PLUGINSD: '%s' is requesting a FLUSH", cd->fullfilename);
398
+ st = NULL;
399
+ }
400
else if(unlikely(hash == DISABLE_HASH && !strcmp(s, PLUGINSD_KEYWORD_DISABLE))) {
401
info("PLUGINSD: '%s' called DISABLE. Disabling it.", cd->fullfilename);
402
enabled = 0;
src/plugins_d.h
+1
@@ -11,6 +11,7 @@
11
#define PLUGINSD_KEYWORD_END "END"
12
#define PLUGINSD_KEYWORD_FLUSH "FLUSH"
13
#define PLUGINSD_KEYWORD_DISABLE "DISABLE"
14
+#define PLUGINSD_KEYWORD_VARIABLE "VARIABLE"
15
16
#define PLUGINSD_LINE_MAX 1024
17
#define PLUGINSD_MAX_WORDS 20
src/rrdpush.c
+27
-6
@@ -115,12 +115,6 @@ static inline void rrdpush_send_chart_definition_nolock(RRDSET *st) {
115
);
116
rd->exposed = 1;
117
}
118
- /*
119
- RRDSETVAR *rs;
120
- for(rs = st->variables; rs ;rs = rs->next) {
121
- if(unlikely(rs->type == ))
122
- }
123
- */
118
119
st->upstream_resync_time = st->last_collected_time.tv_sec + (remote_clock_resync_iterations * st->update_every);
120
}
@@ -194,6 +188,32 @@ void rrdset_done_push(RRDSET *st) {
188
// ----------------------------------------------------------------------------
189
// rrdpush sender thread
190
191
+static int rrdpush_sender_thread_custom_variables_callback(void *rrdvar_ptr, void *host_ptr) {
192
+ RRDVAR *rv = (RRDVAR *)rrdvar_ptr;
193
+ RRDHOST *host = (RRDHOST *)host_ptr;
194
+
195
+ if(unlikely(rv->type == RRDVAR_TYPE_CALCULATED_ALLOCATED)) {
196
+ calculated_number *value = (calculated_number *)rv->value;
197
+
198
+ buffer_sprintf(
199
+ host->rrdpush_sender_buffer
200
+ , "VARIABLE %s = " CALCULATED_NUMBER_FORMAT "\n"
201
+ , rv->name
202
+ , *value
203
+ );
204
+
205
+ return 1;
206
+ }
207
+
208
+ // returning a negative number will break the traversal
209
+ return 0;
210
+}
211
+
212
+static void rrdpush_sender_thread_send_custom_variables(RRDHOST *host) {
213
+ int ret = rrdvar_callback_for_all_variables(host, rrdpush_sender_thread_custom_variables_callback, host);
214
+ info("RRDVAR printed %d VARIABLES", ret);
215
+}
216
+
217
// resets all the chart, so that their definitions
218
// will be resent to the central netdata
219
static void rrdpush_sender_thread_reset_all_charts(RRDHOST *host) {
@@ -225,6 +245,7 @@ static inline void rrdpush_sender_thread_data_flush(RRDHOST *host) {
245
buffer_flush(host->rrdpush_sender_buffer);
246
247
rrdpush_sender_thread_reset_all_charts(host);
248
+ rrdpush_sender_thread_send_custom_variables(host);
249
250
rrdpush_buffer_unlock(host);
251
}
src/rrdvar.c
+8
-1
@@ -59,6 +59,9 @@ inline void rrdvar_free(RRDHOST *host, avl_tree_lock *tree, RRDVAR *rv) {
59
error("Attempted to delete variable '%s' from host '%s', but it is not found.", rv->name, host->hostname);
60
}
61
62
+ if(rv->type == RRDVAR_TYPE_CALCULATED_ALLOCATED)
63
+ freez(rv->value);
64
+
65
freez(rv->name);
66
freez(rv);
67
}
@@ -104,13 +107,17 @@ inline RRDVAR *rrdvar_create_and_index(const char *scope, avl_tree_lock *tree, c
107
// ----------------------------------------------------------------------------
108
// CUSTOM VARIABLES
109
110
+inline int rrdvar_callback_for_all_variables(RRDHOST *host, int (*callback)(void *rrdvar, void *data), void *data) {
111
+ return avl_traverse_lock(&host->variables_root_index, callback, data);
112
+}
113
+
114
RRDVAR *rrdvar_custom_host_variable_create(RRDHOST *host, const char *name) {
115
calculated_number *v = callocz(1, sizeof(calculated_number));
116
*v = NAN;
117
RRDVAR *rv = rrdvar_create_and_index("host", &host->variables_root_index, name, RRDVAR_TYPE_CALCULATED_ALLOCATED, v);
118
if(unlikely(!rv)) {
119
free(v);
113
- error("Requested variable '%s' already exists - possibly 2 plugins will be updating it at the same time", name);
120
+ error("Requested variable '%s' already exists - possibly 2 plugins are updating it at the same time.", name);
121
122
char *variable = strdupz(name);
123
rrdvar_fix_name(variable);