3 #define RRDVAR_MAX_LENGTH 1024
5 static const char *health_default_exec = PLUGINS_DIR "/alarm-email.sh";
6 static const char *health_default_recipient = "root";
7 int health_enabled = 1;
9 // ----------------------------------------------------------------------------
12 static inline int rrdvar_fix_name(char *variable) {
15 if (!isalnum(*variable) && *variable != '.' && *variable != '_') {
26 int rrdvar_compare(void* a, void* b) {
27 if(((RRDVAR *)a)->hash < ((RRDVAR *)b)->hash) return -1;
28 else if(((RRDVAR *)a)->hash > ((RRDVAR *)b)->hash) return 1;
29 else return strcmp(((RRDVAR *)a)->name, ((RRDVAR *)b)->name);
32 static inline RRDVAR *rrdvar_index_add(avl_tree_lock *tree, RRDVAR *rv) {
33 RRDVAR *ret = (RRDVAR *)avl_insert_lock(tree, (avl *)(rv));
35 debug(D_VARIABLES, "Request to insert RRDVAR '%s' into index failed. Already exists.", rv->name);
40 static inline RRDVAR *rrdvar_index_del(avl_tree_lock *tree, RRDVAR *rv) {
41 RRDVAR *ret = (RRDVAR *)avl_remove_lock(tree, (avl *)(rv));
43 error("Request to remove RRDVAR '%s' from index failed. Not Found.", rv->name);
48 static inline RRDVAR *rrdvar_index_find(avl_tree_lock *tree, const char *name, uint32_t hash) {
50 tmp.name = (char *)name;
51 tmp.hash = (hash)?hash:simple_hash(tmp.name);
53 return (RRDVAR *)avl_search_lock(tree, (avl *)&tmp);
56 static inline void rrdvar_free(RRDHOST *host, avl_tree_lock *tree, RRDVAR *rv) {
62 rrdvar_index_del(tree, rv);
68 static inline RRDVAR *rrdvar_create_and_index(const char *scope, avl_tree_lock *tree, const char *name, int type, calculated_number *value) {
69 char *variable = strdupz(name);
70 rrdvar_fix_name(variable);
71 uint32_t hash = simple_hash(variable);
73 RRDVAR *rv = rrdvar_index_find(tree, variable, hash);
75 debug(D_VARIABLES, "Variable '%s' not found in scope '%s'. Creating a new one.", variable, scope);
77 rv = callocz(1, sizeof(RRDVAR));
83 RRDVAR *ret = rrdvar_index_add(tree, rv);
84 if(unlikely(ret != rv)) {
85 debug(D_VARIABLES, "Variable '%s' in scope '%s' already exists", variable, scope);
86 rrdvar_free(NULL, NULL, rv);
90 debug(D_VARIABLES, "Variable '%s' created in scope '%s'", variable, scope);
101 // ----------------------------------------------------------------------------
104 calculated_number rrdvar2number(RRDVAR *rv) {
106 case RRDVAR_TYPE_CALCULATED: {
107 calculated_number *n = (calculated_number *)rv->value;
111 case RRDVAR_TYPE_TIME_T: {
112 time_t *n = (time_t *)rv->value;
116 case RRDVAR_TYPE_COLLECTED: {
117 collected_number *n = (collected_number *)rv->value;
121 case RRDVAR_TYPE_TOTAL: {
122 total_number *n = (total_number *)rv->value;
126 case RRDVAR_TYPE_INT: {
127 int *n = (int *)rv->value;
132 error("I don't know how to convert RRDVAR type %d to calculated_number", rv->type);
137 void dump_variable(void *data) {
138 RRDVAR *rv = (RRDVAR *)data;
139 debug(D_HEALTH, "%50s : %20.5Lf", rv->name, rrdvar2number(rv));
142 int health_variable_lookup(const char *variable, uint32_t hash, RRDCALC *rc, calculated_number *result) {
143 RRDSET *st = rc->rrdset;
148 rv = rrdvar_index_find(&st->variables_root_index, variable, hash);
150 *result = rrdvar2number(rv);
154 rv = rrdvar_index_find(&st->rrdfamily->variables_root_index, variable, hash);
156 *result = rrdvar2number(rv);
160 rv = rrdvar_index_find(&st->rrdhost->variables_root_index, variable, hash);
162 *result = rrdvar2number(rv);
166 debug(D_HEALTH, "Available local chart '%s' variables:", st->id);
167 avl_traverse_lock(&st->variables_root_index, dump_variable);
169 debug(D_HEALTH, "Available family '%s' variables:", st->rrdfamily->family);
170 avl_traverse_lock(&st->rrdfamily->variables_root_index, dump_variable);
172 debug(D_HEALTH, "Available host '%s' variables:", st->rrdhost->hostname);
173 avl_traverse_lock(&st->rrdhost->variables_root_index, dump_variable);
178 // ----------------------------------------------------------------------------
179 // RRDSETVAR management
181 RRDSETVAR *rrdsetvar_create(RRDSET *st, const char *variable, int type, void *value, uint32_t options) {
182 debug(D_VARIABLES, "RRDVARSET create for chart id '%s' name '%s' with variable name '%s'", st->id, st->name, variable);
183 RRDSETVAR *rs = (RRDSETVAR *)callocz(1, sizeof(RRDSETVAR));
185 char buffer[RRDVAR_MAX_LENGTH + 1];
186 snprintfz(buffer, RRDVAR_MAX_LENGTH, "%s.%s", st->id, variable);
187 rs->fullid = strdupz(buffer);
189 snprintfz(buffer, RRDVAR_MAX_LENGTH, "%s.%s", st->name, variable);
190 rs->fullname = strdupz(buffer);
192 rs->variable = strdupz(variable);
196 rs->options = options;
199 rs->local = rrdvar_create_and_index("local", &st->variables_root_index, rs->variable, rs->type, rs->value);
200 rs->family = rrdvar_create_and_index("family", &st->rrdfamily->variables_root_index, rs->fullid, rs->type, rs->value);
201 rs->host = rrdvar_create_and_index("host", &st->rrdhost->variables_root_index, rs->fullid, rs->type, rs->value);
202 rs->family_name = rrdvar_create_and_index("family", &st->rrdfamily->variables_root_index, rs->fullname, rs->type, rs->value);
203 rs->host_name = rrdvar_create_and_index("host", &st->rrdhost->variables_root_index, rs->fullname, rs->type, rs->value);
205 rs->next = st->variables;
211 void rrdsetvar_rename_all(RRDSET *st) {
212 debug(D_VARIABLES, "RRDSETVAR rename for chart id '%s' name '%s'", st->id, st->name);
214 // only these 2 can change name
218 char buffer[RRDVAR_MAX_LENGTH + 1];
219 RRDSETVAR *rs, *next = st->variables;
223 snprintfz(buffer, RRDVAR_MAX_LENGTH, "%s.%s", st->name, rs->variable);
225 if (strcmp(buffer, rs->fullname)) {
227 rrdvar_free(st->rrdhost, &st->rrdfamily->variables_root_index, rs->family_name);
228 rrdvar_free(st->rrdhost, &st->rrdhost->variables_root_index, rs->host_name);
231 rs->fullname = strdupz(st->name);
232 rs->family_name = rrdvar_create_and_index("family", &st->rrdfamily->variables_root_index, rs->fullname, rs->type, rs->value);
233 rs->host_name = rrdvar_create_and_index("host", &st->rrdhost->variables_root_index, rs->fullname, rs->type, rs->value);
237 rrdsetcalc_link_matching(st);
240 void rrdsetvar_free(RRDSETVAR *rs) {
241 RRDSET *st = rs->rrdset;
242 debug(D_VARIABLES, "RRDSETVAR free for chart id '%s' name '%s', variable '%s'", st->id, st->name, rs->variable);
244 rrdvar_free(st->rrdhost, &st->variables_root_index, rs->local);
245 rrdvar_free(st->rrdhost, &st->rrdfamily->variables_root_index, rs->family);
246 rrdvar_free(st->rrdhost, &st->rrdhost->variables_root_index, rs->host);
247 rrdvar_free(st->rrdhost, &st->rrdfamily->variables_root_index, rs->family_name);
248 rrdvar_free(st->rrdhost, &st->rrdhost->variables_root_index, rs->host_name);
250 if(st->variables == rs) {
251 st->variables = rs->next;
255 for (t = st->variables; t && t->next != rs; t = t->next);
256 if(!t) error("RRDSETVAR '%s' not found in chart '%s' variables linked list", rs->fullname, st->id);
257 else t->next = rs->next;
266 // ----------------------------------------------------------------------------
267 // RRDDIMVAR management
269 #define RRDDIMVAR_ID_MAX 1024
271 RRDDIMVAR *rrddimvar_create(RRDDIM *rd, int type, const char *prefix, const char *suffix, void *value, uint32_t options) {
272 RRDSET *st = rd->rrdset;
274 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:"");
276 if(!prefix) prefix = "";
277 if(!suffix) suffix = "";
279 char buffer[RRDDIMVAR_ID_MAX + 1];
280 RRDDIMVAR *rs = (RRDDIMVAR *)callocz(1, sizeof(RRDDIMVAR));
282 rs->prefix = strdupz(prefix);
283 rs->suffix = strdupz(suffix);
285 snprintfz(buffer, RRDDIMVAR_ID_MAX, "%s%s%s", rs->prefix, rd->id, rs->suffix);
286 rs->id = strdupz(buffer);
288 snprintfz(buffer, RRDDIMVAR_ID_MAX, "%s%s%s", rs->prefix, rd->name, rs->suffix);
289 rs->name = strdupz(buffer);
291 snprintfz(buffer, RRDDIMVAR_ID_MAX, "%s.%s", rd->rrdset->id, rs->id);
292 rs->fullidid = strdupz(buffer);
294 snprintfz(buffer, RRDDIMVAR_ID_MAX, "%s.%s", rd->rrdset->id, rs->name);
295 rs->fullidname = strdupz(buffer);
297 snprintfz(buffer, RRDDIMVAR_ID_MAX, "%s.%s", rd->rrdset->name, rs->id);
298 rs->fullnameid = strdupz(buffer);
300 snprintfz(buffer, RRDDIMVAR_ID_MAX, "%s.%s", rd->rrdset->name, rs->name);
301 rs->fullnamename = strdupz(buffer);
305 rs->options = options;
308 rs->local_id = rrdvar_create_and_index("local", &st->variables_root_index, rs->id, rs->type, rs->value);
309 rs->local_name = rrdvar_create_and_index("local", &st->variables_root_index, rs->name, rs->type, rs->value);
311 rs->family_id = rrdvar_create_and_index("family", &st->rrdfamily->variables_root_index, rs->id, rs->type, rs->value);
312 rs->family_name = rrdvar_create_and_index("family", &st->rrdfamily->variables_root_index, rs->name, rs->type, rs->value);
314 rs->host_fullidid = rrdvar_create_and_index("host", &st->rrdhost->variables_root_index, rs->fullidid, rs->type, rs->value);
315 rs->host_fullidname = rrdvar_create_and_index("host", &st->rrdhost->variables_root_index, rs->fullidname, rs->type, rs->value);
316 rs->host_fullnameid = rrdvar_create_and_index("host", &st->rrdhost->variables_root_index, rs->fullnameid, rs->type, rs->value);
317 rs->host_fullnamename = rrdvar_create_and_index("host", &st->rrdhost->variables_root_index, rs->fullnamename, rs->type, rs->value);
319 rs->next = rd->variables;
325 void rrddimvar_rename_all(RRDDIM *rd) {
326 RRDSET *st = rd->rrdset;
327 debug(D_VARIABLES, "RRDDIMSET rename for chart id '%s' name '%s', dimension id '%s', name '%s'", st->id, st->name, rd->id, rd->name);
329 RRDDIMVAR *rs, *next = rd->variables;
333 if (strcmp(rd->name, rs->name)) {
334 char buffer[RRDDIMVAR_ID_MAX + 1];
338 rrdvar_free(st->rrdhost, &st->variables_root_index, rs->local_name);
340 snprintfz(buffer, RRDDIMVAR_ID_MAX, "%s%s%s", rs->prefix, rd->name, rs->suffix);
341 rs->name = strdupz(buffer);
342 rs->local_name = rrdvar_create_and_index("local", &st->variables_root_index, rs->name, rs->type, rs->value);
344 rrdvar_free(st->rrdhost, &st->rrdhost->variables_root_index, rs->host_fullidname);
345 freez(rs->fullidname);
346 snprintfz(buffer, RRDDIMVAR_ID_MAX, "%s.%s", st->id, rs->name);
347 rs->fullidname = strdupz(buffer);
348 rs->host_fullidname = rrdvar_create_and_index("host", &st->rrdhost->variables_root_index,
349 rs->fullidname, rs->type, rs->value);
352 rrdvar_free(st->rrdhost, &st->rrdhost->variables_root_index, rs->host_fullnameid);
353 freez(rs->fullnameid);
354 snprintfz(buffer, RRDDIMVAR_ID_MAX, "%s.%s", st->name, rs->id);
355 rs->fullnameid = strdupz(buffer);
356 rs->host_fullnameid = rrdvar_create_and_index("host", &st->rrdhost->variables_root_index,
357 rs->fullnameid, rs->type, rs->value);
360 rrdvar_free(st->rrdhost, &st->rrdhost->variables_root_index, rs->host_fullnamename);
361 freez(rs->fullnamename);
362 snprintfz(buffer, RRDDIMVAR_ID_MAX, "%s.%s", st->name, rs->name);
363 rs->fullnamename = strdupz(buffer);
364 rs->host_fullnamename = rrdvar_create_and_index("host", &st->rrdhost->variables_root_index,
365 rs->fullnamename, rs->type, rs->value);
370 void rrddimvar_free(RRDDIMVAR *rs) {
371 RRDDIM *rd = rs->rrddim;
372 RRDSET *st = rd->rrdset;
373 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);
375 rrdvar_free(st->rrdhost, &st->variables_root_index, rs->local_id);
376 rrdvar_free(st->rrdhost, &st->variables_root_index, rs->local_name);
378 rrdvar_free(st->rrdhost, &st->rrdfamily->variables_root_index, rs->family_id);
379 rrdvar_free(st->rrdhost, &st->rrdfamily->variables_root_index, rs->family_name);
381 rrdvar_free(st->rrdhost, &st->rrdhost->variables_root_index, rs->host_fullidid);
382 rrdvar_free(st->rrdhost, &st->rrdhost->variables_root_index, rs->host_fullidname);
383 rrdvar_free(st->rrdhost, &st->rrdhost->variables_root_index, rs->host_fullnameid);
384 rrdvar_free(st->rrdhost, &st->rrdhost->variables_root_index, rs->host_fullnamename);
386 if(rd->variables == rs) {
387 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);
388 rd->variables = rs->next;
391 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);
393 for (t = rd->variables; t && t->next != rs; t = t->next) ;
394 if(!t) error("RRDDIMVAR '%s' not found in dimension '%s/%s' variables linked list", rs->name, st->id, rd->id);
395 else t->next = rs->next;
403 freez(rs->fullidname);
404 freez(rs->fullnameid);
405 freez(rs->fullnamename);
409 // ----------------------------------------------------------------------------
410 // RRDCALC management
412 static inline const char *rrdcalc_status2string(int status) {
414 case RRDCALC_STATUS_UNINITIALIZED:
415 return "UNINITIALIZED";
417 case RRDCALC_STATUS_UNDEFINED:
420 case RRDCALC_STATUS_CLEAR:
423 case RRDCALC_STATUS_RAISED:
426 case RRDCALC_STATUS_WARNING:
429 case RRDCALC_STATUS_CRITICAL:
437 static void rrdsetcalc_link(RRDSET *st, RRDCALC *rc) {
438 debug(D_HEALTH, "Health linking alarm '%s.%s' to chart '%s' of host '%s'", rc->chart?rc->chart:"NOCHART", rc->name, st->id, st->rrdhost->hostname);
440 rc->last_status_change = time(NULL);
443 rc->rrdset_next = st->alarms;
444 rc->rrdset_prev = NULL;
447 rc->rrdset_next->rrdset_prev = rc;
451 if(rc->update_every < rc->rrdset->update_every) {
452 error("Health alarm '%s.%s' has update every %d, less than chart update every %d. Setting alarm update frequency to %d.", rc->rrdset->id, rc->name, rc->update_every, rc->rrdset->update_every, rc->rrdset->update_every);
453 rc->update_every = rc->rrdset->update_every;
456 if(!isnan(rc->green) && isnan(st->green)) {
457 debug(D_HEALTH, "Health alarm '%s.%s' green threshold set from %Lf to %Lf.", rc->rrdset->id, rc->name, rc->rrdset->green, rc->green);
458 st->green = rc->green;
461 if(!isnan(rc->red) && isnan(st->red)) {
462 debug(D_HEALTH, "Health alarm '%s.%s' red threshold set from %Lf to %Lf.", rc->rrdset->id, rc->name, rc->rrdset->red, rc->red);
466 rc->local = rrdvar_create_and_index("local", &st->variables_root_index, rc->name, RRDVAR_TYPE_CALCULATED, &rc->value);
467 rc->family = rrdvar_create_and_index("family", &st->rrdfamily->variables_root_index, rc->name, RRDVAR_TYPE_CALCULATED, &rc->value);
469 char fullname[RRDVAR_MAX_LENGTH + 1];
470 snprintfz(fullname, RRDVAR_MAX_LENGTH, "%s.%s", st->id, rc->name);
471 rc->hostid = rrdvar_create_and_index("host", &st->rrdhost->variables_root_index, fullname, RRDVAR_TYPE_CALCULATED, &rc->value);
473 snprintfz(fullname, RRDVAR_MAX_LENGTH, "%s.%s", st->name, rc->name);
474 rc->hostname = rrdvar_create_and_index("host", &st->rrdhost->variables_root_index, fullname, RRDVAR_TYPE_CALCULATED, &rc->value);
476 if(!rc->units) rc->units = strdupz(st->units);
479 static inline int rrdcalc_is_matching_this_rrdset(RRDCALC *rc, RRDSET *st) {
480 if( (rc->hash_chart == st->hash && !strcmp(rc->chart, st->id)) ||
481 (rc->hash_chart == st->hash_name && !strcmp(rc->chart, st->name)))
487 // this has to be called while the RRDHOST is locked
488 inline void rrdsetcalc_link_matching(RRDSET *st) {
489 // debug(D_HEALTH, "find matching alarms for chart '%s'", st->id);
492 for(rc = st->rrdhost->alarms; rc ; rc = rc->next) {
493 if(unlikely(rc->rrdset))
496 if(unlikely(rrdcalc_is_matching_this_rrdset(rc, st)))
497 rrdsetcalc_link(st, rc);
501 // this has to be called while the RRDHOST is locked
502 inline void rrdsetcalc_unlink(RRDCALC *rc) {
503 RRDSET *st = rc->rrdset;
506 debug(D_HEALTH, "Requested to unlink RRDCALC '%s.%s' which is not linked to any RRDSET", rc->chart?rc->chart:"NOCHART", rc->name);
507 error("Requested to unlink RRDCALC '%s.%s' which is not linked to any RRDSET", rc->chart?rc->chart:"NOCHART", rc->name);
511 RRDHOST *host = st->rrdhost;
513 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);
517 rc->rrdset_prev->rrdset_next = rc->rrdset_next;
520 rc->rrdset_next->rrdset_prev = rc->rrdset_prev;
523 st->alarms = rc->rrdset_next;
525 rc->rrdset_prev = rc->rrdset_next = NULL;
527 rrdvar_free(st->rrdhost, &st->variables_root_index, rc->local);
530 rrdvar_free(st->rrdhost, &st->rrdfamily->variables_root_index, rc->family);
533 rrdvar_free(st->rrdhost, &st->rrdhost->variables_root_index, rc->hostid);
536 rrdvar_free(st->rrdhost, &st->rrdhost->variables_root_index, rc->hostname);
541 // RRDCALC will remain in RRDHOST
542 // so that if the matching chart is found in the future
543 // it will be applied automatically
546 RRDCALC *rrdcalc_find(RRDSET *st, const char *name) {
548 uint32_t hash = simple_hash(name);
550 for( rc = st->alarms; rc ; rc = rc->rrdset_next ) {
551 if(unlikely(rc->hash == hash && !strcmp(rc->name, name)))
558 static inline int rrdcalc_exists(RRDHOST *host, const char *chart, const char *name, uint32_t hash_chart, uint32_t hash_name) {
561 if(unlikely(!chart)) {
562 error("attempt to find RRDCALC '%s' without giving a chart name", name);
566 if(unlikely(!hash_chart)) hash_chart = simple_hash(chart);
567 if(unlikely(!hash_name)) hash_name = simple_hash(name);
569 // make sure it does not already exist
570 for(rc = host->alarms; rc ; rc = rc->next) {
571 if (unlikely(rc->chart && rc->hash == hash_name && rc->hash_chart == hash_chart && !strcmp(name, rc->name) && !strcmp(chart, rc->chart))) {
572 debug(D_HEALTH, "Health alarm '%s.%s' already exists in host '%s'.", chart, name, host->hostname);
573 error("Health alarm '%s.%s' already exists in host '%s'.", chart, name, host->hostname);
581 static inline void rrdcalc_create_part2(RRDHOST *host, RRDCALC *rc) {
582 rrdhost_check_rdlock(host);
584 if(rc->calculation) {
585 rc->calculation->this = &rc->value;
586 rc->calculation->after = &rc->db_after;
587 rc->calculation->before = &rc->db_before;
588 rc->calculation->rrdcalc = rc;
592 rc->warning->this = &rc->value;
593 rc->warning->after = &rc->db_after;
594 rc->warning->before = &rc->db_before;
595 rc->warning->rrdcalc = rc;
599 rc->critical->this = &rc->value;
600 rc->critical->after = &rc->db_after;
601 rc->critical->before = &rc->db_before;
602 rc->critical->rrdcalc = rc;
605 // link it to the host
606 if(likely(host->alarms)) {
609 for(t = host->alarms; t && t->next ; t = t->next) ;
616 // link it to its chart
618 for(st = host->rrdset_root; st ; st = st->next) {
619 if(rrdcalc_is_matching_this_rrdset(rc, st)) {
620 rrdsetcalc_link(st, rc);
626 static inline RRDCALC *rrdcalc_create(RRDHOST *host, const char *name, const char *chart, const char *dimensions,
627 const char *units, const char *info,
628 int group_method, int after, int before, int update_every, uint32_t options,
629 calculated_number green, calculated_number red,
630 const char *exec, const char *recipient, const char *source,
631 const char *calc, const char *warn, const char *crit) {
633 debug(D_HEALTH, "Health creating dynamic alarm (from template) '%s.%s'", chart, name);
635 if(rrdcalc_exists(host, chart, name, 0, 0))
638 RRDCALC *rc = callocz(1, sizeof(RRDCALC));
639 rc->id = host->health_log.next_alarm_id++;
640 rc->next_event_id = 1;
641 rc->name = strdupz(name);
642 rc->hash = simple_hash(rc->name);
643 rc->chart = strdupz(chart);
644 rc->hash_chart = simple_hash(rc->chart);
646 if(dimensions) rc->dimensions = strdupz(dimensions);
653 rc->group = group_method;
656 rc->update_every = update_every;
657 rc->options = options;
659 if(exec) rc->exec = strdupz(exec);
660 if(recipient) rc->recipient = strdupz(recipient);
661 if(source) rc->source = strdupz(source);
662 if(units) rc->units = strdupz(units);
663 if(info) rc->info = strdupz(info);
666 rc->calculation = expression_parse(calc, NULL, NULL);
668 error("Health alarm '%s.%s': failed to parse calculation expression '%s'", chart, name, calc);
671 rc->warning = expression_parse(warn, NULL, NULL);
673 error("Health alarm '%s.%s': failed to re-parse warning expression '%s'", chart, name, warn);
676 rc->critical = expression_parse(crit, NULL, NULL);
678 error("Health alarm '%s.%s': failed to re-parse critical expression '%s'", chart, name, crit);
681 debug(D_HEALTH, "Health runtime added alarm '%s.%s': exec '%s', recipient '%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",
682 (rc->chart)?rc->chart:"NOCHART",
684 (rc->exec)?rc->exec:"DEFAULT",
685 (rc->recipient)?rc->recipient:"DEFAULT",
692 (rc->dimensions)?rc->dimensions:"NONE",
694 (rc->calculation)?rc->calculation->parsed_as:"NONE",
695 (rc->warning)?rc->warning->parsed_as:"NONE",
696 (rc->critical)?rc->critical->parsed_as:"NONE",
700 rrdcalc_create_part2(host, rc);
704 void rrdcalc_free(RRDHOST *host, RRDCALC *rc) {
707 debug(D_HEALTH, "Health removing alarm '%s.%s' of host '%s'", rc->chart?rc->chart:"NOCHART", rc->name, host->hostname);
709 // unlink it from RRDSET
710 if(rc->rrdset) rrdsetcalc_unlink(rc);
712 // unlink it from RRDHOST
713 if(unlikely(rc == host->alarms))
714 host->alarms = rc->next;
716 else if(likely(host->alarms)) {
717 RRDCALC *t, *last = host->alarms;
718 for(t = last->next; t && t != rc; last = t, t = t->next) ;
719 if(last && last->next == rc)
720 last->next = rc->next;
722 error("Cannot unlink alarm '%s.%s' from host '%s': not found", rc->chart?rc->chart:"NOCHART", rc->name, host->hostname);
725 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);
727 expression_free(rc->calculation);
728 expression_free(rc->warning);
729 expression_free(rc->critical);
734 freez(rc->dimensions);
736 freez(rc->recipient);
743 // ----------------------------------------------------------------------------
744 // RRDCALCTEMPLATE management
746 void rrdcalctemplate_link_matching(RRDSET *st) {
749 for(rt = st->rrdhost->templates; rt ; rt = rt->next) {
750 if(rt->hash_context == st->hash_context && !strcmp(rt->context, st->context)) {
751 RRDCALC *rc = rrdcalc_create(st->rrdhost, rt->name, st->id,
752 rt->dimensions, rt->units, rt->info, rt->group, rt->after, rt->before, rt->update_every, rt->options,
754 rt->exec, rt->recipient, rt->source,
755 (rt->calculation)?rt->calculation->source:NULL,
756 (rt->warning)?rt->warning->source:NULL,
757 (rt->critical)?rt->critical->source:NULL);
760 error("Health tried to create alarm from template '%s', but it failed", rt->name);
762 #ifdef NETDATA_INTERNAL_CHECKS
763 else if(rc->rrdset != st)
764 error("Health alarm '%s.%s' should be linked to chart '%s', but it is not", rc->chart?rc->chart:"NOCHART", rc->name, st->id);
770 static inline void rrdcalctemplate_free(RRDHOST *host, RRDCALCTEMPLATE *rt) {
771 debug(D_HEALTH, "Health removing template '%s' of host '%s'", rt->name, host->hostname);
773 if(host->templates) {
774 if(host->templates == rt) {
775 host->templates = rt->next;
778 RRDCALCTEMPLATE *t, *last = host->templates;
779 for (t = last->next; t && t != rt; last = t, t = t->next ) ;
780 if(last && last->next == rt) {
781 last->next = rt->next;
785 error("Cannot find RRDCALCTEMPLATE '%s' linked in host '%s'", rt->name, host->hostname);
789 expression_free(rt->calculation);
790 expression_free(rt->warning);
791 expression_free(rt->critical);
795 freez(rt->recipient);
800 freez(rt->dimensions);
804 // ----------------------------------------------------------------------------
805 // load health configuration
807 #define HEALTH_CONF_MAX_LINE 4096
809 #define HEALTH_ALARM_KEY "alarm"
810 #define HEALTH_TEMPLATE_KEY "template"
811 #define HEALTH_ON_KEY "on"
812 #define HEALTH_LOOKUP_KEY "lookup"
813 #define HEALTH_CALC_KEY "calc"
814 #define HEALTH_EVERY_KEY "every"
815 #define HEALTH_GREEN_KEY "green"
816 #define HEALTH_RED_KEY "red"
817 #define HEALTH_WARN_KEY "warn"
818 #define HEALTH_CRIT_KEY "crit"
819 #define HEALTH_EXEC_KEY "exec"
820 #define HEALTH_RECIPIENT_KEY "to"
821 #define HEALTH_UNITS_KEY "units"
822 #define HEALTH_INFO_KEY "info"
824 static inline int rrdcalc_add_alarm_from_config(RRDHOST *host, RRDCALC *rc) {
826 error("Health configuration for alarm '%s' does not have a chart", rc->name);
830 if(!rc->update_every) {
831 error("Health configuration for alarm '%s.%s' has no frequency (parameter 'every'). Ignoring it.", rc->chart?rc->chart:"NOCHART", rc->name);
835 if(!RRDCALC_HAS_DB_LOOKUP(rc) && !rc->warning && !rc->critical) {
836 error("Health configuration for alarm '%s.%s' is useless (no calculation, no warning and no critical evaluation)", rc->chart?rc->chart:"NOCHART", rc->name);
840 if (rrdcalc_exists(host, rc->chart, rc->name, rc->hash_chart, rc->hash))
843 debug(D_HEALTH, "Health configuration adding alarm '%s.%s': exec '%s', recipient '%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",
844 rc->chart?rc->chart:"NOCHART",
846 (rc->exec)?rc->exec:"DEFAULT",
847 (rc->recipient)?rc->recipient:"DEFAULT",
854 (rc->dimensions)?rc->dimensions:"NONE",
856 (rc->calculation)?rc->calculation->parsed_as:"NONE",
857 (rc->warning)?rc->warning->parsed_as:"NONE",
858 (rc->critical)?rc->critical->parsed_as:"NONE",
862 rrdcalc_create_part2(host, rc);
866 static inline int rrdcalctemplate_add_template_from_config(RRDHOST *host, RRDCALCTEMPLATE *rt) {
867 if(unlikely(!rt->context)) {
868 error("Health configuration for template '%s' does not have a context", rt->name);
872 if(unlikely(!rt->update_every)) {
873 error("Health configuration for template '%s' has no frequency (parameter 'every'). Ignoring it.", rt->name);
877 if(unlikely(!RRDCALCTEMPLATE_HAS_CALCULATION(rt) && !rt->warning && !rt->critical)) {
878 error("Health configuration for template '%s' is useless (no calculation, no warning and no critical evaluation)", rt->name);
882 RRDCALCTEMPLATE *t, *last = NULL;
883 for (t = host->templates; t ; last = t, t = t->next) {
884 if(unlikely(t->hash_name == rt->hash_name && !strcmp(t->name, rt->name))) {
885 error("Health configuration template '%s' already exists for host '%s'.", rt->name, host->hostname);
890 debug(D_HEALTH, "Health configuration adding template '%s': context '%s', exec '%s', recipient '%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'",
892 (rt->context)?rt->context:"NONE",
893 (rt->exec)?rt->exec:"DEFAULT",
894 (rt->recipient)?rt->recipient:"DEFAULT",
901 (rt->dimensions)?rt->dimensions:"NONE",
903 (rt->calculation)?rt->calculation->parsed_as:"NONE",
904 (rt->warning)?rt->warning->parsed_as:"NONE",
905 (rt->critical)?rt->critical->parsed_as:"NONE",
913 rt->next = host->templates;
914 host->templates = rt;
920 static inline int health_parse_duration(char *string, int *result) {
921 // make sure it is a number
922 if(!*string || !(isdigit(*string) || *string == '+' || *string == '-')) {
928 calculated_number n = strtold(string, &e);
932 *result = (int) (n * 86400 * 365);
935 *result = (int) (n * 86400 * 30);
938 *result = (int) (n * 86400 * 7);
941 *result = (int) (n * 86400);
944 *result = (int) (n * 3600);
947 *result = (int) (n * 60);
962 static inline int health_parse_db_lookup(
963 size_t line, const char *path, const char *file, char *string,
964 int *group_method, int *after, int *before, int *every,
965 uint32_t *options, char **dimensions
967 debug(D_HEALTH, "Health configuration parsing database lookup %zu@%s/%s: %s", line, path, file, string);
969 if(*dimensions) freez(*dimensions);
976 char *s = string, *key;
978 // first is the group method
980 while(*s && !isspace(*s)) s++;
981 while(*s && isspace(*s)) *s++ = '\0';
983 error("Health configuration invalid chart calculation at line %zu of file '%s/%s': expected group method followed by the 'after' time, but got '%s'",
984 line, path, file, key);
988 if((*group_method = web_client_api_request_v1_data_group(key, -1)) == -1) {
989 error("Health configuration at line %zu of file '%s/%s': invalid group method '%s'",
990 line, path, file, key);
994 // then is the 'after' time
996 while(*s && !isspace(*s)) s++;
997 while(*s && isspace(*s)) *s++ = '\0';
999 if(!health_parse_duration(key, after)) {
1000 error("Health configuration at line %zu of file '%s/%s': invalid duration '%s' after group method",
1001 line, path, file, key);
1006 *every = abs(*after);
1008 // now we may have optional parameters
1011 while(*s && !isspace(*s)) s++;
1012 while(*s && isspace(*s)) *s++ = '\0';
1015 if(!strcasecmp(key, "at")) {
1017 while(*s && !isspace(*s)) s++;
1018 while(*s && isspace(*s)) *s++ = '\0';
1020 if (!health_parse_duration(value, before)) {
1021 error("Health configuration at line %zu of file '%s/%s': invalid duration '%s' for '%s' keyword",
1022 line, path, file, value, key);
1025 else if(!strcasecmp(key, HEALTH_EVERY_KEY)) {
1027 while(*s && !isspace(*s)) s++;
1028 while(*s && isspace(*s)) *s++ = '\0';
1030 if (!health_parse_duration(value, every)) {
1031 error("Health configuration at line %zu of file '%s/%s': invalid duration '%s' for '%s' keyword",
1032 line, path, file, value, key);
1035 else if(!strcasecmp(key, "absolute") || !strcasecmp(key, "abs") || !strcasecmp(key, "absolute_sum")) {
1036 *options |= RRDR_OPTION_ABSOLUTE;
1038 else if(!strcasecmp(key, "min2max")) {
1039 *options |= RRDR_OPTION_MIN2MAX;
1041 else if(!strcasecmp(key, "null2zero")) {
1042 *options |= RRDR_OPTION_NULL2ZERO;
1044 else if(!strcasecmp(key, "percentage")) {
1045 *options |= RRDR_OPTION_PERCENTAGE;
1047 else if(!strcasecmp(key, "unaligned")) {
1048 *options |= RRDR_OPTION_NOT_ALIGNED;
1050 else if(!strcasecmp(key, "of")) {
1051 if(*s && strcasecmp(s, "all"))
1052 *dimensions = strdupz(s);
1056 error("Health configuration at line %zu of file '%s/%s': unknown keyword '%s'",
1057 line, path, file, key);
1064 static inline char *health_source_file(size_t line, const char *path, const char *filename) {
1065 char buffer[FILENAME_MAX + 1];
1066 snprintfz(buffer, FILENAME_MAX, "%zu@%s/%s", line, path, filename);
1067 return strdupz(buffer);
1070 static inline void strip_quotes(char *s) {
1072 if(*s == '\'' || *s == '"') *s = ' ';
1077 int health_readfile(const char *path, const char *filename) {
1078 debug(D_HEALTH, "Health configuration reading file '%s/%s'", path, filename);
1080 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, hash_units = 0, hash_info = 0, hash_recipient = 0;
1081 char buffer[HEALTH_CONF_MAX_LINE + 1];
1083 if(unlikely(!hash_alarm)) {
1084 hash_alarm = simple_uhash(HEALTH_ALARM_KEY);
1085 hash_template = simple_uhash(HEALTH_TEMPLATE_KEY);
1086 hash_on = simple_uhash(HEALTH_ON_KEY);
1087 hash_calc = simple_uhash(HEALTH_CALC_KEY);
1088 hash_lookup = simple_uhash(HEALTH_LOOKUP_KEY);
1089 hash_green = simple_uhash(HEALTH_GREEN_KEY);
1090 hash_red = simple_uhash(HEALTH_RED_KEY);
1091 hash_warn = simple_uhash(HEALTH_WARN_KEY);
1092 hash_crit = simple_uhash(HEALTH_CRIT_KEY);
1093 hash_exec = simple_uhash(HEALTH_EXEC_KEY);
1094 hash_every = simple_uhash(HEALTH_EVERY_KEY);
1095 hash_units = simple_hash(HEALTH_UNITS_KEY);
1096 hash_info = simple_hash(HEALTH_INFO_KEY);
1097 hash_recipient = simple_hash(HEALTH_RECIPIENT_KEY);
1100 snprintfz(buffer, HEALTH_CONF_MAX_LINE, "%s/%s", path, filename);
1101 FILE *fp = fopen(buffer, "r");
1103 error("Health configuration cannot read file '%s'.", buffer);
1108 RRDCALCTEMPLATE *rt = NULL;
1110 size_t line = 0, append = 0;
1112 while((s = fgets(&buffer[append], (int)(HEALTH_CONF_MAX_LINE - append), fp)) || append) {
1113 int stop_appending = !s;
1115 // info("Line %zu of file '%s/%s': '%s'", line, path, filename, s);
1118 // info("Trimmed line %zu of file '%s/%s': '%s'", line, path, filename, s);
1121 if(!stop_appending && s[append - 1] == '\\') {
1122 s[append - 1] = ' ';
1123 append = &s[append] - buffer;
1124 if(append < HEALTH_CONF_MAX_LINE)
1127 error("Health configuration has too long muli-line at line %zu of file '%s/%s'.", line, path, filename);
1133 while(*s && *s != ':') s++;
1135 error("Health configuration has invalid line %zu of file '%s/%s'. It does not contain a ':'. Ignoring it.", line, path, filename);
1143 value = trim(value);
1146 error("Health configuration has invalid line %zu of file '%s/%s'. Keyword is empty. Ignoring it.", line, path, filename);
1151 error("Health configuration has invalid line %zu of file '%s/%s'. value is empty. Ignoring it.", line, path, filename);
1155 // info("Health file '%s/%s', key '%s', value '%s'", path, filename, key, value);
1156 uint32_t hash = simple_uhash(key);
1158 if(hash == hash_alarm && !strcasecmp(key, HEALTH_ALARM_KEY)) {
1159 if(rc && !rrdcalc_add_alarm_from_config(&localhost, rc))
1160 rrdcalc_free(&localhost, rc);
1163 if (!rrdcalctemplate_add_template_from_config(&localhost, rt))
1164 rrdcalctemplate_free(&localhost, rt);
1168 rc = callocz(1, sizeof(RRDCALC));
1169 rc->id = localhost.health_log.next_alarm_id++;
1170 rc->next_event_id = 1;
1171 rc->name = strdupz(value);
1172 rc->hash = simple_hash(rc->name);
1173 rc->source = health_source_file(line, path, filename);
1177 rc->old_value = NAN;
1179 if(rrdvar_fix_name(rc->name))
1180 error("Health configuration renamed alarm '%s' to '%s'", value, rc->name);
1182 else if(hash == hash_template && !strcasecmp(key, HEALTH_TEMPLATE_KEY)) {
1184 if(!rrdcalc_add_alarm_from_config(&localhost, rc))
1185 rrdcalc_free(&localhost, rc);
1189 if(rt && !rrdcalctemplate_add_template_from_config(&localhost, rt))
1190 rrdcalctemplate_free(&localhost, rt);
1192 rt = callocz(1, sizeof(RRDCALCTEMPLATE));
1193 rt->name = strdupz(value);
1194 rt->hash_name = simple_hash(rt->name);
1195 rt->source = health_source_file(line, path, filename);
1199 if(rrdvar_fix_name(rt->name))
1200 error("Health configuration renamed template '%s' to '%s'", value, rt->name);
1203 if(hash == hash_on && !strcasecmp(key, HEALTH_ON_KEY)) {
1205 if(strcmp(rc->chart, value))
1206 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').",
1207 line, path, filename, rc->name, key, rc->chart, value, value);
1211 rc->chart = strdupz(value);
1212 rc->hash_chart = simple_hash(rc->chart);
1214 else if(hash == hash_lookup && !strcasecmp(key, HEALTH_LOOKUP_KEY)) {
1215 health_parse_db_lookup(line, path, filename, value, &rc->group, &rc->after, &rc->before,
1217 &rc->options, &rc->dimensions);
1219 else if(hash == hash_every && !strcasecmp(key, HEALTH_EVERY_KEY)) {
1220 if(!health_parse_duration(value, &rc->update_every))
1221 info("Health configuration at line %zu of file '%s/%s' for alarm '%s' at key '%s' cannot parse duration: '%s'.",
1222 line, path, filename, rc->name, key, value);
1224 else if(hash == hash_green && !strcasecmp(key, HEALTH_GREEN_KEY)) {
1226 rc->green = strtold(value, &e);
1228 info("Health configuration at line %zu of file '%s/%s' for alarm '%s' at key '%s' leaves this string unmatched: '%s'.",
1229 line, path, filename, rc->name, key, e);
1232 else if(hash == hash_red && !strcasecmp(key, HEALTH_RED_KEY)) {
1234 rc->red = strtold(value, &e);
1236 info("Health configuration at line %zu of file '%s/%s' for alarm '%s' at key '%s' leaves this string unmatched: '%s'.",
1237 line, path, filename, rc->name, key, e);
1240 else if(hash == hash_calc && !strcasecmp(key, HEALTH_CALC_KEY)) {
1241 const char *failed_at = NULL;
1243 rc->calculation = expression_parse(value, &failed_at, &error);
1244 if(!rc->calculation) {
1245 error("Health configuration at line %zu of file '%s/%s' for alarm '%s' at key '%s' has unparse-able expression '%s': %s at '%s'",
1246 line, path, filename, rc->name, key, value, expression_strerror(error), failed_at);
1249 else if(hash == hash_warn && !strcasecmp(key, HEALTH_WARN_KEY)) {
1250 const char *failed_at = NULL;
1252 rc->warning = expression_parse(value, &failed_at, &error);
1254 error("Health configuration at line %zu of file '%s/%s' for alarm '%s' at key '%s' has unparse-able expression '%s': %s at '%s'",
1255 line, path, filename, rc->name, key, value, expression_strerror(error), failed_at);
1258 else if(hash == hash_crit && !strcasecmp(key, HEALTH_CRIT_KEY)) {
1259 const char *failed_at = NULL;
1261 rc->critical = expression_parse(value, &failed_at, &error);
1263 error("Health configuration at line %zu of file '%s/%s' for alarm '%s' at key '%s' has unparse-able expression '%s': %s at '%s'",
1264 line, path, filename, rc->name, key, value, expression_strerror(error), failed_at);
1267 else if(hash == hash_exec && !strcasecmp(key, HEALTH_EXEC_KEY)) {
1269 if(strcmp(rc->exec, value))
1270 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').",
1271 line, path, filename, rc->name, key, rc->exec, value, value);
1275 rc->exec = strdupz(value);
1277 else if(hash == hash_recipient && !strcasecmp(key, HEALTH_RECIPIENT_KEY)) {
1279 if(strcmp(rc->recipient, value))
1280 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').",
1281 line, path, filename, rc->name, key, rc->recipient, value, value);
1283 freez(rc->recipient);
1285 rc->recipient = strdupz(value);
1287 else if(hash == hash_units && !strcasecmp(key, HEALTH_UNITS_KEY)) {
1289 if(strcmp(rc->units, value))
1290 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').",
1291 line, path, filename, rc->name, key, rc->units, value, value);
1295 rc->units = strdupz(value);
1296 strip_quotes(rc->units);
1298 else if(hash == hash_info && !strcasecmp(key, HEALTH_INFO_KEY)) {
1300 if(strcmp(rc->info, value))
1301 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').",
1302 line, path, filename, rc->name, key, rc->info, value, value);
1306 rc->info = strdupz(value);
1307 strip_quotes(rc->info);
1310 error("Health configuration at line %zu of file '%s/%s' for alarm '%s' has unknown key '%s'.",
1311 line, path, filename, rc->name, key);
1315 if(hash == hash_on && !strcasecmp(key, HEALTH_ON_KEY)) {
1317 if(strcmp(rt->context, value))
1318 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').",
1319 line, path, filename, rt->name, key, rt->context, value, value);
1323 rt->context = strdupz(value);
1324 rt->hash_context = simple_hash(rt->context);
1326 else if(hash == hash_lookup && !strcasecmp(key, HEALTH_LOOKUP_KEY)) {
1327 health_parse_db_lookup(line, path, filename, value, &rt->group, &rt->after, &rt->before,
1329 &rt->options, &rt->dimensions);
1331 else if(hash == hash_every && !strcasecmp(key, HEALTH_EVERY_KEY)) {
1332 if(!health_parse_duration(value, &rt->update_every))
1333 info("Health configuration at line %zu of file '%s/%s' for template '%s' at key '%s' cannot parse duration: '%s'.",
1334 line, path, filename, rt->name, key, value);
1336 else if(hash == hash_green && !strcasecmp(key, HEALTH_GREEN_KEY)) {
1338 rt->green = strtold(value, &e);
1340 info("Health configuration at line %zu of file '%s/%s' for template '%s' at key '%s' leaves this string unmatched: '%s'.",
1341 line, path, filename, rt->name, key, e);
1344 else if(hash == hash_red && !strcasecmp(key, HEALTH_RED_KEY)) {
1346 rt->red = strtold(value, &e);
1348 info("Health configuration at line %zu of file '%s/%s' for template '%s' at key '%s' leaves this string unmatched: '%s'.",
1349 line, path, filename, rt->name, key, e);
1352 else if(hash == hash_calc && !strcasecmp(key, HEALTH_CALC_KEY)) {
1353 const char *failed_at = NULL;
1355 rt->calculation = expression_parse(value, &failed_at, &error);
1356 if(!rt->calculation) {
1357 error("Health configuration at line %zu of file '%s/%s' for template '%s' at key '%s' has unparse-able expression '%s': %s at '%s'",
1358 line, path, filename, rt->name, key, value, expression_strerror(error), failed_at);
1361 else if(hash == hash_warn && !strcasecmp(key, HEALTH_WARN_KEY)) {
1362 const char *failed_at = NULL;
1364 rt->warning = expression_parse(value, &failed_at, &error);
1366 error("Health configuration at line %zu of file '%s/%s' for template '%s' at key '%s' has unparse-able expression '%s': %s at '%s'",
1367 line, path, filename, rt->name, key, value, expression_strerror(error), failed_at);
1370 else if(hash == hash_crit && !strcasecmp(key, HEALTH_CRIT_KEY)) {
1371 const char *failed_at = NULL;
1373 rt->critical = expression_parse(value, &failed_at, &error);
1375 error("Health configuration at line %zu of file '%s/%s' for template '%s' at key '%s' has unparse-able expression '%s': %s at '%s'",
1376 line, path, filename, rt->name, key, value, expression_strerror(error), failed_at);
1379 else if(hash == hash_exec && !strcasecmp(key, HEALTH_EXEC_KEY)) {
1381 if(strcmp(rt->exec, value))
1382 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').",
1383 line, path, filename, rt->name, key, rt->exec, value, value);
1387 rt->exec = strdupz(value);
1389 else if(hash == hash_recipient && !strcasecmp(key, HEALTH_RECIPIENT_KEY)) {
1391 if(strcmp(rt->recipient, value))
1392 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').",
1393 line, path, filename, rt->name, key, rt->recipient, value, value);
1395 freez(rt->recipient);
1397 rt->recipient = strdupz(value);
1399 else if(hash == hash_units && !strcasecmp(key, HEALTH_UNITS_KEY)) {
1401 if(strcmp(rt->units, value))
1402 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').",
1403 line, path, filename, rt->name, key, rt->units, value, value);
1407 rt->units = strdupz(value);
1408 strip_quotes(rt->units);
1410 else if(hash == hash_info && !strcasecmp(key, HEALTH_INFO_KEY)) {
1412 if(strcmp(rt->info, value))
1413 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').",
1414 line, path, filename, rt->name, key, rt->info, value, value);
1418 rt->info = strdupz(value);
1419 strip_quotes(rt->info);
1422 error("Health configuration at line %zu of file '%s/%s' for template '%s' has unknown key '%s'.",
1423 line, path, filename, rt->name, key);
1427 error("Health configuration at line %zu of file '%s/%s' has unknown key '%s'. Expected either '" HEALTH_ALARM_KEY "' or '" HEALTH_TEMPLATE_KEY "'.",
1428 line, path, filename, key);
1432 if(rc && !rrdcalc_add_alarm_from_config(&localhost, rc))
1433 rrdcalc_free(&localhost, rc);
1435 if(rt && !rrdcalctemplate_add_template_from_config(&localhost, rt))
1436 rrdcalctemplate_free(&localhost, rt);
1442 void health_readdir(const char *path) {
1443 size_t pathlen = strlen(path);
1445 debug(D_HEALTH, "Health configuration reading directory '%s'", path);
1447 DIR *dir = opendir(path);
1449 error("Health configuration cannot open directory '%s'.", path);
1453 struct dirent *de = NULL;
1454 while ((de = readdir(dir))) {
1455 size_t len = strlen(de->d_name);
1457 if(de->d_type == DT_DIR
1459 (de->d_name[0] == '.' && de->d_name[1] == '\0')
1460 || (de->d_name[0] == '.' && de->d_name[1] == '.' && de->d_name[2] == '\0')
1462 debug(D_HEALTH, "Ignoring directory '%s'", de->d_name);
1466 else if(de->d_type == DT_DIR) {
1467 char *s = mallocz(pathlen + strlen(de->d_name) + 2);
1470 strcat(s, de->d_name);
1476 else if((de->d_type == DT_LNK || de->d_type == DT_REG) &&
1477 len > 5 && !strcmp(&de->d_name[len - 5], ".conf")) {
1478 health_readfile(path, de->d_name);
1481 else debug(D_HEALTH, "Ignoring file '%s'", de->d_name);
1487 static inline char *health_config_dir(void) {
1488 char buffer[FILENAME_MAX + 1];
1489 snprintfz(buffer, FILENAME_MAX, "%s/health.d", config_get("global", "config directory", CONFIG_DIR));
1490 return config_get("health", "health configuration directory", buffer);
1493 void health_init(void) {
1494 debug(D_HEALTH, "Health configuration initializing");
1496 if(!(health_enabled = config_get_boolean("health", "enabled", 1))) {
1497 debug(D_HEALTH, "Health is disabled.");
1501 char *path = health_config_dir();
1504 char buffer[FILENAME_MAX + 1];
1505 snprintfz(buffer, FILENAME_MAX, "%s/alarm-email.sh", config_get("global", "plugins directory", PLUGINS_DIR));
1506 health_default_exec = config_get("health", "script to execute on alarm", buffer);
1509 long n = config_get_number("health", "in memory max health log entries", (long)localhost.health_log.max);
1511 error("Health configuration has invalid max log entries %ld. Using default %u", n, localhost.health_log.max);
1512 config_set_number("health", "in memory max health log entries", (long)localhost.health_log.max);
1514 else localhost.health_log.max = (unsigned int)n;
1516 rrdhost_rwlock(&localhost);
1517 health_readdir(path);
1518 rrdhost_unlock(&localhost);
1521 // ----------------------------------------------------------------------------
1524 static inline void health_string2json(BUFFER *wb, const char *prefix, const char *label, const char *value, const char *suffix) {
1526 buffer_sprintf(wb, "%s\"%s\":\"%s\"%s", prefix, label, value, suffix);
1528 buffer_sprintf(wb, "%s\"%s\":null%s", prefix, label, suffix);
1531 static inline void health_alarm_entry2json_nolock(BUFFER *wb, ALARM_ENTRY *ae, RRDHOST *host) {
1532 buffer_sprintf(wb, "\n\t{\n"
1533 "\t\t\"hostname\":\"%s\",\n"
1534 "\t\t\"unique_id\":%u,\n"
1535 "\t\t\"alarm_id\":%u,\n"
1536 "\t\t\"alarm_event_id\":%u,\n"
1537 "\t\t\"name\":\"%s\",\n"
1538 "\t\t\"chart\":\"%s\",\n"
1539 "\t\t\"family\":\"%s\",\n"
1540 "\t\t\"processed\":%s,\n"
1541 "\t\t\"updated\":%s,\n"
1542 "\t\t\"exec_run\":%s,\n"
1543 "\t\t\"exec_failed\":%s,\n"
1544 "\t\t\"exec\":\"%s\",\n"
1545 "\t\t\"recipient\":\"%s\",\n"
1546 "\t\t\"exec_code\":%d,\n"
1547 "\t\t\"source\":\"%s\",\n"
1548 "\t\t\"units\":\"%s\",\n"
1549 "\t\t\"info\":\"%s\",\n"
1550 "\t\t\"when\":%lu,\n"
1551 "\t\t\"duration\":%lu,\n"
1552 "\t\t\"non_clear_duration\":%lu,\n"
1553 "\t\t\"status\":\"%s\",\n"
1554 "\t\t\"old_status\":\"%s\",\n",
1562 (ae->notifications & HEALTH_ENTRY_NOTIFICATIONS_PROCESSED)?"true":"false",
1563 (ae->notifications & HEALTH_ENTRY_NOTIFICATIONS_UPDATED)?"true":"false",
1564 (ae->notifications & HEALTH_ENTRY_NOTIFICATIONS_EXEC_RUN)?"true":"false",
1565 (ae->notifications & HEALTH_ENTRY_NOTIFICATIONS_EXEC_FAILED)?"true":"false",
1566 ae->exec?ae->exec:health_default_exec,
1567 ae->recipient?ae->recipient:health_default_recipient,
1570 ae->units?ae->units:"",
1571 ae->info?ae->info:"",
1572 (unsigned long)ae->when,
1573 (unsigned long)ae->duration,
1574 (unsigned long)ae->non_clear_duration,
1575 rrdcalc_status2string(ae->new_status),
1576 rrdcalc_status2string(ae->old_status)
1579 buffer_strcat(wb, "\t\t\"value\":");
1580 buffer_rrd_value(wb, ae->new_value);
1581 buffer_strcat(wb, ",\n");
1583 buffer_strcat(wb, "\t\t\"old_value\":");
1584 buffer_rrd_value(wb, ae->old_value);
1585 buffer_strcat(wb, "\n");
1587 buffer_strcat(wb, "\t}");
1590 void health_alarm_log2json(RRDHOST *host, BUFFER *wb, uint32_t after) {
1591 pthread_rwlock_rdlock(&host->health_log.alarm_log_rwlock);
1593 buffer_strcat(wb, "[");
1595 unsigned int max = host->health_log.max;
1596 unsigned int count = 0;
1598 for(ae = host->health_log.alarms; ae && count < max ; count++, ae = ae->next) {
1599 if(ae->unique_id > after) {
1600 if(likely(count)) buffer_strcat(wb, ",");
1601 health_alarm_entry2json_nolock(wb, ae, host);
1605 buffer_strcat(wb, "\n]\n");
1607 pthread_rwlock_unlock(&host->health_log.alarm_log_rwlock);
1610 static inline void health_rrdcalc2json_nolock(BUFFER *wb, RRDCALC *rc) {
1612 "\t\t\"%s.%s\": {\n"
1613 "\t\t\t\"name\": \"%s\",\n"
1614 "\t\t\t\"chart\": \"%s\",\n"
1615 "\t\t\t\"family\": \"%s\",\n"
1616 "\t\t\t\"active\": %s,\n"
1617 "\t\t\t\"exec\": \"%s\",\n"
1618 "\t\t\t\"recipient\": \"%s\",\n"
1619 "\t\t\t\"source\": \"%s\",\n"
1620 "\t\t\t\"units\": \"%s\",\n"
1621 "\t\t\t\"info\": \"%s\",\n"
1622 "\t\t\t\"status\": \"%s\",\n"
1623 "\t\t\t\"last_status_change\": %lu,\n"
1624 "\t\t\t\"last_updated\": %lu,\n"
1625 "\t\t\t\"next_update\": %lu,\n"
1626 "\t\t\t\"update_every\": %d,\n"
1627 , rc->chart, rc->name
1630 , (rc->rrdset && rc->rrdset->family)?rc->rrdset->family:""
1631 , (rc->rrdset)?"true":"false"
1632 , rc->exec?rc->exec:health_default_exec
1633 , rc->recipient?rc->recipient:health_default_recipient
1635 , rc->units?rc->units:""
1636 , rc->info?rc->info:""
1637 , rrdcalc_status2string(rc->status)
1638 , (unsigned long)rc->last_status_change
1639 , (unsigned long)rc->last_updated
1640 , (unsigned long)rc->next_update
1644 if(RRDCALC_HAS_DB_LOOKUP(rc)) {
1645 if(rc->dimensions && *rc->dimensions)
1646 health_string2json(wb, "\t\t\t", "lookup_dimensions", rc->dimensions, ",\n");
1649 "\t\t\t\"db_after\": %lu,\n"
1650 "\t\t\t\"db_before\": %lu,\n"
1651 "\t\t\t\"lookup_method\": \"%s\",\n"
1652 "\t\t\t\"lookup_after\": %d,\n"
1653 "\t\t\t\"lookup_before\": %d,\n"
1654 "\t\t\t\"lookup_options\": \"",
1655 (unsigned long) rc->db_after,
1656 (unsigned long) rc->db_before,
1657 group_method2string(rc->group),
1661 buffer_data_options2string(wb, rc->options);
1662 buffer_strcat(wb, "\",\n");
1665 if(rc->calculation) {
1666 health_string2json(wb, "\t\t\t", "calc", rc->calculation->source, ",\n");
1667 health_string2json(wb, "\t\t\t", "calc_parsed", rc->calculation->parsed_as, ",\n");
1671 health_string2json(wb, "\t\t\t", "warn", rc->warning->source, ",\n");
1672 health_string2json(wb, "\t\t\t", "warn_parsed", rc->warning->parsed_as, ",\n");
1676 health_string2json(wb, "\t\t\t", "crit", rc->critical->source, ",\n");
1677 health_string2json(wb, "\t\t\t", "crit_parsed", rc->critical->parsed_as, ",\n");
1680 buffer_strcat(wb, "\t\t\t\"green\":");
1681 buffer_rrd_value(wb, rc->green);
1682 buffer_strcat(wb, ",\n");
1684 buffer_strcat(wb, "\t\t\t\"red\":");
1685 buffer_rrd_value(wb, rc->red);
1686 buffer_strcat(wb, ",\n");
1688 buffer_strcat(wb, "\t\t\t\"value\":");
1689 buffer_rrd_value(wb, rc->value);
1690 buffer_strcat(wb, "\n");
1692 buffer_strcat(wb, "\t\t}");
1695 //void health_rrdcalctemplate2json_nolock(BUFFER *wb, RRDCALCTEMPLATE *rt) {
1699 void health_alarms2json(RRDHOST *host, BUFFER *wb, int all) {
1701 rrdhost_rdlock(&localhost);
1703 buffer_strcat(wb, "{\n\t\"alarms\": {\n");
1705 for(i = 0, rc = host->alarms; rc ; rc = rc->next) {
1706 if(unlikely(!rc->rrdset || !rc->rrdset->last_collected_time.tv_sec))
1709 if(likely(!all && !(rc->status == RRDCALC_STATUS_WARNING || rc->status == RRDCALC_STATUS_CRITICAL)))
1712 if(likely(i)) buffer_strcat(wb, ",\n");
1713 health_rrdcalc2json_nolock(wb, rc);
1717 // buffer_strcat(wb, "\n\t},\n\t\"templates\": {");
1719 // RRDCALCTEMPLATE *rt;
1720 // for(rt = host->templates; rt ; rt = rt->next)
1721 // health_rrdcalctemplate2json_nolock(wb, rt);
1723 buffer_sprintf(wb, "\n\t},\n\t\"now\": %lu\n}\n", (unsigned long)time(NULL));
1724 rrdhost_unlock(&localhost);
1728 // ----------------------------------------------------------------------------
1729 // re-load health configuration
1731 static inline void health_free_all_nolock(RRDHOST *host) {
1732 while(host->templates)
1733 rrdcalctemplate_free(host, host->templates);
1736 rrdcalc_free(host, host->alarms);
1739 void health_reload(void) {
1740 if(!health_enabled) {
1741 error("Health reload is requested, but health is not enabled.");
1745 char *path = health_config_dir();
1747 // free all running alarms
1748 rrdhost_rwlock(&localhost);
1749 health_free_all_nolock(&localhost);
1750 rrdhost_unlock(&localhost);
1752 // invalidate all previous entries in the alarm log
1754 for(t = localhost.health_log.alarms ; t ; t = t->next) {
1755 t->notifications |= HEALTH_ENTRY_NOTIFICATIONS_UPDATED;
1758 // reset all thresholds to all charts
1760 for(st = localhost.rrdset_root; st ; st = st->next) {
1765 // load the new alarms
1766 rrdhost_rwlock(&localhost);
1767 health_readdir(path);
1768 rrdhost_unlock(&localhost);
1770 // link the loaded alarms to their charts
1771 for(st = localhost.rrdset_root; st ; st = st->next) {
1772 rrdhost_rwlock(&localhost);
1774 rrdsetcalc_link_matching(st);
1775 rrdcalctemplate_link_matching(st);
1777 rrdhost_unlock(&localhost);
1782 // ----------------------------------------------------------------------------
1783 // health main thread and friends
1785 static inline int rrdcalc_value2status(calculated_number n) {
1786 if(isnan(n)) return RRDCALC_STATUS_UNDEFINED;
1787 if(n) return RRDCALC_STATUS_RAISED;
1788 return RRDCALC_STATUS_CLEAR;
1791 static inline void health_alarm_execute(RRDHOST *host, ALARM_ENTRY *ae) {
1792 if(ae->old_status == RRDCALC_STATUS_UNINITIALIZED && ae->new_status == RRDCALC_STATUS_CLEAR)
1795 char buffer[FILENAME_MAX + 1];
1798 const char *exec = ae->exec;
1799 if(!exec) exec = health_default_exec;
1801 const char *recipient = ae->recipient;
1802 if(!recipient) recipient = health_default_recipient;
1804 snprintfz(buffer, FILENAME_MAX, "exec %s '%s' '%s' '%u' '%u' '%u' '%zu' '%s' '%s' '%s' '%s' '%s' '%0.0Lf' '%0.0Lf' '%s' '%u' '%u' '%s' '%s'",
1813 ae->chart?ae->chart:"NOCAHRT",
1814 ae->family?ae->family:"NOFAMILY",
1815 rrdcalc_status2string(ae->new_status),
1816 rrdcalc_status2string(ae->old_status),
1819 ae->source?ae->source:"UNKNOWN",
1820 (uint32_t)ae->duration,
1821 (uint32_t)ae->non_clear_duration,
1822 ae->units?ae->units:"",
1823 ae->info?ae->info:""
1826 ae->notifications |= HEALTH_ENTRY_NOTIFICATIONS_EXEC_RUN;
1828 debug(D_HEALTH, "executing command '%s'", buffer);
1829 FILE *fp = mypopen(buffer, &command_pid);
1831 error("HEALTH: Cannot popen(\"%s\", \"r\").", buffer);
1834 debug(D_HEALTH, "HEALTH reading from command");
1835 char *s = fgets(buffer, FILENAME_MAX, fp);
1837 debug(D_HEALTH, "HEALTH closing command");
1838 ae->exec_code = mypclose(fp, command_pid);
1839 debug(D_HEALTH, "done executing command - returned with code %d", ae->exec_code);
1841 if(ae->exec_code != 0)
1842 ae->notifications |= HEALTH_ENTRY_NOTIFICATIONS_EXEC_FAILED;
1845 static inline void health_process_notifications(RRDHOST *host, ALARM_ENTRY *ae) {
1846 info("Health alarm '%s.%s' = %0.2Lf - changed status from %s to %s",
1847 ae->chart?ae->chart:"NOCHART", ae->name,
1849 rrdcalc_status2string(ae->old_status),
1850 rrdcalc_status2string(ae->new_status)
1853 health_alarm_execute(host, ae);
1856 static inline void health_alarm_log(RRDHOST *host,
1857 uint32_t alarm_id, uint32_t alarm_event_id,
1859 const char *name, const char *chart, const char *family,
1860 const char *exec, const char *recipient, time_t duration,
1861 calculated_number old_value, calculated_number new_value,
1862 int old_status, int new_status,
1867 ALARM_ENTRY *ae = callocz(1, sizeof(ALARM_ENTRY));
1868 ae->name = strdupz(name);
1869 ae->hash_name = simple_hash(ae->name);
1872 ae->chart = strdupz(chart);
1873 ae->hash_chart = simple_hash(ae->chart);
1877 ae->family = strdupz(family);
1879 if(exec) ae->exec = strdupz(exec);
1880 if(recipient) ae->recipient = strdupz(recipient);
1881 if(source) ae->source = strdupz(source);
1882 if(units) ae->units = strdupz(units);
1883 if(info) ae->info = strdupz(info);
1885 ae->unique_id = host->health_log.next_log_id++;
1886 ae->alarm_id = alarm_id;
1887 ae->alarm_event_id = alarm_event_id;
1889 ae->old_value = old_value;
1890 ae->new_value = new_value;
1891 ae->old_status = old_status;
1892 ae->new_status = new_status;
1893 ae->duration = duration;
1895 if(ae->old_status == RRDCALC_STATUS_WARNING || ae->old_status == RRDCALC_STATUS_CRITICAL)
1896 ae->non_clear_duration += ae->duration;
1899 pthread_rwlock_wrlock(&host->health_log.alarm_log_rwlock);
1900 ae->next = host->health_log.alarms;
1901 host->health_log.alarms = ae;
1902 host->health_log.count++;
1903 pthread_rwlock_unlock(&host->health_log.alarm_log_rwlock);
1905 // match previous alarms
1906 pthread_rwlock_rdlock(&host->health_log.alarm_log_rwlock);
1908 for(t = host->health_log.alarms ; t ; t = t->next) {
1910 t->hash_name == ae->hash_name &&
1911 t->hash_chart == ae->hash_chart &&
1912 !strcmp(t->name, ae->name) &&
1913 t->chart && ae->chart && !strcmp(t->chart, ae->chart)) {
1915 if(!(t->notifications & HEALTH_ENTRY_NOTIFICATIONS_UPDATED) && !t->updated_by) {
1916 t->notifications |= HEALTH_ENTRY_NOTIFICATIONS_UPDATED;
1919 if((t->new_status == RRDCALC_STATUS_WARNING || t->new_status == RRDCALC_STATUS_CRITICAL) &&
1920 (t->old_status == RRDCALC_STATUS_WARNING || t->old_status == RRDCALC_STATUS_CRITICAL))
1921 ae->non_clear_duration += t->non_clear_duration;
1924 // no need to continue
1929 pthread_rwlock_unlock(&host->health_log.alarm_log_rwlock);
1932 static inline void health_alarm_log_process(RRDHOST *host) {
1933 static uint32_t last_processed = 0;
1936 pthread_rwlock_rdlock(&host->health_log.alarm_log_rwlock);
1938 for(ae = host->health_log.alarms; ae ;ae = ae->next) {
1939 if(last_processed >= ae->unique_id) break;
1941 if(!(ae->notifications & HEALTH_ENTRY_NOTIFICATIONS_PROCESSED) &&
1942 !(ae->notifications & HEALTH_ENTRY_NOTIFICATIONS_UPDATED)) {
1943 ae->notifications |= HEALTH_ENTRY_NOTIFICATIONS_PROCESSED;
1944 health_process_notifications(host, ae);
1948 if(host->health_log.alarms)
1949 last_processed = host->health_log.alarms->unique_id;
1951 pthread_rwlock_unlock(&host->health_log.alarm_log_rwlock);
1953 if(host->health_log.count <= host->health_log.max)
1956 // cleanup excess entries in the log
1957 pthread_rwlock_wrlock(&host->health_log.alarm_log_rwlock);
1959 ALARM_ENTRY *last = NULL;
1960 unsigned int count = host->health_log.max;
1961 for(ae = host->health_log.alarms; ae && count ; count--, last = ae, ae = ae->next) ;
1963 if(ae && last && last->next == ae)
1969 ALARM_ENTRY *t = ae->next;
1975 freez(ae->recipient);
1984 pthread_rwlock_unlock(&host->health_log.alarm_log_rwlock);
1987 static inline int rrdcalc_isrunnable(RRDCALC *rc, time_t now, time_t *next_run) {
1988 if (unlikely(!rc->rrdset)) {
1989 debug(D_HEALTH, "Health not running alarm '%s.%s'. It is not linked to a chart.", rc->chart?rc->chart:"NOCHART", rc->name);
1993 if (unlikely(!rc->rrdset->last_collected_time.tv_sec)) {
1994 debug(D_HEALTH, "Health not running alarm '%s.%s'. Chart is not yet collected.", rc->chart?rc->chart:"NOCHART", rc->name);
1998 if (unlikely(!rc->update_every)) {
1999 debug(D_HEALTH, "Health not running alarm '%s.%s'. It does not have an update frequency", rc->chart?rc->chart:"NOCHART", rc->name);
2003 if (unlikely(rc->next_update > now)) {
2004 if (unlikely(*next_run > rc->next_update))
2005 *next_run = rc->next_update;
2007 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));
2012 // we should check that the DB lookup is possible
2014 // - the duration of the chart includes the required timeframe
2015 // we SHOULD NOT check the dimensions - there might be alarms that refer non-existing dimensions (e.g. cpu steal)
2020 void *health_main(void *ptr) {
2023 info("HEALTH thread created with task id %d", gettid());
2025 if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
2026 error("Cannot set pthread cancel type to DEFERRED.");
2028 if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
2029 error("Cannot set pthread cancel state to ENABLE.");
2031 int min_run_every = (int)config_get_number("health", "run at least every seconds", 10);
2032 if(min_run_every < 1) min_run_every = 1;
2034 BUFFER *wb = buffer_create(100);
2036 unsigned int loop = 0;
2037 while(health_enabled) {
2039 debug(D_HEALTH, "Health monitoring iteration no %u started", loop);
2041 int oldstate, runnable = 0;
2042 time_t now = time(NULL);
2043 time_t next_run = now + min_run_every;
2046 if (unlikely(pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldstate) != 0))
2047 error("Cannot set pthread cancel state to DISABLE.");
2049 rrdhost_rdlock(&localhost);
2051 // the first loop is to lookup values from the db
2052 for (rc = localhost.alarms; rc; rc = rc->next) {
2053 if (unlikely(!rrdcalc_isrunnable(rc, now, &next_run)))
2057 rc->old_value = rc->value;
2059 // 1. if there is database lookup, do it
2060 // 2. if there is calculation expression, run it
2062 if (unlikely(RRDCALC_HAS_DB_LOOKUP(rc))) {
2063 time_t old_db_timestamp = rc->db_before;
2064 int value_is_null = 0;
2066 int ret = rrd2value(rc->rrdset, wb, &rc->value,
2067 rc->dimensions, 1, rc->after, rc->before, rc->group,
2068 rc->options, &rc->db_after, &rc->db_before, &value_is_null);
2070 if (unlikely(ret != 200)) {
2071 // database lookup failed
2074 debug(D_HEALTH, "Health alarm '%s.%s': database lookup returned error %d", rc->chart?rc->chart:"NOCHART", rc->name, ret);
2076 if (unlikely(!(rc->rrdcalc_flags & RRDCALC_FLAG_DB_ERROR))) {
2077 rc->rrdcalc_flags |= RRDCALC_FLAG_DB_ERROR;
2078 error("Health alarm '%s.%s': database lookup returned error %d", rc->chart?rc->chart:"NOCHART", rc->name, ret);
2081 else if (unlikely(rc->rrdcalc_flags & RRDCALC_FLAG_DB_ERROR))
2082 rc->rrdcalc_flags &= ~RRDCALC_FLAG_DB_ERROR;
2084 if (unlikely(old_db_timestamp == rc->db_before)) {
2085 // database is stale
2087 debug(D_HEALTH, "Health alarm '%s.%s': database is stale", rc->chart?rc->chart:"NOCHART", rc->name);
2089 if (unlikely(!(rc->rrdcalc_flags & RRDCALC_FLAG_DB_STALE))) {
2090 rc->rrdcalc_flags |= RRDCALC_FLAG_DB_STALE;
2091 error("Health alarm '%s.%s': database is stale", rc->chart?rc->chart:"NOCHART", rc->name);
2094 else if (unlikely(rc->rrdcalc_flags & RRDCALC_FLAG_DB_STALE))
2095 rc->rrdcalc_flags &= ~RRDCALC_FLAG_DB_STALE;
2097 if (unlikely(value_is_null)) {
2098 // collected value is null
2102 debug(D_HEALTH, "Health alarm '%s.%s': database lookup returned empty value (possibly value is not collected yet)",
2103 rc->chart?rc->chart:"NOCHART", rc->name);
2105 if (unlikely(!(rc->rrdcalc_flags & RRDCALC_FLAG_DB_NAN))) {
2106 rc->rrdcalc_flags |= RRDCALC_FLAG_DB_NAN;
2107 error("Health alarm '%s.%s': database lookup returned empty value (possibly value is not collected yet)",
2108 rc->chart?rc->chart:"NOCHART", rc->name);
2111 else if (unlikely(rc->rrdcalc_flags & RRDCALC_FLAG_DB_NAN))
2112 rc->rrdcalc_flags &= ~RRDCALC_FLAG_DB_NAN;
2114 debug(D_HEALTH, "Health alarm '%s.%s': database lookup gave value "
2115 CALCULATED_NUMBER_FORMAT, rc->chart?rc->chart:"NOCHART", rc->name, rc->value);
2118 if(unlikely(rc->calculation)) {
2119 if (unlikely(!expression_evaluate(rc->calculation))) {
2120 // calculation failed
2124 debug(D_HEALTH, "Health alarm '%s.%s': failed to evaluate calculation with error: %s",
2125 rc->chart?rc->chart:"NOCHART", rc->name, buffer_tostring(rc->calculation->error_msg));
2127 if (unlikely(!(rc->rrdcalc_flags & RRDCALC_FLAG_CALC_ERROR))) {
2128 rc->rrdcalc_flags |= RRDCALC_FLAG_CALC_ERROR;
2129 error("Health alarm '%s.%s': failed to evaluate calculation with error: %s",
2130 rc->chart?rc->chart:"NOCHART", rc->name, buffer_tostring(rc->calculation->error_msg));
2134 if (unlikely(rc->rrdcalc_flags & RRDCALC_FLAG_CALC_ERROR))
2135 rc->rrdcalc_flags &= ~RRDCALC_FLAG_CALC_ERROR;
2137 debug(D_HEALTH, "Health alarm '%s.%s': calculation expression gave value "
2138 CALCULATED_NUMBER_FORMAT
2139 ": %s (source: %s)",
2140 rc->chart?rc->chart:"NOCHART", rc->name,
2141 rc->calculation->result,
2142 buffer_tostring(rc->calculation->error_msg),
2146 rc->value = rc->calculation->result;
2150 rrdhost_unlock(&localhost);
2153 rrdhost_rdlock(&localhost);
2155 for (rc = localhost.alarms; rc; rc = rc->next) {
2156 if (unlikely(!rrdcalc_isrunnable(rc, now, &next_run)))
2159 int warning_status = RRDCALC_STATUS_UNDEFINED;
2160 int critical_status = RRDCALC_STATUS_UNDEFINED;
2162 if(likely(rc->warning)) {
2163 if(unlikely(!expression_evaluate(rc->warning))) {
2164 // calculation failed
2166 debug(D_HEALTH, "Health alarm '%s.%s': warning expression failed with error: %s",
2167 rc->chart?rc->chart:"NOCHART", rc->name, buffer_tostring(rc->warning->error_msg));
2169 if (unlikely(!(rc->rrdcalc_flags & RRDCALC_FLAG_WARN_ERROR))) {
2170 rc->rrdcalc_flags |= RRDCALC_FLAG_WARN_ERROR;
2171 error("Health alarm '%s.%s': warning expression failed with error: %s",
2172 rc->chart?rc->chart:"NOCHART", rc->name, buffer_tostring(rc->warning->error_msg));
2176 if(unlikely(rc->rrdcalc_flags & RRDCALC_FLAG_WARN_ERROR))
2177 rc->rrdcalc_flags &= ~RRDCALC_FLAG_WARN_ERROR;
2179 debug(D_HEALTH, "Health alarm '%s.%s': warning expression gave value "
2180 CALCULATED_NUMBER_FORMAT
2181 ": %s (source: %s)",
2182 rc->chart?rc->chart:"NOCHART", rc->name,
2183 rc->warning->result,
2184 buffer_tostring(rc->warning->error_msg),
2188 warning_status = rrdcalc_value2status(rc->warning->result);
2192 if(likely(rc->critical)) {
2193 if(unlikely(!expression_evaluate(rc->critical))) {
2194 // calculation failed
2196 debug(D_HEALTH, "Health alarm '%s.%s': critical expression failed with error: %s",
2197 rc->chart?rc->chart:"NOCHART", rc->name, buffer_tostring(rc->critical->error_msg));
2199 if (unlikely(!(rc->rrdcalc_flags & RRDCALC_FLAG_CRIT_ERROR))) {
2200 rc->rrdcalc_flags |= RRDCALC_FLAG_CRIT_ERROR;
2201 error("Health alarm '%s.%s': critical expression failed with error: %s",
2202 rc->chart?rc->chart:"NOCHART", rc->name, buffer_tostring(rc->critical->error_msg));
2206 if(unlikely(rc->rrdcalc_flags & RRDCALC_FLAG_CRIT_ERROR))
2207 rc->rrdcalc_flags &= ~RRDCALC_FLAG_CRIT_ERROR;
2209 debug(D_HEALTH, "Health alarm '%s.%s': critical expression gave value "
2210 CALCULATED_NUMBER_FORMAT
2211 ": %s (source: %s)",
2212 rc->chart?rc->chart:"NOCHART", rc->name,
2213 rc->critical->result,
2214 buffer_tostring(rc->critical->error_msg),
2218 critical_status = rrdcalc_value2status(rc->critical->result);
2222 int status = RRDCALC_STATUS_UNDEFINED;
2224 switch(warning_status) {
2225 case RRDCALC_STATUS_CLEAR:
2226 status = RRDCALC_STATUS_CLEAR;
2229 case RRDCALC_STATUS_RAISED:
2230 status = RRDCALC_STATUS_WARNING;
2237 switch(critical_status) {
2238 case RRDCALC_STATUS_CLEAR:
2239 if(status == RRDCALC_STATUS_UNDEFINED)
2240 status = RRDCALC_STATUS_CLEAR;
2243 case RRDCALC_STATUS_RAISED:
2244 status = RRDCALC_STATUS_CRITICAL;
2251 if(status != rc->status) {
2252 health_alarm_log(&localhost, rc->id, rc->next_event_id++, time(NULL), rc->name, rc->rrdset->id, rc->rrdset->family, rc->exec, rc->recipient, now - rc->last_status_change, rc->old_value, rc->value, rc->status, status, rc->source, rc->units, rc->info);
2253 rc->last_status_change = now;
2254 rc->status = status;
2257 rc->last_updated = now;
2258 rc->next_update = now + rc->update_every;
2260 if (next_run > rc->next_update)
2261 next_run = rc->next_update;
2264 rrdhost_unlock(&localhost);
2267 if (unlikely(pthread_setcancelstate(oldstate, NULL) != 0))
2268 error("Cannot set pthread cancel state to RESTORE (%d).", oldstate);
2270 // execute notifications
2272 health_alarm_log_process(&localhost);
2275 if(now < next_run) {
2276 debug(D_HEALTH, "Health monitoring iteration no %u done. Next iteration in %d secs",
2277 loop, (int) (next_run - now));
2278 sleep_usec(1000000 * (unsigned long long) (next_run - now));
2281 debug(D_HEALTH, "Health monitoring iteration no %u done. Next iteration now", loop);
2287 info("HEALTH thread exiting");