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;
9 size_t log_entries_written;
13 static struct health_options health = {
14 .health_default_exec = PLUGINS_DIR "/alarm-notify.sh",
15 .health_default_recipient = "root",
16 .log_filename = VARLIB_DIR "/health/alarm_log.db",
17 .log_entries_written = 0,
21 int health_enabled = 1;
23 // ----------------------------------------------------------------------------
24 // health alarm log load/save
25 // no need for locking - only one thread is reading / writing the alarms log
27 static inline int health_alarm_log_open(void) {
29 fclose(health.log_fp);
31 health.log_fp = fopen(health.log_filename, "a");
34 if (setvbuf(health.log_fp, NULL, _IOLBF, 0) != 0)
35 error("Health: cannot set line buffering on health log file.");
39 error("Health: cannot open health log file '%s'. Health data will be lost in case of netdata or server crash.", health.log_filename);
43 static inline void health_alarm_log_close(void) {
45 fclose(health.log_fp);
50 static inline void health_log_rotate(void) {
51 static size_t rotate_every = 0;
53 if(unlikely(rotate_every == 0)) {
54 rotate_every = (size_t)config_get_number("health", "rotate log every lines", 100);
55 if(rotate_every < 100) rotate_every = 100;
58 if(unlikely(health.log_entries_written > rotate_every)) {
59 health_alarm_log_close();
61 char old_filename[FILENAME_MAX + 1];
62 snprintfz(old_filename, FILENAME_MAX, "%s.old", health.log_filename);
64 if(unlink(old_filename) == -1 && errno != ENOENT)
65 error("Health: cannot remove old alarms log file '%s'", old_filename);
67 if(link(health.log_filename, old_filename) == -1 && errno != ENOENT)
68 error("Health: cannot move file '%s' to '%s'.", health.log_filename, old_filename);
70 if(unlink(health.log_filename) == -1 && errno != ENOENT)
71 error("Health: cannot remove old alarms log file '%s'", health.log_filename);
73 health_alarm_log_open();
77 static inline void health_alarm_log_save(RRDHOST *host, ALARM_ENTRY *ae) {
80 if(likely(health.log_fp)) {
81 if(unlikely(fprintf(health.log_fp
83 "\t%08x\t%08x\t%08x\t%08x\t%08x"
86 "\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s"
90 , (ae->flags & HEALTH_ENTRY_FLAG_SAVED)?'U':'A'
100 , (uint32_t)ae->duration
101 , (uint32_t)ae->non_clear_duration
102 , (uint32_t)ae->flags
103 , (uint32_t)ae->exec_run_timestamp
104 , (uint32_t)ae->delay_up_to_timestamp
106 , (ae->name)?ae->name:""
107 , (ae->chart)?ae->chart:""
108 , (ae->family)?ae->family:""
109 , (ae->exec)?ae->exec:""
110 , (ae->recipient)?ae->recipient:""
111 , (ae->source)?ae->source:""
112 , (ae->units)?ae->units:""
113 , (ae->info)?ae->info:""
120 , (long double)ae->new_value
121 , (long double)ae->old_value
123 error("Health: failed to save alarm log entry. Health data may be lost in case of abnormal restart.");
125 ae->flags |= HEALTH_ENTRY_FLAG_SAVED;
126 health.log_entries_written++;
131 static inline ssize_t health_alarm_log_read(RRDHOST *host, FILE *fp, const char *filename) {
132 uint32_t max_unique_id = 0, max_alarm_id = 0;
133 ssize_t loaded = -1, updated = -1, errored = -1, duplicate = -1;
137 char *s, *buf = mallocz(65536 + 1);
138 size_t line = 0, len = 0;
139 loaded = updated = errored = duplicate = 0;
141 pthread_rwlock_rdlock(&host->health_log.alarm_log_rwlock);
143 while((s = fgets_trim_len(buf, 65536, fp, &len))) {
144 health.log_entries_written++;
147 int max_entries = 30, entries = 0;
148 char *pointers[max_entries];
150 pointers[entries++] = s++;
152 if(unlikely(*s == '\t')) {
154 pointers[entries++] = ++s;
155 if(entries >= max_entries) {
156 error("Health: line %zu of file '%s' has more than %d entries. Ignoring excessive entries.", line, filename, max_entries);
163 if(likely(*pointers[0] == 'U' || *pointers[0] == 'A')) {
164 ALARM_ENTRY *ae = NULL;
167 error("Health: line %zu of file '%s' should have at least 26 entries, but it has %d. Ignoring line.", line, filename, entries);
172 // check that we have valid ids
173 uint32_t unique_id = (uint32_t)strtoul(pointers[2], NULL, 16);
175 error("Health: line %zu of file '%s' states alarm entry with unique id %u (%s). Ignoring line.", line, filename, unique_id, pointers[2]);
180 uint32_t alarm_id = (uint32_t)strtoul(pointers[3], NULL, 16);
182 error("Health: line %zu of file '%s' states alarm entry for alarm id %u (%s). Ignoring line.", line, filename, alarm_id, pointers[3]);
187 // find a possible overwrite
188 for(ae = host->health_log.alarms; ae; ae = ae->next) {
189 if(unlikely(ae->unique_id == unique_id)) {
190 if(unlikely(*pointers[0] == 'A')) {
191 error("Health: line %zu of file '%s' adds duplicate alarm log entry with unique id %u."
192 , line, filename, unique_id);
200 // if not found, create a new one
203 // if it is an update, but we haven't found it, make it an addition
204 if(unlikely(*pointers[0] == 'U')) {
206 error("Health: line %zu of file '%s' updates alarm log entry with unique id %u, but it is not found.", line, filename, unique_id);
209 // alarms should be added in the right order
210 if(unlikely(unique_id < max_unique_id)) {
211 error("Health: line %zu of file '%s' has alarm log entry with %u in wrong order.", line
212 , filename, ae->unique_id);
215 ae = callocz(1, sizeof(ALARM_ENTRY));
218 // check for a possible host missmatch
219 if(strcmp(pointers[1], host->hostname))
220 error("Health: line %zu of file '%s' provides an alarm for host '%s' but this is named '%s'.", line, filename, pointers[1], host->hostname);
222 ae->unique_id = unique_id;
223 ae->alarm_id = alarm_id;
224 ae->alarm_event_id = (uint32_t)strtoul(pointers[4], NULL, 16);
225 ae->updated_by_id = (uint32_t)strtoul(pointers[5], NULL, 16);
226 ae->updates_id = (uint32_t)strtoul(pointers[6], NULL, 16);
228 ae->when = (uint32_t)strtoul(pointers[7], NULL, 16);
229 ae->duration = (uint32_t)strtoul(pointers[8], NULL, 16);
230 ae->non_clear_duration = (uint32_t)strtoul(pointers[9], NULL, 16);
232 ae->flags = (uint32_t)strtoul(pointers[10], NULL, 16);
233 ae->flags |= HEALTH_ENTRY_FLAG_SAVED;
235 ae->exec_run_timestamp = (uint32_t)strtoul(pointers[11], NULL, 16);
236 ae->delay_up_to_timestamp = (uint32_t)strtoul(pointers[12], NULL, 16);
238 if(unlikely(ae->name)) freez(ae->name);
239 ae->name = strdupz(pointers[13]);
241 if(unlikely(ae->chart)) freez(ae->chart);
242 ae->chart = strdupz(pointers[14]);
244 if(unlikely(ae->family)) freez(ae->family);
245 ae->family = strdupz(pointers[15]);
247 if(unlikely(ae->exec)) freez(ae->exec);
248 ae->exec = strdupz(pointers[16]);
249 if(!*ae->exec) { freez(ae->exec); ae->exec = NULL; }
251 if(unlikely(ae->recipient)) freez(ae->recipient);
252 ae->recipient = strdupz(pointers[17]);
253 if(!*ae->recipient) { freez(ae->recipient); ae->recipient = NULL; }
255 if(unlikely(ae->source)) freez(ae->source);
256 ae->source = strdupz(pointers[18]);
257 if(!*ae->source) { freez(ae->source); ae->source = NULL; }
259 if(unlikely(ae->units)) freez(ae->units);
260 ae->units = strdupz(pointers[19]);
261 if(!*ae->units) { freez(ae->units); ae->units = NULL; }
263 if(unlikely(ae->info)) freez(ae->info);
264 ae->info = strdupz(pointers[20]);
265 if(!*ae->info) { freez(ae->info); ae->info = NULL; }
267 ae->exec_code = atoi(pointers[21]);
268 ae->new_status = atoi(pointers[22]);
269 ae->old_status = atoi(pointers[23]);
270 ae->delay = atoi(pointers[24]);
272 ae->new_value = strtold(pointers[25], NULL);
273 ae->old_value = strtold(pointers[26], NULL);
275 // add it to host if not already there
276 if(unlikely(*pointers[0] == 'A')) {
277 ae->next = host->health_log.alarms;
278 host->health_log.alarms = ae;
283 if(unlikely(ae->unique_id > max_unique_id))
284 max_unique_id = ae->unique_id;
286 if(unlikely(ae->alarm_id >= max_alarm_id))
287 max_alarm_id = ae->alarm_id;
290 error("Health: line %zu of file '%s' is invalid (unrecognized entry type '%s').", line, filename, pointers[0]);
295 pthread_rwlock_unlock(&host->health_log.alarm_log_rwlock);
299 if(!max_unique_id) max_unique_id = (uint32_t)time(NULL);
300 if(!max_alarm_id) max_alarm_id = (uint32_t)time(NULL);
302 host->health_log.next_log_id = max_unique_id + 1;
303 host->health_log.next_alarm_id = max_alarm_id + 1;
305 info("Health: loaded file '%s' with %zd new alarm entries, updated %zd alarms, errors %zd entries, duplicate %zd", filename, loaded, updated, errored, duplicate);
309 static inline void health_alarm_log_load(RRDHOST *host) {
310 health_alarm_log_close();
312 char buffer[FILENAME_MAX + 1];
313 snprintfz(buffer, FILENAME_MAX, "%s.old", health.log_filename);
314 FILE *fp = fopen(buffer, "r");
316 error("Health: cannot open health file: %s", health.log_filename);
318 health_alarm_log_read(host, fp, health.log_filename);
322 health.log_entries_written = 0;
323 fp = fopen(health.log_filename, "r");
325 error("Health: cannot open health file: %s", health.log_filename);
327 health_alarm_log_read(host, fp, health.log_filename);
331 health_alarm_log_open();
335 // ----------------------------------------------------------------------------
336 // health alarm log management
338 static inline void health_alarm_log(RRDHOST *host,
339 uint32_t alarm_id, uint32_t alarm_event_id,
341 const char *name, const char *chart, const char *family,
342 const char *exec, const char *recipient, time_t duration,
343 calculated_number old_value, calculated_number new_value,
344 int old_status, int new_status,
350 debug(D_HEALTH, "Health adding alarm log entry with id: %u", host->health_log.next_log_id);
352 ALARM_ENTRY *ae = callocz(1, sizeof(ALARM_ENTRY));
353 ae->name = strdupz(name);
354 ae->hash_name = simple_hash(ae->name);
357 ae->chart = strdupz(chart);
358 ae->hash_chart = simple_hash(ae->chart);
362 ae->family = strdupz(family);
364 if(exec) ae->exec = strdupz(exec);
365 if(recipient) ae->recipient = strdupz(recipient);
366 if(source) ae->source = strdupz(source);
367 if(units) ae->units = strdupz(units);
368 if(info) ae->info = strdupz(info);
370 ae->unique_id = host->health_log.next_log_id++;
371 ae->alarm_id = alarm_id;
372 ae->alarm_event_id = alarm_event_id;
374 ae->old_value = old_value;
375 ae->new_value = new_value;
376 ae->old_status = old_status;
377 ae->new_status = new_status;
378 ae->duration = duration;
380 ae->delay_up_to_timestamp = when + delay;
382 if(ae->old_status == RRDCALC_STATUS_WARNING || ae->old_status == RRDCALC_STATUS_CRITICAL)
383 ae->non_clear_duration += ae->duration;
386 pthread_rwlock_wrlock(&host->health_log.alarm_log_rwlock);
387 ae->next = host->health_log.alarms;
388 host->health_log.alarms = ae;
389 host->health_log.count++;
390 pthread_rwlock_unlock(&host->health_log.alarm_log_rwlock);
392 // match previous alarms
393 pthread_rwlock_rdlock(&host->health_log.alarm_log_rwlock);
395 for(t = host->health_log.alarms ; t ; t = t->next) {
396 if(t != ae && t->alarm_id == ae->alarm_id) {
397 if(!(t->flags & HEALTH_ENTRY_FLAG_UPDATED) && !t->updated_by_id) {
398 t->flags |= HEALTH_ENTRY_FLAG_UPDATED;
399 t->updated_by_id = ae->unique_id;
400 ae->updates_id = t->unique_id;
402 if((t->new_status == RRDCALC_STATUS_WARNING || t->new_status == RRDCALC_STATUS_CRITICAL) &&
403 (t->old_status == RRDCALC_STATUS_WARNING || t->old_status == RRDCALC_STATUS_CRITICAL))
404 ae->non_clear_duration += t->non_clear_duration;
406 health_alarm_log_save(host, t);
409 // no need to continue
413 pthread_rwlock_unlock(&host->health_log.alarm_log_rwlock);
415 health_alarm_log_save(host, ae);
418 // ----------------------------------------------------------------------------
421 static inline int rrdvar_fix_name(char *variable) {
424 if (!isalnum(*variable) && *variable != '.' && *variable != '_') {
435 int rrdvar_compare(void* a, void* b) {
436 if(((RRDVAR *)a)->hash < ((RRDVAR *)b)->hash) return -1;
437 else if(((RRDVAR *)a)->hash > ((RRDVAR *)b)->hash) return 1;
438 else return strcmp(((RRDVAR *)a)->name, ((RRDVAR *)b)->name);
441 static inline RRDVAR *rrdvar_index_add(avl_tree_lock *tree, RRDVAR *rv) {
442 RRDVAR *ret = (RRDVAR *)avl_insert_lock(tree, (avl *)(rv));
444 debug(D_VARIABLES, "Request to insert RRDVAR '%s' into index failed. Already exists.", rv->name);
449 static inline RRDVAR *rrdvar_index_del(avl_tree_lock *tree, RRDVAR *rv) {
450 RRDVAR *ret = (RRDVAR *)avl_remove_lock(tree, (avl *)(rv));
452 error("Request to remove RRDVAR '%s' from index failed. Not Found.", rv->name);
457 static inline RRDVAR *rrdvar_index_find(avl_tree_lock *tree, const char *name, uint32_t hash) {
459 tmp.name = (char *)name;
460 tmp.hash = (hash)?hash:simple_hash(tmp.name);
462 return (RRDVAR *)avl_search_lock(tree, (avl *)&tmp);
465 static inline void rrdvar_free(RRDHOST *host, avl_tree_lock *tree, RRDVAR *rv) {
471 rrdvar_index_del(tree, rv);
477 static inline RRDVAR *rrdvar_create_and_index(const char *scope, avl_tree_lock *tree, const char *name, int type, calculated_number *value) {
478 char *variable = strdupz(name);
479 rrdvar_fix_name(variable);
480 uint32_t hash = simple_hash(variable);
482 RRDVAR *rv = rrdvar_index_find(tree, variable, hash);
484 debug(D_VARIABLES, "Variable '%s' not found in scope '%s'. Creating a new one.", variable, scope);
486 rv = callocz(1, sizeof(RRDVAR));
492 RRDVAR *ret = rrdvar_index_add(tree, rv);
493 if(unlikely(ret != rv)) {
494 debug(D_VARIABLES, "Variable '%s' in scope '%s' already exists", variable, scope);
495 rrdvar_free(NULL, NULL, rv);
499 debug(D_VARIABLES, "Variable '%s' created in scope '%s'", variable, scope);
510 // ----------------------------------------------------------------------------
513 calculated_number rrdvar2number(RRDVAR *rv) {
515 case RRDVAR_TYPE_CALCULATED: {
516 calculated_number *n = (calculated_number *)rv->value;
520 case RRDVAR_TYPE_TIME_T: {
521 time_t *n = (time_t *)rv->value;
525 case RRDVAR_TYPE_COLLECTED: {
526 collected_number *n = (collected_number *)rv->value;
530 case RRDVAR_TYPE_TOTAL: {
531 total_number *n = (total_number *)rv->value;
535 case RRDVAR_TYPE_INT: {
536 int *n = (int *)rv->value;
541 error("I don't know how to convert RRDVAR type %d to calculated_number", rv->type);
546 void dump_variable(void *data) {
547 RRDVAR *rv = (RRDVAR *)data;
548 debug(D_HEALTH, "%50s : %20.5Lf", rv->name, rrdvar2number(rv));
551 int health_variable_lookup(const char *variable, uint32_t hash, RRDCALC *rc, calculated_number *result) {
552 RRDSET *st = rc->rrdset;
557 rv = rrdvar_index_find(&st->variables_root_index, variable, hash);
559 *result = rrdvar2number(rv);
563 rv = rrdvar_index_find(&st->rrdfamily->variables_root_index, variable, hash);
565 *result = rrdvar2number(rv);
569 rv = rrdvar_index_find(&st->rrdhost->variables_root_index, variable, hash);
571 *result = rrdvar2number(rv);
575 debug(D_HEALTH, "Available local chart '%s' variables:", st->id);
576 avl_traverse_lock(&st->variables_root_index, dump_variable);
578 debug(D_HEALTH, "Available family '%s' variables:", st->rrdfamily->family);
579 avl_traverse_lock(&st->rrdfamily->variables_root_index, dump_variable);
581 debug(D_HEALTH, "Available host '%s' variables:", st->rrdhost->hostname);
582 avl_traverse_lock(&st->rrdhost->variables_root_index, dump_variable);
587 // ----------------------------------------------------------------------------
588 // RRDSETVAR management
590 RRDSETVAR *rrdsetvar_create(RRDSET *st, const char *variable, int type, void *value, uint32_t options) {
591 debug(D_VARIABLES, "RRDVARSET create for chart id '%s' name '%s' with variable name '%s'", st->id, st->name, variable);
592 RRDSETVAR *rs = (RRDSETVAR *)callocz(1, sizeof(RRDSETVAR));
594 char buffer[RRDVAR_MAX_LENGTH + 1];
595 snprintfz(buffer, RRDVAR_MAX_LENGTH, "%s.%s", st->id, variable);
596 rs->fullid = strdupz(buffer);
598 snprintfz(buffer, RRDVAR_MAX_LENGTH, "%s.%s", st->name, variable);
599 rs->fullname = strdupz(buffer);
601 rs->variable = strdupz(variable);
605 rs->options = options;
608 rs->local = rrdvar_create_and_index("local", &st->variables_root_index, rs->variable, rs->type, rs->value);
609 rs->family = rrdvar_create_and_index("family", &st->rrdfamily->variables_root_index, rs->fullid, rs->type, rs->value);
610 rs->host = rrdvar_create_and_index("host", &st->rrdhost->variables_root_index, rs->fullid, rs->type, rs->value);
611 rs->family_name = rrdvar_create_and_index("family", &st->rrdfamily->variables_root_index, rs->fullname, rs->type, rs->value);
612 rs->host_name = rrdvar_create_and_index("host", &st->rrdhost->variables_root_index, rs->fullname, rs->type, rs->value);
614 rs->next = st->variables;
620 void rrdsetvar_rename_all(RRDSET *st) {
621 debug(D_VARIABLES, "RRDSETVAR rename for chart id '%s' name '%s'", st->id, st->name);
623 // only these 2 can change name
627 char buffer[RRDVAR_MAX_LENGTH + 1];
628 RRDSETVAR *rs, *next = st->variables;
632 snprintfz(buffer, RRDVAR_MAX_LENGTH, "%s.%s", st->name, rs->variable);
634 if (strcmp(buffer, rs->fullname)) {
636 rrdvar_free(st->rrdhost, &st->rrdfamily->variables_root_index, rs->family_name);
637 rrdvar_free(st->rrdhost, &st->rrdhost->variables_root_index, rs->host_name);
640 rs->fullname = strdupz(st->name);
641 rs->family_name = rrdvar_create_and_index("family", &st->rrdfamily->variables_root_index, rs->fullname, rs->type, rs->value);
642 rs->host_name = rrdvar_create_and_index("host", &st->rrdhost->variables_root_index, rs->fullname, rs->type, rs->value);
646 rrdsetcalc_link_matching(st);
649 void rrdsetvar_free(RRDSETVAR *rs) {
650 RRDSET *st = rs->rrdset;
651 debug(D_VARIABLES, "RRDSETVAR free for chart id '%s' name '%s', variable '%s'", st->id, st->name, rs->variable);
653 rrdvar_free(st->rrdhost, &st->variables_root_index, rs->local);
654 rrdvar_free(st->rrdhost, &st->rrdfamily->variables_root_index, rs->family);
655 rrdvar_free(st->rrdhost, &st->rrdhost->variables_root_index, rs->host);
656 rrdvar_free(st->rrdhost, &st->rrdfamily->variables_root_index, rs->family_name);
657 rrdvar_free(st->rrdhost, &st->rrdhost->variables_root_index, rs->host_name);
659 if(st->variables == rs) {
660 st->variables = rs->next;
664 for (t = st->variables; t && t->next != rs; t = t->next);
665 if(!t) error("RRDSETVAR '%s' not found in chart '%s' variables linked list", rs->fullname, st->id);
666 else t->next = rs->next;
675 // ----------------------------------------------------------------------------
676 // RRDDIMVAR management
678 #define RRDDIMVAR_ID_MAX 1024
680 RRDDIMVAR *rrddimvar_create(RRDDIM *rd, int type, const char *prefix, const char *suffix, void *value, uint32_t options) {
681 RRDSET *st = rd->rrdset;
683 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:"");
685 if(!prefix) prefix = "";
686 if(!suffix) suffix = "";
688 char buffer[RRDDIMVAR_ID_MAX + 1];
689 RRDDIMVAR *rs = (RRDDIMVAR *)callocz(1, sizeof(RRDDIMVAR));
691 rs->prefix = strdupz(prefix);
692 rs->suffix = strdupz(suffix);
694 snprintfz(buffer, RRDDIMVAR_ID_MAX, "%s%s%s", rs->prefix, rd->id, rs->suffix);
695 rs->id = strdupz(buffer);
697 snprintfz(buffer, RRDDIMVAR_ID_MAX, "%s%s%s", rs->prefix, rd->name, rs->suffix);
698 rs->name = strdupz(buffer);
700 snprintfz(buffer, RRDDIMVAR_ID_MAX, "%s.%s", rd->rrdset->id, rs->id);
701 rs->fullidid = strdupz(buffer);
703 snprintfz(buffer, RRDDIMVAR_ID_MAX, "%s.%s", rd->rrdset->id, rs->name);
704 rs->fullidname = strdupz(buffer);
706 snprintfz(buffer, RRDDIMVAR_ID_MAX, "%s.%s", rd->rrdset->name, rs->id);
707 rs->fullnameid = strdupz(buffer);
709 snprintfz(buffer, RRDDIMVAR_ID_MAX, "%s.%s", rd->rrdset->name, rs->name);
710 rs->fullnamename = strdupz(buffer);
714 rs->options = options;
717 rs->local_id = rrdvar_create_and_index("local", &st->variables_root_index, rs->id, rs->type, rs->value);
718 rs->local_name = rrdvar_create_and_index("local", &st->variables_root_index, rs->name, rs->type, rs->value);
720 rs->family_id = rrdvar_create_and_index("family", &st->rrdfamily->variables_root_index, rs->id, rs->type, rs->value);
721 rs->family_name = rrdvar_create_and_index("family", &st->rrdfamily->variables_root_index, rs->name, rs->type, rs->value);
723 rs->host_fullidid = rrdvar_create_and_index("host", &st->rrdhost->variables_root_index, rs->fullidid, rs->type, rs->value);
724 rs->host_fullidname = rrdvar_create_and_index("host", &st->rrdhost->variables_root_index, rs->fullidname, rs->type, rs->value);
725 rs->host_fullnameid = rrdvar_create_and_index("host", &st->rrdhost->variables_root_index, rs->fullnameid, rs->type, rs->value);
726 rs->host_fullnamename = rrdvar_create_and_index("host", &st->rrdhost->variables_root_index, rs->fullnamename, rs->type, rs->value);
728 rs->next = rd->variables;
734 void rrddimvar_rename_all(RRDDIM *rd) {
735 RRDSET *st = rd->rrdset;
736 debug(D_VARIABLES, "RRDDIMSET rename for chart id '%s' name '%s', dimension id '%s', name '%s'", st->id, st->name, rd->id, rd->name);
738 RRDDIMVAR *rs, *next = rd->variables;
742 if (strcmp(rd->name, rs->name)) {
743 char buffer[RRDDIMVAR_ID_MAX + 1];
747 rrdvar_free(st->rrdhost, &st->variables_root_index, rs->local_name);
749 snprintfz(buffer, RRDDIMVAR_ID_MAX, "%s%s%s", rs->prefix, rd->name, rs->suffix);
750 rs->name = strdupz(buffer);
751 rs->local_name = rrdvar_create_and_index("local", &st->variables_root_index, rs->name, rs->type, rs->value);
753 rrdvar_free(st->rrdhost, &st->rrdhost->variables_root_index, rs->host_fullidname);
754 freez(rs->fullidname);
755 snprintfz(buffer, RRDDIMVAR_ID_MAX, "%s.%s", st->id, rs->name);
756 rs->fullidname = strdupz(buffer);
757 rs->host_fullidname = rrdvar_create_and_index("host", &st->rrdhost->variables_root_index,
758 rs->fullidname, rs->type, rs->value);
761 rrdvar_free(st->rrdhost, &st->rrdhost->variables_root_index, rs->host_fullnameid);
762 freez(rs->fullnameid);
763 snprintfz(buffer, RRDDIMVAR_ID_MAX, "%s.%s", st->name, rs->id);
764 rs->fullnameid = strdupz(buffer);
765 rs->host_fullnameid = rrdvar_create_and_index("host", &st->rrdhost->variables_root_index,
766 rs->fullnameid, rs->type, rs->value);
769 rrdvar_free(st->rrdhost, &st->rrdhost->variables_root_index, rs->host_fullnamename);
770 freez(rs->fullnamename);
771 snprintfz(buffer, RRDDIMVAR_ID_MAX, "%s.%s", st->name, rs->name);
772 rs->fullnamename = strdupz(buffer);
773 rs->host_fullnamename = rrdvar_create_and_index("host", &st->rrdhost->variables_root_index,
774 rs->fullnamename, rs->type, rs->value);
779 void rrddimvar_free(RRDDIMVAR *rs) {
780 RRDDIM *rd = rs->rrddim;
781 RRDSET *st = rd->rrdset;
782 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);
784 rrdvar_free(st->rrdhost, &st->variables_root_index, rs->local_id);
785 rrdvar_free(st->rrdhost, &st->variables_root_index, rs->local_name);
787 rrdvar_free(st->rrdhost, &st->rrdfamily->variables_root_index, rs->family_id);
788 rrdvar_free(st->rrdhost, &st->rrdfamily->variables_root_index, rs->family_name);
790 rrdvar_free(st->rrdhost, &st->rrdhost->variables_root_index, rs->host_fullidid);
791 rrdvar_free(st->rrdhost, &st->rrdhost->variables_root_index, rs->host_fullidname);
792 rrdvar_free(st->rrdhost, &st->rrdhost->variables_root_index, rs->host_fullnameid);
793 rrdvar_free(st->rrdhost, &st->rrdhost->variables_root_index, rs->host_fullnamename);
795 if(rd->variables == rs) {
796 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);
797 rd->variables = rs->next;
800 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);
802 for (t = rd->variables; t && t->next != rs; t = t->next) ;
803 if(!t) error("RRDDIMVAR '%s' not found in dimension '%s/%s' variables linked list", rs->name, st->id, rd->id);
804 else t->next = rs->next;
812 freez(rs->fullidname);
813 freez(rs->fullnameid);
814 freez(rs->fullnamename);
818 // ----------------------------------------------------------------------------
819 // RRDCALC management
821 static inline const char *rrdcalc_status2string(int status) {
823 case RRDCALC_STATUS_REMOVED:
826 case RRDCALC_STATUS_UNDEFINED:
829 case RRDCALC_STATUS_UNINITIALIZED:
830 return "UNINITIALIZED";
832 case RRDCALC_STATUS_CLEAR:
835 case RRDCALC_STATUS_RAISED:
838 case RRDCALC_STATUS_WARNING:
841 case RRDCALC_STATUS_CRITICAL:
845 error("Unknown alarm status %d", status);
850 static void rrdsetcalc_link(RRDSET *st, RRDCALC *rc) {
851 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);
853 rc->last_status_change = time(NULL);
856 rc->rrdset_next = st->alarms;
857 rc->rrdset_prev = NULL;
860 rc->rrdset_next->rrdset_prev = rc;
864 if(rc->update_every < rc->rrdset->update_every) {
865 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);
866 rc->update_every = rc->rrdset->update_every;
869 if(!isnan(rc->green) && isnan(st->green)) {
870 debug(D_HEALTH, "Health alarm '%s.%s' green threshold set from %Lf to %Lf.", rc->rrdset->id, rc->name, rc->rrdset->green, rc->green);
871 st->green = rc->green;
874 if(!isnan(rc->red) && isnan(st->red)) {
875 debug(D_HEALTH, "Health alarm '%s.%s' red threshold set from %Lf to %Lf.", rc->rrdset->id, rc->name, rc->rrdset->red, rc->red);
879 rc->local = rrdvar_create_and_index("local", &st->variables_root_index, rc->name, RRDVAR_TYPE_CALCULATED, &rc->value);
880 rc->family = rrdvar_create_and_index("family", &st->rrdfamily->variables_root_index, rc->name, RRDVAR_TYPE_CALCULATED, &rc->value);
882 char fullname[RRDVAR_MAX_LENGTH + 1];
883 snprintfz(fullname, RRDVAR_MAX_LENGTH, "%s.%s", st->id, rc->name);
884 rc->hostid = rrdvar_create_and_index("host", &st->rrdhost->variables_root_index, fullname, RRDVAR_TYPE_CALCULATED, &rc->value);
886 snprintfz(fullname, RRDVAR_MAX_LENGTH, "%s.%s", st->name, rc->name);
887 rc->hostname = rrdvar_create_and_index("host", &st->rrdhost->variables_root_index, fullname, RRDVAR_TYPE_CALCULATED, &rc->value);
889 if(!rc->units) rc->units = strdupz(st->units);
892 time_t now = time(NULL);
893 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);
897 static inline int rrdcalc_is_matching_this_rrdset(RRDCALC *rc, RRDSET *st) {
898 if( (rc->hash_chart == st->hash && !strcmp(rc->chart, st->id)) ||
899 (rc->hash_chart == st->hash_name && !strcmp(rc->chart, st->name)))
905 // this has to be called while the RRDHOST is locked
906 inline void rrdsetcalc_link_matching(RRDSET *st) {
907 // debug(D_HEALTH, "find matching alarms for chart '%s'", st->id);
910 for(rc = st->rrdhost->alarms; rc ; rc = rc->next) {
911 if(unlikely(rc->rrdset))
914 if(unlikely(rrdcalc_is_matching_this_rrdset(rc, st)))
915 rrdsetcalc_link(st, rc);
919 // this has to be called while the RRDHOST is locked
920 inline void rrdsetcalc_unlink(RRDCALC *rc) {
921 RRDSET *st = rc->rrdset;
924 debug(D_HEALTH, "Requested to unlink RRDCALC '%s.%s' which is not linked to any RRDSET", rc->chart?rc->chart:"NOCHART", rc->name);
925 error("Requested to unlink RRDCALC '%s.%s' which is not linked to any RRDSET", rc->chart?rc->chart:"NOCHART", rc->name);
930 time_t now = time(NULL);
931 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);
934 RRDHOST *host = st->rrdhost;
936 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);
940 rc->rrdset_prev->rrdset_next = rc->rrdset_next;
943 rc->rrdset_next->rrdset_prev = rc->rrdset_prev;
946 st->alarms = rc->rrdset_next;
948 rc->rrdset_prev = rc->rrdset_next = NULL;
950 rrdvar_free(st->rrdhost, &st->variables_root_index, rc->local);
953 rrdvar_free(st->rrdhost, &st->rrdfamily->variables_root_index, rc->family);
956 rrdvar_free(st->rrdhost, &st->rrdhost->variables_root_index, rc->hostid);
959 rrdvar_free(st->rrdhost, &st->rrdhost->variables_root_index, rc->hostname);
964 // RRDCALC will remain in RRDHOST
965 // so that if the matching chart is found in the future
966 // it will be applied automatically
969 RRDCALC *rrdcalc_find(RRDSET *st, const char *name) {
971 uint32_t hash = simple_hash(name);
973 for( rc = st->alarms; rc ; rc = rc->rrdset_next ) {
974 if(unlikely(rc->hash == hash && !strcmp(rc->name, name)))
981 static inline int rrdcalc_exists(RRDHOST *host, const char *chart, const char *name, uint32_t hash_chart, uint32_t hash_name) {
984 if(unlikely(!chart)) {
985 error("attempt to find RRDCALC '%s' without giving a chart name", name);
989 if(unlikely(!hash_chart)) hash_chart = simple_hash(chart);
990 if(unlikely(!hash_name)) hash_name = simple_hash(name);
992 // make sure it does not already exist
993 for(rc = host->alarms; rc ; rc = rc->next) {
994 if (unlikely(rc->chart && rc->hash == hash_name && rc->hash_chart == hash_chart && !strcmp(name, rc->name) && !strcmp(chart, rc->chart))) {
995 debug(D_HEALTH, "Health alarm '%s.%s' already exists in host '%s'.", chart, name, host->hostname);
996 error("Health alarm '%s.%s' already exists in host '%s'.", chart, name, host->hostname);
1004 static inline uint32_t rrdcalc_get_unique_id(RRDHOST *host, const char *chart, const char *name, uint32_t *next_event_id) {
1006 uint32_t hash_chart = simple_hash(chart);
1007 uint32_t hash_name = simple_hash(name);
1009 // re-use old IDs, by looking them up in the alarm log
1011 for(ae = host->health_log.alarms; ae ;ae = ae->next) {
1012 if(unlikely(ae->hash_name == hash_name && ae->hash_chart == hash_chart && !strcmp(name, ae->name) && !strcmp(chart, ae->chart))) {
1013 if(next_event_id) *next_event_id = ae->alarm_event_id + 1;
1014 return ae->alarm_id;
1019 return host->health_log.next_alarm_id++;
1022 static inline void rrdcalc_create_part2(RRDHOST *host, RRDCALC *rc) {
1023 rrdhost_check_rdlock(host);
1025 if(rc->calculation) {
1026 rc->calculation->status = &rc->status;
1027 rc->calculation->this = &rc->value;
1028 rc->calculation->after = &rc->db_after;
1029 rc->calculation->before = &rc->db_before;
1030 rc->calculation->rrdcalc = rc;
1034 rc->warning->status = &rc->status;
1035 rc->warning->this = &rc->value;
1036 rc->warning->after = &rc->db_after;
1037 rc->warning->before = &rc->db_before;
1038 rc->warning->rrdcalc = rc;
1042 rc->critical->status = &rc->status;
1043 rc->critical->this = &rc->value;
1044 rc->critical->after = &rc->db_after;
1045 rc->critical->before = &rc->db_before;
1046 rc->critical->rrdcalc = rc;
1049 // link it to the host
1050 if(likely(host->alarms)) {
1053 for(t = host->alarms; t && t->next ; t = t->next) ;
1060 // link it to its chart
1062 for(st = host->rrdset_root; st ; st = st->next) {
1063 if(rrdcalc_is_matching_this_rrdset(rc, st)) {
1064 rrdsetcalc_link(st, rc);
1070 static inline RRDCALC *rrdcalc_create(RRDHOST *host, RRDCALCTEMPLATE *rt, const char *chart) {
1072 debug(D_HEALTH, "Health creating dynamic alarm (from template) '%s.%s'", chart, rt->name);
1074 if(rrdcalc_exists(host, chart, rt->name, 0, 0))
1077 RRDCALC *rc = callocz(1, sizeof(RRDCALC));
1078 rc->next_event_id = 1;
1079 rc->id = rrdcalc_get_unique_id(host, chart, rt->name, &rc->next_event_id);
1080 rc->name = strdupz(rt->name);
1081 rc->hash = simple_hash(rc->name);
1082 rc->chart = strdupz(chart);
1083 rc->hash_chart = simple_hash(rc->chart);
1085 if(rt->dimensions) rc->dimensions = strdupz(rt->dimensions);
1087 rc->green = rt->green;
1090 rc->old_value = NAN;
1092 rc->delay_up_duration = rt->delay_up_duration;
1093 rc->delay_down_duration = rt->delay_down_duration;
1094 rc->delay_max_duration = rt->delay_max_duration;
1095 rc->delay_multiplier = rt->delay_multiplier;
1097 rc->group = rt->group;
1098 rc->after = rt->after;
1099 rc->before = rt->before;
1100 rc->update_every = rt->update_every;
1101 rc->options = rt->options;
1103 if(rt->exec) rc->exec = strdupz(rt->exec);
1104 if(rt->recipient) rc->recipient = strdupz(rt->recipient);
1105 if(rt->source) rc->source = strdupz(rt->source);
1106 if(rt->units) rc->units = strdupz(rt->units);
1107 if(rt->info) rc->info = strdupz(rt->info);
1109 if(rt->calculation) {
1110 rc->calculation = expression_parse(rt->calculation->source, NULL, NULL);
1111 if(!rc->calculation)
1112 error("Health alarm '%s.%s': failed to parse calculation expression '%s'", chart, rt->name, rt->calculation->source);
1115 rc->warning = expression_parse(rt->warning->source, NULL, NULL);
1117 error("Health alarm '%s.%s': failed to re-parse warning expression '%s'", chart, rt->name, rt->warning->source);
1120 rc->critical = expression_parse(rt->critical->source, NULL, NULL);
1122 error("Health alarm '%s.%s': failed to re-parse critical expression '%s'", chart, rt->name, rt->critical->source);
1125 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",
1126 (rc->chart)?rc->chart:"NOCHART",
1128 (rc->exec)?rc->exec:"DEFAULT",
1129 (rc->recipient)?rc->recipient:"DEFAULT",
1136 (rc->dimensions)?rc->dimensions:"NONE",
1138 (rc->calculation)?rc->calculation->parsed_as:"NONE",
1139 (rc->warning)?rc->warning->parsed_as:"NONE",
1140 (rc->critical)?rc->critical->parsed_as:"NONE",
1142 rc->delay_up_duration,
1143 rc->delay_down_duration,
1144 rc->delay_max_duration,
1145 rc->delay_multiplier
1148 rrdcalc_create_part2(host, rc);
1152 void rrdcalc_free(RRDHOST *host, RRDCALC *rc) {
1155 debug(D_HEALTH, "Health removing alarm '%s.%s' of host '%s'", rc->chart?rc->chart:"NOCHART", rc->name, host->hostname);
1157 // unlink it from RRDSET
1158 if(rc->rrdset) rrdsetcalc_unlink(rc);
1160 // unlink it from RRDHOST
1161 if(unlikely(rc == host->alarms))
1162 host->alarms = rc->next;
1164 else if(likely(host->alarms)) {
1165 RRDCALC *t, *last = host->alarms;
1166 for(t = last->next; t && t != rc; last = t, t = t->next) ;
1167 if(last->next == rc)
1168 last->next = rc->next;
1170 error("Cannot unlink alarm '%s.%s' from host '%s': not found", rc->chart?rc->chart:"NOCHART", rc->name, host->hostname);
1173 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);
1175 expression_free(rc->calculation);
1176 expression_free(rc->warning);
1177 expression_free(rc->critical);
1182 freez(rc->dimensions);
1184 freez(rc->recipient);
1191 // ----------------------------------------------------------------------------
1192 // RRDCALCTEMPLATE management
1194 void rrdcalctemplate_link_matching(RRDSET *st) {
1195 RRDCALCTEMPLATE *rt;
1197 for(rt = st->rrdhost->templates; rt ; rt = rt->next) {
1198 if(rt->hash_context == st->hash_context && !strcmp(rt->context, st->context)) {
1199 RRDCALC *rc = rrdcalc_create(st->rrdhost, rt, st->id);
1201 error("Health tried to create alarm from template '%s', but it failed", rt->name);
1203 #ifdef NETDATA_INTERNAL_CHECKS
1204 else if(rc->rrdset != st)
1205 error("Health alarm '%s.%s' should be linked to chart '%s', but it is not", rc->chart?rc->chart:"NOCHART", rc->name, st->id);
1211 static inline void rrdcalctemplate_free(RRDHOST *host, RRDCALCTEMPLATE *rt) {
1212 debug(D_HEALTH, "Health removing template '%s' of host '%s'", rt->name, host->hostname);
1214 if(host->templates) {
1215 if(host->templates == rt) {
1216 host->templates = rt->next;
1219 RRDCALCTEMPLATE *t, *last = host->templates;
1220 for (t = last->next; t && t != rt; last = t, t = t->next ) ;
1221 if(last && last->next == rt) {
1222 last->next = rt->next;
1226 error("Cannot find RRDCALCTEMPLATE '%s' linked in host '%s'", rt->name, host->hostname);
1230 expression_free(rt->calculation);
1231 expression_free(rt->warning);
1232 expression_free(rt->critical);
1236 freez(rt->recipient);
1241 freez(rt->dimensions);
1245 // ----------------------------------------------------------------------------
1246 // load health configuration
1248 #define HEALTH_CONF_MAX_LINE 4096
1250 #define HEALTH_ALARM_KEY "alarm"
1251 #define HEALTH_TEMPLATE_KEY "template"
1252 #define HEALTH_ON_KEY "on"
1253 #define HEALTH_LOOKUP_KEY "lookup"
1254 #define HEALTH_CALC_KEY "calc"
1255 #define HEALTH_EVERY_KEY "every"
1256 #define HEALTH_GREEN_KEY "green"
1257 #define HEALTH_RED_KEY "red"
1258 #define HEALTH_WARN_KEY "warn"
1259 #define HEALTH_CRIT_KEY "crit"
1260 #define HEALTH_EXEC_KEY "exec"
1261 #define HEALTH_RECIPIENT_KEY "to"
1262 #define HEALTH_UNITS_KEY "units"
1263 #define HEALTH_INFO_KEY "info"
1264 #define HEALTH_DELAY_KEY "delay"
1266 static inline int rrdcalc_add_alarm_from_config(RRDHOST *host, RRDCALC *rc) {
1268 error("Health configuration for alarm '%s' does not have a chart", rc->name);
1272 if(!rc->update_every) {
1273 error("Health configuration for alarm '%s.%s' has no frequency (parameter 'every'). Ignoring it.", rc->chart?rc->chart:"NOCHART", rc->name);
1277 if(!RRDCALC_HAS_DB_LOOKUP(rc) && !rc->warning && !rc->critical) {
1278 error("Health configuration for alarm '%s.%s' is useless (no calculation, no warning and no critical evaluation)", rc->chart?rc->chart:"NOCHART", rc->name);
1282 if (rrdcalc_exists(host, rc->chart, rc->name, rc->hash_chart, rc->hash))
1285 rc->id = rrdcalc_get_unique_id(&localhost, rc->chart, rc->name, &rc->next_event_id);
1287 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",
1288 rc->chart?rc->chart:"NOCHART",
1291 (rc->exec)?rc->exec:"DEFAULT",
1292 (rc->recipient)?rc->recipient:"DEFAULT",
1299 (rc->dimensions)?rc->dimensions:"NONE",
1301 (rc->calculation)?rc->calculation->parsed_as:"NONE",
1302 (rc->warning)?rc->warning->parsed_as:"NONE",
1303 (rc->critical)?rc->critical->parsed_as:"NONE",
1305 rc->delay_up_duration,
1306 rc->delay_down_duration,
1307 rc->delay_max_duration,
1308 rc->delay_multiplier
1311 rrdcalc_create_part2(host, rc);
1315 static inline int rrdcalctemplate_add_template_from_config(RRDHOST *host, RRDCALCTEMPLATE *rt) {
1316 if(unlikely(!rt->context)) {
1317 error("Health configuration for template '%s' does not have a context", rt->name);
1321 if(unlikely(!rt->update_every)) {
1322 error("Health configuration for template '%s' has no frequency (parameter 'every'). Ignoring it.", rt->name);
1326 if(unlikely(!RRDCALCTEMPLATE_HAS_CALCULATION(rt) && !rt->warning && !rt->critical)) {
1327 error("Health configuration for template '%s' is useless (no calculation, no warning and no critical evaluation)", rt->name);
1331 RRDCALCTEMPLATE *t, *last = NULL;
1332 for (t = host->templates; t ; last = t, t = t->next) {
1333 if(unlikely(t->hash_name == rt->hash_name && !strcmp(t->name, rt->name))) {
1334 error("Health configuration template '%s' already exists for host '%s'.", rt->name, host->hostname);
1339 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",
1341 (rt->context)?rt->context:"NONE",
1342 (rt->exec)?rt->exec:"DEFAULT",
1343 (rt->recipient)?rt->recipient:"DEFAULT",
1350 (rt->dimensions)?rt->dimensions:"NONE",
1352 (rt->calculation)?rt->calculation->parsed_as:"NONE",
1353 (rt->warning)?rt->warning->parsed_as:"NONE",
1354 (rt->critical)?rt->critical->parsed_as:"NONE",
1356 rt->delay_up_duration,
1357 rt->delay_down_duration,
1358 rt->delay_max_duration,
1359 rt->delay_multiplier
1366 rt->next = host->templates;
1367 host->templates = rt;
1373 static inline int health_parse_duration(char *string, int *result) {
1374 // make sure it is a number
1375 if(!*string || !(isdigit(*string) || *string == '+' || *string == '-')) {
1381 calculated_number n = strtold(string, &e);
1385 *result = (int) (n * 86400 * 365);
1388 *result = (int) (n * 86400 * 30);
1391 *result = (int) (n * 86400 * 7);
1394 *result = (int) (n * 86400);
1397 *result = (int) (n * 3600);
1400 *result = (int) (n * 60);
1405 *result = (int) (n);
1415 static inline int health_parse_delay(
1416 size_t line, const char *path, const char *file, char *string,
1417 int *delay_up_duration,
1418 int *delay_down_duration,
1419 int *delay_max_duration,
1420 float *delay_multiplier) {
1423 char given_down = 0;
1425 char given_multiplier = 0;
1431 while(*s && !isspace(*s)) s++;
1432 while(*s && isspace(*s)) *s++ = '\0';
1437 while(*s && !isspace(*s)) s++;
1438 while(*s && isspace(*s)) *s++ = '\0';
1440 if(!strcasecmp(key, "up")) {
1441 if (!health_parse_duration(value, delay_up_duration)) {
1442 error("Health configuration at line %zu of file '%s/%s': invalid value '%s' for '%s' keyword",
1443 line, path, file, value, key);
1447 else if(!strcasecmp(key, "down")) {
1448 if (!health_parse_duration(value, delay_down_duration)) {
1449 error("Health configuration at line %zu of file '%s/%s': invalid value '%s' for '%s' keyword",
1450 line, path, file, value, key);
1452 else given_down = 1;
1454 else if(!strcasecmp(key, "multiplier")) {
1455 *delay_multiplier = strtof(value, NULL);
1456 if(isnan(*delay_multiplier) || isinf(*delay_multiplier) || islessequal(*delay_multiplier, 0)) {
1457 error("Health configuration at line %zu of file '%s/%s': invalid value '%s' for '%s' keyword",
1458 line, path, file, value, key);
1460 else given_multiplier = 1;
1462 else if(!strcasecmp(key, "max")) {
1463 if (!health_parse_duration(value, delay_max_duration)) {
1464 error("Health configuration at line %zu of file '%s/%s': invalid value '%s' for '%s' keyword",
1465 line, path, file, value, key);
1470 error("Health configuration at line %zu of file '%s/%s': unknown keyword '%s'",
1471 line, path, file, key);
1476 *delay_up_duration = 0;
1479 *delay_down_duration = 0;
1481 if(!given_multiplier)
1482 *delay_multiplier = 1.0;
1485 if((*delay_max_duration) < (*delay_up_duration) * (*delay_multiplier))
1486 *delay_max_duration = (*delay_up_duration) * (*delay_multiplier);
1488 if((*delay_max_duration) < (*delay_down_duration) * (*delay_multiplier))
1489 *delay_max_duration = (*delay_down_duration) * (*delay_multiplier);
1495 static inline int health_parse_db_lookup(
1496 size_t line, const char *path, const char *file, char *string,
1497 int *group_method, int *after, int *before, int *every,
1498 uint32_t *options, char **dimensions
1500 debug(D_HEALTH, "Health configuration parsing database lookup %zu@%s/%s: %s", line, path, file, string);
1502 if(*dimensions) freez(*dimensions);
1509 char *s = string, *key;
1511 // first is the group method
1513 while(*s && !isspace(*s)) s++;
1514 while(*s && isspace(*s)) *s++ = '\0';
1516 error("Health configuration invalid chart calculation at line %zu of file '%s/%s': expected group method followed by the 'after' time, but got '%s'",
1517 line, path, file, key);
1521 if((*group_method = web_client_api_request_v1_data_group(key, -1)) == -1) {
1522 error("Health configuration at line %zu of file '%s/%s': invalid group method '%s'",
1523 line, path, file, key);
1527 // then is the 'after' time
1529 while(*s && !isspace(*s)) s++;
1530 while(*s && isspace(*s)) *s++ = '\0';
1532 if(!health_parse_duration(key, after)) {
1533 error("Health configuration at line %zu of file '%s/%s': invalid duration '%s' after group method",
1534 line, path, file, key);
1539 *every = abs(*after);
1541 // now we may have optional parameters
1544 while(*s && !isspace(*s)) s++;
1545 while(*s && isspace(*s)) *s++ = '\0';
1548 if(!strcasecmp(key, "at")) {
1550 while(*s && !isspace(*s)) s++;
1551 while(*s && isspace(*s)) *s++ = '\0';
1553 if (!health_parse_duration(value, before)) {
1554 error("Health configuration at line %zu of file '%s/%s': invalid duration '%s' for '%s' keyword",
1555 line, path, file, value, key);
1558 else if(!strcasecmp(key, HEALTH_EVERY_KEY)) {
1560 while(*s && !isspace(*s)) s++;
1561 while(*s && isspace(*s)) *s++ = '\0';
1563 if (!health_parse_duration(value, every)) {
1564 error("Health configuration at line %zu of file '%s/%s': invalid duration '%s' for '%s' keyword",
1565 line, path, file, value, key);
1568 else if(!strcasecmp(key, "absolute") || !strcasecmp(key, "abs") || !strcasecmp(key, "absolute_sum")) {
1569 *options |= RRDR_OPTION_ABSOLUTE;
1571 else if(!strcasecmp(key, "min2max")) {
1572 *options |= RRDR_OPTION_MIN2MAX;
1574 else if(!strcasecmp(key, "null2zero")) {
1575 *options |= RRDR_OPTION_NULL2ZERO;
1577 else if(!strcasecmp(key, "percentage")) {
1578 *options |= RRDR_OPTION_PERCENTAGE;
1580 else if(!strcasecmp(key, "unaligned")) {
1581 *options |= RRDR_OPTION_NOT_ALIGNED;
1583 else if(!strcasecmp(key, "of")) {
1584 if(*s && strcasecmp(s, "all"))
1585 *dimensions = strdupz(s);
1589 error("Health configuration at line %zu of file '%s/%s': unknown keyword '%s'",
1590 line, path, file, key);
1597 static inline char *tabs2spaces(char *s) {
1600 if(unlikely(*t == '\t')) *t = ' ';
1607 static inline char *health_source_file(size_t line, const char *path, const char *filename) {
1608 char buffer[FILENAME_MAX + 1];
1609 snprintfz(buffer, FILENAME_MAX, "%zu@%s/%s", line, path, filename);
1610 return strdupz(buffer);
1613 static inline void strip_quotes(char *s) {
1615 if(*s == '\'' || *s == '"') *s = ' ';
1620 int health_readfile(const char *path, const char *filename) {
1621 debug(D_HEALTH, "Health configuration reading file '%s/%s'", path, filename);
1623 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;
1624 char buffer[HEALTH_CONF_MAX_LINE + 1];
1626 if(unlikely(!hash_alarm)) {
1627 hash_alarm = simple_uhash(HEALTH_ALARM_KEY);
1628 hash_template = simple_uhash(HEALTH_TEMPLATE_KEY);
1629 hash_on = simple_uhash(HEALTH_ON_KEY);
1630 hash_calc = simple_uhash(HEALTH_CALC_KEY);
1631 hash_lookup = simple_uhash(HEALTH_LOOKUP_KEY);
1632 hash_green = simple_uhash(HEALTH_GREEN_KEY);
1633 hash_red = simple_uhash(HEALTH_RED_KEY);
1634 hash_warn = simple_uhash(HEALTH_WARN_KEY);
1635 hash_crit = simple_uhash(HEALTH_CRIT_KEY);
1636 hash_exec = simple_uhash(HEALTH_EXEC_KEY);
1637 hash_every = simple_uhash(HEALTH_EVERY_KEY);
1638 hash_units = simple_hash(HEALTH_UNITS_KEY);
1639 hash_info = simple_hash(HEALTH_INFO_KEY);
1640 hash_recipient = simple_hash(HEALTH_RECIPIENT_KEY);
1641 hash_delay = simple_uhash(HEALTH_DELAY_KEY);
1644 snprintfz(buffer, HEALTH_CONF_MAX_LINE, "%s/%s", path, filename);
1645 FILE *fp = fopen(buffer, "r");
1647 error("Health configuration cannot read file '%s'.", buffer);
1652 RRDCALCTEMPLATE *rt = NULL;
1654 size_t line = 0, append = 0;
1656 while((s = fgets(&buffer[append], (int)(HEALTH_CONF_MAX_LINE - append), fp)) || append) {
1657 int stop_appending = !s;
1659 // info("Line %zu of file '%s/%s': '%s'", line, path, filename, s);
1662 // info("Trimmed line %zu of file '%s/%s': '%s'", line, path, filename, s);
1665 if(!stop_appending && s[append - 1] == '\\') {
1666 s[append - 1] = ' ';
1667 append = &s[append] - buffer;
1668 if(append < HEALTH_CONF_MAX_LINE)
1671 error("Health configuration has too long muli-line at line %zu of file '%s/%s'.", line, path, filename);
1677 while(*s && *s != ':') s++;
1679 error("Health configuration has invalid line %zu of file '%s/%s'. It does not contain a ':'. Ignoring it.", line, path, filename);
1687 value = trim(value);
1690 error("Health configuration has invalid line %zu of file '%s/%s'. Keyword is empty. Ignoring it.", line, path, filename);
1695 error("Health configuration has invalid line %zu of file '%s/%s'. value is empty. Ignoring it.", line, path, filename);
1699 // info("Health file '%s/%s', key '%s', value '%s'", path, filename, key, value);
1700 uint32_t hash = simple_uhash(key);
1702 if(hash == hash_alarm && !strcasecmp(key, HEALTH_ALARM_KEY)) {
1703 if(rc && !rrdcalc_add_alarm_from_config(&localhost, rc))
1704 rrdcalc_free(&localhost, rc);
1707 if (!rrdcalctemplate_add_template_from_config(&localhost, rt))
1708 rrdcalctemplate_free(&localhost, rt);
1712 rc = callocz(1, sizeof(RRDCALC));
1713 rc->next_event_id = 1;
1714 rc->name = tabs2spaces(strdupz(value));
1715 rc->hash = simple_hash(rc->name);
1716 rc->source = health_source_file(line, path, filename);
1720 rc->old_value = NAN;
1721 rc->delay_multiplier = 1.0;
1723 if(rrdvar_fix_name(rc->name))
1724 error("Health configuration renamed alarm '%s' to '%s'", value, rc->name);
1726 else if(hash == hash_template && !strcasecmp(key, HEALTH_TEMPLATE_KEY)) {
1728 if(!rrdcalc_add_alarm_from_config(&localhost, rc))
1729 rrdcalc_free(&localhost, rc);
1733 if(rt && !rrdcalctemplate_add_template_from_config(&localhost, rt))
1734 rrdcalctemplate_free(&localhost, rt);
1736 rt = callocz(1, sizeof(RRDCALCTEMPLATE));
1737 rt->name = tabs2spaces(strdupz(value));
1738 rt->hash_name = simple_hash(rt->name);
1739 rt->source = health_source_file(line, path, filename);
1742 rt->delay_multiplier = 1.0;
1744 if(rrdvar_fix_name(rt->name))
1745 error("Health configuration renamed template '%s' to '%s'", value, rt->name);
1748 if(hash == hash_on && !strcasecmp(key, HEALTH_ON_KEY)) {
1750 if(strcmp(rc->chart, value))
1751 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').",
1752 line, path, filename, rc->name, key, rc->chart, value, value);
1756 rc->chart = tabs2spaces(strdupz(value));
1757 rc->hash_chart = simple_hash(rc->chart);
1759 else if(hash == hash_lookup && !strcasecmp(key, HEALTH_LOOKUP_KEY)) {
1760 health_parse_db_lookup(line, path, filename, value, &rc->group, &rc->after, &rc->before,
1762 &rc->options, &rc->dimensions);
1764 else if(hash == hash_every && !strcasecmp(key, HEALTH_EVERY_KEY)) {
1765 if(!health_parse_duration(value, &rc->update_every))
1766 info("Health configuration at line %zu of file '%s/%s' for alarm '%s' at key '%s' cannot parse duration: '%s'.",
1767 line, path, filename, rc->name, key, value);
1769 else if(hash == hash_green && !strcasecmp(key, HEALTH_GREEN_KEY)) {
1771 rc->green = strtold(value, &e);
1773 info("Health configuration at line %zu of file '%s/%s' for alarm '%s' at key '%s' leaves this string unmatched: '%s'.",
1774 line, path, filename, rc->name, key, e);
1777 else if(hash == hash_red && !strcasecmp(key, HEALTH_RED_KEY)) {
1779 rc->red = strtold(value, &e);
1781 info("Health configuration at line %zu of file '%s/%s' for alarm '%s' at key '%s' leaves this string unmatched: '%s'.",
1782 line, path, filename, rc->name, key, e);
1785 else if(hash == hash_calc && !strcasecmp(key, HEALTH_CALC_KEY)) {
1786 const char *failed_at = NULL;
1788 rc->calculation = expression_parse(value, &failed_at, &error);
1789 if(!rc->calculation) {
1790 error("Health configuration at line %zu of file '%s/%s' for alarm '%s' at key '%s' has unparse-able expression '%s': %s at '%s'",
1791 line, path, filename, rc->name, key, value, expression_strerror(error), failed_at);
1794 else if(hash == hash_warn && !strcasecmp(key, HEALTH_WARN_KEY)) {
1795 const char *failed_at = NULL;
1797 rc->warning = expression_parse(value, &failed_at, &error);
1799 error("Health configuration at line %zu of file '%s/%s' for alarm '%s' at key '%s' has unparse-able expression '%s': %s at '%s'",
1800 line, path, filename, rc->name, key, value, expression_strerror(error), failed_at);
1803 else if(hash == hash_crit && !strcasecmp(key, HEALTH_CRIT_KEY)) {
1804 const char *failed_at = NULL;
1806 rc->critical = expression_parse(value, &failed_at, &error);
1808 error("Health configuration at line %zu of file '%s/%s' for alarm '%s' at key '%s' has unparse-able expression '%s': %s at '%s'",
1809 line, path, filename, rc->name, key, value, expression_strerror(error), failed_at);
1812 else if(hash == hash_exec && !strcasecmp(key, HEALTH_EXEC_KEY)) {
1814 if(strcmp(rc->exec, value))
1815 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').",
1816 line, path, filename, rc->name, key, rc->exec, value, value);
1820 rc->exec = tabs2spaces(strdupz(value));
1822 else if(hash == hash_recipient && !strcasecmp(key, HEALTH_RECIPIENT_KEY)) {
1824 if(strcmp(rc->recipient, value))
1825 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').",
1826 line, path, filename, rc->name, key, rc->recipient, value, value);
1828 freez(rc->recipient);
1830 rc->recipient = tabs2spaces(strdupz(value));
1832 else if(hash == hash_units && !strcasecmp(key, HEALTH_UNITS_KEY)) {
1834 if(strcmp(rc->units, value))
1835 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').",
1836 line, path, filename, rc->name, key, rc->units, value, value);
1840 rc->units = tabs2spaces(strdupz(value));
1841 strip_quotes(rc->units);
1843 else if(hash == hash_info && !strcasecmp(key, HEALTH_INFO_KEY)) {
1845 if(strcmp(rc->info, value))
1846 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').",
1847 line, path, filename, rc->name, key, rc->info, value, value);
1851 rc->info = tabs2spaces(strdupz(value));
1852 strip_quotes(rc->info);
1854 else if(hash == hash_delay && !strcasecmp(key, HEALTH_DELAY_KEY)) {
1855 health_parse_delay(line, path, filename, value, &rc->delay_up_duration, &rc->delay_down_duration, &rc->delay_max_duration, &rc->delay_multiplier);
1858 error("Health configuration at line %zu of file '%s/%s' for alarm '%s' has unknown key '%s'.",
1859 line, path, filename, rc->name, key);
1863 if(hash == hash_on && !strcasecmp(key, HEALTH_ON_KEY)) {
1865 if(strcmp(rt->context, value))
1866 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').",
1867 line, path, filename, rt->name, key, rt->context, value, value);
1871 rt->context = tabs2spaces(strdupz(value));
1872 rt->hash_context = simple_hash(rt->context);
1874 else if(hash == hash_lookup && !strcasecmp(key, HEALTH_LOOKUP_KEY)) {
1875 health_parse_db_lookup(line, path, filename, value, &rt->group, &rt->after, &rt->before,
1877 &rt->options, &rt->dimensions);
1879 else if(hash == hash_every && !strcasecmp(key, HEALTH_EVERY_KEY)) {
1880 if(!health_parse_duration(value, &rt->update_every))
1881 info("Health configuration at line %zu of file '%s/%s' for template '%s' at key '%s' cannot parse duration: '%s'.",
1882 line, path, filename, rt->name, key, value);
1884 else if(hash == hash_green && !strcasecmp(key, HEALTH_GREEN_KEY)) {
1886 rt->green = strtold(value, &e);
1888 info("Health configuration at line %zu of file '%s/%s' for template '%s' at key '%s' leaves this string unmatched: '%s'.",
1889 line, path, filename, rt->name, key, e);
1892 else if(hash == hash_red && !strcasecmp(key, HEALTH_RED_KEY)) {
1894 rt->red = strtold(value, &e);
1896 info("Health configuration at line %zu of file '%s/%s' for template '%s' at key '%s' leaves this string unmatched: '%s'.",
1897 line, path, filename, rt->name, key, e);
1900 else if(hash == hash_calc && !strcasecmp(key, HEALTH_CALC_KEY)) {
1901 const char *failed_at = NULL;
1903 rt->calculation = expression_parse(value, &failed_at, &error);
1904 if(!rt->calculation) {
1905 error("Health configuration at line %zu of file '%s/%s' for template '%s' at key '%s' has unparse-able expression '%s': %s at '%s'",
1906 line, path, filename, rt->name, key, value, expression_strerror(error), failed_at);
1909 else if(hash == hash_warn && !strcasecmp(key, HEALTH_WARN_KEY)) {
1910 const char *failed_at = NULL;
1912 rt->warning = expression_parse(value, &failed_at, &error);
1914 error("Health configuration at line %zu of file '%s/%s' for template '%s' at key '%s' has unparse-able expression '%s': %s at '%s'",
1915 line, path, filename, rt->name, key, value, expression_strerror(error), failed_at);
1918 else if(hash == hash_crit && !strcasecmp(key, HEALTH_CRIT_KEY)) {
1919 const char *failed_at = NULL;
1921 rt->critical = expression_parse(value, &failed_at, &error);
1923 error("Health configuration at line %zu of file '%s/%s' for template '%s' at key '%s' has unparse-able expression '%s': %s at '%s'",
1924 line, path, filename, rt->name, key, value, expression_strerror(error), failed_at);
1927 else if(hash == hash_exec && !strcasecmp(key, HEALTH_EXEC_KEY)) {
1929 if(strcmp(rt->exec, value))
1930 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').",
1931 line, path, filename, rt->name, key, rt->exec, value, value);
1935 rt->exec = tabs2spaces(strdupz(value));
1937 else if(hash == hash_recipient && !strcasecmp(key, HEALTH_RECIPIENT_KEY)) {
1939 if(strcmp(rt->recipient, value))
1940 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').",
1941 line, path, filename, rt->name, key, rt->recipient, value, value);
1943 freez(rt->recipient);
1945 rt->recipient = tabs2spaces(strdupz(value));
1947 else if(hash == hash_units && !strcasecmp(key, HEALTH_UNITS_KEY)) {
1949 if(strcmp(rt->units, value))
1950 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').",
1951 line, path, filename, rt->name, key, rt->units, value, value);
1955 rt->units = tabs2spaces(strdupz(value));
1956 strip_quotes(rt->units);
1958 else if(hash == hash_info && !strcasecmp(key, HEALTH_INFO_KEY)) {
1960 if(strcmp(rt->info, value))
1961 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').",
1962 line, path, filename, rt->name, key, rt->info, value, value);
1966 rt->info = tabs2spaces(strdupz(value));
1967 strip_quotes(rt->info);
1969 else if(hash == hash_delay && !strcasecmp(key, HEALTH_DELAY_KEY)) {
1970 health_parse_delay(line, path, filename, value, &rt->delay_up_duration, &rt->delay_down_duration, &rt->delay_max_duration, &rt->delay_multiplier);
1973 error("Health configuration at line %zu of file '%s/%s' for template '%s' has unknown key '%s'.",
1974 line, path, filename, rt->name, key);
1978 error("Health configuration at line %zu of file '%s/%s' has unknown key '%s'. Expected either '" HEALTH_ALARM_KEY "' or '" HEALTH_TEMPLATE_KEY "'.",
1979 line, path, filename, key);
1983 if(rc && !rrdcalc_add_alarm_from_config(&localhost, rc))
1984 rrdcalc_free(&localhost, rc);
1986 if(rt && !rrdcalctemplate_add_template_from_config(&localhost, rt))
1987 rrdcalctemplate_free(&localhost, rt);
1993 void health_readdir(const char *path) {
1994 size_t pathlen = strlen(path);
1996 debug(D_HEALTH, "Health configuration reading directory '%s'", path);
1998 DIR *dir = opendir(path);
2000 error("Health configuration cannot open directory '%s'.", path);
2004 struct dirent *de = NULL;
2005 while ((de = readdir(dir))) {
2006 size_t len = strlen(de->d_name);
2008 if(de->d_type == DT_DIR
2010 (de->d_name[0] == '.' && de->d_name[1] == '\0')
2011 || (de->d_name[0] == '.' && de->d_name[1] == '.' && de->d_name[2] == '\0')
2013 debug(D_HEALTH, "Ignoring directory '%s'", de->d_name);
2017 else if(de->d_type == DT_DIR) {
2018 char *s = mallocz(pathlen + strlen(de->d_name) + 2);
2021 strcat(s, de->d_name);
2027 else if((de->d_type == DT_LNK || de->d_type == DT_REG || de->d_type == DT_UNKNOWN) &&
2028 len > 5 && !strcmp(&de->d_name[len - 5], ".conf")) {
2029 health_readfile(path, de->d_name);
2032 else debug(D_HEALTH, "Ignoring file '%s'", de->d_name);
2038 static inline char *health_config_dir(void) {
2039 char buffer[FILENAME_MAX + 1];
2040 snprintfz(buffer, FILENAME_MAX, "%s/health.d", config_get("global", "config directory", CONFIG_DIR));
2041 return config_get("health", "health configuration directory", buffer);
2044 void health_init(void) {
2045 debug(D_HEALTH, "Health configuration initializing");
2047 if(!(health_enabled = config_get_boolean("health", "enabled", 1))) {
2048 debug(D_HEALTH, "Health is disabled.");
2052 char *pathname = config_get("health", "health db directory", VARLIB_DIR "/health");
2053 if(mkdir(pathname, 0770) == -1 && errno != EEXIST)
2054 fatal("Cannot create directory '%s'.", pathname);
2056 char filename[FILENAME_MAX + 1];
2057 snprintfz(filename, FILENAME_MAX, "%s/health-log.db", pathname);
2058 health.log_filename = config_get("health", "health db file", filename);
2060 health_alarm_log_load(&localhost);
2061 health_alarm_log_open();
2063 char *path = health_config_dir();
2066 char buffer[FILENAME_MAX + 1];
2067 snprintfz(buffer, FILENAME_MAX, "%s/alarm-notify.sh", config_get("global", "plugins directory", PLUGINS_DIR));
2068 health.health_default_exec = config_get("health", "script to execute on alarm", buffer);
2071 long n = config_get_number("health", "in memory max health log entries", (long)localhost.health_log.max);
2073 error("Health configuration has invalid max log entries %ld. Using default %u", n, localhost.health_log.max);
2074 config_set_number("health", "in memory max health log entries", (long)localhost.health_log.max);
2076 else localhost.health_log.max = (unsigned int)n;
2078 rrdhost_rwlock(&localhost);
2079 health_readdir(path);
2080 rrdhost_unlock(&localhost);
2083 // ----------------------------------------------------------------------------
2086 static inline void health_string2json(BUFFER *wb, const char *prefix, const char *label, const char *value, const char *suffix) {
2088 buffer_sprintf(wb, "%s\"%s\":\"%s\"%s", prefix, label, value, suffix);
2090 buffer_sprintf(wb, "%s\"%s\":null%s", prefix, label, suffix);
2093 static inline void health_alarm_entry2json_nolock(BUFFER *wb, ALARM_ENTRY *ae, RRDHOST *host) {
2094 buffer_sprintf(wb, "\n\t{\n"
2095 "\t\t\"hostname\": \"%s\",\n"
2096 "\t\t\"unique_id\": %u,\n"
2097 "\t\t\"alarm_id\": %u,\n"
2098 "\t\t\"alarm_event_id\": %u,\n"
2099 "\t\t\"name\": \"%s\",\n"
2100 "\t\t\"chart\": \"%s\",\n"
2101 "\t\t\"family\": \"%s\",\n"
2102 "\t\t\"processed\": %s,\n"
2103 "\t\t\"updated\": %s,\n"
2104 "\t\t\"exec_run\": %lu,\n"
2105 "\t\t\"exec_failed\": %s,\n"
2106 "\t\t\"exec\": \"%s\",\n"
2107 "\t\t\"recipient\": \"%s\",\n"
2108 "\t\t\"exec_code\": %d,\n"
2109 "\t\t\"source\": \"%s\",\n"
2110 "\t\t\"units\": \"%s\",\n"
2111 "\t\t\"info\": \"%s\",\n"
2112 "\t\t\"when\": %lu,\n"
2113 "\t\t\"duration\": %lu,\n"
2114 "\t\t\"non_clear_duration\": %lu,\n"
2115 "\t\t\"status\": \"%s\",\n"
2116 "\t\t\"old_status\": \"%s\",\n"
2117 "\t\t\"delay\": %d,\n"
2118 "\t\t\"delay_up_to_timestamp\": %lu,\n"
2119 "\t\t\"updated_by_id\": %u,\n"
2120 "\t\t\"updates_id\": %u,\n",
2128 (ae->flags & HEALTH_ENTRY_FLAG_PROCESSED)?"true":"false",
2129 (ae->flags & HEALTH_ENTRY_FLAG_UPDATED)?"true":"false",
2130 (unsigned long)ae->exec_run_timestamp,
2131 (ae->flags & HEALTH_ENTRY_FLAG_EXEC_FAILED)?"true":"false",
2132 ae->exec?ae->exec:health.health_default_exec,
2133 ae->recipient?ae->recipient:health.health_default_recipient,
2136 ae->units?ae->units:"",
2137 ae->info?ae->info:"",
2138 (unsigned long)ae->when,
2139 (unsigned long)ae->duration,
2140 (unsigned long)ae->non_clear_duration,
2141 rrdcalc_status2string(ae->new_status),
2142 rrdcalc_status2string(ae->old_status),
2144 (unsigned long)ae->delay_up_to_timestamp,
2149 buffer_strcat(wb, "\t\t\"value\":");
2150 buffer_rrd_value(wb, ae->new_value);
2151 buffer_strcat(wb, ",\n");
2153 buffer_strcat(wb, "\t\t\"old_value\":");
2154 buffer_rrd_value(wb, ae->old_value);
2155 buffer_strcat(wb, "\n");
2157 buffer_strcat(wb, "\t}");
2160 void health_alarm_log2json(RRDHOST *host, BUFFER *wb, uint32_t after) {
2161 pthread_rwlock_rdlock(&host->health_log.alarm_log_rwlock);
2163 buffer_strcat(wb, "[");
2165 unsigned int max = host->health_log.max;
2166 unsigned int count = 0;
2168 for(ae = host->health_log.alarms; ae && count < max ; count++, ae = ae->next) {
2169 if(ae->unique_id > after) {
2170 if(likely(count)) buffer_strcat(wb, ",");
2171 health_alarm_entry2json_nolock(wb, ae, host);
2175 buffer_strcat(wb, "\n]\n");
2177 pthread_rwlock_unlock(&host->health_log.alarm_log_rwlock);
2180 static inline void health_rrdcalc2json_nolock(BUFFER *wb, RRDCALC *rc) {
2182 "\t\t\"%s.%s\": {\n"
2183 "\t\t\t\"id\": %lu,\n"
2184 "\t\t\t\"name\": \"%s\",\n"
2185 "\t\t\t\"chart\": \"%s\",\n"
2186 "\t\t\t\"family\": \"%s\",\n"
2187 "\t\t\t\"active\": %s,\n"
2188 "\t\t\t\"exec\": \"%s\",\n"
2189 "\t\t\t\"recipient\": \"%s\",\n"
2190 "\t\t\t\"source\": \"%s\",\n"
2191 "\t\t\t\"units\": \"%s\",\n"
2192 "\t\t\t\"info\": \"%s\",\n"
2193 "\t\t\t\"status\": \"%s\",\n"
2194 "\t\t\t\"last_status_change\": %lu,\n"
2195 "\t\t\t\"last_updated\": %lu,\n"
2196 "\t\t\t\"next_update\": %lu,\n"
2197 "\t\t\t\"update_every\": %d,\n"
2198 "\t\t\t\"delay_up_duration\": %d,\n"
2199 "\t\t\t\"delay_down_duration\": %d,\n"
2200 "\t\t\t\"delay_max_duration\": %d,\n"
2201 "\t\t\t\"delay_multiplier\": %f,\n"
2202 "\t\t\t\"delay\": %d,\n"
2203 "\t\t\t\"delay_up_to_timestamp\": %lu,\n"
2204 , rc->chart, rc->name
2205 , (unsigned long)rc->id
2208 , (rc->rrdset && rc->rrdset->family)?rc->rrdset->family:""
2209 , (rc->rrdset)?"true":"false"
2210 , rc->exec?rc->exec:health.health_default_exec
2211 , rc->recipient?rc->recipient:health.health_default_recipient
2213 , rc->units?rc->units:""
2214 , rc->info?rc->info:""
2215 , rrdcalc_status2string(rc->status)
2216 , (unsigned long)rc->last_status_change
2217 , (unsigned long)rc->last_updated
2218 , (unsigned long)rc->next_update
2220 , rc->delay_up_duration
2221 , rc->delay_down_duration
2222 , rc->delay_max_duration
2223 , rc->delay_multiplier
2225 , (unsigned long)rc->delay_up_to_timestamp
2228 if(RRDCALC_HAS_DB_LOOKUP(rc)) {
2229 if(rc->dimensions && *rc->dimensions)
2230 health_string2json(wb, "\t\t\t", "lookup_dimensions", rc->dimensions, ",\n");
2233 "\t\t\t\"db_after\": %lu,\n"
2234 "\t\t\t\"db_before\": %lu,\n"
2235 "\t\t\t\"lookup_method\": \"%s\",\n"
2236 "\t\t\t\"lookup_after\": %d,\n"
2237 "\t\t\t\"lookup_before\": %d,\n"
2238 "\t\t\t\"lookup_options\": \"",
2239 (unsigned long) rc->db_after,
2240 (unsigned long) rc->db_before,
2241 group_method2string(rc->group),
2245 buffer_data_options2string(wb, rc->options);
2246 buffer_strcat(wb, "\",\n");
2249 if(rc->calculation) {
2250 health_string2json(wb, "\t\t\t", "calc", rc->calculation->source, ",\n");
2251 health_string2json(wb, "\t\t\t", "calc_parsed", rc->calculation->parsed_as, ",\n");
2255 health_string2json(wb, "\t\t\t", "warn", rc->warning->source, ",\n");
2256 health_string2json(wb, "\t\t\t", "warn_parsed", rc->warning->parsed_as, ",\n");
2260 health_string2json(wb, "\t\t\t", "crit", rc->critical->source, ",\n");
2261 health_string2json(wb, "\t\t\t", "crit_parsed", rc->critical->parsed_as, ",\n");
2264 buffer_strcat(wb, "\t\t\t\"green\":");
2265 buffer_rrd_value(wb, rc->green);
2266 buffer_strcat(wb, ",\n");
2268 buffer_strcat(wb, "\t\t\t\"red\":");
2269 buffer_rrd_value(wb, rc->red);
2270 buffer_strcat(wb, ",\n");
2272 buffer_strcat(wb, "\t\t\t\"value\":");
2273 buffer_rrd_value(wb, rc->value);
2274 buffer_strcat(wb, "\n");
2276 buffer_strcat(wb, "\t\t}");
2279 //void health_rrdcalctemplate2json_nolock(BUFFER *wb, RRDCALCTEMPLATE *rt) {
2283 void health_alarms2json(RRDHOST *host, BUFFER *wb, int all) {
2286 rrdhost_rdlock(&localhost);
2287 buffer_sprintf(wb, "{\n\t\"hostname\": \"%s\","
2288 "\n\t\"latest_alarm_log_unique_id\": %u,"
2289 "\n\t\"status\": %s,"
2291 "\n\t\"alarms\": {\n",
2293 (host->health_log.next_log_id > 0)?(host->health_log.next_log_id - 1):0,
2294 health_enabled?"true":"false",
2295 (unsigned long)time(NULL));
2298 for(i = 0, rc = host->alarms; rc ; rc = rc->next) {
2299 if(unlikely(!rc->rrdset || !rc->rrdset->last_collected_time.tv_sec))
2302 if(likely(!all && !(rc->status == RRDCALC_STATUS_WARNING || rc->status == RRDCALC_STATUS_CRITICAL)))
2305 if(likely(i)) buffer_strcat(wb, ",\n");
2306 health_rrdcalc2json_nolock(wb, rc);
2310 // buffer_strcat(wb, "\n\t},\n\t\"templates\": {");
2311 // RRDCALCTEMPLATE *rt;
2312 // for(rt = host->templates; rt ; rt = rt->next)
2313 // health_rrdcalctemplate2json_nolock(wb, rt);
2315 buffer_strcat(wb, "\n\t}\n}\n");
2316 rrdhost_unlock(&localhost);
2320 // ----------------------------------------------------------------------------
2321 // re-load health configuration
2323 static inline void health_free_all_nolock(RRDHOST *host) {
2324 while(host->templates)
2325 rrdcalctemplate_free(host, host->templates);
2328 rrdcalc_free(host, host->alarms);
2331 void health_reload(void) {
2332 if(!health_enabled) {
2333 error("Health reload is requested, but health is not enabled.");
2337 char *path = health_config_dir();
2339 // free all running alarms
2340 rrdhost_rwlock(&localhost);
2341 health_free_all_nolock(&localhost);
2342 rrdhost_unlock(&localhost);
2344 // invalidate all previous entries in the alarm log
2346 for(t = localhost.health_log.alarms ; t ; t = t->next) {
2347 if(t->new_status != RRDCALC_STATUS_REMOVED)
2348 t->flags |= HEALTH_ENTRY_FLAG_UPDATED;
2351 // reset all thresholds to all charts
2353 for(st = localhost.rrdset_root; st ; st = st->next) {
2358 // load the new alarms
2359 rrdhost_rwlock(&localhost);
2360 health_readdir(path);
2361 rrdhost_unlock(&localhost);
2363 // link the loaded alarms to their charts
2364 for(st = localhost.rrdset_root; st ; st = st->next) {
2365 rrdhost_rwlock(&localhost);
2367 rrdsetcalc_link_matching(st);
2368 rrdcalctemplate_link_matching(st);
2370 rrdhost_unlock(&localhost);
2374 // ----------------------------------------------------------------------------
2375 // health main thread and friends
2377 static inline int rrdcalc_value2status(calculated_number n) {
2378 if(isnan(n)) return RRDCALC_STATUS_UNDEFINED;
2379 if(n) return RRDCALC_STATUS_RAISED;
2380 return RRDCALC_STATUS_CLEAR;
2383 static inline void health_alarm_execute(RRDHOST *host, ALARM_ENTRY *ae) {
2384 ae->flags |= HEALTH_ENTRY_FLAG_PROCESSED;
2386 // find the previous notification for the same alarm
2388 for(t = ae->next; t ;t = t->next) {
2389 if(t->alarm_id == ae->alarm_id && t->flags & HEALTH_ENTRY_FLAG_EXEC_RUN)
2393 if(t && t->new_status == ae->new_status) {
2394 // don't send the same notification again
2395 info("Health not sending again notification for alarm '%s.%s' status %s", ae->chart, ae->name, rrdcalc_status2string(ae->new_status));
2399 if((ae->old_status == RRDCALC_STATUS_UNDEFINED && ae->new_status == RRDCALC_STATUS_UNINITIALIZED)
2400 || (ae->old_status == RRDCALC_STATUS_UNINITIALIZED && ae->new_status == RRDCALC_STATUS_CLEAR)) {
2401 info("Health not sending notification for first initialization of alarm '%s.%s' status %s", ae->chart, ae->name, rrdcalc_status2string(ae->new_status));
2405 char buffer[FILENAME_MAX + 1];
2408 const char *exec = ae->exec;
2409 if(!exec) exec = health.health_default_exec;
2411 const char *recipient = ae->recipient;
2412 if(!recipient) recipient = health.health_default_recipient;
2414 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'",
2421 (unsigned long)ae->when,
2423 ae->chart?ae->chart:"NOCAHRT",
2424 ae->family?ae->family:"NOFAMILY",
2425 rrdcalc_status2string(ae->new_status),
2426 rrdcalc_status2string(ae->old_status),
2429 ae->source?ae->source:"UNKNOWN",
2430 (uint32_t)ae->duration,
2431 (uint32_t)ae->non_clear_duration,
2432 ae->units?ae->units:"",
2433 ae->info?ae->info:""
2436 ae->flags |= HEALTH_ENTRY_FLAG_EXEC_RUN;
2437 ae->exec_run_timestamp = time(NULL);
2439 debug(D_HEALTH, "executing command '%s'", buffer);
2440 FILE *fp = mypopen(buffer, &command_pid);
2442 error("HEALTH: Cannot popen(\"%s\", \"r\").", buffer);
2445 debug(D_HEALTH, "HEALTH reading from command");
2446 char *s = fgets(buffer, FILENAME_MAX, fp);
2448 ae->exec_code = mypclose(fp, command_pid);
2449 debug(D_HEALTH, "done executing command - returned with code %d", ae->exec_code);
2451 if(ae->exec_code != 0)
2452 ae->flags |= HEALTH_ENTRY_FLAG_EXEC_FAILED;
2455 health_alarm_log_save(host, ae);
2459 static inline void health_process_notifications(RRDHOST *host, ALARM_ENTRY *ae) {
2460 info("Health alarm '%s.%s' = %0.2Lf - changed status from %s to %s",
2461 ae->chart?ae->chart:"NOCHART", ae->name,
2463 rrdcalc_status2string(ae->old_status),
2464 rrdcalc_status2string(ae->new_status)
2467 health_alarm_execute(host, ae);
2470 static inline void health_alarm_log_process(RRDHOST *host) {
2471 static uint32_t stop_at_id = 0;
2472 uint32_t first_waiting = (host->health_log.alarms)?host->health_log.alarms->unique_id:0;
2473 time_t now = time(NULL);
2475 pthread_rwlock_rdlock(&host->health_log.alarm_log_rwlock);
2478 for(ae = host->health_log.alarms; ae && ae->unique_id >= stop_at_id ; ae = ae->next) {
2480 !(ae->flags & HEALTH_ENTRY_FLAG_PROCESSED) &&
2481 !(ae->flags & HEALTH_ENTRY_FLAG_UPDATED)
2484 if(unlikely(ae->unique_id < first_waiting))
2485 first_waiting = ae->unique_id;
2487 if(likely(now >= ae->delay_up_to_timestamp))
2488 health_process_notifications(host, ae);
2492 // remember this for the next iteration
2493 stop_at_id = first_waiting;
2495 pthread_rwlock_unlock(&host->health_log.alarm_log_rwlock);
2497 if(host->health_log.count <= host->health_log.max)
2500 // cleanup excess entries in the log
2501 pthread_rwlock_wrlock(&host->health_log.alarm_log_rwlock);
2503 ALARM_ENTRY *last = NULL;
2504 unsigned int count = host->health_log.max * 2 / 3;
2505 for(ae = host->health_log.alarms; ae && count ; count--, last = ae, ae = ae->next) ;
2507 if(ae && last && last->next == ae)
2513 debug(D_HEALTH, "Health removing alarm log entry with id: %u", ae->unique_id);
2515 ALARM_ENTRY *t = ae->next;
2521 freez(ae->recipient);
2528 host->health_log.count--;
2531 pthread_rwlock_unlock(&host->health_log.alarm_log_rwlock);
2534 static inline int rrdcalc_isrunnable(RRDCALC *rc, time_t now, time_t *next_run) {
2535 if (unlikely(!rc->rrdset)) {
2536 debug(D_HEALTH, "Health not running alarm '%s.%s'. It is not linked to a chart.", rc->chart?rc->chart:"NOCHART", rc->name);
2540 if (unlikely(!rc->rrdset->last_collected_time.tv_sec)) {
2541 debug(D_HEALTH, "Health not running alarm '%s.%s'. Chart is not yet collected.", rc->chart?rc->chart:"NOCHART", rc->name);
2545 if (unlikely(!rc->update_every)) {
2546 debug(D_HEALTH, "Health not running alarm '%s.%s'. It does not have an update frequency", rc->chart?rc->chart:"NOCHART", rc->name);
2550 if (unlikely(rc->next_update > now)) {
2551 if (unlikely(*next_run > rc->next_update))
2552 *next_run = rc->next_update;
2554 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));
2559 // we should check that the DB lookup is possible
2561 // - the duration of the chart includes the required timeframe
2562 // we SHOULD NOT check the dimensions - there might be alarms that refer non-existing dimensions (e.g. cpu steal)
2567 void *health_main(void *ptr) {
2570 info("HEALTH thread created with task id %d", gettid());
2572 if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
2573 error("Cannot set pthread cancel type to DEFERRED.");
2575 if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
2576 error("Cannot set pthread cancel state to ENABLE.");
2578 int min_run_every = (int)config_get_number("health", "run at least every seconds", 10);
2579 if(min_run_every < 1) min_run_every = 1;
2581 BUFFER *wb = buffer_create(100);
2583 unsigned int loop = 0;
2584 while(health_enabled && !netdata_exit) {
2586 debug(D_HEALTH, "Health monitoring iteration no %u started", loop);
2588 int oldstate, runnable = 0;
2589 time_t now = time(NULL);
2590 time_t next_run = now + min_run_every;
2593 if (unlikely(pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldstate) != 0))
2594 error("Cannot set pthread cancel state to DISABLE.");
2596 rrdhost_rdlock(&localhost);
2598 // the first loop is to lookup values from the db
2599 for (rc = localhost.alarms; rc; rc = rc->next) {
2600 if (unlikely(!rrdcalc_isrunnable(rc, now, &next_run)))
2604 rc->old_value = rc->value;
2606 // 1. if there is database lookup, do it
2607 // 2. if there is calculation expression, run it
2609 if (unlikely(RRDCALC_HAS_DB_LOOKUP(rc))) {
2610 time_t old_db_timestamp = rc->db_before;
2611 int value_is_null = 0;
2613 int ret = rrd2value(rc->rrdset, wb, &rc->value,
2614 rc->dimensions, 1, rc->after, rc->before, rc->group,
2615 rc->options, &rc->db_after, &rc->db_before, &value_is_null);
2617 if (unlikely(ret != 200)) {
2618 // database lookup failed
2621 debug(D_HEALTH, "Health alarm '%s.%s': database lookup returned error %d", rc->chart?rc->chart:"NOCHART", rc->name, ret);
2623 if (unlikely(!(rc->rrdcalc_flags & RRDCALC_FLAG_DB_ERROR))) {
2624 rc->rrdcalc_flags |= RRDCALC_FLAG_DB_ERROR;
2625 error("Health alarm '%s.%s': database lookup returned error %d", rc->chart?rc->chart:"NOCHART", rc->name, ret);
2628 else if (unlikely(rc->rrdcalc_flags & RRDCALC_FLAG_DB_ERROR))
2629 rc->rrdcalc_flags &= ~RRDCALC_FLAG_DB_ERROR;
2631 if (unlikely(old_db_timestamp == rc->db_before)) {
2632 // database is stale
2634 debug(D_HEALTH, "Health alarm '%s.%s': database is stale", rc->chart?rc->chart:"NOCHART", rc->name);
2636 if (unlikely(!(rc->rrdcalc_flags & RRDCALC_FLAG_DB_STALE))) {
2637 rc->rrdcalc_flags |= RRDCALC_FLAG_DB_STALE;
2638 error("Health alarm '%s.%s': database is stale", rc->chart?rc->chart:"NOCHART", rc->name);
2641 else if (unlikely(rc->rrdcalc_flags & RRDCALC_FLAG_DB_STALE))
2642 rc->rrdcalc_flags &= ~RRDCALC_FLAG_DB_STALE;
2644 if (unlikely(value_is_null)) {
2645 // collected value is null
2649 debug(D_HEALTH, "Health alarm '%s.%s': database lookup returned empty value (possibly value is not collected yet)",
2650 rc->chart?rc->chart:"NOCHART", rc->name);
2652 if (unlikely(!(rc->rrdcalc_flags & RRDCALC_FLAG_DB_NAN))) {
2653 rc->rrdcalc_flags |= RRDCALC_FLAG_DB_NAN;
2654 error("Health alarm '%s.%s': database lookup returned empty value (possibly value is not collected yet)",
2655 rc->chart?rc->chart:"NOCHART", rc->name);
2658 else if (unlikely(rc->rrdcalc_flags & RRDCALC_FLAG_DB_NAN))
2659 rc->rrdcalc_flags &= ~RRDCALC_FLAG_DB_NAN;
2661 debug(D_HEALTH, "Health alarm '%s.%s': database lookup gave value "
2662 CALCULATED_NUMBER_FORMAT, rc->chart?rc->chart:"NOCHART", rc->name, rc->value);
2665 if(unlikely(rc->calculation)) {
2666 if (unlikely(!expression_evaluate(rc->calculation))) {
2667 // calculation failed
2671 debug(D_HEALTH, "Health alarm '%s.%s': failed to evaluate calculation with error: %s",
2672 rc->chart?rc->chart:"NOCHART", rc->name, buffer_tostring(rc->calculation->error_msg));
2674 if (unlikely(!(rc->rrdcalc_flags & RRDCALC_FLAG_CALC_ERROR))) {
2675 rc->rrdcalc_flags |= RRDCALC_FLAG_CALC_ERROR;
2676 error("Health alarm '%s.%s': failed to evaluate calculation with error: %s",
2677 rc->chart?rc->chart:"NOCHART", rc->name, buffer_tostring(rc->calculation->error_msg));
2681 if (unlikely(rc->rrdcalc_flags & RRDCALC_FLAG_CALC_ERROR))
2682 rc->rrdcalc_flags &= ~RRDCALC_FLAG_CALC_ERROR;
2684 debug(D_HEALTH, "Health alarm '%s.%s': calculation expression gave value "
2685 CALCULATED_NUMBER_FORMAT
2686 ": %s (source: %s)",
2687 rc->chart?rc->chart:"NOCHART", rc->name,
2688 rc->calculation->result,
2689 buffer_tostring(rc->calculation->error_msg),
2693 rc->value = rc->calculation->result;
2697 rrdhost_unlock(&localhost);
2699 if (unlikely(runnable && !netdata_exit)) {
2700 rrdhost_rdlock(&localhost);
2702 for (rc = localhost.alarms; rc; rc = rc->next) {
2703 if (unlikely(!rrdcalc_isrunnable(rc, now, &next_run)))
2706 int warning_status = RRDCALC_STATUS_UNDEFINED;
2707 int critical_status = RRDCALC_STATUS_UNDEFINED;
2709 if(likely(rc->warning)) {
2710 if(unlikely(!expression_evaluate(rc->warning))) {
2711 // calculation failed
2713 debug(D_HEALTH, "Health alarm '%s.%s': warning expression failed with error: %s",
2714 rc->chart?rc->chart:"NOCHART", rc->name, buffer_tostring(rc->warning->error_msg));
2716 if (unlikely(!(rc->rrdcalc_flags & RRDCALC_FLAG_WARN_ERROR))) {
2717 rc->rrdcalc_flags |= RRDCALC_FLAG_WARN_ERROR;
2718 error("Health alarm '%s.%s': warning expression failed with error: %s",
2719 rc->chart?rc->chart:"NOCHART", rc->name, buffer_tostring(rc->warning->error_msg));
2723 if(unlikely(rc->rrdcalc_flags & RRDCALC_FLAG_WARN_ERROR))
2724 rc->rrdcalc_flags &= ~RRDCALC_FLAG_WARN_ERROR;
2726 debug(D_HEALTH, "Health alarm '%s.%s': warning expression gave value "
2727 CALCULATED_NUMBER_FORMAT
2728 ": %s (source: %s)",
2729 rc->chart?rc->chart:"NOCHART", rc->name,
2730 rc->warning->result,
2731 buffer_tostring(rc->warning->error_msg),
2735 warning_status = rrdcalc_value2status(rc->warning->result);
2739 if(likely(rc->critical)) {
2740 if(unlikely(!expression_evaluate(rc->critical))) {
2741 // calculation failed
2743 debug(D_HEALTH, "Health alarm '%s.%s': critical expression failed with error: %s",
2744 rc->chart?rc->chart:"NOCHART", rc->name, buffer_tostring(rc->critical->error_msg));
2746 if (unlikely(!(rc->rrdcalc_flags & RRDCALC_FLAG_CRIT_ERROR))) {
2747 rc->rrdcalc_flags |= RRDCALC_FLAG_CRIT_ERROR;
2748 error("Health alarm '%s.%s': critical expression failed with error: %s",
2749 rc->chart?rc->chart:"NOCHART", rc->name, buffer_tostring(rc->critical->error_msg));
2753 if(unlikely(rc->rrdcalc_flags & RRDCALC_FLAG_CRIT_ERROR))
2754 rc->rrdcalc_flags &= ~RRDCALC_FLAG_CRIT_ERROR;
2756 debug(D_HEALTH, "Health alarm '%s.%s': critical expression gave value "
2757 CALCULATED_NUMBER_FORMAT
2758 ": %s (source: %s)",
2759 rc->chart?rc->chart:"NOCHART", rc->name,
2760 rc->critical->result,
2761 buffer_tostring(rc->critical->error_msg),
2765 critical_status = rrdcalc_value2status(rc->critical->result);
2769 int status = RRDCALC_STATUS_UNDEFINED;
2771 switch(warning_status) {
2772 case RRDCALC_STATUS_CLEAR:
2773 status = RRDCALC_STATUS_CLEAR;
2776 case RRDCALC_STATUS_RAISED:
2777 status = RRDCALC_STATUS_WARNING;
2784 switch(critical_status) {
2785 case RRDCALC_STATUS_CLEAR:
2786 if(status == RRDCALC_STATUS_UNDEFINED)
2787 status = RRDCALC_STATUS_CLEAR;
2790 case RRDCALC_STATUS_RAISED:
2791 status = RRDCALC_STATUS_CRITICAL;
2798 if(status != rc->status) {
2801 if(now > rc->delay_up_to_timestamp) {
2802 rc->delay_up_current = rc->delay_up_duration;
2803 rc->delay_down_current = rc->delay_down_duration;
2805 rc->delay_up_to_timestamp = 0;
2808 rc->delay_up_current = (int)(rc->delay_up_current * rc->delay_multiplier);
2809 if(rc->delay_up_current > rc->delay_max_duration) rc->delay_up_current = rc->delay_max_duration;
2811 rc->delay_down_current = (int)(rc->delay_down_current * rc->delay_multiplier);
2812 if(rc->delay_down_current > rc->delay_max_duration) rc->delay_down_current = rc->delay_max_duration;
2815 if(status > rc->status)
2816 delay = rc->delay_up_current;
2818 delay = rc->delay_down_current;
2820 // COMMENTED: because we do need to send raising alarms
2821 // if(now + delay < rc->delay_up_to_timestamp)
2822 // delay = (int)(rc->delay_up_to_timestamp - now);
2824 rc->delay_last = delay;
2825 rc->delay_up_to_timestamp = now + delay;
2826 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);
2827 rc->last_status_change = now;
2828 rc->status = status;
2831 rc->last_updated = now;
2832 rc->next_update = now + rc->update_every;
2834 if (next_run > rc->next_update)
2835 next_run = rc->next_update;
2838 rrdhost_unlock(&localhost);
2841 if (unlikely(pthread_setcancelstate(oldstate, NULL) != 0))
2842 error("Cannot set pthread cancel state to RESTORE (%d).", oldstate);
2844 if(unlikely(netdata_exit))
2847 // execute notifications
2849 health_alarm_log_process(&localhost);
2851 if(unlikely(netdata_exit))
2855 if(now < next_run) {
2856 debug(D_HEALTH, "Health monitoring iteration no %u done. Next iteration in %d secs",
2857 loop, (int) (next_run - now));
2858 sleep_usec(1000000 * (unsigned long long) (next_run - now));
2861 debug(D_HEALTH, "Health monitoring iteration no %u done. Next iteration now", loop);
2867 info("HEALTH thread exiting");