@cryptotaxi247 / netdata-1 / commits / 9e7e3f1b4

added support for custom variables that can be exposed to alarm expressions without the need for charts

Costa Tsaousis (ktsaou) committed Nov 13, 2016 at 01:19 UTC 9e7e3f1b4db18fa8afbccb51811e08d5699a430c
2 files changed +73 -7
src/health.c
+62 -2
@@ -488,14 +488,15 @@ static inline void rrdvar_free(RRDHOST *host, avl_tree_lock *tree, RRDVAR *rv) {
488
489 if(tree) {
490 debug(D_VARIABLES, "Deleting variable '%s'", rv->name);
491 - rrdvar_index_del(tree, rv);
491 + if(unlikely(!rrdvar_index_del(tree, rv)))
492 + error("Attempted to delete variable '%s' from host '%s', but it is not found.", rv->name, host->hostname);
493 }
494
495 freez(rv->name);
496 freez(rv);
497 }
498
498 -static inline RRDVAR *rrdvar_create_and_index(const char *scope, avl_tree_lock *tree, const char *name, int type, calculated_number *value) {
499 +static inline RRDVAR *rrdvar_create_and_index(const char *scope, avl_tree_lock *tree, const char *name, int type, void *value) {
500 char *variable = strdupz(name);
501 rrdvar_fix_name(variable);
502 uint32_t hash = simple_hash(variable);
@@ -533,11 +534,70 @@ static inline RRDVAR *rrdvar_create_and_index(const char *scope, avl_tree_lock *
534 return rv;
535 }
536
537 +// ----------------------------------------------------------------------------
538 +// CUSTOM VARIABLES
539 +
540 +RRDVAR *rrdvar_custom_host_variable_create(RRDHOST *host, const char *name) {
541 + calculated_number *v = callocz(1, sizeof(calculated_number));
542 + *v = NAN;
543 + RRDVAR *rv = rrdvar_create_and_index("host", &host->variables_root_index, name, RRDVAR_TYPE_CALCULATED_ALLOCATED, v);
544 + if(unlikely(!rv)) {
545 + free(v);
546 + error("Requested variable '%s' already exists - possibly 2 plugins will be updating it at the same time", name);
547 +
548 + char *variable = strdupz(name);
549 + rrdvar_fix_name(variable);
550 + uint32_t hash = simple_hash(variable);
551 +
552 + rv = rrdvar_index_find(&host->variables_root_index, variable, hash);
553 + }
554 +
555 + return rv;
556 +}
557 +
558 +void rrdvar_custom_host_variable_destroy(RRDHOST *host, const char *name) {
559 + char *variable = strdupz(name);
560 + rrdvar_fix_name(variable);
561 + uint32_t hash = simple_hash(variable);
562 +
563 + RRDVAR *rv = rrdvar_index_find(&host->variables_root_index, variable, hash);
564 + freez(variable);
565 +
566 + if(!rv) {
567 + error("Attempted to remove variable '%s' from host '%s', but it does not exist.", name, host->hostname);
568 + return;
569 + }
570 +
571 + if(rv->type != RRDVAR_TYPE_CALCULATED_ALLOCATED) {
572 + error("Attempted to remove variable '%s' from host '%s', but it does not a custom allocated variable.", name, host->hostname);
573 + return;
574 + }
575 +
576 + if(!rrdvar_index_del(&host->variables_root_index, rv)) {
577 + error("Attempted to remove variable '%s' from host '%s', but it cannot be found.", name, host->hostname);
578 + return;
579 + }
580 +
581 + freez(rv->name);
582 + freez(rv->value);
583 + freez(rv);
584 +}
585 +
586 +void rrdvar_custom_host_variable_set(RRDVAR *rv, calculated_number value) {
587 + if(rv->type != RRDVAR_TYPE_CALCULATED_ALLOCATED)
588 + error("requested to set variable '%s' to value " CALCULATED_NUMBER_FORMAT " but the variable is not a custom one.", rv->name, value);
589 + else {
590 + calculated_number *v = rv->value;
591 + *v = value;
592 + }
593 +}
594 +
595 // ----------------------------------------------------------------------------
596 // RRDVAR lookup
597
598 static calculated_number rrdvar2number(RRDVAR *rv) {
599 switch(rv->type) {
600 + case RRDVAR_TYPE_CALCULATED_ALLOCATED:
601 case RRDVAR_TYPE_CALCULATED: {
602 calculated_number *n = (calculated_number *)rv->value;
603 return *n;
src/health.h
+11 -5
@@ -5,11 +5,13 @@ extern int health_enabled;
5
6 extern int rrdvar_compare(void *a, void *b);
7
8 -#define RRDVAR_TYPE_CALCULATED 1
9 -#define RRDVAR_TYPE_TIME_T 2
10 -#define RRDVAR_TYPE_COLLECTED 3
11 -#define RRDVAR_TYPE_TOTAL 4
12 -#define RRDVAR_TYPE_INT 5
8 +#define RRDVAR_TYPE_CALCULATED 1
9 +#define RRDVAR_TYPE_TIME_T 2
10 +#define RRDVAR_TYPE_COLLECTED 3
11 +#define RRDVAR_TYPE_TOTAL 4
12 +#define RRDVAR_TYPE_INT 5
13 +#define RRDVAR_TYPE_CALCULATED_ALLOCATED 6
14 +
15
16 // the variables as stored in the variables indexes
17 // there are 3 indexes:
@@ -352,4 +354,8 @@ extern void health_alarm_log2json(RRDHOST *host, BUFFER *wb, uint32_t after);
354
355 void health_api_v1_chart_variables2json(RRDSET *st, BUFFER *buf);
356
357 +extern RRDVAR *rrdvar_custom_host_variable_create(RRDHOST *host, const char *name);
358 +extern void rrdvar_custom_host_variable_destroy(RRDHOST *host, const char *name);
359 +extern void rrdvar_custom_host_variable_set(RRDVAR *rv, calculated_number value);
360 +
361 #endif //NETDATA_HEALTH_H