allow expressions to examine the current status of the alarm with $status; special variables are also defined: $REMOVED, $UNINITIALIZED, $UNDEFINED, $CLEAR, $WARNING, $CRITICAL and their values are incremental so that $status > $CLEAR can be checked
Costa Tsaousis (ktsaou) committed
Sep 14, 2016 at 23:12 UTC
b64fab4c7dff65e4dad0627b89d55805700fad69
3 files changed
+72
-5
src/eval.c
+68
-5
@@ -74,7 +74,7 @@ static inline calculated_number eval_check_number(calculated_number n, int *erro
74
}
75
76
static inline calculated_number eval_variable(EVAL_EXPRESSION *exp, EVAL_VARIABLE *v, int *error) {
77
- static uint32_t this_hash = 0, now_hash = 0, after_hash = 0, before_hash = 0;
77
+ 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;
78
calculated_number n;
79
80
if(unlikely(this_hash == 0)) {
@@ -82,9 +82,16 @@ static inline calculated_number eval_variable(EVAL_EXPRESSION *exp, EVAL_VARIABL
82
now_hash = simple_hash("now");
83
after_hash = simple_hash("after");
84
before_hash = simple_hash("before");
85
+ status_hash = simple_hash("status");
86
+ removed_hash = simple_hash("REMOVED");
87
+ uninitialized_hash = simple_hash("UNINITIALIZED");
88
+ undefined_hash = simple_hash("UNDEFINED");
89
+ clear_hash = simple_hash("CLEAR");
90
+ warning_hash = simple_hash("WARNING");
91
+ critical_hash = simple_hash("CRITICAL");
92
}
93
87
- if(v->hash == this_hash && !strcmp(v->name, "this")) {
94
+ if(unlikely(v->hash == this_hash && !strcmp(v->name, "this"))) {
95
n = (exp->this)?*exp->this:NAN;
96
buffer_strcat(exp->error_msg, "[ $this = ");
97
print_parsed_as_constant(exp->error_msg, n);
@@ -92,7 +99,7 @@ static inline calculated_number eval_variable(EVAL_EXPRESSION *exp, EVAL_VARIABL
99
return n;
100
}
101
95
- if(v->hash == after_hash && !strcmp(v->name, "after")) {
102
+ if(unlikely(v->hash == after_hash && !strcmp(v->name, "after"))) {
103
n = (exp->after && *exp->after)?*exp->after:NAN;
104
buffer_strcat(exp->error_msg, "[ $after = ");
105
print_parsed_as_constant(exp->error_msg, n);
@@ -100,7 +107,7 @@ static inline calculated_number eval_variable(EVAL_EXPRESSION *exp, EVAL_VARIABL
107
return n;
108
}
109
103
- if(v->hash == before_hash && !strcmp(v->name, "before")) {
110
+ if(unlikely(v->hash == before_hash && !strcmp(v->name, "before"))) {
111
n = (exp->before && *exp->before)?*exp->before:NAN;
112
buffer_strcat(exp->error_msg, "[ $before = ");
113
print_parsed_as_constant(exp->error_msg, n);
@@ -108,7 +115,7 @@ static inline calculated_number eval_variable(EVAL_EXPRESSION *exp, EVAL_VARIABL
115
return n;
116
}
117
111
- if(v->hash == now_hash && !strcmp(v->name, "now")) {
118
+ if(unlikely(v->hash == now_hash && !strcmp(v->name, "now"))) {
119
n = time(NULL);
120
buffer_strcat(exp->error_msg, "[ $now = ");
121
print_parsed_as_constant(exp->error_msg, n);
@@ -116,6 +123,62 @@ static inline calculated_number eval_variable(EVAL_EXPRESSION *exp, EVAL_VARIABL
123
return n;
124
}
125
126
+ if(unlikely(v->hash == status_hash && !strcmp(v->name, "status"))) {
127
+ n = (exp->status)?*exp->status:RRDCALC_STATUS_UNINITIALIZED;
128
+ buffer_strcat(exp->error_msg, "[ $status = ");
129
+ print_parsed_as_constant(exp->error_msg, n);
130
+ buffer_strcat(exp->error_msg, " ] ");
131
+ return n;
132
+ }
133
+
134
+ if(unlikely(v->hash == removed_hash && !strcmp(v->name, "REMOVED"))) {
135
+ n = RRDCALC_STATUS_REMOVED;
136
+ buffer_strcat(exp->error_msg, "[ $REMOVED = ");
137
+ print_parsed_as_constant(exp->error_msg, n);
138
+ buffer_strcat(exp->error_msg, " ] ");
139
+ return n;
140
+ }
141
+
142
+ if(unlikely(v->hash == uninitialized_hash && !strcmp(v->name, "UNINITIALIZED"))) {
143
+ n = RRDCALC_STATUS_UNINITIALIZED;
144
+ buffer_strcat(exp->error_msg, "[ $UNINITIALIZED = ");
145
+ print_parsed_as_constant(exp->error_msg, n);
146
+ buffer_strcat(exp->error_msg, " ] ");
147
+ return n;
148
+ }
149
+
150
+ if(unlikely(v->hash == undefined_hash && !strcmp(v->name, "UNDEFINED"))) {
151
+ n = RRDCALC_STATUS_UNDEFINED;
152
+ buffer_strcat(exp->error_msg, "[ $UNDEFINED = ");
153
+ print_parsed_as_constant(exp->error_msg, n);
154
+ buffer_strcat(exp->error_msg, " ] ");
155
+ return n;
156
+ }
157
+
158
+ if(unlikely(v->hash == clear_hash && !strcmp(v->name, "CLEAR"))) {
159
+ n = RRDCALC_STATUS_CLEAR;
160
+ buffer_strcat(exp->error_msg, "[ $CLEAR = ");
161
+ print_parsed_as_constant(exp->error_msg, n);
162
+ buffer_strcat(exp->error_msg, " ] ");
163
+ return n;
164
+ }
165
+
166
+ if(unlikely(v->hash == warning_hash && !strcmp(v->name, "WARNING"))) {
167
+ n = RRDCALC_STATUS_WARNING;
168
+ buffer_strcat(exp->error_msg, "[ $WARNING = ");
169
+ print_parsed_as_constant(exp->error_msg, n);
170
+ buffer_strcat(exp->error_msg, " ] ");
171
+ return n;
172
+ }
173
+
174
+ if(unlikely(v->hash == critical_hash && !strcmp(v->name, "CRITICAL"))) {
175
+ n = RRDCALC_STATUS_CRITICAL;
176
+ buffer_strcat(exp->error_msg, "[ $CRITICAL = ");
177
+ print_parsed_as_constant(exp->error_msg, n);
178
+ buffer_strcat(exp->error_msg, " ] ");
179
+ return n;
180
+ }
181
+
182
if(exp->rrdcalc && health_variable_lookup(v->name, v->hash, exp->rrdcalc, &n)) {
183
buffer_sprintf(exp->error_msg, "[ $%s = ", v->name);
184
print_parsed_as_constant(exp->error_msg, n);
src/eval.h
+1
@@ -14,6 +14,7 @@ typedef struct eval_expression {
14
const char *source;
15
const char *parsed_as;
16
17
+ int *status;
18
calculated_number *this;
19
time_t *after;
20
time_t *before;
src/health.c
+3
@@ -693,6 +693,7 @@ static inline void rrdcalc_create_part2(RRDHOST *host, RRDCALC *rc) {
693
rrdhost_check_rdlock(host);
694
695
if(rc->calculation) {
696
+ rc->calculation->status = &rc->status;
697
rc->calculation->this = &rc->value;
698
rc->calculation->after = &rc->db_after;
699
rc->calculation->before = &rc->db_before;
@@ -700,6 +701,7 @@ static inline void rrdcalc_create_part2(RRDHOST *host, RRDCALC *rc) {
701
}
702
703
if(rc->warning) {
704
+ rc->warning->status = &rc->status;
705
rc->warning->this = &rc->value;
706
rc->warning->after = &rc->db_after;
707
rc->warning->before = &rc->db_before;
@@ -707,6 +709,7 @@ static inline void rrdcalc_create_part2(RRDHOST *host, RRDCALC *rc) {
709
}
710
711
if(rc->critical) {
712
+ rc->critical->status = &rc->status;
713
rc->critical->this = &rc->value;
714
rc->critical->after = &rc->db_after;
715
rc->critical->before = &rc->db_before;