3 #define RRDVAR_MAX_LENGTH 1024
5 static const char *health_default_exec = PLUGINS_DIR "/alarm.sh";
6 int health_enabled = 1;
8 // ----------------------------------------------------------------------------
11 static inline int rrdvar_fix_name(char *variable) {
14 if (!isalnum(*variable) && *variable != '.' && *variable != '_') {
25 int rrdvar_compare(void* a, void* b) {
26 if(((RRDVAR *)a)->hash < ((RRDVAR *)b)->hash) return -1;
27 else if(((RRDVAR *)a)->hash > ((RRDVAR *)b)->hash) return 1;
28 else return strcmp(((RRDVAR *)a)->name, ((RRDVAR *)b)->name);
31 static inline RRDVAR *rrdvar_index_add(avl_tree_lock *tree, RRDVAR *rv) {
32 RRDVAR *ret = (RRDVAR *)avl_insert_lock(tree, (avl *)(rv));
34 debug(D_VARIABLES, "Request to insert RRDVAR '%s' into index failed. Already exists.", rv->name);
39 static inline RRDVAR *rrdvar_index_del(avl_tree_lock *tree, RRDVAR *rv) {
40 RRDVAR *ret = (RRDVAR *)avl_remove_lock(tree, (avl *)(rv));
42 error("Request to remove RRDVAR '%s' from index failed. Not Found.", rv->name);
47 static inline RRDVAR *rrdvar_index_find(avl_tree_lock *tree, const char *name, uint32_t hash) {
49 tmp.name = (char *)name;
50 tmp.hash = (hash)?hash:simple_hash(tmp.name);
52 return (RRDVAR *)avl_search_lock(tree, (avl *)&tmp);
55 static inline void rrdvar_free(RRDHOST *host, avl_tree_lock *tree, RRDVAR *rv) {
61 rrdvar_index_del(tree, rv);
67 static inline RRDVAR *rrdvar_create_and_index(const char *scope, avl_tree_lock *tree, const char *name, int type, calculated_number *value) {
68 char *variable = strdupz(name);
69 rrdvar_fix_name(variable);
70 uint32_t hash = simple_hash(variable);
72 RRDVAR *rv = rrdvar_index_find(tree, variable, hash);
74 debug(D_VARIABLES, "Variable '%s' not found in scope '%s'. Creating a new one.", variable, scope);
76 rv = callocz(1, sizeof(RRDVAR));
82 RRDVAR *ret = rrdvar_index_add(tree, rv);
83 if(unlikely(ret != rv)) {
84 debug(D_VARIABLES, "Variable '%s' in scope '%s' already exists", variable, scope);
85 rrdvar_free(NULL, NULL, rv);
89 debug(D_VARIABLES, "Variable '%s' created in scope '%s'", variable, scope);
100 // ----------------------------------------------------------------------------
103 calculated_number rrdvar2number(RRDVAR *rv) {
105 case RRDVAR_TYPE_CALCULATED: {
106 calculated_number *n = (calculated_number *)rv->value;
111 case RRDVAR_TYPE_TIME_T: {
112 time_t *n = (time_t *)rv->value;
117 case RRDVAR_TYPE_COLLECTED: {
118 collected_number *n = (collected_number *)rv->value;
123 case RRDVAR_TYPE_TOTAL: {
124 total_number *n = (total_number *)rv->value;
129 error("I don't know how to convert RRDVAR type %d to calculated_number", rv->type);
135 void dump_variable(void *data) {
136 RRDVAR *rv = (RRDVAR *)data;
137 debug(D_HEALTH, "%50s : %20.5Lf", rv->name, rrdvar2number(rv));
140 int health_variable_lookup(const char *variable, uint32_t hash, RRDCALC *rc, calculated_number *result) {
141 RRDSET *st = rc->rrdset;
146 rv = rrdvar_index_find(&st->variables_root_index, variable, hash);
148 *result = rrdvar2number(rv);
152 rv = rrdvar_index_find(&st->rrdcontext->variables_root_index, variable, hash);
154 *result = rrdvar2number(rv);
158 rv = rrdvar_index_find(&st->rrdhost->variables_root_index, variable, hash);
160 *result = rrdvar2number(rv);
164 debug(D_HEALTH, "Available local chart '%s' variables:", st->id);
165 avl_traverse_lock(&st->variables_root_index, dump_variable);
167 debug(D_HEALTH, "Available context '%s' variables:", st->rrdcontext->id);
168 avl_traverse_lock(&st->rrdcontext->variables_root_index, dump_variable);
170 debug(D_HEALTH, "Available host '%s' variables:", st->rrdhost->hostname);
171 avl_traverse_lock(&st->rrdhost->variables_root_index, dump_variable);
176 // ----------------------------------------------------------------------------
177 // RRDSETVAR management
179 RRDSETVAR *rrdsetvar_create(RRDSET *st, const char *variable, int type, void *value, uint32_t options) {
180 debug(D_VARIABLES, "RRDVARSET create for chart id '%s' name '%s' with variable name '%s'", st->id, st->name, variable);
181 RRDSETVAR *rs = (RRDSETVAR *)callocz(1, sizeof(RRDSETVAR));
183 char buffer[RRDVAR_MAX_LENGTH + 1];
184 snprintfz(buffer, RRDVAR_MAX_LENGTH, "%s.%s", st->id, variable);
185 rs->fullid = strdupz(buffer);
187 snprintfz(buffer, RRDVAR_MAX_LENGTH, "%s.%s", st->name, variable);
188 rs->fullname = strdupz(buffer);
190 rs->variable = strdupz(variable);
194 rs->options = options;
197 rs->local = rrdvar_create_and_index("local", &st->variables_root_index, rs->variable, rs->type, rs->value);
198 rs->context = rrdvar_create_and_index("context", &st->rrdcontext->variables_root_index, rs->fullid, rs->type, rs->value);
199 rs->host = rrdvar_create_and_index("host", &st->rrdhost->variables_root_index, rs->fullid, rs->type, rs->value);
200 rs->context_name = rrdvar_create_and_index("context", &st->rrdcontext->variables_root_index, rs->fullname, rs->type, rs->value);
201 rs->host_name = rrdvar_create_and_index("host", &st->rrdhost->variables_root_index, rs->fullname, rs->type, rs->value);
203 rs->next = st->variables;
209 void rrdsetvar_rename_all(RRDSET *st) {
210 debug(D_VARIABLES, "RRDSETVAR rename for chart id '%s' name '%s'", st->id, st->name);
212 // only these 2 can change name
216 char buffer[RRDVAR_MAX_LENGTH + 1];
217 RRDSETVAR *rs, *next = st->variables;
221 snprintfz(buffer, RRDVAR_MAX_LENGTH, "%s.%s", st->name, rs->variable);
223 if (strcmp(buffer, rs->fullname)) {
225 rrdvar_free(st->rrdhost, &st->rrdcontext->variables_root_index, rs->context_name);
226 rrdvar_free(st->rrdhost, &st->rrdhost->variables_root_index, rs->host_name);
229 rs->fullname = strdupz(st->name);
230 rs->context_name = rrdvar_create_and_index("context", &st->rrdcontext->variables_root_index, rs->fullname, rs->type, rs->value);
231 rs->host_name = rrdvar_create_and_index("host", &st->rrdhost->variables_root_index, rs->fullname, rs->type, rs->value);
235 rrdsetcalc_link_matching(st);
238 void rrdsetvar_free(RRDSETVAR *rs) {
239 RRDSET *st = rs->rrdset;
240 debug(D_VARIABLES, "RRDSETVAR free for chart id '%s' name '%s', variable '%s'", st->id, st->name, rs->variable);
242 rrdvar_free(st->rrdhost, &st->variables_root_index, rs->local);
243 rrdvar_free(st->rrdhost, &st->rrdcontext->variables_root_index, rs->context);
244 rrdvar_free(st->rrdhost, &st->rrdhost->variables_root_index, rs->host);
245 rrdvar_free(st->rrdhost, &st->rrdcontext->variables_root_index, rs->context_name);
246 rrdvar_free(st->rrdhost, &st->rrdhost->variables_root_index, rs->host_name);
248 if(st->variables == rs) {
249 st->variables = rs->next;
253 for (t = st->variables; t && t->next != rs; t = t->next);
254 if(!t) error("RRDSETVAR '%s' not found in chart '%s' variables linked list", rs->fullname, st->id);
255 else t->next = rs->next;
264 // ----------------------------------------------------------------------------
265 // RRDDIMVAR management
267 #define RRDDIMVAR_ID_MAX 1024
269 RRDDIMVAR *rrddimvar_create(RRDDIM *rd, int type, const char *prefix, const char *suffix, void *value, uint32_t options) {
270 RRDSET *st = rd->rrdset;
272 debug(D_VARIABLES, "RRDDIMSET create for chart id '%s' name '%s', dimension id '%s', name '%s%s%s'", st->id, st->name, rd->id, (prefix)?prefix:"", rd->name, (suffix)?suffix:"");
274 if(!prefix) prefix = "";
275 if(!suffix) suffix = "";
277 char buffer[RRDDIMVAR_ID_MAX + 1];
278 RRDDIMVAR *rs = (RRDDIMVAR *)callocz(1, sizeof(RRDDIMVAR));
280 rs->prefix = strdupz(prefix);
281 rs->suffix = strdupz(suffix);
283 snprintfz(buffer, RRDDIMVAR_ID_MAX, "%s%s%s", rs->prefix, rd->id, rs->suffix);
284 rs->id = strdupz(buffer);
286 snprintfz(buffer, RRDDIMVAR_ID_MAX, "%s%s%s", rs->prefix, rd->name, rs->suffix);
287 rs->name = strdupz(buffer);
289 snprintfz(buffer, RRDDIMVAR_ID_MAX, "%s.%s", rd->rrdset->id, rs->id);
290 rs->fullidid = strdupz(buffer);
292 snprintfz(buffer, RRDDIMVAR_ID_MAX, "%s.%s", rd->rrdset->id, rs->name);
293 rs->fullidname = strdupz(buffer);
295 snprintfz(buffer, RRDDIMVAR_ID_MAX, "%s.%s", rd->rrdset->name, rs->id);
296 rs->fullnameid = strdupz(buffer);
298 snprintfz(buffer, RRDDIMVAR_ID_MAX, "%s.%s", rd->rrdset->name, rs->name);
299 rs->fullnamename = strdupz(buffer);
303 rs->options = options;
306 rs->local_id = rrdvar_create_and_index("local", &st->variables_root_index, rs->id, rs->type, rs->value);
307 rs->local_name = rrdvar_create_and_index("local", &st->variables_root_index, rs->name, rs->type, rs->value);
309 rs->context_id = rrdvar_create_and_index("context", &st->rrdcontext->variables_root_index, rs->id, rs->type, rs->value);
310 rs->context_name = rrdvar_create_and_index("context", &st->rrdcontext->variables_root_index, rs->name, rs->type, rs->value);
312 rs->host_fullidid = rrdvar_create_and_index("host", &st->rrdhost->variables_root_index, rs->fullidid, rs->type, rs->value);
313 rs->host_fullidname = rrdvar_create_and_index("host", &st->rrdhost->variables_root_index, rs->fullidname, rs->type, rs->value);
314 rs->host_fullnameid = rrdvar_create_and_index("host", &st->rrdhost->variables_root_index, rs->fullnameid, rs->type, rs->value);
315 rs->host_fullnamename = rrdvar_create_and_index("host", &st->rrdhost->variables_root_index, rs->fullnamename, rs->type, rs->value);
317 rs->next = rd->variables;
323 void rrddimvar_rename_all(RRDDIM *rd) {
324 RRDSET *st = rd->rrdset;
325 debug(D_VARIABLES, "RRDDIMSET rename for chart id '%s' name '%s', dimension id '%s', name '%s'", st->id, st->name, rd->id, rd->name);
327 RRDDIMVAR *rs, *next = rd->variables;
331 if (strcmp(rd->name, rs->name)) {
332 char buffer[RRDDIMVAR_ID_MAX + 1];
336 rrdvar_free(st->rrdhost, &st->variables_root_index, rs->local_name);
338 snprintfz(buffer, RRDDIMVAR_ID_MAX, "%s%s%s", rs->prefix, rd->name, rs->suffix);
339 rs->name = strdupz(buffer);
340 rs->local_name = rrdvar_create_and_index("local", &st->variables_root_index, rs->name, rs->type, rs->value);
342 rrdvar_free(st->rrdhost, &st->rrdhost->variables_root_index, rs->host_fullidname);
343 freez(rs->fullidname);
344 snprintfz(buffer, RRDDIMVAR_ID_MAX, "%s.%s", st->id, rs->name);
345 rs->fullidname = strdupz(buffer);
346 rs->host_fullidname = rrdvar_create_and_index("host", &st->rrdhost->variables_root_index,
347 rs->fullidname, rs->type, rs->value);
350 rrdvar_free(st->rrdhost, &st->rrdhost->variables_root_index, rs->host_fullnameid);
351 freez(rs->fullnameid);
352 snprintfz(buffer, RRDDIMVAR_ID_MAX, "%s.%s", st->name, rs->id);
353 rs->fullnameid = strdupz(buffer);
354 rs->host_fullnameid = rrdvar_create_and_index("host", &st->rrdhost->variables_root_index,
355 rs->fullnameid, rs->type, rs->value);
358 rrdvar_free(st->rrdhost, &st->rrdhost->variables_root_index, rs->host_fullnamename);
359 freez(rs->fullnamename);
360 snprintfz(buffer, RRDDIMVAR_ID_MAX, "%s.%s", st->name, rs->name);
361 rs->fullnamename = strdupz(buffer);
362 rs->host_fullnamename = rrdvar_create_and_index("host", &st->rrdhost->variables_root_index,
363 rs->fullnamename, rs->type, rs->value);
368 void rrddimvar_free(RRDDIMVAR *rs) {
369 RRDDIM *rd = rs->rrddim;
370 RRDSET *st = rd->rrdset;
371 debug(D_VARIABLES, "RRDDIMSET free for chart id '%s' name '%s', dimension id '%s', name '%s', prefix='%s', suffix='%s'", st->id, st->name, rd->id, rd->name, rs->prefix, rs->suffix);
373 rrdvar_free(st->rrdhost, &st->variables_root_index, rs->local_id);
374 rrdvar_free(st->rrdhost, &st->variables_root_index, rs->local_name);
376 rrdvar_free(st->rrdhost, &st->rrdcontext->variables_root_index, rs->context_id);
377 rrdvar_free(st->rrdhost, &st->rrdcontext->variables_root_index, rs->context_name);
379 rrdvar_free(st->rrdhost, &st->rrdhost->variables_root_index, rs->host_fullidid);
380 rrdvar_free(st->rrdhost, &st->rrdhost->variables_root_index, rs->host_fullidname);
381 rrdvar_free(st->rrdhost, &st->rrdhost->variables_root_index, rs->host_fullnameid);
382 rrdvar_free(st->rrdhost, &st->rrdhost->variables_root_index, rs->host_fullnamename);
384 if(rd->variables == rs) {
385 debug(D_VARIABLES, "RRDDIMSET removing first entry for chart id '%s' name '%s', dimension id '%s', name '%s'", st->id, st->name, rd->id, rd->name);
386 rd->variables = rs->next;
389 debug(D_VARIABLES, "RRDDIMSET removing non-first entry for chart id '%s' name '%s', dimension id '%s', name '%s'", st->id, st->name, rd->id, rd->name);
391 for (t = rd->variables; t && t->next != rs; t = t->next) ;
392 if(!t) error("RRDDIMVAR '%s' not found in dimension '%s/%s' variables linked list", rs->name, st->id, rd->id);
393 else t->next = rs->next;
401 freez(rs->fullidname);
402 freez(rs->fullnameid);
403 freez(rs->fullnamename);
407 // ----------------------------------------------------------------------------
408 // RRDCALC management
410 static void rrdsetcalc_link(RRDSET *st, RRDCALC *rc) {
411 debug(D_HEALTH, "Health linking alarm '%s.%s' from chart '%s' of host '%s'", rc->chart?rc->chart:"NOCHART", rc->name, st->id, st->rrdhost->hostname);
415 if(rc->green && !st->green)
416 st->green = rc->green;
418 if(rc->red && !st->red)
421 rc->local = rrdvar_create_and_index("local", &st->variables_root_index, rc->name, RRDVAR_TYPE_CALCULATED, &rc->value);
422 rc->context = rrdvar_create_and_index("context", &st->rrdcontext->variables_root_index, rc->name, RRDVAR_TYPE_CALCULATED, &rc->value);
424 char fullname[RRDVAR_MAX_LENGTH + 1];
425 snprintfz(fullname, RRDVAR_MAX_LENGTH, "%s.%s", st->id, rc->name);
426 rc->hostid = rrdvar_create_and_index("host", &st->rrdhost->variables_root_index, fullname, RRDVAR_TYPE_CALCULATED, &rc->value);
428 snprintfz(fullname, RRDVAR_MAX_LENGTH, "%s.%s", st->name, rc->name);
429 rc->hostname = rrdvar_create_and_index("host", &st->rrdhost->variables_root_index, fullname, RRDVAR_TYPE_CALCULATED, &rc->value);
432 static inline int rrdcalc_is_matching_this_rrdset(RRDCALC *rc, RRDSET *st) {
433 if( (rc->hash_chart == st->hash && !strcmp(rc->chart, st->id)) ||
434 (rc->hash_chart == st->hash_name && !strcmp(rc->chart, st->name)))
440 // this has to be called while the RRDHOST is locked
441 inline void rrdsetcalc_link_matching(RRDSET *st) {
442 // debug(D_HEALTH, "find matching alarms for chart '%s'", st->id);
445 for(rc = st->rrdhost->alarms; rc ; rc = rc->next) {
446 if(rc->rrdset) continue;
448 if(rrdcalc_is_matching_this_rrdset(rc, st))
449 rrdsetcalc_link(st, rc);
453 // this has to be called while the RRDHOST is locked
454 inline void rrdsetcalc_unlink(RRDCALC *rc) {
455 RRDSET *st = rc->rrdset;
458 error("Requested to unlink RRDCALC '%s.%s' which is not linked to any RRDSET", rc->chart?rc->chart:"NOCHART", rc->name);
462 RRDHOST *host = st->rrdhost;
464 debug(D_HEALTH, "Health unlinking alarm '%s.%s' from chart '%s' of host '%s'", rc->chart?rc->chart:"NOCHART", rc->name, st->id, host->hostname);
468 rc->rrdset_prev->rrdset_next = rc->rrdset_next;
471 rc->rrdset_next->rrdset_prev = rc->rrdset_prev;
474 st->alarms = rc->rrdset_next;
476 rc->rrdset_prev = rc->rrdset_next = NULL;
478 rrdvar_free(st->rrdhost, &st->variables_root_index, rc->local);
481 rrdvar_free(st->rrdhost, &st->rrdcontext->variables_root_index, rc->context);
484 rrdvar_free(st->rrdhost, &st->rrdhost->variables_root_index, rc->hostid);
487 rrdvar_free(st->rrdhost, &st->rrdhost->variables_root_index, rc->hostname);
492 // RRDCALC will remain in RRDHOST
493 // so that if the matching chart is found in the future
494 // it will be applied automatically
497 static inline int rrdcalc_exists(RRDHOST *host, const char *name, uint32_t hash) {
500 // make sure it does not already exist
501 for(rc = host->alarms; rc ; rc = rc->next) {
502 if (rc->hash == hash && !strcmp(name, rc->name)) {
503 error("Health alarm '%s' already exists in host '%s'.", name, host->hostname);
511 static inline void rrdcalc_create_part2(RRDHOST *host, RRDCALC *rc) {
512 rrdhost_check_rdlock(host);
514 if(rc->calculation) {
515 rc->calculation->this = &rc->value;
516 rc->calculation->rrdcalc = rc;
520 rc->warning->this = &rc->value;
521 rc->warning->rrdcalc = rc;
525 rc->critical->this = &rc->value;
526 rc->critical->rrdcalc = rc;
529 // link it to the host
530 rc->next = host->alarms;
533 // link it to its chart
535 for(st = host->rrdset_root; st ; st = st->next) {
536 if(rrdcalc_is_matching_this_rrdset(rc, st)) {
537 rrdsetcalc_link(st, rc);
543 static inline uint32_t rrdcalc_fullname(char *fullname, size_t len, const char *chart, const char *name) {
544 snprintfz(fullname, len - 1, "%s%s%s", chart?chart:"", chart?".":"", name);
545 rrdvar_fix_name(fullname);
546 return simple_hash(fullname);
549 static inline RRDCALC *rrdcalc_create(RRDHOST *host, const char *name, const char *chart, const char *dimensions, int group_method,
550 int after, int before, int update_every, uint32_t options,
551 calculated_number green, calculated_number red,
552 const char *exec, const char *source,
553 const char *calc, const char *warn, const char *crit) {
555 char fullname[RRDVAR_MAX_LENGTH + 1];
556 uint32_t hash = rrdcalc_fullname(fullname, RRDVAR_MAX_LENGTH + 1, chart, name);
558 if(rrdcalc_exists(host, fullname, hash))
561 RRDCALC *rc = callocz(1, sizeof(RRDCALC));
563 rc->name = strdupz(name);
564 rc->hash = simple_hash(rc->name);
566 rc->chart = strdupz(chart);
567 rc->hash_chart = simple_hash(rc->chart);
569 if(dimensions) rc->dimensions = strdupz(dimensions);
571 rc->group = group_method;
574 rc->update_every = update_every;
575 rc->options = options;
580 if(exec) rc->exec = strdupz(exec);
581 if(source) rc->source = strdupz(source);
584 rc->calculation = expression_parse(calc, NULL, NULL);
586 error("Health alarm '%s.%s': failed to parse calculation expression '%s'", chart, name, calc);
589 rc->warning = expression_parse(warn, NULL, NULL);
591 error("Health alarm '%s.%s': failed to re-parse warning expression '%s'", chart, name, warn);
594 rc->critical = expression_parse(crit, NULL, NULL);
596 error("Health alarm '%s.%s': failed to re-parse critical expression '%s'", chart, name, crit);
599 debug(D_HEALTH, "Health runtime added alarm '%s.%s': exec '%s', green %Lf, red %Lf, lookup: group %d, after %d, before %d, options %u, dimensions '%s', update every %d, calculation '%s', warning '%s', critical '%s', source '%s",
600 (rc->chart)?rc->chart:"NOCHART",
602 (rc->exec)?rc->exec:"DEFAULT",
609 (rc->dimensions)?rc->dimensions:"NONE",
611 (rc->calculation)?rc->calculation->parsed_as:"NONE",
612 (rc->warning)?rc->warning->parsed_as:"NONE",
613 (rc->critical)?rc->critical->parsed_as:"NONE",
617 rrdcalc_create_part2(host, rc);
621 void rrdcalc_free(RRDHOST *host, RRDCALC *rc) {
624 debug(D_HEALTH, "Health removing alarm '%s.%s' of host '%s'", rc->chart?rc->chart:"NOCHART", rc->name, host->hostname);
626 // unlink it from RRDSET
627 if(rc->rrdset) rrdsetcalc_unlink(rc);
629 // unlink it from RRDHOST
630 if(rc == host->alarms)
631 host->alarms = rc->next;
633 else if(host->alarms) {
634 RRDCALC *t, *last = host->alarms;
636 for(t = last->next; t && t != rc; last = t, t = t->next) ;
637 if(last && last->next == rc)
638 last->next = rc->next;
640 error("Cannot unlink alarm '%s.%s' from host '%s': not found", rc->chart?rc->chart:"NOCHART", rc->name, host->hostname);
643 error("Cannot unlink unlink '%s.%s' from host '%s': This host does not have any calculations", rc->chart?rc->chart:"NOCHART", rc->name, host->hostname);
645 expression_free(rc->calculation);
646 expression_free(rc->warning);
647 expression_free(rc->critical);
652 freez(rc->dimensions);
657 // ----------------------------------------------------------------------------
658 // RRDCALCTEMPLATE management
660 void rrdcalctemplate_link_matching(RRDSET *st) {
663 for(rt = st->rrdhost->templates; rt ; rt = rt->next) {
664 if(rt->hash_context == st->hash_context && !strcmp(rt->context, st->context)) {
666 RRDCALC *rc = rrdcalc_create(st->rrdhost, rt->name, st->id,
667 rt->dimensions, rt->group, rt->after, rt->before, rt->update_every, rt->options,
668 rt->green, rt->red, rt->exec, rt->source,
669 (rt->calculation)?rt->calculation->source:NULL,
670 (rt->warning)?rt->warning->source:NULL,
671 (rt->critical)?rt->critical->source:NULL);
674 error("Health tried to create alarm from template '%s', but it failed", rt->name);
676 #ifdef NETDATA_INTERNAL_CHECKS
677 else if(rc->rrdset != st)
678 error("Health alarm '%s.%s' should be linked to chart '%s', but it is not", rc->chart?rc->chart:"NOCHART", rc->name, st->id);
686 static inline void rrdcalctemplate_free(RRDHOST *host, RRDCALCTEMPLATE *rt) {
687 debug(D_HEALTH, "Health removing template '%s' of host '%s'", rt->name, host->hostname);
689 if(host->templates) {
690 if(host->templates == rt) {
691 host->templates = rt->next;
694 RRDCALCTEMPLATE *t, *last = host->templates;
695 for (t = last->next; t && t != rt; last = t, t = t->next ) ;
696 if(last && last->next == rt) {
697 last->next = rt->next;
701 error("Cannot find RRDCALCTEMPLATE '%s' linked in host '%s'", rt->name, host->hostname);
705 expression_free(rt->calculation);
706 expression_free(rt->warning);
707 expression_free(rt->critical);
709 freez(rt->dimensions);
717 // ----------------------------------------------------------------------------
718 // load health configuration
720 #define HEALTH_CONF_MAX_LINE 4096
722 #define HEALTH_ALARM_KEY "alarm"
723 #define HEALTH_TEMPLATE_KEY "template"
724 #define HEALTH_ON_KEY "on"
725 #define HEALTH_LOOKUP_KEY "lookup"
726 #define HEALTH_CALC_KEY "calc"
727 #define HEALTH_EVERY_KEY "every"
728 #define HEALTH_GREEN_KEY "green"
729 #define HEALTH_RED_KEY "red"
730 #define HEALTH_WARN_KEY "warn"
731 #define HEALTH_CRIT_KEY "crit"
732 #define HEALTH_EXEC_KEY "exec"
734 static inline int rrdcalc_add_alarm_from_config(RRDHOST *host, RRDCALC *rc) {
736 char fullname[RRDVAR_MAX_LENGTH + 1];
737 uint32_t hash = rrdcalc_fullname(fullname, RRDVAR_MAX_LENGTH + 1, rc->chart, rc->name);
739 if (rrdcalc_exists(host, fullname, hash))
744 error("Health configuration for alarm '%s' does not have a chart", rc->name);
748 if(!RRDCALC_HAS_DB_LOOKUP(rc) && !rc->warning && !rc->critical) {
749 error("Health configuration for alarm '%s.%s' is useless (no calculation, no warning and no critical evaluation)", rc->chart?rc->chart:"NOCHART", rc->name);
753 debug(D_HEALTH, "Health configuration adding alarm '%s.%s': exec '%s', green %Lf, red %Lf, lookup: group %d, after %d, before %d, options %u, dimensions '%s', update every %d, calculation '%s', warning '%s', critical '%s', source '%s",
754 rc->chart?rc->chart:"NOCHART",
756 (rc->exec)?rc->exec:"DEFAULT",
763 (rc->dimensions)?rc->dimensions:"NONE",
765 (rc->calculation)?rc->calculation->parsed_as:"NONE",
766 (rc->warning)?rc->warning->parsed_as:"NONE",
767 (rc->critical)?rc->critical->parsed_as:"NONE",
771 rrdcalc_create_part2(host, rc);
775 static inline int rrdcalctemplate_add_template_from_config(RRDHOST *host, RRDCALCTEMPLATE *rt) {
777 error("Health configuration for template '%s' does not have a context", rt->name);
781 if(!RRDCALCTEMPLATE_HAS_CALCULATION(rt) && !rt->warning && !rt->critical) {
782 error("Health configuration for template '%s' is useless (no calculation, no warning and no critical evaluation)", rt->name);
787 for (t = host->templates; t ; t = t->next) {
788 if(t->hash_name == rt->hash_name && !strcmp(t->name, rt->name)) {
789 error("Health configuration template '%s' already exists for host '%s'.", rt->name, host->hostname);
794 debug(D_HEALTH, "Health configuration adding template '%s': context '%s', exec '%s', green %Lf, red %Lf, lookup: group %d, after %d, before %d, options %u, dimensions '%s', update every %d, calculation '%s', warning '%s', critical '%s', source '%s'",
796 (rt->context)?rt->context:"NONE",
797 (rt->exec)?rt->exec:"DEFAULT",
804 (rt->dimensions)?rt->dimensions:"NONE",
806 (rt->calculation)?rt->calculation->parsed_as:"NONE",
807 (rt->warning)?rt->warning->parsed_as:"NONE",
808 (rt->critical)?rt->critical->parsed_as:"NONE",
812 rt->next = host->templates;
813 host->templates = rt;
817 static inline int health_parse_duration(char *string, int *result) {
818 // make sure it is a number
819 if(!*string || !(isdigit(*string) || *string == '+' || *string == '-')) {
825 calculated_number n = strtold(string, &e);
829 *result = (int) (n * 86400 * 365);
832 *result = (int) (n * 86400 * 30);
835 *result = (int) (n * 86400 * 7);
838 *result = (int) (n * 86400);
841 *result = (int) (n * 3600);
844 *result = (int) (n * 60);
859 static inline int health_parse_db_lookup(
860 size_t line, const char *path, const char *file, char *string,
861 int *group_method, int *after, int *before, int *every,
862 uint32_t *options, char **dimensions
864 debug(D_HEALTH, "Health configuration parsing database lookup %zu@%s/%s: %s", line, path, file, string);
866 if(*dimensions) freez(*dimensions);
873 char *s = string, *key;
875 // first is the group method
877 while(*s && !isspace(*s)) s++;
878 while(*s && isspace(*s)) *s++ = '\0';
880 error("Health configuration invalid chart calculation at line %zu of file '%s/%s': expected group method followed by the 'after' time, but got '%s'",
881 line, path, file, key);
885 if((*group_method = web_client_api_request_v1_data_group(key, -1)) == -1) {
886 error("Health configuration at line %zu of file '%s/%s': invalid group method '%s'",
887 line, path, file, key);
891 // then is the 'after' time
893 while(*s && !isspace(*s)) s++;
894 while(*s && isspace(*s)) *s++ = '\0';
896 if(!health_parse_duration(key, after)) {
897 error("Health configuration at line %zu of file '%s/%s': invalid duration '%s' after group method",
898 line, path, file, key);
903 *every = abs(*after);
905 // now we may have optional parameters
908 while(*s && !isspace(*s)) s++;
909 while(*s && isspace(*s)) *s++ = '\0';
912 if(!strcasecmp(key, "at")) {
914 while(*s && !isspace(*s)) s++;
915 while(*s && isspace(*s)) *s++ = '\0';
917 if (!health_parse_duration(value, before)) {
918 error("Health configuration at line %zu of file '%s/%s': invalid duration '%s' for '%s' keyword",
919 line, path, file, value, key);
922 else if(!strcasecmp(key, HEALTH_EVERY_KEY)) {
924 while(*s && !isspace(*s)) s++;
925 while(*s && isspace(*s)) *s++ = '\0';
927 if (!health_parse_duration(value, every)) {
928 error("Health configuration at line %zu of file '%s/%s': invalid duration '%s' for '%s' keyword",
929 line, path, file, value, key);
932 else if(!strcasecmp(key, "absolute") || !strcasecmp(key, "abs") || !strcasecmp(key, "absolute_sum")) {
933 *options |= RRDR_OPTION_ABSOLUTE;
935 else if(!strcasecmp(key, "min2max")) {
936 *options |= RRDR_OPTION_MIN2MAX;
938 else if(!strcasecmp(key, "null2zero")) {
939 *options |= RRDR_OPTION_NULL2ZERO;
941 else if(!strcasecmp(key, "percentage")) {
942 *options |= RRDR_OPTION_PERCENTAGE;
944 else if(!strcasecmp(key, "unaligned")) {
945 *options |= RRDR_OPTION_NOT_ALIGNED;
947 else if(!strcasecmp(key, "of")) {
948 if(*s && strcasecmp(s, "all"))
949 *dimensions = strdupz(s);
953 error("Health configuration at line %zu of file '%s/%s': unknown keyword '%s'",
954 line, path, file, key);
961 static inline char *health_source_file(int line, const char *path, const char *filename) {
962 char buffer[FILENAME_MAX + 1];
963 snprintfz(buffer, FILENAME_MAX, "%d@%s/%s", line, path, filename);
964 return strdupz(buffer);
967 int health_readfile(const char *path, const char *filename) {
968 debug(D_HEALTH, "Health configuration reading file '%s/%s'", path, filename);
970 static uint32_t hash_alarm = 0, hash_template = 0, hash_on = 0, hash_calc = 0, hash_green = 0, hash_red = 0, hash_warn = 0, hash_crit = 0, hash_exec = 0, hash_every = 0, hash_lookup = 0;
971 char buffer[HEALTH_CONF_MAX_LINE + 1];
973 if(unlikely(!hash_alarm)) {
974 hash_alarm = simple_uhash(HEALTH_ALARM_KEY);
975 hash_template = simple_uhash(HEALTH_TEMPLATE_KEY);
976 hash_on = simple_uhash(HEALTH_ON_KEY);
977 hash_calc = simple_uhash(HEALTH_CALC_KEY);
978 hash_lookup = simple_uhash(HEALTH_LOOKUP_KEY);
979 hash_green = simple_uhash(HEALTH_GREEN_KEY);
980 hash_red = simple_uhash(HEALTH_RED_KEY);
981 hash_warn = simple_uhash(HEALTH_WARN_KEY);
982 hash_crit = simple_uhash(HEALTH_CRIT_KEY);
983 hash_exec = simple_uhash(HEALTH_EXEC_KEY);
984 hash_every = simple_uhash(HEALTH_EVERY_KEY);
987 snprintfz(buffer, HEALTH_CONF_MAX_LINE, "%s/%s", path, filename);
988 FILE *fp = fopen(buffer, "r");
990 error("Health configuration cannot read file '%s'.", buffer);
995 RRDCALCTEMPLATE *rt = NULL;
997 size_t line = 0, append = 0;
999 while((s = fgets(&buffer[append], (int)(HEALTH_CONF_MAX_LINE - append), fp)) || append) {
1000 int stop_appending = !s;
1002 // info("Line %zu of file '%s/%s': '%s'", line, path, filename, s);
1005 // info("Trimmed line %zu of file '%s/%s': '%s'", line, path, filename, s);
1008 if(!stop_appending && s[append - 1] == '\\') {
1009 s[append - 1] = ' ';
1010 append = &s[append] - buffer;
1011 if(append < HEALTH_CONF_MAX_LINE)
1018 while(*s && *s != ':') s++;
1020 error("Health configuration has invalid line %zu of file '%s/%s'. It does not contain a ':'. Ignoring it.", line, path, filename);
1028 value = trim(value);
1031 error("Health configuration has invalid line %zu of file '%s/%s'. Keyword is empty. Ignoring it.", line, path, filename);
1036 error("Health configuration has invalid line %zu of file '%s/%s'. value is empty. Ignoring it.", line, path, filename);
1040 // info("Health file '%s/%s', key '%s', value '%s'", path, filename, key, value);
1041 uint32_t hash = simple_uhash(key);
1043 if(hash == hash_alarm && !strcasecmp(key, HEALTH_ALARM_KEY)) {
1044 if(rc && !rrdcalc_add_alarm_from_config(&localhost, rc))
1045 rrdcalc_free(&localhost, rc);
1048 if (!rrdcalctemplate_add_template_from_config(&localhost, rt))
1049 rrdcalctemplate_free(&localhost, rt);
1053 rc = callocz(1, sizeof(RRDCALC));
1054 rc->name = strdupz(value);
1055 rc->hash = simple_hash(rc->name);
1056 rc->source = health_source_file(line, path, filename);
1058 if(rrdvar_fix_name(rc->name))
1059 error("Health configuration renamed alarm '%s' to '%s'", value, rc->name);
1061 else if(hash == hash_template && !strcasecmp(key, HEALTH_TEMPLATE_KEY)) {
1063 if(!rrdcalc_add_alarm_from_config(&localhost, rc))
1064 rrdcalc_free(&localhost, rc);
1068 if(rt && !rrdcalctemplate_add_template_from_config(&localhost, rt))
1069 rrdcalctemplate_free(&localhost, rt);
1071 rt = callocz(1, sizeof(RRDCALCTEMPLATE));
1072 rt->name = strdupz(value);
1073 rt->hash_name = simple_hash(rt->name);
1074 rt->source = health_source_file(line, path, filename);
1076 if(rrdvar_fix_name(rt->name))
1077 error("Health configuration renamed template '%s' to '%s'", value, rt->name);
1080 if(hash == hash_on && !strcasecmp(key, HEALTH_ON_KEY)) {
1082 if(strcmp(rc->chart, value))
1083 info("Health configuration at line %zu of file '%s/%s' for alarm '%s' has key '%s' twice, once with value '%s' and later with value '%s'. Using ('%s').",
1084 line, path, filename, rc->name, key, rc->chart, value, value);
1088 rc->chart = strdupz(value);
1089 rc->hash_chart = simple_hash(rc->chart);
1091 else if(hash == hash_lookup && !strcasecmp(key, HEALTH_LOOKUP_KEY)) {
1092 health_parse_db_lookup(line, path, filename, value, &rc->group, &rc->after, &rc->before,
1094 &rc->options, &rc->dimensions);
1096 else if(hash == hash_every && !strcasecmp(key, HEALTH_EVERY_KEY)) {
1097 if(!health_parse_duration(value, &rc->update_every))
1098 info("Health configuration at line %zu of file '%s/%s' for alarm '%s' at key '%s' cannot parse duration: '%s'.",
1099 line, path, filename, rc->name, key, value);
1101 else if(hash == hash_green && !strcasecmp(key, HEALTH_GREEN_KEY)) {
1103 rc->green = strtold(value, &e);
1105 info("Health configuration at line %zu of file '%s/%s' for alarm '%s' at key '%s' leaves this string unmatched: '%s'.",
1106 line, path, filename, rc->name, key, e);
1109 else if(hash == hash_red && !strcasecmp(key, HEALTH_RED_KEY)) {
1111 rc->red = strtold(value, &e);
1113 info("Health configuration at line %zu of file '%s/%s' for alarm '%s' at key '%s' leaves this string unmatched: '%s'.",
1114 line, path, filename, rc->name, key, e);
1117 else if(hash == hash_calc && !strcasecmp(key, HEALTH_CALC_KEY)) {
1118 const char *failed_at = NULL;
1120 rc->calculation = expression_parse(value, &failed_at, &error);
1121 if(!rc->calculation) {
1122 error("Health configuration at line %zu of file '%s/%s' for alarm '%s' at key '%s' has unparse-able expression '%s': %s at '%s'",
1123 line, path, filename, rc->name, key, value, expression_strerror(error), failed_at);
1126 else if(hash == hash_warn && !strcasecmp(key, HEALTH_WARN_KEY)) {
1127 const char *failed_at = NULL;
1129 rc->warning = expression_parse(value, &failed_at, &error);
1131 error("Health configuration at line %zu of file '%s/%s' for alarm '%s' at key '%s' has unparse-able expression '%s': %s at '%s'",
1132 line, path, filename, rc->name, key, value, expression_strerror(error), failed_at);
1135 else if(hash == hash_crit && !strcasecmp(key, HEALTH_CRIT_KEY)) {
1136 const char *failed_at = NULL;
1138 rc->critical = expression_parse(value, &failed_at, &error);
1140 error("Health configuration at line %zu of file '%s/%s' for alarm '%s' at key '%s' has unparse-able expression '%s': %s at '%s'",
1141 line, path, filename, rc->name, key, value, expression_strerror(error), failed_at);
1144 else if(hash == hash_exec && !strcasecmp(key, HEALTH_EXEC_KEY)) {
1146 if(strcmp(rc->exec, value))
1147 info("Health configuration at line %zu of file '%s/%s' for alarm '%s' has key '%s' twice, once with value '%s' and later with value '%s'. Using ('%s').",
1148 line, path, filename, rc->name, key, rc->exec, value, value);
1152 rc->exec = strdupz(value);
1155 error("Health configuration at line %zu of file '%s/%s' for alarm '%s' has unknown key '%s'.",
1156 line, path, filename, rc->name, key);
1160 if(hash == hash_on && !strcasecmp(key, HEALTH_ON_KEY)) {
1162 if(strcmp(rt->context, value))
1163 info("Health configuration at line %zu of file '%s/%s' for template '%s' has key '%s' twice, once with value '%s' and later with value '%s'. Using ('%s').",
1164 line, path, filename, rt->name, key, rt->context, value, value);
1168 rt->context = strdupz(value);
1169 rt->hash_context = simple_hash(rt->context);
1171 else if(hash == hash_lookup && !strcasecmp(key, HEALTH_LOOKUP_KEY)) {
1172 health_parse_db_lookup(line, path, filename, value, &rt->group, &rt->after, &rt->before,
1174 &rt->options, &rt->dimensions);
1176 else if(hash == hash_every && !strcasecmp(key, HEALTH_EVERY_KEY)) {
1177 if(!health_parse_duration(value, &rt->update_every))
1178 info("Health configuration at line %zu of file '%s/%s' for template '%s' at key '%s' cannot parse duration: '%s'.",
1179 line, path, filename, rt->name, key, value);
1181 else if(hash == hash_green && !strcasecmp(key, HEALTH_GREEN_KEY)) {
1183 rt->green = strtold(value, &e);
1185 info("Health configuration at line %zu of file '%s/%s' for template '%s' at key '%s' leaves this string unmatched: '%s'.",
1186 line, path, filename, rt->name, key, e);
1189 else if(hash == hash_red && !strcasecmp(key, HEALTH_RED_KEY)) {
1191 rt->red = strtold(value, &e);
1193 info("Health configuration at line %zu of file '%s/%s' for template '%s' at key '%s' leaves this string unmatched: '%s'.",
1194 line, path, filename, rt->name, key, e);
1197 else if(hash == hash_calc && !strcasecmp(key, HEALTH_CALC_KEY)) {
1198 const char *failed_at = NULL;
1200 rt->calculation = expression_parse(value, &failed_at, &error);
1201 if(!rt->calculation) {
1202 error("Health configuration at line %zu of file '%s/%s' for template '%s' at key '%s' has unparse-able expression '%s': %s at '%s'",
1203 line, path, filename, rt->name, key, value, expression_strerror(error), failed_at);
1206 else if(hash == hash_warn && !strcasecmp(key, HEALTH_WARN_KEY)) {
1207 const char *failed_at = NULL;
1209 rt->warning = expression_parse(value, &failed_at, &error);
1211 error("Health configuration at line %zu of file '%s/%s' for template '%s' at key '%s' has unparse-able expression '%s': %s at '%s'",
1212 line, path, filename, rt->name, key, value, expression_strerror(error), failed_at);
1215 else if(hash == hash_crit && !strcasecmp(key, HEALTH_CRIT_KEY)) {
1216 const char *failed_at = NULL;
1218 rt->critical = expression_parse(value, &failed_at, &error);
1220 error("Health configuration at line %zu of file '%s/%s' for template '%s' at key '%s' has unparse-able expression '%s': %s at '%s'",
1221 line, path, filename, rt->name, key, value, expression_strerror(error), failed_at);
1224 else if(hash == hash_exec && !strcasecmp(key, HEALTH_EXEC_KEY)) {
1226 if(strcmp(rt->exec, value))
1227 info("Health configuration at line %zu of file '%s/%s' for template '%s' has key '%s' twice, once with value '%s' and later with value '%s'. Using ('%s').",
1228 line, path, filename, rt->name, key, rt->exec, value, value);
1232 rt->exec = strdupz(value);
1235 error("Health configuration at line %zu of file '%s/%s' for template '%s' has unknown key '%s'.",
1236 line, path, filename, rt->name, key);
1240 error("Health configuration at line %zu of file '%s/%s' has unknown key '%s'. Expected either '" HEALTH_ALARM_KEY "' or '" HEALTH_TEMPLATE_KEY "'.",
1241 line, path, filename, key);
1245 if(rc && !rrdcalc_add_alarm_from_config(&localhost, rc))
1246 rrdcalc_free(&localhost, rc);
1248 if(rt && !rrdcalctemplate_add_template_from_config(&localhost, rt))
1249 rrdcalctemplate_free(&localhost, rt);
1255 void health_readdir(const char *path) {
1256 size_t pathlen = strlen(path);
1258 debug(D_HEALTH, "Health configuration reading directory '%s'", path);
1260 DIR *dir = opendir(path);
1262 error("Health configuration cannot open directory '%s'.", path);
1266 struct dirent *de = NULL;
1267 while ((de = readdir(dir))) {
1268 size_t len = strlen(de->d_name);
1270 if(de->d_type == DT_DIR
1272 (de->d_name[0] == '.' && de->d_name[1] == '\0')
1273 || (de->d_name[0] == '.' && de->d_name[1] == '.' && de->d_name[2] == '\0')
1277 else if(de->d_type == DT_DIR) {
1278 char *s = mallocz(pathlen + strlen(de->d_name) + 2);
1281 strcat(s, de->d_name);
1287 else if((de->d_type == DT_LNK || de->d_type == DT_REG) &&
1288 len > 5 && !strcmp(&de->d_name[len - 5], ".conf")) {
1289 health_readfile(path, de->d_name);
1296 static inline char *health_config_dir(void) {
1297 char buffer[FILENAME_MAX + 1];
1298 snprintfz(buffer, FILENAME_MAX, "%s/health.d", config_get("global", "config directory", CONFIG_DIR));
1299 return config_get("health", "health configuration directory", buffer);
1302 void health_init(void) {
1303 debug(D_HEALTH, "Health configuration initializing");
1305 if(!(health_enabled = config_get_boolean("health", "enabled", 1))) {
1306 debug(D_HEALTH, "Health is disabled.");
1310 char *path = health_config_dir();
1313 char buffer[FILENAME_MAX + 1];
1314 snprintfz(buffer, FILENAME_MAX, "%s/alarm.sh", config_get("global", "plugins directory", PLUGINS_DIR));
1315 health_default_exec = config_get("health", "script to execute on alarm", buffer);
1318 rrdhost_rwlock(&localhost);
1319 health_readdir(path);
1320 rrdhost_unlock(&localhost);
1323 // ----------------------------------------------------------------------------
1324 // re-load health configuration
1326 static inline void health_free_all_nolock(RRDHOST *host) {
1327 while(host->templates)
1328 rrdcalctemplate_free(host, host->templates);
1331 rrdcalc_free(host, host->alarms);
1334 void health_reload(void) {
1335 if(!health_enabled) {
1336 error("Health reload is requested, but health is not enabled.");
1340 char *path = health_config_dir();
1342 rrdhost_rwlock(&localhost);
1343 health_free_all_nolock(&localhost);
1344 rrdhost_unlock(&localhost);
1346 rrdhost_rwlock(&localhost);
1347 health_readdir(path);
1348 rrdhost_unlock(&localhost);
1351 for(st = localhost.rrdset_root; st ; st = st->next) {
1352 rrdhost_rwlock(&localhost);
1354 rrdsetcalc_link_matching(st);
1355 rrdcalctemplate_link_matching(st);
1357 rrdhost_unlock(&localhost);
1362 // ----------------------------------------------------------------------------
1363 // health main thread and friends
1365 static inline int rrdcalc_isrunnable(RRDCALC *rc, time_t now, time_t *next_run) {
1366 if (unlikely(!rc->rrdset)) {
1367 debug(D_HEALTH, "Health not running alarm '%s.%s'. It is not linked to a chart.", rc->chart?rc->chart:"NOCHART", rc->name);
1371 if (unlikely(!rc->update_every)) {
1372 debug(D_HEALTH, "Health not running alarm '%s.%s'. It does not have an update frequency", rc->chart?rc->chart:"NOCHART", rc->name);
1376 if (unlikely(rc->next_update > now)) {
1377 if (*next_run > rc->next_update)
1378 *next_run = rc->next_update;
1380 debug(D_HEALTH, "Health not examining alarm '%s.%s' yet (will do in %d secs).", rc->chart?rc->chart:"NOCHART", rc->name, (int) (rc->next_update - now));
1387 static inline int rrdcalc_value2status(calculated_number n) {
1388 if(isnan(n)) return RRDCALC_STATUS_UNDEFINED;
1389 if(n) return RRDCALC_STATUS_RAISED;
1390 return RRDCALC_STATUS_OFF;
1393 static inline const char *rrdcalc_status2string(int status) {
1395 case RRDCALC_STATUS_UNINITIALIZED:
1396 return "UNINITIALIZED";
1398 case RRDCALC_STATUS_UNDEFINED:
1401 case RRDCALC_STATUS_RAISED:
1404 case RRDCALC_STATUS_OFF:
1412 void rrdcalc_check_critical_event(RRDCALC *rc) {
1413 calculated_number n = rc->critical->result;
1415 int old_status = rc->critical_status;
1416 int new_status = rrdcalc_value2status(n);
1418 if(new_status != old_status) {
1419 info("Health alarm '%s.%s' = %0.2Lf - CRITICAL condition changed status from %s to %s",
1420 rc->chart?rc->chart:"NOCHART", rc->name,
1422 rrdcalc_status2string(old_status),
1423 rrdcalc_status2string(new_status)
1426 rc->critical_status = new_status;
1430 void rrdcalc_check_warning_event(RRDCALC *rc) {
1431 calculated_number n = rc->warning->result;
1433 int old_status = rc->warning_status;
1434 int new_status = rrdcalc_value2status(n);
1436 if(new_status != old_status) {
1437 info("Health alarm '%s.%s' = %0.2Lf - WARNING condition changed status from %s to %s",
1438 rc->chart?rc->chart:"NOCHART", rc->name,
1440 rrdcalc_status2string(old_status),
1441 rrdcalc_status2string(new_status)
1444 rc->warning_status = new_status;
1448 void *health_main(void *ptr) {
1451 info("HEALTH thread created with task id %d", gettid());
1453 if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
1454 error("Cannot set pthread cancel type to DEFERRED.");
1456 if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
1457 error("Cannot set pthread cancel state to ENABLE.");
1459 int min_run_every = (int)config_get_number("health", "run at least every seconds", 10);
1460 if(min_run_every < 1) min_run_every = 1;
1462 BUFFER *wb = buffer_create(100);
1464 unsigned int loop = 0;
1465 while(health_enabled) {
1467 debug(D_HEALTH, "Health monitoring iteration no %u started", loop);
1469 int oldstate, runnable = 0;
1470 time_t now = time(NULL);
1471 time_t next_run = now + min_run_every;
1474 if (unlikely(pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldstate) != 0))
1475 error("Cannot set pthread cancel state to DISABLE.");
1477 rrdhost_rdlock(&localhost);
1479 // the first loop is to lookup values from the db
1480 for (rc = localhost.alarms; rc; rc = rc->next) {
1481 if (unlikely(!rrdcalc_isrunnable(rc, now, &next_run)))
1486 // 1. if there is database lookup, do it
1487 // 2. if there is calculation expression, run it
1489 if (unlikely(RRDCALC_HAS_DB_LOOKUP(rc))) {
1490 time_t old_db_timestamp = rc->db_timestamp;
1491 int value_is_null = 0;
1493 int ret = rrd2value(rc->rrdset, wb, &rc->value,
1494 rc->dimensions, 1, rc->after, rc->before, rc->group,
1495 rc->options, &rc->db_timestamp, &value_is_null);
1497 if (unlikely(ret != 200)) {
1498 // database lookup failed
1501 if (unlikely(!(rc->rrdcalc_options & RRDCALC_OPTION_DB_ERROR))) {
1502 rc->rrdcalc_options |= RRDCALC_OPTION_DB_ERROR;
1503 error("Health alarm '%s.%s': database lookup returned error %d", rc->chart?rc->chart:"NOCHART", rc->name, ret);
1506 else if (unlikely(rc->rrdcalc_options & RRDCALC_OPTION_DB_ERROR))
1507 rc->rrdcalc_options &= ~RRDCALC_OPTION_DB_ERROR;
1509 if (unlikely(old_db_timestamp == rc->db_timestamp)) {
1510 // database is stale
1512 if (unlikely(!(rc->rrdcalc_options & RRDCALC_OPTION_DB_STALE))) {
1513 rc->rrdcalc_options |= RRDCALC_OPTION_DB_STALE;
1514 error("Health alarm '%s.%s': database is stale", rc->chart?rc->chart:"NOCHART", rc->name);
1517 else if (unlikely(rc->rrdcalc_options & RRDCALC_OPTION_DB_STALE))
1518 rc->rrdcalc_options &= ~RRDCALC_OPTION_DB_STALE;
1520 if (unlikely(value_is_null)) {
1521 // collected value is null
1525 if (unlikely(!(rc->rrdcalc_options & RRDCALC_OPTION_DB_NAN))) {
1526 rc->rrdcalc_options |= RRDCALC_OPTION_DB_NAN;
1527 error("Health alarm '%s.%s': database lookup returned empty value (possibly value is not collected yet)",
1528 rc->chart?rc->chart:"NOCHART", rc->name);
1531 else if (unlikely(rc->rrdcalc_options & RRDCALC_OPTION_DB_NAN))
1532 rc->rrdcalc_options &= ~RRDCALC_OPTION_DB_NAN;
1534 debug(D_HEALTH, "Health alarm '%s.%s': database lookup gave value "
1535 CALCULATED_NUMBER_FORMAT, rc->chart?rc->chart:"NOCHART", rc->name, rc->value);
1538 if(unlikely(rc->calculation)) {
1539 if (unlikely(!expression_evaluate(rc->calculation))) {
1540 // calculation failed
1544 if (unlikely(!(rc->rrdcalc_options & RRDCALC_OPTION_CALC_ERROR))) {
1545 rc->rrdcalc_options |= RRDCALC_OPTION_CALC_ERROR;
1546 error("Health alarm '%s.%s': failed to evaluate calculation with error: %s",
1547 rc->chart?rc->chart:"NOCHART", rc->name, buffer_tostring(rc->calculation->error_msg));
1551 if (unlikely(rc->rrdcalc_options & RRDCALC_OPTION_CALC_ERROR))
1552 rc->rrdcalc_options &= ~RRDCALC_OPTION_CALC_ERROR;
1554 debug(D_HEALTH, "Health alarm '%s.%s': calculation expression gave value "
1555 CALCULATED_NUMBER_FORMAT
1556 ": %s (source: %s)",
1557 rc->chart?rc->chart:"NOCHART", rc->name,
1558 rc->calculation->result,
1559 buffer_tostring(rc->calculation->error_msg),
1563 rc->value = rc->calculation->result;
1567 rrdhost_unlock(&localhost);
1570 rrdhost_rdlock(&localhost);
1572 for (rc = localhost.alarms; rc; rc = rc->next) {
1573 if (unlikely(!rrdcalc_isrunnable(rc, now, &next_run)))
1576 if(unlikely(rc->warning)) {
1577 if(unlikely(!expression_evaluate(rc->warning))) {
1578 // calculation failed
1579 if (unlikely(!(rc->rrdcalc_options & RRDCALC_OPTION_WARN_ERROR))) {
1580 rc->rrdcalc_options |= RRDCALC_OPTION_WARN_ERROR;
1581 error("Health alarm '%s.%s': warning expression failed with error: %s",
1582 rc->chart?rc->chart:"NOCHART", rc->name, buffer_tostring(rc->warning->error_msg));
1586 if(unlikely(rc->rrdcalc_options & RRDCALC_OPTION_WARN_ERROR))
1587 rc->rrdcalc_options &= ~RRDCALC_OPTION_WARN_ERROR;
1589 debug(D_HEALTH, "Health alarm '%s.%s': warning expression gave value "
1590 CALCULATED_NUMBER_FORMAT
1591 ": %s (source: %s)",
1592 rc->chart?rc->chart:"NOCHART", rc->name,
1593 rc->warning->result,
1594 buffer_tostring(rc->warning->error_msg),
1599 rrdcalc_check_warning_event(rc);
1602 if(unlikely(rc->critical)) {
1603 if(unlikely(!expression_evaluate(rc->critical))) {
1604 // calculation failed
1605 if (unlikely(!(rc->rrdcalc_options & RRDCALC_OPTION_CRIT_ERROR))) {
1606 rc->rrdcalc_options |= RRDCALC_OPTION_CRIT_ERROR;
1607 error("Health alarm '%s.%s': critical expression failed with error: %s",
1608 rc->chart?rc->chart:"NOCHART", rc->name, buffer_tostring(rc->critical->error_msg));
1612 if(unlikely(rc->rrdcalc_options & RRDCALC_OPTION_CRIT_ERROR))
1613 rc->rrdcalc_options &= ~RRDCALC_OPTION_CRIT_ERROR;
1615 debug(D_HEALTH, "Health alarm '%s.%s': critical expression gave value "
1616 CALCULATED_NUMBER_FORMAT
1617 ": %s (source: %s)",
1618 rc->chart?rc->chart:"NOCHART", rc->name,
1619 rc->critical->result,
1620 buffer_tostring(rc->critical->error_msg),
1625 rrdcalc_check_critical_event(rc);
1628 rc->last_updated = now;
1629 rc->next_update = now + rc->update_every;
1631 if (next_run > rc->next_update)
1632 next_run = rc->next_update;
1635 rrdhost_unlock(&localhost);
1639 if (unlikely(pthread_setcancelstate(oldstate, NULL) != 0))
1640 error("Cannot set pthread cancel state to RESTORE (%d).", oldstate);
1642 debug(D_HEALTH, "Health monitoring iteration no %u done. Next iteration in %d secs",
1643 loop, (int) (next_run - now));
1645 sleep_usec(1000000 * (unsigned long long) (next_run - now));
1650 info("HEALTH thread exiting");