]> arthur.barton.de Git - netdata.git/commitdiff
allow expressions to examine the current status of the alarm with $status; special...
authorCosta Tsaousis (ktsaou) <costa@tsaousis.gr>
Wed, 14 Sep 2016 20:12:57 +0000 (23:12 +0300)
committerCosta Tsaousis (ktsaou) <costa@tsaousis.gr>
Wed, 14 Sep 2016 20:12:57 +0000 (23:12 +0300)
src/eval.c
src/eval.h
src/health.c

index 8866ee95dfb2b3ef6925dd0cccd5c1d713f20906..e307040e36040b494d49905ac5b768dbe44fcb56 100644 (file)
@@ -74,7 +74,7 @@ static inline calculated_number eval_check_number(calculated_number n, int *erro
 }
 
 static inline calculated_number eval_variable(EVAL_EXPRESSION *exp, EVAL_VARIABLE *v, int *error) {
-    static uint32_t this_hash = 0, now_hash = 0, after_hash = 0, before_hash = 0;
+    static uint32_t this_hash = 0, now_hash = 0, after_hash = 0, before_hash = 0, status_hash = 0, removed_hash = 0, uninitialized_hash = 0, undefined_hash = 0, clear_hash = 0, warning_hash = 0, critical_hash = 0;
     calculated_number n;
 
     if(unlikely(this_hash == 0)) {
@@ -82,9 +82,16 @@ static inline calculated_number eval_variable(EVAL_EXPRESSION *exp, EVAL_VARIABL
         now_hash = simple_hash("now");
         after_hash = simple_hash("after");
         before_hash = simple_hash("before");
+        status_hash = simple_hash("status");
+        removed_hash = simple_hash("REMOVED");
+        uninitialized_hash = simple_hash("UNINITIALIZED");
+        undefined_hash = simple_hash("UNDEFINED");
+        clear_hash = simple_hash("CLEAR");
+        warning_hash = simple_hash("WARNING");
+        critical_hash = simple_hash("CRITICAL");
     }
 
-    if(v->hash == this_hash && !strcmp(v->name, "this")) {
+    if(unlikely(v->hash == this_hash && !strcmp(v->name, "this"))) {
         n = (exp->this)?*exp->this:NAN;
         buffer_strcat(exp->error_msg, "[ $this = ");
         print_parsed_as_constant(exp->error_msg, n);
@@ -92,7 +99,7 @@ static inline calculated_number eval_variable(EVAL_EXPRESSION *exp, EVAL_VARIABL
         return n;
     }
 
-    if(v->hash == after_hash && !strcmp(v->name, "after")) {
+    if(unlikely(v->hash == after_hash && !strcmp(v->name, "after"))) {
         n = (exp->after && *exp->after)?*exp->after:NAN;
         buffer_strcat(exp->error_msg, "[ $after = ");
         print_parsed_as_constant(exp->error_msg, n);
@@ -100,7 +107,7 @@ static inline calculated_number eval_variable(EVAL_EXPRESSION *exp, EVAL_VARIABL
         return n;
     }
 
-    if(v->hash == before_hash && !strcmp(v->name, "before")) {
+    if(unlikely(v->hash == before_hash && !strcmp(v->name, "before"))) {
         n = (exp->before && *exp->before)?*exp->before:NAN;
         buffer_strcat(exp->error_msg, "[ $before = ");
         print_parsed_as_constant(exp->error_msg, n);
@@ -108,7 +115,7 @@ static inline calculated_number eval_variable(EVAL_EXPRESSION *exp, EVAL_VARIABL
         return n;
     }
 
-    if(v->hash == now_hash && !strcmp(v->name, "now")) {
+    if(unlikely(v->hash == now_hash && !strcmp(v->name, "now"))) {
         n = time(NULL);
         buffer_strcat(exp->error_msg, "[ $now = ");
         print_parsed_as_constant(exp->error_msg, n);
@@ -116,6 +123,62 @@ static inline calculated_number eval_variable(EVAL_EXPRESSION *exp, EVAL_VARIABL
         return n;
     }
 
+    if(unlikely(v->hash == status_hash && !strcmp(v->name, "status"))) {
+        n = (exp->status)?*exp->status:RRDCALC_STATUS_UNINITIALIZED;
+        buffer_strcat(exp->error_msg, "[ $status = ");
+        print_parsed_as_constant(exp->error_msg, n);
+        buffer_strcat(exp->error_msg, " ] ");
+        return n;
+    }
+
+    if(unlikely(v->hash == removed_hash && !strcmp(v->name, "REMOVED"))) {
+        n = RRDCALC_STATUS_REMOVED;
+        buffer_strcat(exp->error_msg, "[ $REMOVED = ");
+        print_parsed_as_constant(exp->error_msg, n);
+        buffer_strcat(exp->error_msg, " ] ");
+        return n;
+    }
+
+    if(unlikely(v->hash == uninitialized_hash && !strcmp(v->name, "UNINITIALIZED"))) {
+        n = RRDCALC_STATUS_UNINITIALIZED;
+        buffer_strcat(exp->error_msg, "[ $UNINITIALIZED = ");
+        print_parsed_as_constant(exp->error_msg, n);
+        buffer_strcat(exp->error_msg, " ] ");
+        return n;
+    }
+
+    if(unlikely(v->hash == undefined_hash && !strcmp(v->name, "UNDEFINED"))) {
+        n = RRDCALC_STATUS_UNDEFINED;
+        buffer_strcat(exp->error_msg, "[ $UNDEFINED = ");
+        print_parsed_as_constant(exp->error_msg, n);
+        buffer_strcat(exp->error_msg, " ] ");
+        return n;
+    }
+
+    if(unlikely(v->hash == clear_hash && !strcmp(v->name, "CLEAR"))) {
+        n = RRDCALC_STATUS_CLEAR;
+        buffer_strcat(exp->error_msg, "[ $CLEAR = ");
+        print_parsed_as_constant(exp->error_msg, n);
+        buffer_strcat(exp->error_msg, " ] ");
+        return n;
+    }
+
+    if(unlikely(v->hash == warning_hash && !strcmp(v->name, "WARNING"))) {
+        n = RRDCALC_STATUS_WARNING;
+        buffer_strcat(exp->error_msg, "[ $WARNING = ");
+        print_parsed_as_constant(exp->error_msg, n);
+        buffer_strcat(exp->error_msg, " ] ");
+        return n;
+    }
+
+    if(unlikely(v->hash == critical_hash && !strcmp(v->name, "CRITICAL"))) {
+        n = RRDCALC_STATUS_CRITICAL;
+        buffer_strcat(exp->error_msg, "[ $CRITICAL = ");
+        print_parsed_as_constant(exp->error_msg, n);
+        buffer_strcat(exp->error_msg, " ] ");
+        return n;
+    }
+
     if(exp->rrdcalc && health_variable_lookup(v->name, v->hash, exp->rrdcalc, &n)) {
         buffer_sprintf(exp->error_msg, "[ $%s = ", v->name);
         print_parsed_as_constant(exp->error_msg, n);
index b509a9fa83f687ebc8a6f1bcc9ee4c4a7114c667..d68b9af474ce7f5806746dc7db2321d059104b7f 100644 (file)
@@ -14,6 +14,7 @@ typedef struct eval_expression {
     const char *source;
     const char *parsed_as;
 
+    int *status;
     calculated_number *this;
     time_t *after;
     time_t *before;
index 0c8f3ee2e33531563c5d1d5506c9479929cc63fa..672ef539015a9e1280404b9dc4f0678a376b3846 100644 (file)
@@ -693,6 +693,7 @@ static inline void rrdcalc_create_part2(RRDHOST *host, RRDCALC *rc) {
     rrdhost_check_rdlock(host);
 
     if(rc->calculation) {
+        rc->calculation->status = &rc->status;
         rc->calculation->this = &rc->value;
         rc->calculation->after = &rc->db_after;
         rc->calculation->before = &rc->db_before;
@@ -700,6 +701,7 @@ static inline void rrdcalc_create_part2(RRDHOST *host, RRDCALC *rc) {
     }
 
     if(rc->warning) {
+        rc->warning->status = &rc->status;
         rc->warning->this = &rc->value;
         rc->warning->after = &rc->db_after;
         rc->warning->before = &rc->db_before;
@@ -707,6 +709,7 @@ static inline void rrdcalc_create_part2(RRDHOST *host, RRDCALC *rc) {
     }
 
     if(rc->critical) {
+        rc->critical->status = &rc->status;
         rc->critical->this = &rc->value;
         rc->critical->after = &rc->db_after;
         rc->critical->before = &rc->db_before;