3 #define RRDVAR_MAX_LENGTH 1024
5 struct health_options {
6 const char *health_default_exec;
7 const char *health_default_recipient;
8 const char *log_filename;
12 static struct health_options health = {
13 .health_default_exec = PLUGINS_DIR "/alarm-notify.sh",
14 .health_default_recipient = "root",
15 .log_filename = VARLIB_DIR "/health/alarm_log.db",
19 int health_enabled = 1;
21 // ----------------------------------------------------------------------------
22 // health alarm log load/save
23 // no need for locking - only one thread is reading / writing the alarms log
25 static inline int health_alarm_log_open(void) {
27 fclose(health.log_fp);
29 health.log_fp = fopen(health.log_filename, "a");
32 if (setvbuf(health.log_fp, NULL, _IOLBF, 0) != 0)
33 error("Cannot set line buffering on health log file.");
37 error("Cannot open health log file '%s'. Health data will be lost in case of netdata or server crash.", health.log_filename);
41 static inline void health_alarm_log_close(void) {
43 fclose(health.log_fp);
48 static inline void health_log_recreate(void) {
49 if(health.log_fp != NULL) {
50 health_alarm_log_close();
52 // open it with truncate
53 health.log_fp = fopen(health.log_filename, "w");
54 if(health.log_fp) fclose(health.log_fp);
55 else error("Cannot truncate health log '%s'", health.log_filename);
59 health_alarm_log_open();
63 static inline void health_alarm_log_save(RRDHOST *host, ALARM_ENTRY *ae) {
67 if(likely(health.log_fp)) {
68 if(unlikely(fprintf(health.log_fp
70 "\t%08x\t%08x\t%08x\t%08x\t%08x"
73 "\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s"
77 , (ae->flags & HEALTH_ENTRY_FLAG_SAVED)?'U':'A'
87 , (uint32_t)ae->duration
88 , (uint32_t)ae->non_clear_duration
90 , (uint32_t)ae->exec_run_timestamp
91 , (uint32_t)ae->delay_up_to_timestamp
93 , (ae->name)?ae->name:""
94 , (ae->chart)?ae->chart:""
95 , (ae->family)?ae->family:""
96 , (ae->exec)?ae->exec:""
97 , (ae->recipient)?ae->recipient:""
98 , (ae->source)?ae->source:""
99 , (ae->units)?ae->units:""
100 , (ae->info)?ae->info:""
107 , (long double)ae->new_value
108 , (long double)ae->old_value
110 error("Health: failed to save alarm log entry. Health data may be lost in case of abnormal restart.");
112 ae->flags |= HEALTH_ENTRY_FLAG_SAVED;
116 static inline void health_alarm_log_load(RRDHOST *host) {
119 health_alarm_log_close();
121 FILE *fp = fopen(health.log_filename, "r");
123 error("Registry: cannot open health file: %s", health.log_filename);
127 char *s, *buf = mallocz(65536 + 1);
131 while((s = fgets_trim_len(buf, 65536, fp, &len))) {
133 // fprintf(stderr, "line %zu: '%s'\n", line, s);
135 int max_entries = 30, entries = 0;
136 char *pointers[max_entries];
138 pointers[entries++] = s;
140 if(unlikely(*s == '\t')) {
142 pointers[entries++] = s;
143 if(entries > max_entries) {
144 error("Line %zd of file '%s' has more than %d entries. Ignoring excessive entries.", line, health.log_filename, max_entries);
151 if(likely(*pointers[0] == 'U' || *pointers[0] == 'A')) {
152 ALARM_ENTRY *ae = NULL;
155 error("Line %zd of file '%s' should have at least 26 entries, but it has %d. Ignoring line.", line, health.log_filename, entries);
159 // if this is an update, find it
160 if(unlikely(*pointers[0] == 'U')) {
161 uint32_t unique_id = (uint32_t)strtoul(pointers[2], NULL, 16);
163 // fprintf(stderr, "searching for alarm entry with unique id %u\n", unique_id);
166 for(ae = host->health_log.alarms; ae ;ae = ae->next) {
167 if(unlikely(ae->unique_id == unique_id)) break;
172 error("Line %zd of file '%s' updates alarm log entry with unique id %u, but it is not found.", line, health.log_filename, unique_id);
178 ae = callocz(1, sizeof(ALARM_ENTRY));
181 if(strcmp(pointers[1], host->hostname))
182 error("Line %zd of file '%s' provides an alarm for host '%s' but this is named '%s'.", line, health.log_filename, pointers[1], host->hostname);
184 ae->unique_id = (uint32_t)strtoul(pointers[2], NULL, 16);
185 ae->alarm_id = (uint32_t)strtoul(pointers[3], NULL, 16);
186 ae->alarm_event_id = (uint32_t)strtoul(pointers[4], NULL, 16);
187 ae->updated_by_id = (uint32_t)strtoul(pointers[5], NULL, 16);
188 ae->updates_id = (uint32_t)strtoul(pointers[6], NULL, 16);
190 ae->when = (uint32_t)strtoul(pointers[7], NULL, 16);
191 ae->duration = (uint32_t)strtoul(pointers[8], NULL, 16);
192 ae->non_clear_duration = (uint32_t)strtoul(pointers[9], NULL, 16);
193 ae->flags = (uint32_t)strtoul(pointers[10], NULL, 16);
194 ae->exec_run_timestamp = (uint32_t)strtoul(pointers[11], NULL, 16);
195 ae->delay_up_to_timestamp = (uint32_t)strtoul(pointers[12], NULL, 16);
197 if(unlikely(ae->name)) freez(ae->name);
198 ae->name = strdupz(pointers[13]);
200 if(unlikely(ae->chart)) freez(ae->chart);
201 ae->chart = strdupz(pointers[14]);
203 if(unlikely(ae->family)) freez(ae->family);
204 ae->family = strdupz(pointers[15]);
206 if(unlikely(ae->exec)) freez(ae->exec);
207 ae->exec = strdupz(pointers[16]);
208 if(!*ae->exec) { freez(ae->exec); ae->exec = NULL; }
210 if(unlikely(ae->recipient)) freez(ae->recipient);
211 ae->recipient = strdupz(pointers[17]);
212 if(!*ae->recipient) { freez(ae->recipient); ae->recipient = NULL; }
214 if(unlikely(ae->source)) freez(ae->source);
215 ae->source = strdupz(pointers[18]);
216 if(!*ae->source) { freez(ae->source); ae->source = NULL; }
218 if(unlikely(ae->units)) freez(ae->units);
219 ae->units = strdupz(pointers[19]);
220 if(!*ae->units) { freez(ae->units); ae->units = NULL; }
222 if(unlikely(ae->info)) freez(ae->info);
223 ae->info = strdupz(pointers[20]);
224 if(!*ae->info) { freez(ae->info); ae->info = NULL; }
226 ae->exec_code = atoi(pointers[21]);
227 ae->new_status = atoi(pointers[22]);
228 ae->old_status = atoi(pointers[23]);
229 ae->delay = atoi(pointers[24]);
231 ae->new_value = strtold(pointers[25], NULL);
232 ae->old_value = strtold(pointers[26], NULL);
234 // add it to host if not already there
235 if(unlikely(*pointers[0] == 'A')) {
236 ae->next = host->health_log.alarms;
237 host->health_log.alarms = ae;
240 if(unlikely(ae->unique_id >= host->health_log.next_log_id))
241 host->health_log.next_log_id = ae->unique_id + 1;
243 if(unlikely(ae->alarm_id >= host->health_log.next_alarm_id))
244 host->health_log.next_alarm_id = ae->alarm_id + 1;
247 error("Line %zd of file '%s' is invalid (unrecognized entry type '%s').", line, health.log_filename, pointers[0]);
255 health_alarm_log_open();
258 // ----------------------------------------------------------------------------
259 // health alarm log management
261 static inline void health_alarm_log(RRDHOST *host,
262 uint32_t alarm_id, uint32_t alarm_event_id,
264 const char *name, const char *chart, const char *family,
265 const char *exec, const char *recipient, time_t duration,
266 calculated_number old_value, calculated_number new_value,
267 int old_status, int new_status,
273 debug(D_HEALTH, "Health adding alarm log entry with id: %u", host->health_log.next_log_id);
275 ALARM_ENTRY *ae = callocz(1, sizeof(ALARM_ENTRY));
276 ae->name = strdupz(name);
277 ae->hash_name = simple_hash(ae->name);
280 ae->chart = strdupz(chart);
281 ae->hash_chart = simple_hash(ae->chart);
285 ae->family = strdupz(family);
287 if(exec) ae->exec = strdupz(exec);
288 if(recipient) ae->recipient = strdupz(recipient);
289 if(source) ae->source = strdupz(source);
290 if(units) ae->units = strdupz(units);
291 if(info) ae->info = strdupz(info);
293 ae->unique_id = host->health_log.next_log_id++;
294 ae->alarm_id = alarm_id;
295 ae->alarm_event_id = alarm_event_id;
297 ae->old_value = old_value;
298 ae->new_value = new_value;
299 ae->old_status = old_status;
300 ae->new_status = new_status;
301 ae->duration = duration;
303 ae->delay_up_to_timestamp = when + delay;
305 if(ae->old_status == RRDCALC_STATUS_WARNING || ae->old_status == RRDCALC_STATUS_CRITICAL)
306 ae->non_clear_duration += ae->duration;
309 pthread_rwlock_wrlock(&host->health_log.alarm_log_rwlock);
310 ae->next = host->health_log.alarms;
311 host->health_log.alarms = ae;
312 host->health_log.count++;
313 pthread_rwlock_unlock(&host->health_log.alarm_log_rwlock);
315 // match previous alarms
316 pthread_rwlock_rdlock(&host->health_log.alarm_log_rwlock);
318 for(t = host->health_log.alarms ; t ; t = t->next) {
319 if(t != ae && t->alarm_id == ae->alarm_id) {
320 if(!(t->flags & HEALTH_ENTRY_FLAG_UPDATED) && !t->updated_by_id) {
321 t->flags |= HEALTH_ENTRY_FLAG_UPDATED;
322 t->updated_by_id = ae->unique_id;
323 ae->updates_id = t->unique_id;
325 if((t->new_status == RRDCALC_STATUS_WARNING || t->new_status == RRDCALC_STATUS_CRITICAL) &&
326 (t->old_status == RRDCALC_STATUS_WARNING || t->old_status == RRDCALC_STATUS_CRITICAL))
327 ae->non_clear_duration += t->non_clear_duration;
329 health_alarm_log_save(host, t);
332 // no need to continue
337 pthread_rwlock_unlock(&host->health_log.alarm_log_rwlock);
339 health_alarm_log_save(host, ae);
342 // ----------------------------------------------------------------------------
345 static inline int rrdvar_fix_name(char *variable) {
348 if (!isalnum(*variable) && *variable != '.' && *variable != '_') {
359 int rrdvar_compare(void* a, void* b) {
360 if(((RRDVAR *)a)->hash < ((RRDVAR *)b)->hash) return -1;
361 else if(((RRDVAR *)a)->hash > ((RRDVAR *)b)->hash) return 1;
362 else return strcmp(((RRDVAR *)a)->name, ((RRDVAR *)b)->name);
365 static inline RRDVAR *rrdvar_index_add(avl_tree_lock *tree, RRDVAR *rv) {
366 RRDVAR *ret = (RRDVAR *)avl_insert_lock(tree, (avl *)(rv));
368 debug(D_VARIABLES, "Request to insert RRDVAR '%s' into index failed. Already exists.", rv->name);
373 static inline RRDVAR *rrdvar_index_del(avl_tree_lock *tree, RRDVAR *rv) {
374 RRDVAR *ret = (RRDVAR *)avl_remove_lock(tree, (avl *)(rv));
376 error("Request to remove RRDVAR '%s' from index failed. Not Found.", rv->name);
381 static inline RRDVAR *rrdvar_index_find(avl_tree_lock *tree, const char *name, uint32_t hash) {
383 tmp.name = (char *)name;
384 tmp.hash = (hash)?hash:simple_hash(tmp.name);
386 return (RRDVAR *)avl_search_lock(tree, (avl *)&tmp);
389 static inline void rrdvar_free(RRDHOST *host, avl_tree_lock *tree, RRDVAR *rv) {
395 rrdvar_index_del(tree, rv);
401 static inline RRDVAR *rrdvar_create_and_index(const char *scope, avl_tree_lock *tree, const char *name, int type, calculated_number *value) {
402 char *variable = strdupz(name);
403 rrdvar_fix_name(variable);
404 uint32_t hash = simple_hash(variable);
406 RRDVAR *rv = rrdvar_index_find(tree, variable, hash);
408 debug(D_VARIABLES, "Variable '%s' not found in scope '%s'. Creating a new one.", variable, scope);
410 rv = callocz(1, sizeof(RRDVAR));
416 RRDVAR *ret = rrdvar_index_add(tree, rv);
417 if(unlikely(ret != rv)) {
418 debug(D_VARIABLES, "Variable '%s' in scope '%s' already exists", variable, scope);
419 rrdvar_free(NULL, NULL, rv);
423 debug(D_VARIABLES, "Variable '%s' created in scope '%s'", variable, scope);
434 // ----------------------------------------------------------------------------
437 calculated_number rrdvar2number(RRDVAR *rv) {
439 case RRDVAR_TYPE_CALCULATED: {
440 calculated_number *n = (calculated_number *)rv->value;
444 case RRDVAR_TYPE_TIME_T: {
445 time_t *n = (time_t *)rv->value;
449 case RRDVAR_TYPE_COLLECTED: {
450 collected_number *n = (collected_number *)rv->value;
454 case RRDVAR_TYPE_TOTAL: {
455 total_number *n = (total_number *)rv->value;
459 case RRDVAR_TYPE_INT: {
460 int *n = (int *)rv->value;
465 error("I don't know how to convert RRDVAR type %d to calculated_number", rv->type);
470 void dump_variable(void *data) {
471 RRDVAR *rv = (RRDVAR *)data;
472 debug(D_HEALTH, "%50s : %20.5Lf", rv->name, rrdvar2number(rv));
475 int health_variable_lookup(const char *variable, uint32_t hash, RRDCALC *rc, calculated_number *result) {
476 RRDSET *st = rc->rrdset;
481 rv = rrdvar_index_find(&st->variables_root_index, variable, hash);
483 *result = rrdvar2number(rv);
487 rv = rrdvar_index_find(&st->rrdfamily->variables_root_index, variable, hash);
489 *result = rrdvar2number(rv);
493 rv = rrdvar_index_find(&st->rrdhost->variables_root_index, variable, hash);
495 *result = rrdvar2number(rv);
499 debug(D_HEALTH, "Available local chart '%s' variables:", st->id);
500 avl_traverse_lock(&st->variables_root_index, dump_variable);
502 debug(D_HEALTH, "Available family '%s' variables:", st->rrdfamily->family);
503 avl_traverse_lock(&st->rrdfamily->variables_root_index, dump_variable);
505 debug(D_HEALTH, "Available host '%s' variables:", st->rrdhost->hostname);
506 avl_traverse_lock(&st->rrdhost->variables_root_index, dump_variable);
511 // ----------------------------------------------------------------------------
512 // RRDSETVAR management
514 RRDSETVAR *rrdsetvar_create(RRDSET *st, const char *variable, int type, void *value, uint32_t options) {
515 debug(D_VARIABLES, "RRDVARSET create for chart id '%s' name '%s' with variable name '%s'", st->id, st->name, variable);
516 RRDSETVAR *rs = (RRDSETVAR *)callocz(1, sizeof(RRDSETVAR));
518 char buffer[RRDVAR_MAX_LENGTH + 1];
519 snprintfz(buffer, RRDVAR_MAX_LENGTH, "%s.%s", st->id, variable);
520 rs->fullid = strdupz(buffer);
522 snprintfz(buffer, RRDVAR_MAX_LENGTH, "%s.%s", st->name, variable);
523 rs->fullname = strdupz(buffer);
525 rs->variable = strdupz(variable);
529 rs->options = options;
532 rs->local = rrdvar_create_and_index("local", &st->variables_root_index, rs->variable, rs->type, rs->value);
533 rs->family = rrdvar_create_and_index("family", &st->rrdfamily->variables_root_index, rs->fullid, rs->type, rs->value);
534 rs->host = rrdvar_create_and_index("host", &st->rrdhost->variables_root_index, rs->fullid, rs->type, rs->value);
535 rs->family_name = rrdvar_create_and_index("family", &st->rrdfamily->variables_root_index, rs->fullname, rs->type, rs->value);
536 rs->host_name = rrdvar_create_and_index("host", &st->rrdhost->variables_root_index, rs->fullname, rs->type, rs->value);
538 rs->next = st->variables;
544 void rrdsetvar_rename_all(RRDSET *st) {
545 debug(D_VARIABLES, "RRDSETVAR rename for chart id '%s' name '%s'", st->id, st->name);
547 // only these 2 can change name
551 char buffer[RRDVAR_MAX_LENGTH + 1];
552 RRDSETVAR *rs, *next = st->variables;
556 snprintfz(buffer, RRDVAR_MAX_LENGTH, "%s.%s", st->name, rs->variable);
558 if (strcmp(buffer, rs->fullname)) {
560 rrdvar_free(st->rrdhost, &st->rrdfamily->variables_root_index, rs->family_name);
561 rrdvar_free(st->rrdhost, &st->rrdhost->variables_root_index, rs->host_name);
564 rs->fullname = strdupz(st->name);
565 rs->family_name = rrdvar_create_and_index("family", &st->rrdfamily->variables_root_index, rs->fullname, rs->type, rs->value);
566 rs->host_name = rrdvar_create_and_index("host", &st->rrdhost->variables_root_index, rs->fullname, rs->type, rs->value);
570 rrdsetcalc_link_matching(st);
573 void rrdsetvar_free(RRDSETVAR *rs) {
574 RRDSET *st = rs->rrdset;
575 debug(D_VARIABLES, "RRDSETVAR free for chart id '%s' name '%s', variable '%s'", st->id, st->name, rs->variable);
577 rrdvar_free(st->rrdhost, &st->variables_root_index, rs->local);
578 rrdvar_free(st->rrdhost, &st->rrdfamily->variables_root_index, rs->family);
579 rrdvar_free(st->rrdhost, &st->rrdhost->variables_root_index, rs->host);
580 rrdvar_free(st->rrdhost, &st->rrdfamily->variables_root_index, rs->family_name);
581 rrdvar_free(st->rrdhost, &st->rrdhost->variables_root_index, rs->host_name);
583 if(st->variables == rs) {
584 st->variables = rs->next;
588 for (t = st->variables; t && t->next != rs; t = t->next);
589 if(!t) error("RRDSETVAR '%s' not found in chart '%s' variables linked list", rs->fullname, st->id);
590 else t->next = rs->next;
599 // ----------------------------------------------------------------------------
600 // RRDDIMVAR management
602 #define RRDDIMVAR_ID_MAX 1024
604 RRDDIMVAR *rrddimvar_create(RRDDIM *rd, int type, const char *prefix, const char *suffix, void *value, uint32_t options) {
605 RRDSET *st = rd->rrdset;
607 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:"");
609 if(!prefix) prefix = "";
610 if(!suffix) suffix = "";
612 char buffer[RRDDIMVAR_ID_MAX + 1];
613 RRDDIMVAR *rs = (RRDDIMVAR *)callocz(1, sizeof(RRDDIMVAR));
615 rs->prefix = strdupz(prefix);
616 rs->suffix = strdupz(suffix);
618 snprintfz(buffer, RRDDIMVAR_ID_MAX, "%s%s%s", rs->prefix, rd->id, rs->suffix);
619 rs->id = strdupz(buffer);
621 snprintfz(buffer, RRDDIMVAR_ID_MAX, "%s%s%s", rs->prefix, rd->name, rs->suffix);
622 rs->name = strdupz(buffer);
624 snprintfz(buffer, RRDDIMVAR_ID_MAX, "%s.%s", rd->rrdset->id, rs->id);
625 rs->fullidid = strdupz(buffer);
627 snprintfz(buffer, RRDDIMVAR_ID_MAX, "%s.%s", rd->rrdset->id, rs->name);
628 rs->fullidname = strdupz(buffer);
630 snprintfz(buffer, RRDDIMVAR_ID_MAX, "%s.%s", rd->rrdset->name, rs->id);
631 rs->fullnameid = strdupz(buffer);
633 snprintfz(buffer, RRDDIMVAR_ID_MAX, "%s.%s", rd->rrdset->name, rs->name);
634 rs->fullnamename = strdupz(buffer);
638 rs->options = options;
641 rs->local_id = rrdvar_create_and_index("local", &st->variables_root_index, rs->id, rs->type, rs->value);
642 rs->local_name = rrdvar_create_and_index("local", &st->variables_root_index, rs->name, rs->type, rs->value);
644 rs->family_id = rrdvar_create_and_index("family", &st->rrdfamily->variables_root_index, rs->id, rs->type, rs->value);
645 rs->family_name = rrdvar_create_and_index("family", &st->rrdfamily->variables_root_index, rs->name, rs->type, rs->value);
647 rs->host_fullidid = rrdvar_create_and_index("host", &st->rrdhost->variables_root_index, rs->fullidid, rs->type, rs->value);
648 rs->host_fullidname = rrdvar_create_and_index("host", &st->rrdhost->variables_root_index, rs->fullidname, rs->type, rs->value);
649 rs->host_fullnameid = rrdvar_create_and_index("host", &st->rrdhost->variables_root_index, rs->fullnameid, rs->type, rs->value);
650 rs->host_fullnamename = rrdvar_create_and_index("host", &st->rrdhost->variables_root_index, rs->fullnamename, rs->type, rs->value);
652 rs->next = rd->variables;
658 void rrddimvar_rename_all(RRDDIM *rd) {
659 RRDSET *st = rd->rrdset;
660 debug(D_VARIABLES, "RRDDIMSET rename for chart id '%s' name '%s', dimension id '%s', name '%s'", st->id, st->name, rd->id, rd->name);
662 RRDDIMVAR *rs, *next = rd->variables;
666 if (strcmp(rd->name, rs->name)) {
667 char buffer[RRDDIMVAR_ID_MAX + 1];
671 rrdvar_free(st->rrdhost, &st->variables_root_index, rs->local_name);
673 snprintfz(buffer, RRDDIMVAR_ID_MAX, "%s%s%s", rs->prefix, rd->name, rs->suffix);
674 rs->name = strdupz(buffer);
675 rs->local_name = rrdvar_create_and_index("local", &st->variables_root_index, rs->name, rs->type, rs->value);
677 rrdvar_free(st->rrdhost, &st->rrdhost->variables_root_index, rs->host_fullidname);
678 freez(rs->fullidname);
679 snprintfz(buffer, RRDDIMVAR_ID_MAX, "%s.%s", st->id, rs->name);
680 rs->fullidname = strdupz(buffer);
681 rs->host_fullidname = rrdvar_create_and_index("host", &st->rrdhost->variables_root_index,
682 rs->fullidname, rs->type, rs->value);
685 rrdvar_free(st->rrdhost, &st->rrdhost->variables_root_index, rs->host_fullnameid);
686 freez(rs->fullnameid);
687 snprintfz(buffer, RRDDIMVAR_ID_MAX, "%s.%s", st->name, rs->id);
688 rs->fullnameid = strdupz(buffer);
689 rs->host_fullnameid = rrdvar_create_and_index("host", &st->rrdhost->variables_root_index,
690 rs->fullnameid, rs->type, rs->value);
693 rrdvar_free(st->rrdhost, &st->rrdhost->variables_root_index, rs->host_fullnamename);
694 freez(rs->fullnamename);
695 snprintfz(buffer, RRDDIMVAR_ID_MAX, "%s.%s", st->name, rs->name);
696 rs->fullnamename = strdupz(buffer);
697 rs->host_fullnamename = rrdvar_create_and_index("host", &st->rrdhost->variables_root_index,
698 rs->fullnamename, rs->type, rs->value);
703 void rrddimvar_free(RRDDIMVAR *rs) {
704 RRDDIM *rd = rs->rrddim;
705 RRDSET *st = rd->rrdset;
706 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);
708 rrdvar_free(st->rrdhost, &st->variables_root_index, rs->local_id);
709 rrdvar_free(st->rrdhost, &st->variables_root_index, rs->local_name);
711 rrdvar_free(st->rrdhost, &st->rrdfamily->variables_root_index, rs->family_id);
712 rrdvar_free(st->rrdhost, &st->rrdfamily->variables_root_index, rs->family_name);
714 rrdvar_free(st->rrdhost, &st->rrdhost->variables_root_index, rs->host_fullidid);
715 rrdvar_free(st->rrdhost, &st->rrdhost->variables_root_index, rs->host_fullidname);
716 rrdvar_free(st->rrdhost, &st->rrdhost->variables_root_index, rs->host_fullnameid);
717 rrdvar_free(st->rrdhost, &st->rrdhost->variables_root_index, rs->host_fullnamename);
719 if(rd->variables == rs) {
720 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);
721 rd->variables = rs->next;
724 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);
726 for (t = rd->variables; t && t->next != rs; t = t->next) ;
727 if(!t) error("RRDDIMVAR '%s' not found in dimension '%s/%s' variables linked list", rs->name, st->id, rd->id);
728 else t->next = rs->next;
736 freez(rs->fullidname);
737 freez(rs->fullnameid);
738 freez(rs->fullnamename);
742 // ----------------------------------------------------------------------------
743 // RRDCALC management
745 static inline const char *rrdcalc_status2string(int status) {
747 case RRDCALC_STATUS_REMOVED:
750 case RRDCALC_STATUS_UNDEFINED:
753 case RRDCALC_STATUS_UNINITIALIZED:
754 return "UNINITIALIZED";
756 case RRDCALC_STATUS_CLEAR:
759 case RRDCALC_STATUS_RAISED:
762 case RRDCALC_STATUS_WARNING:
765 case RRDCALC_STATUS_CRITICAL:
769 error("Unknown alarm status %d", status);
774 static void rrdsetcalc_link(RRDSET *st, RRDCALC *rc) {
775 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);
777 rc->last_status_change = time(NULL);
780 rc->rrdset_next = st->alarms;
781 rc->rrdset_prev = NULL;
784 rc->rrdset_next->rrdset_prev = rc;
788 if(rc->update_every < rc->rrdset->update_every) {
789 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);
790 rc->update_every = rc->rrdset->update_every;
793 if(!isnan(rc->green) && isnan(st->green)) {
794 debug(D_HEALTH, "Health alarm '%s.%s' green threshold set from %Lf to %Lf.", rc->rrdset->id, rc->name, rc->rrdset->green, rc->green);
795 st->green = rc->green;
798 if(!isnan(rc->red) && isnan(st->red)) {
799 debug(D_HEALTH, "Health alarm '%s.%s' red threshold set from %Lf to %Lf.", rc->rrdset->id, rc->name, rc->rrdset->red, rc->red);
803 rc->local = rrdvar_create_and_index("local", &st->variables_root_index, rc->name, RRDVAR_TYPE_CALCULATED, &rc->value);
804 rc->family = rrdvar_create_and_index("family", &st->rrdfamily->variables_root_index, rc->name, RRDVAR_TYPE_CALCULATED, &rc->value);
806 char fullname[RRDVAR_MAX_LENGTH + 1];
807 snprintfz(fullname, RRDVAR_MAX_LENGTH, "%s.%s", st->id, rc->name);
808 rc->hostid = rrdvar_create_and_index("host", &st->rrdhost->variables_root_index, fullname, RRDVAR_TYPE_CALCULATED, &rc->value);
810 snprintfz(fullname, RRDVAR_MAX_LENGTH, "%s.%s", st->name, rc->name);
811 rc->hostname = rrdvar_create_and_index("host", &st->rrdhost->variables_root_index, fullname, RRDVAR_TYPE_CALCULATED, &rc->value);
813 if(!rc->units) rc->units = strdupz(st->units);
816 time_t now = time(NULL);
817 health_alarm_log(st->rrdhost, rc->id, rc->next_event_id++, now, rc->name, rc->rrdset->id, rc->rrdset->family, rc->exec, rc->recipient, now - rc->last_status_change, rc->old_value, rc->value, rc->status, RRDCALC_STATUS_UNINITIALIZED, rc->source, rc->units, rc->info, 0);
821 static inline int rrdcalc_is_matching_this_rrdset(RRDCALC *rc, RRDSET *st) {
822 if( (rc->hash_chart == st->hash && !strcmp(rc->chart, st->id)) ||
823 (rc->hash_chart == st->hash_name && !strcmp(rc->chart, st->name)))
829 // this has to be called while the RRDHOST is locked
830 inline void rrdsetcalc_link_matching(RRDSET *st) {
831 // debug(D_HEALTH, "find matching alarms for chart '%s'", st->id);
834 for(rc = st->rrdhost->alarms; rc ; rc = rc->next) {
835 if(unlikely(rc->rrdset))
838 if(unlikely(rrdcalc_is_matching_this_rrdset(rc, st)))
839 rrdsetcalc_link(st, rc);
843 // this has to be called while the RRDHOST is locked
844 inline void rrdsetcalc_unlink(RRDCALC *rc) {
845 RRDSET *st = rc->rrdset;
848 debug(D_HEALTH, "Requested to unlink RRDCALC '%s.%s' which is not linked to any RRDSET", rc->chart?rc->chart:"NOCHART", rc->name);
849 error("Requested to unlink RRDCALC '%s.%s' which is not linked to any RRDSET", rc->chart?rc->chart:"NOCHART", rc->name);
854 time_t now = time(NULL);
855 health_alarm_log(st->rrdhost, rc->id, rc->next_event_id++, now, rc->name, rc->rrdset->id, rc->rrdset->family, rc->exec, rc->recipient, now - rc->last_status_change, rc->old_value, rc->value, rc->status, RRDCALC_STATUS_REMOVED, rc->source, rc->units, rc->info, 0);
858 RRDHOST *host = st->rrdhost;
860 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);
864 rc->rrdset_prev->rrdset_next = rc->rrdset_next;
867 rc->rrdset_next->rrdset_prev = rc->rrdset_prev;
870 st->alarms = rc->rrdset_next;
872 rc->rrdset_prev = rc->rrdset_next = NULL;
874 rrdvar_free(st->rrdhost, &st->variables_root_index, rc->local);
877 rrdvar_free(st->rrdhost, &st->rrdfamily->variables_root_index, rc->family);
880 rrdvar_free(st->rrdhost, &st->rrdhost->variables_root_index, rc->hostid);
883 rrdvar_free(st->rrdhost, &st->rrdhost->variables_root_index, rc->hostname);
888 // RRDCALC will remain in RRDHOST
889 // so that if the matching chart is found in the future
890 // it will be applied automatically
893 RRDCALC *rrdcalc_find(RRDSET *st, const char *name) {
895 uint32_t hash = simple_hash(name);
897 for( rc = st->alarms; rc ; rc = rc->rrdset_next ) {
898 if(unlikely(rc->hash == hash && !strcmp(rc->name, name)))
905 static inline int rrdcalc_exists(RRDHOST *host, const char *chart, const char *name, uint32_t hash_chart, uint32_t hash_name) {
908 if(unlikely(!chart)) {
909 error("attempt to find RRDCALC '%s' without giving a chart name", name);
913 if(unlikely(!hash_chart)) hash_chart = simple_hash(chart);
914 if(unlikely(!hash_name)) hash_name = simple_hash(name);
916 // make sure it does not already exist
917 for(rc = host->alarms; rc ; rc = rc->next) {
918 if (unlikely(rc->chart && rc->hash == hash_name && rc->hash_chart == hash_chart && !strcmp(name, rc->name) && !strcmp(chart, rc->chart))) {
919 debug(D_HEALTH, "Health alarm '%s.%s' already exists in host '%s'.", chart, name, host->hostname);
920 error("Health alarm '%s.%s' already exists in host '%s'.", chart, name, host->hostname);
928 static inline uint32_t rrdcalc_get_unique_id(RRDHOST *host, const char *chart, const char *name, uint32_t *next_event_id) {
930 uint32_t hash_chart = simple_hash(chart);
931 uint32_t hash_name = simple_hash(name);
933 // re-use old IDs, by looking them up in the alarm log
935 for(ae = host->health_log.alarms; ae ;ae = ae->next) {
936 if(unlikely(ae->hash_name == hash_name && ae->hash_chart == hash_chart && !strcmp(name, ae->name) && !strcmp(chart, ae->chart))) {
937 if(next_event_id) *next_event_id = ae->alarm_event_id + 1;
943 return host->health_log.next_alarm_id++;
946 static inline void rrdcalc_create_part2(RRDHOST *host, RRDCALC *rc) {
947 rrdhost_check_rdlock(host);
949 if(rc->calculation) {
950 rc->calculation->status = &rc->status;
951 rc->calculation->this = &rc->value;
952 rc->calculation->after = &rc->db_after;
953 rc->calculation->before = &rc->db_before;
954 rc->calculation->rrdcalc = rc;
958 rc->warning->status = &rc->status;
959 rc->warning->this = &rc->value;
960 rc->warning->after = &rc->db_after;
961 rc->warning->before = &rc->db_before;
962 rc->warning->rrdcalc = rc;
966 rc->critical->status = &rc->status;
967 rc->critical->this = &rc->value;
968 rc->critical->after = &rc->db_after;
969 rc->critical->before = &rc->db_before;
970 rc->critical->rrdcalc = rc;
973 // link it to the host
974 if(likely(host->alarms)) {
977 for(t = host->alarms; t && t->next ; t = t->next) ;
984 // link it to its chart
986 for(st = host->rrdset_root; st ; st = st->next) {
987 if(rrdcalc_is_matching_this_rrdset(rc, st)) {
988 rrdsetcalc_link(st, rc);
994 static inline RRDCALC *rrdcalc_create(RRDHOST *host, RRDCALCTEMPLATE *rt, const char *chart) {
996 debug(D_HEALTH, "Health creating dynamic alarm (from template) '%s.%s'", chart, rt->name);
998 if(rrdcalc_exists(host, chart, rt->name, 0, 0))
1001 RRDCALC *rc = callocz(1, sizeof(RRDCALC));
1002 rc->next_event_id = 1;
1003 rc->id = rrdcalc_get_unique_id(host, chart, rt->name, &rc->next_event_id);
1004 rc->name = strdupz(rt->name);
1005 rc->hash = simple_hash(rc->name);
1006 rc->chart = strdupz(chart);
1007 rc->hash_chart = simple_hash(rc->chart);
1009 if(rt->dimensions) rc->dimensions = strdupz(rt->dimensions);
1011 rc->green = rt->green;
1014 rc->old_value = NAN;
1016 rc->delay_up_duration = rt->delay_up_duration;
1017 rc->delay_down_duration = rt->delay_down_duration;
1018 rc->delay_max_duration = rt->delay_max_duration;
1019 rc->delay_multiplier = rt->delay_multiplier;
1021 rc->group = rt->group;
1022 rc->after = rt->after;
1023 rc->before = rt->before;
1024 rc->update_every = rt->update_every;
1025 rc->options = rt->options;
1027 if(rt->exec) rc->exec = strdupz(rt->exec);
1028 if(rt->recipient) rc->recipient = strdupz(rt->recipient);
1029 if(rt->source) rc->source = strdupz(rt->source);
1030 if(rt->units) rc->units = strdupz(rt->units);
1031 if(rt->info) rc->info = strdupz(rt->info);
1033 if(rt->calculation) {
1034 rc->calculation = expression_parse(rt->calculation->source, NULL, NULL);
1035 if(!rc->calculation)
1036 error("Health alarm '%s.%s': failed to parse calculation expression '%s'", chart, rt->name, rt->calculation->source);
1039 rc->warning = expression_parse(rt->warning->source, NULL, NULL);
1041 error("Health alarm '%s.%s': failed to re-parse warning expression '%s'", chart, rt->name, rt->warning->source);
1044 rc->critical = expression_parse(rt->critical->source, NULL, NULL);
1046 error("Health alarm '%s.%s': failed to re-parse critical expression '%s'", chart, rt->name, rt->critical->source);
1049 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', delay up %d, delay down %d, delay max %d, delay_multiplier %f",
1050 (rc->chart)?rc->chart:"NOCHART",
1052 (rc->exec)?rc->exec:"DEFAULT",
1053 (rc->recipient)?rc->recipient:"DEFAULT",
1060 (rc->dimensions)?rc->dimensions:"NONE",
1062 (rc->calculation)?rc->calculation->parsed_as:"NONE",
1063 (rc->warning)?rc->warning->parsed_as:"NONE",
1064 (rc->critical)?rc->critical->parsed_as:"NONE",
1066 rc->delay_up_duration,
1067 rc->delay_down_duration,
1068 rc->delay_max_duration,
1069 rc->delay_multiplier
1072 rrdcalc_create_part2(host, rc);
1076 void rrdcalc_free(RRDHOST *host, RRDCALC *rc) {
1079 debug(D_HEALTH, "Health removing alarm '%s.%s' of host '%s'", rc->chart?rc->chart:"NOCHART", rc->name, host->hostname);
1081 // unlink it from RRDSET
1082 if(rc->rrdset) rrdsetcalc_unlink(rc);
1084 // unlink it from RRDHOST
1085 if(unlikely(rc == host->alarms))
1086 host->alarms = rc->next;
1088 else if(likely(host->alarms)) {
1089 RRDCALC *t, *last = host->alarms;
1090 for(t = last->next; t && t != rc; last = t, t = t->next) ;
1091 if(last->next == rc)
1092 last->next = rc->next;
1094 error("Cannot unlink alarm '%s.%s' from host '%s': not found", rc->chart?rc->chart:"NOCHART", rc->name, host->hostname);
1097 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);
1099 expression_free(rc->calculation);
1100 expression_free(rc->warning);
1101 expression_free(rc->critical);
1106 freez(rc->dimensions);
1108 freez(rc->recipient);
1115 // ----------------------------------------------------------------------------
1116 // RRDCALCTEMPLATE management
1118 void rrdcalctemplate_link_matching(RRDSET *st) {
1119 RRDCALCTEMPLATE *rt;
1121 for(rt = st->rrdhost->templates; rt ; rt = rt->next) {
1122 if(rt->hash_context == st->hash_context && !strcmp(rt->context, st->context)) {
1123 RRDCALC *rc = rrdcalc_create(st->rrdhost, rt, st->id);
1125 error("Health tried to create alarm from template '%s', but it failed", rt->name);
1127 #ifdef NETDATA_INTERNAL_CHECKS
1128 else if(rc->rrdset != st)
1129 error("Health alarm '%s.%s' should be linked to chart '%s', but it is not", rc->chart?rc->chart:"NOCHART", rc->name, st->id);
1135 static inline void rrdcalctemplate_free(RRDHOST *host, RRDCALCTEMPLATE *rt) {
1136 debug(D_HEALTH, "Health removing template '%s' of host '%s'", rt->name, host->hostname);
1138 if(host->templates) {
1139 if(host->templates == rt) {
1140 host->templates = rt->next;
1143 RRDCALCTEMPLATE *t, *last = host->templates;
1144 for (t = last->next; t && t != rt; last = t, t = t->next ) ;
1145 if(last && last->next == rt) {
1146 last->next = rt->next;
1150 error("Cannot find RRDCALCTEMPLATE '%s' linked in host '%s'", rt->name, host->hostname);
1154 expression_free(rt->calculation);
1155 expression_free(rt->warning);
1156 expression_free(rt->critical);
1160 freez(rt->recipient);
1165 freez(rt->dimensions);
1169 // ----------------------------------------------------------------------------
1170 // load health configuration
1172 #define HEALTH_CONF_MAX_LINE 4096
1174 #define HEALTH_ALARM_KEY "alarm"
1175 #define HEALTH_TEMPLATE_KEY "template"
1176 #define HEALTH_ON_KEY "on"
1177 #define HEALTH_LOOKUP_KEY "lookup"
1178 #define HEALTH_CALC_KEY "calc"
1179 #define HEALTH_EVERY_KEY "every"
1180 #define HEALTH_GREEN_KEY "green"
1181 #define HEALTH_RED_KEY "red"
1182 #define HEALTH_WARN_KEY "warn"
1183 #define HEALTH_CRIT_KEY "crit"
1184 #define HEALTH_EXEC_KEY "exec"
1185 #define HEALTH_RECIPIENT_KEY "to"
1186 #define HEALTH_UNITS_KEY "units"
1187 #define HEALTH_INFO_KEY "info"
1188 #define HEALTH_DELAY_KEY "delay"
1190 static inline int rrdcalc_add_alarm_from_config(RRDHOST *host, RRDCALC *rc) {
1192 error("Health configuration for alarm '%s' does not have a chart", rc->name);
1196 if(!rc->update_every) {
1197 error("Health configuration for alarm '%s.%s' has no frequency (parameter 'every'). Ignoring it.", rc->chart?rc->chart:"NOCHART", rc->name);
1201 if(!RRDCALC_HAS_DB_LOOKUP(rc) && !rc->warning && !rc->critical) {
1202 error("Health configuration for alarm '%s.%s' is useless (no calculation, no warning and no critical evaluation)", rc->chart?rc->chart:"NOCHART", rc->name);
1206 if (rrdcalc_exists(host, rc->chart, rc->name, rc->hash_chart, rc->hash))
1209 rc->id = rrdcalc_get_unique_id(&localhost, rc->chart, rc->name, &rc->next_event_id);
1211 debug(D_HEALTH, "Health configuration adding alarm '%s.%s' (%u): 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', delay up %d, delay down %d, delay max %d, delay_multiplier %f",
1212 rc->chart?rc->chart:"NOCHART",
1215 (rc->exec)?rc->exec:"DEFAULT",
1216 (rc->recipient)?rc->recipient:"DEFAULT",
1223 (rc->dimensions)?rc->dimensions:"NONE",
1225 (rc->calculation)?rc->calculation->parsed_as:"NONE",
1226 (rc->warning)?rc->warning->parsed_as:"NONE",
1227 (rc->critical)?rc->critical->parsed_as:"NONE",
1229 rc->delay_up_duration,
1230 rc->delay_down_duration,
1231 rc->delay_max_duration,
1232 rc->delay_multiplier
1235 rrdcalc_create_part2(host, rc);
1239 static inline int rrdcalctemplate_add_template_from_config(RRDHOST *host, RRDCALCTEMPLATE *rt) {
1240 if(unlikely(!rt->context)) {
1241 error("Health configuration for template '%s' does not have a context", rt->name);
1245 if(unlikely(!rt->update_every)) {
1246 error("Health configuration for template '%s' has no frequency (parameter 'every'). Ignoring it.", rt->name);
1250 if(unlikely(!RRDCALCTEMPLATE_HAS_CALCULATION(rt) && !rt->warning && !rt->critical)) {
1251 error("Health configuration for template '%s' is useless (no calculation, no warning and no critical evaluation)", rt->name);
1255 RRDCALCTEMPLATE *t, *last = NULL;
1256 for (t = host->templates; t ; last = t, t = t->next) {
1257 if(unlikely(t->hash_name == rt->hash_name && !strcmp(t->name, rt->name))) {
1258 error("Health configuration template '%s' already exists for host '%s'.", rt->name, host->hostname);
1263 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', delay up %d, delay down %d, delay max %d, delay_multiplier %f",
1265 (rt->context)?rt->context:"NONE",
1266 (rt->exec)?rt->exec:"DEFAULT",
1267 (rt->recipient)?rt->recipient:"DEFAULT",
1274 (rt->dimensions)?rt->dimensions:"NONE",
1276 (rt->calculation)?rt->calculation->parsed_as:"NONE",
1277 (rt->warning)?rt->warning->parsed_as:"NONE",
1278 (rt->critical)?rt->critical->parsed_as:"NONE",
1280 rt->delay_up_duration,
1281 rt->delay_down_duration,
1282 rt->delay_max_duration,
1283 rt->delay_multiplier
1290 rt->next = host->templates;
1291 host->templates = rt;
1297 static inline int health_parse_duration(char *string, int *result) {
1298 // make sure it is a number
1299 if(!*string || !(isdigit(*string) || *string == '+' || *string == '-')) {
1305 calculated_number n = strtold(string, &e);
1309 *result = (int) (n * 86400 * 365);
1312 *result = (int) (n * 86400 * 30);
1315 *result = (int) (n * 86400 * 7);
1318 *result = (int) (n * 86400);
1321 *result = (int) (n * 3600);
1324 *result = (int) (n * 60);
1329 *result = (int) (n);
1339 static inline int health_parse_delay(
1340 size_t line, const char *path, const char *file, char *string,
1341 int *delay_up_duration,
1342 int *delay_down_duration,
1343 int *delay_max_duration,
1344 float *delay_multiplier) {
1347 char given_down = 0;
1349 char given_multiplier = 0;
1355 while(*s && !isspace(*s)) s++;
1356 while(*s && isspace(*s)) *s++ = '\0';
1361 while(*s && !isspace(*s)) s++;
1362 while(*s && isspace(*s)) *s++ = '\0';
1364 if(!strcasecmp(key, "up")) {
1365 if (!health_parse_duration(value, delay_up_duration)) {
1366 error("Health configuration at line %zu of file '%s/%s': invalid value '%s' for '%s' keyword",
1367 line, path, file, value, key);
1371 else if(!strcasecmp(key, "down")) {
1372 if (!health_parse_duration(value, delay_down_duration)) {
1373 error("Health configuration at line %zu of file '%s/%s': invalid value '%s' for '%s' keyword",
1374 line, path, file, value, key);
1376 else given_down = 1;
1378 else if(!strcasecmp(key, "multiplier")) {
1379 *delay_multiplier = strtof(value, NULL);
1380 if(isnan(*delay_multiplier) || isinf(*delay_multiplier) || islessequal(*delay_multiplier, 0)) {
1381 error("Health configuration at line %zu of file '%s/%s': invalid value '%s' for '%s' keyword",
1382 line, path, file, value, key);
1384 else given_multiplier = 1;
1386 else if(!strcasecmp(key, "max")) {
1387 if (!health_parse_duration(value, delay_max_duration)) {
1388 error("Health configuration at line %zu of file '%s/%s': invalid value '%s' for '%s' keyword",
1389 line, path, file, value, key);
1394 error("Health configuration at line %zu of file '%s/%s': unknown keyword '%s'",
1395 line, path, file, key);
1400 *delay_up_duration = 0;
1403 *delay_down_duration = 0;
1405 if(!given_multiplier)
1406 *delay_multiplier = 1.0;
1409 if((*delay_max_duration) < (*delay_up_duration) * (*delay_multiplier))
1410 *delay_max_duration = (*delay_up_duration) * (*delay_multiplier);
1412 if((*delay_max_duration) < (*delay_down_duration) * (*delay_multiplier))
1413 *delay_max_duration = (*delay_down_duration) * (*delay_multiplier);
1419 static inline int health_parse_db_lookup(
1420 size_t line, const char *path, const char *file, char *string,
1421 int *group_method, int *after, int *before, int *every,
1422 uint32_t *options, char **dimensions
1424 debug(D_HEALTH, "Health configuration parsing database lookup %zu@%s/%s: %s", line, path, file, string);
1426 if(*dimensions) freez(*dimensions);
1433 char *s = string, *key;
1435 // first is the group method
1437 while(*s && !isspace(*s)) s++;
1438 while(*s && isspace(*s)) *s++ = '\0';
1440 error("Health configuration invalid chart calculation at line %zu of file '%s/%s': expected group method followed by the 'after' time, but got '%s'",
1441 line, path, file, key);
1445 if((*group_method = web_client_api_request_v1_data_group(key, -1)) == -1) {
1446 error("Health configuration at line %zu of file '%s/%s': invalid group method '%s'",
1447 line, path, file, key);
1451 // then is the 'after' time
1453 while(*s && !isspace(*s)) s++;
1454 while(*s && isspace(*s)) *s++ = '\0';
1456 if(!health_parse_duration(key, after)) {
1457 error("Health configuration at line %zu of file '%s/%s': invalid duration '%s' after group method",
1458 line, path, file, key);
1463 *every = abs(*after);
1465 // now we may have optional parameters
1468 while(*s && !isspace(*s)) s++;
1469 while(*s && isspace(*s)) *s++ = '\0';
1472 if(!strcasecmp(key, "at")) {
1474 while(*s && !isspace(*s)) s++;
1475 while(*s && isspace(*s)) *s++ = '\0';
1477 if (!health_parse_duration(value, before)) {
1478 error("Health configuration at line %zu of file '%s/%s': invalid duration '%s' for '%s' keyword",
1479 line, path, file, value, key);
1482 else if(!strcasecmp(key, HEALTH_EVERY_KEY)) {
1484 while(*s && !isspace(*s)) s++;
1485 while(*s && isspace(*s)) *s++ = '\0';
1487 if (!health_parse_duration(value, every)) {
1488 error("Health configuration at line %zu of file '%s/%s': invalid duration '%s' for '%s' keyword",
1489 line, path, file, value, key);
1492 else if(!strcasecmp(key, "absolute") || !strcasecmp(key, "abs") || !strcasecmp(key, "absolute_sum")) {
1493 *options |= RRDR_OPTION_ABSOLUTE;
1495 else if(!strcasecmp(key, "min2max")) {
1496 *options |= RRDR_OPTION_MIN2MAX;
1498 else if(!strcasecmp(key, "null2zero")) {
1499 *options |= RRDR_OPTION_NULL2ZERO;
1501 else if(!strcasecmp(key, "percentage")) {
1502 *options |= RRDR_OPTION_PERCENTAGE;
1504 else if(!strcasecmp(key, "unaligned")) {
1505 *options |= RRDR_OPTION_NOT_ALIGNED;
1507 else if(!strcasecmp(key, "of")) {
1508 if(*s && strcasecmp(s, "all"))
1509 *dimensions = strdupz(s);
1513 error("Health configuration at line %zu of file '%s/%s': unknown keyword '%s'",
1514 line, path, file, key);
1521 static inline char *tabs2spaces(char *s) {
1524 if(unlikely(*t == '\t')) *t = ' ';
1531 static inline char *health_source_file(size_t line, const char *path, const char *filename) {
1532 char buffer[FILENAME_MAX + 1];
1533 snprintfz(buffer, FILENAME_MAX, "%zu@%s/%s", line, path, filename);
1534 return strdupz(buffer);
1537 static inline void strip_quotes(char *s) {
1539 if(*s == '\'' || *s == '"') *s = ' ';
1544 int health_readfile(const char *path, const char *filename) {
1545 debug(D_HEALTH, "Health configuration reading file '%s/%s'", path, filename);
1547 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, hash_delay = 0;
1548 char buffer[HEALTH_CONF_MAX_LINE + 1];
1550 if(unlikely(!hash_alarm)) {
1551 hash_alarm = simple_uhash(HEALTH_ALARM_KEY);
1552 hash_template = simple_uhash(HEALTH_TEMPLATE_KEY);
1553 hash_on = simple_uhash(HEALTH_ON_KEY);
1554 hash_calc = simple_uhash(HEALTH_CALC_KEY);
1555 hash_lookup = simple_uhash(HEALTH_LOOKUP_KEY);
1556 hash_green = simple_uhash(HEALTH_GREEN_KEY);
1557 hash_red = simple_uhash(HEALTH_RED_KEY);
1558 hash_warn = simple_uhash(HEALTH_WARN_KEY);
1559 hash_crit = simple_uhash(HEALTH_CRIT_KEY);
1560 hash_exec = simple_uhash(HEALTH_EXEC_KEY);
1561 hash_every = simple_uhash(HEALTH_EVERY_KEY);
1562 hash_units = simple_hash(HEALTH_UNITS_KEY);
1563 hash_info = simple_hash(HEALTH_INFO_KEY);
1564 hash_recipient = simple_hash(HEALTH_RECIPIENT_KEY);
1565 hash_delay = simple_uhash(HEALTH_DELAY_KEY);
1568 snprintfz(buffer, HEALTH_CONF_MAX_LINE, "%s/%s", path, filename);
1569 FILE *fp = fopen(buffer, "r");
1571 error("Health configuration cannot read file '%s'.", buffer);
1576 RRDCALCTEMPLATE *rt = NULL;
1578 size_t line = 0, append = 0;
1580 while((s = fgets(&buffer[append], (int)(HEALTH_CONF_MAX_LINE - append), fp)) || append) {
1581 int stop_appending = !s;
1583 // info("Line %zu of file '%s/%s': '%s'", line, path, filename, s);
1586 // info("Trimmed line %zu of file '%s/%s': '%s'", line, path, filename, s);
1589 if(!stop_appending && s[append - 1] == '\\') {
1590 s[append - 1] = ' ';
1591 append = &s[append] - buffer;
1592 if(append < HEALTH_CONF_MAX_LINE)
1595 error("Health configuration has too long muli-line at line %zu of file '%s/%s'.", line, path, filename);
1601 while(*s && *s != ':') s++;
1603 error("Health configuration has invalid line %zu of file '%s/%s'. It does not contain a ':'. Ignoring it.", line, path, filename);
1611 value = trim(value);
1614 error("Health configuration has invalid line %zu of file '%s/%s'. Keyword is empty. Ignoring it.", line, path, filename);
1619 error("Health configuration has invalid line %zu of file '%s/%s'. value is empty. Ignoring it.", line, path, filename);
1623 // info("Health file '%s/%s', key '%s', value '%s'", path, filename, key, value);
1624 uint32_t hash = simple_uhash(key);
1626 if(hash == hash_alarm && !strcasecmp(key, HEALTH_ALARM_KEY)) {
1627 if(rc && !rrdcalc_add_alarm_from_config(&localhost, rc))
1628 rrdcalc_free(&localhost, rc);
1631 if (!rrdcalctemplate_add_template_from_config(&localhost, rt))
1632 rrdcalctemplate_free(&localhost, rt);
1636 rc = callocz(1, sizeof(RRDCALC));
1637 rc->next_event_id = 1;
1638 rc->name = tabs2spaces(strdupz(value));
1639 rc->hash = simple_hash(rc->name);
1640 rc->source = health_source_file(line, path, filename);
1644 rc->old_value = NAN;
1645 rc->delay_multiplier = 1.0;
1647 if(rrdvar_fix_name(rc->name))
1648 error("Health configuration renamed alarm '%s' to '%s'", value, rc->name);
1650 else if(hash == hash_template && !strcasecmp(key, HEALTH_TEMPLATE_KEY)) {
1652 if(!rrdcalc_add_alarm_from_config(&localhost, rc))
1653 rrdcalc_free(&localhost, rc);
1657 if(rt && !rrdcalctemplate_add_template_from_config(&localhost, rt))
1658 rrdcalctemplate_free(&localhost, rt);
1660 rt = callocz(1, sizeof(RRDCALCTEMPLATE));
1661 rt->name = tabs2spaces(strdupz(value));
1662 rt->hash_name = simple_hash(rt->name);
1663 rt->source = health_source_file(line, path, filename);
1666 rt->delay_multiplier = 1.0;
1668 if(rrdvar_fix_name(rt->name))
1669 error("Health configuration renamed template '%s' to '%s'", value, rt->name);
1672 if(hash == hash_on && !strcasecmp(key, HEALTH_ON_KEY)) {
1674 if(strcmp(rc->chart, value))
1675 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').",
1676 line, path, filename, rc->name, key, rc->chart, value, value);
1680 rc->chart = tabs2spaces(strdupz(value));
1681 rc->hash_chart = simple_hash(rc->chart);
1683 else if(hash == hash_lookup && !strcasecmp(key, HEALTH_LOOKUP_KEY)) {
1684 health_parse_db_lookup(line, path, filename, value, &rc->group, &rc->after, &rc->before,
1686 &rc->options, &rc->dimensions);
1688 else if(hash == hash_every && !strcasecmp(key, HEALTH_EVERY_KEY)) {
1689 if(!health_parse_duration(value, &rc->update_every))
1690 info("Health configuration at line %zu of file '%s/%s' for alarm '%s' at key '%s' cannot parse duration: '%s'.",
1691 line, path, filename, rc->name, key, value);
1693 else if(hash == hash_green && !strcasecmp(key, HEALTH_GREEN_KEY)) {
1695 rc->green = strtold(value, &e);
1697 info("Health configuration at line %zu of file '%s/%s' for alarm '%s' at key '%s' leaves this string unmatched: '%s'.",
1698 line, path, filename, rc->name, key, e);
1701 else if(hash == hash_red && !strcasecmp(key, HEALTH_RED_KEY)) {
1703 rc->red = strtold(value, &e);
1705 info("Health configuration at line %zu of file '%s/%s' for alarm '%s' at key '%s' leaves this string unmatched: '%s'.",
1706 line, path, filename, rc->name, key, e);
1709 else if(hash == hash_calc && !strcasecmp(key, HEALTH_CALC_KEY)) {
1710 const char *failed_at = NULL;
1712 rc->calculation = expression_parse(value, &failed_at, &error);
1713 if(!rc->calculation) {
1714 error("Health configuration at line %zu of file '%s/%s' for alarm '%s' at key '%s' has unparse-able expression '%s': %s at '%s'",
1715 line, path, filename, rc->name, key, value, expression_strerror(error), failed_at);
1718 else if(hash == hash_warn && !strcasecmp(key, HEALTH_WARN_KEY)) {
1719 const char *failed_at = NULL;
1721 rc->warning = expression_parse(value, &failed_at, &error);
1723 error("Health configuration at line %zu of file '%s/%s' for alarm '%s' at key '%s' has unparse-able expression '%s': %s at '%s'",
1724 line, path, filename, rc->name, key, value, expression_strerror(error), failed_at);
1727 else if(hash == hash_crit && !strcasecmp(key, HEALTH_CRIT_KEY)) {
1728 const char *failed_at = NULL;
1730 rc->critical = expression_parse(value, &failed_at, &error);
1732 error("Health configuration at line %zu of file '%s/%s' for alarm '%s' at key '%s' has unparse-able expression '%s': %s at '%s'",
1733 line, path, filename, rc->name, key, value, expression_strerror(error), failed_at);
1736 else if(hash == hash_exec && !strcasecmp(key, HEALTH_EXEC_KEY)) {
1738 if(strcmp(rc->exec, value))
1739 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').",
1740 line, path, filename, rc->name, key, rc->exec, value, value);
1744 rc->exec = tabs2spaces(strdupz(value));
1746 else if(hash == hash_recipient && !strcasecmp(key, HEALTH_RECIPIENT_KEY)) {
1748 if(strcmp(rc->recipient, value))
1749 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').",
1750 line, path, filename, rc->name, key, rc->recipient, value, value);
1752 freez(rc->recipient);
1754 rc->recipient = tabs2spaces(strdupz(value));
1756 else if(hash == hash_units && !strcasecmp(key, HEALTH_UNITS_KEY)) {
1758 if(strcmp(rc->units, value))
1759 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').",
1760 line, path, filename, rc->name, key, rc->units, value, value);
1764 rc->units = tabs2spaces(strdupz(value));
1765 strip_quotes(rc->units);
1767 else if(hash == hash_info && !strcasecmp(key, HEALTH_INFO_KEY)) {
1769 if(strcmp(rc->info, value))
1770 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').",
1771 line, path, filename, rc->name, key, rc->info, value, value);
1775 rc->info = tabs2spaces(strdupz(value));
1776 strip_quotes(rc->info);
1778 else if(hash == hash_delay && !strcasecmp(key, HEALTH_DELAY_KEY)) {
1779 health_parse_delay(line, path, filename, value, &rc->delay_up_duration, &rc->delay_down_duration, &rc->delay_max_duration, &rc->delay_multiplier);
1782 error("Health configuration at line %zu of file '%s/%s' for alarm '%s' has unknown key '%s'.",
1783 line, path, filename, rc->name, key);
1787 if(hash == hash_on && !strcasecmp(key, HEALTH_ON_KEY)) {
1789 if(strcmp(rt->context, value))
1790 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').",
1791 line, path, filename, rt->name, key, rt->context, value, value);
1795 rt->context = tabs2spaces(strdupz(value));
1796 rt->hash_context = simple_hash(rt->context);
1798 else if(hash == hash_lookup && !strcasecmp(key, HEALTH_LOOKUP_KEY)) {
1799 health_parse_db_lookup(line, path, filename, value, &rt->group, &rt->after, &rt->before,
1801 &rt->options, &rt->dimensions);
1803 else if(hash == hash_every && !strcasecmp(key, HEALTH_EVERY_KEY)) {
1804 if(!health_parse_duration(value, &rt->update_every))
1805 info("Health configuration at line %zu of file '%s/%s' for template '%s' at key '%s' cannot parse duration: '%s'.",
1806 line, path, filename, rt->name, key, value);
1808 else if(hash == hash_green && !strcasecmp(key, HEALTH_GREEN_KEY)) {
1810 rt->green = strtold(value, &e);
1812 info("Health configuration at line %zu of file '%s/%s' for template '%s' at key '%s' leaves this string unmatched: '%s'.",
1813 line, path, filename, rt->name, key, e);
1816 else if(hash == hash_red && !strcasecmp(key, HEALTH_RED_KEY)) {
1818 rt->red = strtold(value, &e);
1820 info("Health configuration at line %zu of file '%s/%s' for template '%s' at key '%s' leaves this string unmatched: '%s'.",
1821 line, path, filename, rt->name, key, e);
1824 else if(hash == hash_calc && !strcasecmp(key, HEALTH_CALC_KEY)) {
1825 const char *failed_at = NULL;
1827 rt->calculation = expression_parse(value, &failed_at, &error);
1828 if(!rt->calculation) {
1829 error("Health configuration at line %zu of file '%s/%s' for template '%s' at key '%s' has unparse-able expression '%s': %s at '%s'",
1830 line, path, filename, rt->name, key, value, expression_strerror(error), failed_at);
1833 else if(hash == hash_warn && !strcasecmp(key, HEALTH_WARN_KEY)) {
1834 const char *failed_at = NULL;
1836 rt->warning = expression_parse(value, &failed_at, &error);
1838 error("Health configuration at line %zu of file '%s/%s' for template '%s' at key '%s' has unparse-able expression '%s': %s at '%s'",
1839 line, path, filename, rt->name, key, value, expression_strerror(error), failed_at);
1842 else if(hash == hash_crit && !strcasecmp(key, HEALTH_CRIT_KEY)) {
1843 const char *failed_at = NULL;
1845 rt->critical = expression_parse(value, &failed_at, &error);
1847 error("Health configuration at line %zu of file '%s/%s' for template '%s' at key '%s' has unparse-able expression '%s': %s at '%s'",
1848 line, path, filename, rt->name, key, value, expression_strerror(error), failed_at);
1851 else if(hash == hash_exec && !strcasecmp(key, HEALTH_EXEC_KEY)) {
1853 if(strcmp(rt->exec, value))
1854 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').",
1855 line, path, filename, rt->name, key, rt->exec, value, value);
1859 rt->exec = tabs2spaces(strdupz(value));
1861 else if(hash == hash_recipient && !strcasecmp(key, HEALTH_RECIPIENT_KEY)) {
1863 if(strcmp(rt->recipient, value))
1864 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').",
1865 line, path, filename, rt->name, key, rt->recipient, value, value);
1867 freez(rt->recipient);
1869 rt->recipient = tabs2spaces(strdupz(value));
1871 else if(hash == hash_units && !strcasecmp(key, HEALTH_UNITS_KEY)) {
1873 if(strcmp(rt->units, value))
1874 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').",
1875 line, path, filename, rt->name, key, rt->units, value, value);
1879 rt->units = tabs2spaces(strdupz(value));
1880 strip_quotes(rt->units);
1882 else if(hash == hash_info && !strcasecmp(key, HEALTH_INFO_KEY)) {
1884 if(strcmp(rt->info, value))
1885 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').",
1886 line, path, filename, rt->name, key, rt->info, value, value);
1890 rt->info = tabs2spaces(strdupz(value));
1891 strip_quotes(rt->info);
1893 else if(hash == hash_delay && !strcasecmp(key, HEALTH_DELAY_KEY)) {
1894 health_parse_delay(line, path, filename, value, &rt->delay_up_duration, &rt->delay_down_duration, &rt->delay_max_duration, &rt->delay_multiplier);
1897 error("Health configuration at line %zu of file '%s/%s' for template '%s' has unknown key '%s'.",
1898 line, path, filename, rt->name, key);
1902 error("Health configuration at line %zu of file '%s/%s' has unknown key '%s'. Expected either '" HEALTH_ALARM_KEY "' or '" HEALTH_TEMPLATE_KEY "'.",
1903 line, path, filename, key);
1907 if(rc && !rrdcalc_add_alarm_from_config(&localhost, rc))
1908 rrdcalc_free(&localhost, rc);
1910 if(rt && !rrdcalctemplate_add_template_from_config(&localhost, rt))
1911 rrdcalctemplate_free(&localhost, rt);
1917 void health_readdir(const char *path) {
1918 size_t pathlen = strlen(path);
1920 debug(D_HEALTH, "Health configuration reading directory '%s'", path);
1922 DIR *dir = opendir(path);
1924 error("Health configuration cannot open directory '%s'.", path);
1928 struct dirent *de = NULL;
1929 while ((de = readdir(dir))) {
1930 size_t len = strlen(de->d_name);
1932 if(de->d_type == DT_DIR
1934 (de->d_name[0] == '.' && de->d_name[1] == '\0')
1935 || (de->d_name[0] == '.' && de->d_name[1] == '.' && de->d_name[2] == '\0')
1937 debug(D_HEALTH, "Ignoring directory '%s'", de->d_name);
1941 else if(de->d_type == DT_DIR) {
1942 char *s = mallocz(pathlen + strlen(de->d_name) + 2);
1945 strcat(s, de->d_name);
1951 else if((de->d_type == DT_LNK || de->d_type == DT_REG || de->d_type == DT_UNKNOWN) &&
1952 len > 5 && !strcmp(&de->d_name[len - 5], ".conf")) {
1953 health_readfile(path, de->d_name);
1956 else debug(D_HEALTH, "Ignoring file '%s'", de->d_name);
1962 static inline char *health_config_dir(void) {
1963 char buffer[FILENAME_MAX + 1];
1964 snprintfz(buffer, FILENAME_MAX, "%s/health.d", config_get("global", "config directory", CONFIG_DIR));
1965 return config_get("health", "health configuration directory", buffer);
1968 void health_init(void) {
1969 debug(D_HEALTH, "Health configuration initializing");
1971 if(!(health_enabled = config_get_boolean("health", "enabled", 1))) {
1972 debug(D_HEALTH, "Health is disabled.");
1976 char *pathname = config_get("health", "health db directory", VARLIB_DIR "/health");
1977 if(mkdir(pathname, 0770) == -1 && errno != EEXIST)
1978 fatal("Cannot create directory '%s'.", pathname);
1980 char filename[FILENAME_MAX + 1];
1981 snprintfz(filename, FILENAME_MAX, "%s/health-log.db", pathname);
1982 health.log_filename = config_get("health", "health db file", filename);
1984 health_alarm_log_load(&localhost);
1985 health_alarm_log_open();
1987 char *path = health_config_dir();
1990 char buffer[FILENAME_MAX + 1];
1991 snprintfz(buffer, FILENAME_MAX, "%s/alarm-notify.sh", config_get("global", "plugins directory", PLUGINS_DIR));
1992 health.health_default_exec = config_get("health", "script to execute on alarm", buffer);
1995 long n = config_get_number("health", "in memory max health log entries", (long)localhost.health_log.max);
1997 error("Health configuration has invalid max log entries %ld. Using default %u", n, localhost.health_log.max);
1998 config_set_number("health", "in memory max health log entries", (long)localhost.health_log.max);
2000 else localhost.health_log.max = (unsigned int)n;
2002 rrdhost_rwlock(&localhost);
2003 health_readdir(path);
2004 rrdhost_unlock(&localhost);
2007 // ----------------------------------------------------------------------------
2010 static inline void health_string2json(BUFFER *wb, const char *prefix, const char *label, const char *value, const char *suffix) {
2012 buffer_sprintf(wb, "%s\"%s\":\"%s\"%s", prefix, label, value, suffix);
2014 buffer_sprintf(wb, "%s\"%s\":null%s", prefix, label, suffix);
2017 static inline void health_alarm_entry2json_nolock(BUFFER *wb, ALARM_ENTRY *ae, RRDHOST *host) {
2018 buffer_sprintf(wb, "\n\t{\n"
2019 "\t\t\"hostname\": \"%s\",\n"
2020 "\t\t\"unique_id\": %u,\n"
2021 "\t\t\"alarm_id\": %u,\n"
2022 "\t\t\"alarm_event_id\": %u,\n"
2023 "\t\t\"name\": \"%s\",\n"
2024 "\t\t\"chart\": \"%s\",\n"
2025 "\t\t\"family\": \"%s\",\n"
2026 "\t\t\"processed\": %s,\n"
2027 "\t\t\"updated\": %s,\n"
2028 "\t\t\"exec_run\": %lu,\n"
2029 "\t\t\"exec_failed\": %s,\n"
2030 "\t\t\"exec\": \"%s\",\n"
2031 "\t\t\"recipient\": \"%s\",\n"
2032 "\t\t\"exec_code\": %d,\n"
2033 "\t\t\"source\": \"%s\",\n"
2034 "\t\t\"units\": \"%s\",\n"
2035 "\t\t\"info\": \"%s\",\n"
2036 "\t\t\"when\": %lu,\n"
2037 "\t\t\"duration\": %lu,\n"
2038 "\t\t\"non_clear_duration\": %lu,\n"
2039 "\t\t\"status\": \"%s\",\n"
2040 "\t\t\"old_status\": \"%s\",\n"
2041 "\t\t\"delay\": %d,\n"
2042 "\t\t\"delay_up_to_timestamp\": %lu,\n"
2043 "\t\t\"updated_by_id\": %u,\n"
2044 "\t\t\"updates_id\": %u,\n",
2052 (ae->flags & HEALTH_ENTRY_FLAG_PROCESSED)?"true":"false",
2053 (ae->flags & HEALTH_ENTRY_FLAG_UPDATED)?"true":"false",
2054 (unsigned long)ae->exec_run_timestamp,
2055 (ae->flags & HEALTH_ENTRY_FLAG_EXEC_FAILED)?"true":"false",
2056 ae->exec?ae->exec:health.health_default_exec,
2057 ae->recipient?ae->recipient:health.health_default_recipient,
2060 ae->units?ae->units:"",
2061 ae->info?ae->info:"",
2062 (unsigned long)ae->when,
2063 (unsigned long)ae->duration,
2064 (unsigned long)ae->non_clear_duration,
2065 rrdcalc_status2string(ae->new_status),
2066 rrdcalc_status2string(ae->old_status),
2068 (unsigned long)ae->delay_up_to_timestamp,
2073 buffer_strcat(wb, "\t\t\"value\":");
2074 buffer_rrd_value(wb, ae->new_value);
2075 buffer_strcat(wb, ",\n");
2077 buffer_strcat(wb, "\t\t\"old_value\":");
2078 buffer_rrd_value(wb, ae->old_value);
2079 buffer_strcat(wb, "\n");
2081 buffer_strcat(wb, "\t}");
2084 void health_alarm_log2json(RRDHOST *host, BUFFER *wb, uint32_t after) {
2085 pthread_rwlock_rdlock(&host->health_log.alarm_log_rwlock);
2087 buffer_strcat(wb, "[");
2089 unsigned int max = host->health_log.max;
2090 unsigned int count = 0;
2092 for(ae = host->health_log.alarms; ae && count < max ; count++, ae = ae->next) {
2093 if(ae->unique_id > after) {
2094 if(likely(count)) buffer_strcat(wb, ",");
2095 health_alarm_entry2json_nolock(wb, ae, host);
2099 buffer_strcat(wb, "\n]\n");
2101 pthread_rwlock_unlock(&host->health_log.alarm_log_rwlock);
2104 static inline void health_rrdcalc2json_nolock(BUFFER *wb, RRDCALC *rc) {
2106 "\t\t\"%s.%s\": {\n"
2107 "\t\t\t\"id\": %lu,\n"
2108 "\t\t\t\"name\": \"%s\",\n"
2109 "\t\t\t\"chart\": \"%s\",\n"
2110 "\t\t\t\"family\": \"%s\",\n"
2111 "\t\t\t\"active\": %s,\n"
2112 "\t\t\t\"exec\": \"%s\",\n"
2113 "\t\t\t\"recipient\": \"%s\",\n"
2114 "\t\t\t\"source\": \"%s\",\n"
2115 "\t\t\t\"units\": \"%s\",\n"
2116 "\t\t\t\"info\": \"%s\",\n"
2117 "\t\t\t\"status\": \"%s\",\n"
2118 "\t\t\t\"last_status_change\": %lu,\n"
2119 "\t\t\t\"last_updated\": %lu,\n"
2120 "\t\t\t\"next_update\": %lu,\n"
2121 "\t\t\t\"update_every\": %d,\n"
2122 "\t\t\t\"delay_up_duration\": %d,\n"
2123 "\t\t\t\"delay_down_duration\": %d,\n"
2124 "\t\t\t\"delay_max_duration\": %d,\n"
2125 "\t\t\t\"delay_multiplier\": %f,\n"
2126 "\t\t\t\"delay\": %d,\n"
2127 "\t\t\t\"delay_up_to_timestamp\": %lu,\n"
2128 , rc->chart, rc->name
2129 , (unsigned long)rc->id
2132 , (rc->rrdset && rc->rrdset->family)?rc->rrdset->family:""
2133 , (rc->rrdset)?"true":"false"
2134 , rc->exec?rc->exec:health.health_default_exec
2135 , rc->recipient?rc->recipient:health.health_default_recipient
2137 , rc->units?rc->units:""
2138 , rc->info?rc->info:""
2139 , rrdcalc_status2string(rc->status)
2140 , (unsigned long)rc->last_status_change
2141 , (unsigned long)rc->last_updated
2142 , (unsigned long)rc->next_update
2144 , rc->delay_up_duration
2145 , rc->delay_down_duration
2146 , rc->delay_max_duration
2147 , rc->delay_multiplier
2149 , (unsigned long)rc->delay_up_to_timestamp
2152 if(RRDCALC_HAS_DB_LOOKUP(rc)) {
2153 if(rc->dimensions && *rc->dimensions)
2154 health_string2json(wb, "\t\t\t", "lookup_dimensions", rc->dimensions, ",\n");
2157 "\t\t\t\"db_after\": %lu,\n"
2158 "\t\t\t\"db_before\": %lu,\n"
2159 "\t\t\t\"lookup_method\": \"%s\",\n"
2160 "\t\t\t\"lookup_after\": %d,\n"
2161 "\t\t\t\"lookup_before\": %d,\n"
2162 "\t\t\t\"lookup_options\": \"",
2163 (unsigned long) rc->db_after,
2164 (unsigned long) rc->db_before,
2165 group_method2string(rc->group),
2169 buffer_data_options2string(wb, rc->options);
2170 buffer_strcat(wb, "\",\n");
2173 if(rc->calculation) {
2174 health_string2json(wb, "\t\t\t", "calc", rc->calculation->source, ",\n");
2175 health_string2json(wb, "\t\t\t", "calc_parsed", rc->calculation->parsed_as, ",\n");
2179 health_string2json(wb, "\t\t\t", "warn", rc->warning->source, ",\n");
2180 health_string2json(wb, "\t\t\t", "warn_parsed", rc->warning->parsed_as, ",\n");
2184 health_string2json(wb, "\t\t\t", "crit", rc->critical->source, ",\n");
2185 health_string2json(wb, "\t\t\t", "crit_parsed", rc->critical->parsed_as, ",\n");
2188 buffer_strcat(wb, "\t\t\t\"green\":");
2189 buffer_rrd_value(wb, rc->green);
2190 buffer_strcat(wb, ",\n");
2192 buffer_strcat(wb, "\t\t\t\"red\":");
2193 buffer_rrd_value(wb, rc->red);
2194 buffer_strcat(wb, ",\n");
2196 buffer_strcat(wb, "\t\t\t\"value\":");
2197 buffer_rrd_value(wb, rc->value);
2198 buffer_strcat(wb, "\n");
2200 buffer_strcat(wb, "\t\t}");
2203 //void health_rrdcalctemplate2json_nolock(BUFFER *wb, RRDCALCTEMPLATE *rt) {
2207 void health_alarms2json(RRDHOST *host, BUFFER *wb, int all) {
2210 rrdhost_rdlock(&localhost);
2211 buffer_sprintf(wb, "{\n\t\"hostname\": \"%s\","
2212 "\n\t\"latest_alarm_log_unique_id\": %u,"
2213 "\n\t\"status\": %s,"
2215 "\n\t\"alarms\": {\n",
2217 (host->health_log.next_log_id > 0)?(host->health_log.next_log_id - 1):0,
2218 health_enabled?"true":"false",
2219 (unsigned long)time(NULL));
2222 for(i = 0, rc = host->alarms; rc ; rc = rc->next) {
2223 if(unlikely(!rc->rrdset || !rc->rrdset->last_collected_time.tv_sec))
2226 if(likely(!all && !(rc->status == RRDCALC_STATUS_WARNING || rc->status == RRDCALC_STATUS_CRITICAL)))
2229 if(likely(i)) buffer_strcat(wb, ",\n");
2230 health_rrdcalc2json_nolock(wb, rc);
2234 // buffer_strcat(wb, "\n\t},\n\t\"templates\": {");
2235 // RRDCALCTEMPLATE *rt;
2236 // for(rt = host->templates; rt ; rt = rt->next)
2237 // health_rrdcalctemplate2json_nolock(wb, rt);
2239 buffer_strcat(wb, "\n\t}\n}\n");
2240 rrdhost_unlock(&localhost);
2244 // ----------------------------------------------------------------------------
2245 // re-load health configuration
2247 static inline void health_free_all_nolock(RRDHOST *host) {
2248 while(host->templates)
2249 rrdcalctemplate_free(host, host->templates);
2252 rrdcalc_free(host, host->alarms);
2255 void health_reload(void) {
2256 if(!health_enabled) {
2257 error("Health reload is requested, but health is not enabled.");
2261 char *path = health_config_dir();
2263 // free all running alarms
2264 rrdhost_rwlock(&localhost);
2265 health_free_all_nolock(&localhost);
2266 rrdhost_unlock(&localhost);
2268 // invalidate all previous entries in the alarm log
2270 for(t = localhost.health_log.alarms ; t ; t = t->next) {
2271 if(t->new_status != RRDCALC_STATUS_REMOVED)
2272 t->flags |= HEALTH_ENTRY_FLAG_UPDATED;
2275 // reset all thresholds to all charts
2277 for(st = localhost.rrdset_root; st ; st = st->next) {
2282 // load the new alarms
2283 rrdhost_rwlock(&localhost);
2284 health_readdir(path);
2285 rrdhost_unlock(&localhost);
2287 // link the loaded alarms to their charts
2288 for(st = localhost.rrdset_root; st ; st = st->next) {
2289 rrdhost_rwlock(&localhost);
2291 rrdsetcalc_link_matching(st);
2292 rrdcalctemplate_link_matching(st);
2294 rrdhost_unlock(&localhost);
2298 // ----------------------------------------------------------------------------
2299 // health main thread and friends
2301 static inline int rrdcalc_value2status(calculated_number n) {
2302 if(isnan(n)) return RRDCALC_STATUS_UNDEFINED;
2303 if(n) return RRDCALC_STATUS_RAISED;
2304 return RRDCALC_STATUS_CLEAR;
2307 static inline void health_alarm_execute(RRDHOST *host, ALARM_ENTRY *ae) {
2308 ae->flags |= HEALTH_ENTRY_FLAG_PROCESSED;
2310 // find the previous notification for the same alarm
2312 for(t = ae->next; t ;t = t->next) {
2313 if(t->alarm_id == ae->alarm_id && t->flags & HEALTH_ENTRY_FLAG_EXEC_RUN)
2317 if(t && t->new_status == ae->new_status) {
2318 // don't send the same notification again
2319 info("Health not sending again notification for alarm '%s.%s' status %s", ae->chart, ae->name, rrdcalc_status2string(ae->new_status));
2323 if((ae->old_status == RRDCALC_STATUS_UNDEFINED && ae->new_status == RRDCALC_STATUS_UNINITIALIZED)
2324 || (ae->old_status == RRDCALC_STATUS_UNINITIALIZED && ae->new_status == RRDCALC_STATUS_CLEAR)) {
2325 info("Health not sending notification for first initialization of alarm '%s.%s' status %s", ae->chart, ae->name, rrdcalc_status2string(ae->new_status));
2329 char buffer[FILENAME_MAX + 1];
2332 const char *exec = ae->exec;
2333 if(!exec) exec = health.health_default_exec;
2335 const char *recipient = ae->recipient;
2336 if(!recipient) recipient = health.health_default_recipient;
2338 snprintfz(buffer, FILENAME_MAX, "exec %s '%s' '%s' '%u' '%u' '%u' '%lu' '%s' '%s' '%s' '%s' '%s' '%0.0Lf' '%0.0Lf' '%s' '%u' '%u' '%s' '%s'",
2345 (unsigned long)ae->when,
2347 ae->chart?ae->chart:"NOCAHRT",
2348 ae->family?ae->family:"NOFAMILY",
2349 rrdcalc_status2string(ae->new_status),
2350 rrdcalc_status2string(ae->old_status),
2353 ae->source?ae->source:"UNKNOWN",
2354 (uint32_t)ae->duration,
2355 (uint32_t)ae->non_clear_duration,
2356 ae->units?ae->units:"",
2357 ae->info?ae->info:""
2360 ae->flags |= HEALTH_ENTRY_FLAG_EXEC_RUN;
2361 ae->exec_run_timestamp = time(NULL);
2363 debug(D_HEALTH, "executing command '%s'", buffer);
2364 FILE *fp = mypopen(buffer, &command_pid);
2366 error("HEALTH: Cannot popen(\"%s\", \"r\").", buffer);
2369 debug(D_HEALTH, "HEALTH reading from command");
2370 char *s = fgets(buffer, FILENAME_MAX, fp);
2372 ae->exec_code = mypclose(fp, command_pid);
2373 debug(D_HEALTH, "done executing command - returned with code %d", ae->exec_code);
2375 if(ae->exec_code != 0)
2376 ae->flags |= HEALTH_ENTRY_FLAG_EXEC_FAILED;
2379 health_alarm_log_save(host, ae);
2383 static inline void health_process_notifications(RRDHOST *host, ALARM_ENTRY *ae) {
2384 info("Health alarm '%s.%s' = %0.2Lf - changed status from %s to %s",
2385 ae->chart?ae->chart:"NOCHART", ae->name,
2387 rrdcalc_status2string(ae->old_status),
2388 rrdcalc_status2string(ae->new_status)
2391 health_alarm_execute(host, ae);
2394 static inline void health_alarm_log_process(RRDHOST *host) {
2395 static uint32_t stop_at_id = 0;
2396 uint32_t first_waiting = (host->health_log.alarms)?host->health_log.alarms->unique_id:0;
2397 time_t now = time(NULL);
2399 pthread_rwlock_rdlock(&host->health_log.alarm_log_rwlock);
2402 for(ae = host->health_log.alarms; ae && ae->unique_id >= stop_at_id ; ae = ae->next) {
2404 !(ae->flags & HEALTH_ENTRY_FLAG_PROCESSED) &&
2405 !(ae->flags & HEALTH_ENTRY_FLAG_UPDATED)
2408 if(unlikely(ae->unique_id < first_waiting))
2409 first_waiting = ae->unique_id;
2411 if(likely(now >= ae->delay_up_to_timestamp))
2412 health_process_notifications(host, ae);
2416 // remember this for the next iteration
2417 stop_at_id = first_waiting;
2419 pthread_rwlock_unlock(&host->health_log.alarm_log_rwlock);
2421 if(host->health_log.count <= host->health_log.max)
2424 // cleanup excess entries in the log
2425 pthread_rwlock_wrlock(&host->health_log.alarm_log_rwlock);
2427 ALARM_ENTRY *last = NULL;
2428 unsigned int count = host->health_log.max * 2 / 3;
2429 for(ae = host->health_log.alarms; ae && count ; count--, last = ae, ae = ae->next) ;
2431 if(ae && last && last->next == ae)
2437 debug(D_HEALTH, "Health removing alarm log entry with id: %u", ae->unique_id);
2439 ALARM_ENTRY *t = ae->next;
2445 freez(ae->recipient);
2452 host->health_log.count--;
2455 pthread_rwlock_unlock(&host->health_log.alarm_log_rwlock);
2458 static inline int rrdcalc_isrunnable(RRDCALC *rc, time_t now, time_t *next_run) {
2459 if (unlikely(!rc->rrdset)) {
2460 debug(D_HEALTH, "Health not running alarm '%s.%s'. It is not linked to a chart.", rc->chart?rc->chart:"NOCHART", rc->name);
2464 if (unlikely(!rc->rrdset->last_collected_time.tv_sec)) {
2465 debug(D_HEALTH, "Health not running alarm '%s.%s'. Chart is not yet collected.", rc->chart?rc->chart:"NOCHART", rc->name);
2469 if (unlikely(!rc->update_every)) {
2470 debug(D_HEALTH, "Health not running alarm '%s.%s'. It does not have an update frequency", rc->chart?rc->chart:"NOCHART", rc->name);
2474 if (unlikely(rc->next_update > now)) {
2475 if (unlikely(*next_run > rc->next_update))
2476 *next_run = rc->next_update;
2478 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));
2483 // we should check that the DB lookup is possible
2485 // - the duration of the chart includes the required timeframe
2486 // we SHOULD NOT check the dimensions - there might be alarms that refer non-existing dimensions (e.g. cpu steal)
2491 void *health_main(void *ptr) {
2494 info("HEALTH thread created with task id %d", gettid());
2496 if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
2497 error("Cannot set pthread cancel type to DEFERRED.");
2499 if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
2500 error("Cannot set pthread cancel state to ENABLE.");
2502 int min_run_every = (int)config_get_number("health", "run at least every seconds", 10);
2503 if(min_run_every < 1) min_run_every = 1;
2505 BUFFER *wb = buffer_create(100);
2507 unsigned int loop = 0;
2508 while(health_enabled && !netdata_exit) {
2510 debug(D_HEALTH, "Health monitoring iteration no %u started", loop);
2512 int oldstate, runnable = 0;
2513 time_t now = time(NULL);
2514 time_t next_run = now + min_run_every;
2517 if (unlikely(pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldstate) != 0))
2518 error("Cannot set pthread cancel state to DISABLE.");
2520 rrdhost_rdlock(&localhost);
2522 // the first loop is to lookup values from the db
2523 for (rc = localhost.alarms; rc; rc = rc->next) {
2524 if (unlikely(!rrdcalc_isrunnable(rc, now, &next_run)))
2528 rc->old_value = rc->value;
2530 // 1. if there is database lookup, do it
2531 // 2. if there is calculation expression, run it
2533 if (unlikely(RRDCALC_HAS_DB_LOOKUP(rc))) {
2534 time_t old_db_timestamp = rc->db_before;
2535 int value_is_null = 0;
2537 int ret = rrd2value(rc->rrdset, wb, &rc->value,
2538 rc->dimensions, 1, rc->after, rc->before, rc->group,
2539 rc->options, &rc->db_after, &rc->db_before, &value_is_null);
2541 if (unlikely(ret != 200)) {
2542 // database lookup failed
2545 debug(D_HEALTH, "Health alarm '%s.%s': database lookup returned error %d", rc->chart?rc->chart:"NOCHART", rc->name, ret);
2547 if (unlikely(!(rc->rrdcalc_flags & RRDCALC_FLAG_DB_ERROR))) {
2548 rc->rrdcalc_flags |= RRDCALC_FLAG_DB_ERROR;
2549 error("Health alarm '%s.%s': database lookup returned error %d", rc->chart?rc->chart:"NOCHART", rc->name, ret);
2552 else if (unlikely(rc->rrdcalc_flags & RRDCALC_FLAG_DB_ERROR))
2553 rc->rrdcalc_flags &= ~RRDCALC_FLAG_DB_ERROR;
2555 if (unlikely(old_db_timestamp == rc->db_before)) {
2556 // database is stale
2558 debug(D_HEALTH, "Health alarm '%s.%s': database is stale", rc->chart?rc->chart:"NOCHART", rc->name);
2560 if (unlikely(!(rc->rrdcalc_flags & RRDCALC_FLAG_DB_STALE))) {
2561 rc->rrdcalc_flags |= RRDCALC_FLAG_DB_STALE;
2562 error("Health alarm '%s.%s': database is stale", rc->chart?rc->chart:"NOCHART", rc->name);
2565 else if (unlikely(rc->rrdcalc_flags & RRDCALC_FLAG_DB_STALE))
2566 rc->rrdcalc_flags &= ~RRDCALC_FLAG_DB_STALE;
2568 if (unlikely(value_is_null)) {
2569 // collected value is null
2573 debug(D_HEALTH, "Health alarm '%s.%s': database lookup returned empty value (possibly value is not collected yet)",
2574 rc->chart?rc->chart:"NOCHART", rc->name);
2576 if (unlikely(!(rc->rrdcalc_flags & RRDCALC_FLAG_DB_NAN))) {
2577 rc->rrdcalc_flags |= RRDCALC_FLAG_DB_NAN;
2578 error("Health alarm '%s.%s': database lookup returned empty value (possibly value is not collected yet)",
2579 rc->chart?rc->chart:"NOCHART", rc->name);
2582 else if (unlikely(rc->rrdcalc_flags & RRDCALC_FLAG_DB_NAN))
2583 rc->rrdcalc_flags &= ~RRDCALC_FLAG_DB_NAN;
2585 debug(D_HEALTH, "Health alarm '%s.%s': database lookup gave value "
2586 CALCULATED_NUMBER_FORMAT, rc->chart?rc->chart:"NOCHART", rc->name, rc->value);
2589 if(unlikely(rc->calculation)) {
2590 if (unlikely(!expression_evaluate(rc->calculation))) {
2591 // calculation failed
2595 debug(D_HEALTH, "Health alarm '%s.%s': failed to evaluate calculation with error: %s",
2596 rc->chart?rc->chart:"NOCHART", rc->name, buffer_tostring(rc->calculation->error_msg));
2598 if (unlikely(!(rc->rrdcalc_flags & RRDCALC_FLAG_CALC_ERROR))) {
2599 rc->rrdcalc_flags |= RRDCALC_FLAG_CALC_ERROR;
2600 error("Health alarm '%s.%s': failed to evaluate calculation with error: %s",
2601 rc->chart?rc->chart:"NOCHART", rc->name, buffer_tostring(rc->calculation->error_msg));
2605 if (unlikely(rc->rrdcalc_flags & RRDCALC_FLAG_CALC_ERROR))
2606 rc->rrdcalc_flags &= ~RRDCALC_FLAG_CALC_ERROR;
2608 debug(D_HEALTH, "Health alarm '%s.%s': calculation expression gave value "
2609 CALCULATED_NUMBER_FORMAT
2610 ": %s (source: %s)",
2611 rc->chart?rc->chart:"NOCHART", rc->name,
2612 rc->calculation->result,
2613 buffer_tostring(rc->calculation->error_msg),
2617 rc->value = rc->calculation->result;
2621 rrdhost_unlock(&localhost);
2623 if (unlikely(runnable && !netdata_exit)) {
2624 rrdhost_rdlock(&localhost);
2626 for (rc = localhost.alarms; rc; rc = rc->next) {
2627 if (unlikely(!rrdcalc_isrunnable(rc, now, &next_run)))
2630 int warning_status = RRDCALC_STATUS_UNDEFINED;
2631 int critical_status = RRDCALC_STATUS_UNDEFINED;
2633 if(likely(rc->warning)) {
2634 if(unlikely(!expression_evaluate(rc->warning))) {
2635 // calculation failed
2637 debug(D_HEALTH, "Health alarm '%s.%s': warning expression failed with error: %s",
2638 rc->chart?rc->chart:"NOCHART", rc->name, buffer_tostring(rc->warning->error_msg));
2640 if (unlikely(!(rc->rrdcalc_flags & RRDCALC_FLAG_WARN_ERROR))) {
2641 rc->rrdcalc_flags |= RRDCALC_FLAG_WARN_ERROR;
2642 error("Health alarm '%s.%s': warning expression failed with error: %s",
2643 rc->chart?rc->chart:"NOCHART", rc->name, buffer_tostring(rc->warning->error_msg));
2647 if(unlikely(rc->rrdcalc_flags & RRDCALC_FLAG_WARN_ERROR))
2648 rc->rrdcalc_flags &= ~RRDCALC_FLAG_WARN_ERROR;
2650 debug(D_HEALTH, "Health alarm '%s.%s': warning expression gave value "
2651 CALCULATED_NUMBER_FORMAT
2652 ": %s (source: %s)",
2653 rc->chart?rc->chart:"NOCHART", rc->name,
2654 rc->warning->result,
2655 buffer_tostring(rc->warning->error_msg),
2659 warning_status = rrdcalc_value2status(rc->warning->result);
2663 if(likely(rc->critical)) {
2664 if(unlikely(!expression_evaluate(rc->critical))) {
2665 // calculation failed
2667 debug(D_HEALTH, "Health alarm '%s.%s': critical expression failed with error: %s",
2668 rc->chart?rc->chart:"NOCHART", rc->name, buffer_tostring(rc->critical->error_msg));
2670 if (unlikely(!(rc->rrdcalc_flags & RRDCALC_FLAG_CRIT_ERROR))) {
2671 rc->rrdcalc_flags |= RRDCALC_FLAG_CRIT_ERROR;
2672 error("Health alarm '%s.%s': critical expression failed with error: %s",
2673 rc->chart?rc->chart:"NOCHART", rc->name, buffer_tostring(rc->critical->error_msg));
2677 if(unlikely(rc->rrdcalc_flags & RRDCALC_FLAG_CRIT_ERROR))
2678 rc->rrdcalc_flags &= ~RRDCALC_FLAG_CRIT_ERROR;
2680 debug(D_HEALTH, "Health alarm '%s.%s': critical expression gave value "
2681 CALCULATED_NUMBER_FORMAT
2682 ": %s (source: %s)",
2683 rc->chart?rc->chart:"NOCHART", rc->name,
2684 rc->critical->result,
2685 buffer_tostring(rc->critical->error_msg),
2689 critical_status = rrdcalc_value2status(rc->critical->result);
2693 int status = RRDCALC_STATUS_UNDEFINED;
2695 switch(warning_status) {
2696 case RRDCALC_STATUS_CLEAR:
2697 status = RRDCALC_STATUS_CLEAR;
2700 case RRDCALC_STATUS_RAISED:
2701 status = RRDCALC_STATUS_WARNING;
2708 switch(critical_status) {
2709 case RRDCALC_STATUS_CLEAR:
2710 if(status == RRDCALC_STATUS_UNDEFINED)
2711 status = RRDCALC_STATUS_CLEAR;
2714 case RRDCALC_STATUS_RAISED:
2715 status = RRDCALC_STATUS_CRITICAL;
2722 if(status != rc->status) {
2725 if(now > rc->delay_up_to_timestamp) {
2726 rc->delay_up_current = rc->delay_up_duration;
2727 rc->delay_down_current = rc->delay_down_duration;
2729 rc->delay_up_to_timestamp = 0;
2732 rc->delay_up_current = (int)(rc->delay_up_current * rc->delay_multiplier);
2733 if(rc->delay_up_current > rc->delay_max_duration) rc->delay_up_current = rc->delay_max_duration;
2735 rc->delay_down_current = (int)(rc->delay_down_current * rc->delay_multiplier);
2736 if(rc->delay_down_current > rc->delay_max_duration) rc->delay_down_current = rc->delay_max_duration;
2739 if(status > rc->status)
2740 delay = rc->delay_up_current;
2742 delay = rc->delay_down_current;
2744 // COMMENTED: because we do need to send raising alarms
2745 // if(now + delay < rc->delay_up_to_timestamp)
2746 // delay = (int)(rc->delay_up_to_timestamp - now);
2748 rc->delay_last = delay;
2749 rc->delay_up_to_timestamp = now + delay;
2750 health_alarm_log(&localhost, rc->id, rc->next_event_id++, now, 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, rc->delay_last);
2751 rc->last_status_change = now;
2752 rc->status = status;
2755 rc->last_updated = now;
2756 rc->next_update = now + rc->update_every;
2758 if (next_run > rc->next_update)
2759 next_run = rc->next_update;
2762 rrdhost_unlock(&localhost);
2765 if (unlikely(pthread_setcancelstate(oldstate, NULL) != 0))
2766 error("Cannot set pthread cancel state to RESTORE (%d).", oldstate);
2768 if(unlikely(netdata_exit))
2771 // execute notifications
2773 health_alarm_log_process(&localhost);
2775 if(unlikely(netdata_exit))
2779 if(now < next_run) {
2780 debug(D_HEALTH, "Health monitoring iteration no %u done. Next iteration in %d secs",
2781 loop, (int) (next_run - now));
2782 sleep_usec(1000000 * (unsigned long long) (next_run - now));
2785 debug(D_HEALTH, "Health monitoring iteration no %u done. Next iteration now", loop);
2791 info("HEALTH thread exiting");