3 #define RRDVAR_MAX_LENGTH 1024
5 static const char *health_default_exec = PLUGINS_DIR "/alarm-notify.sh";
6 static const char *health_default_recipient = "root";
7 int health_enabled = 1;
9 // ----------------------------------------------------------------------------
10 // Health Alarms Log Management
12 static inline void health_alarm_log(RRDHOST *host,
13 uint32_t alarm_id, uint32_t alarm_event_id,
15 const char *name, const char *chart, const char *family,
16 const char *exec, const char *recipient, time_t duration,
17 calculated_number old_value, calculated_number new_value,
18 int old_status, int new_status,
24 debug(D_HEALTH, "Health adding alarm log entry with id: %u", host->health_log.next_log_id);
26 ALARM_ENTRY *ae = callocz(1, sizeof(ALARM_ENTRY));
27 ae->name = strdupz(name);
28 ae->hash_name = simple_hash(ae->name);
31 ae->chart = strdupz(chart);
32 ae->hash_chart = simple_hash(ae->chart);
36 ae->family = strdupz(family);
38 if(exec) ae->exec = strdupz(exec);
39 if(recipient) ae->recipient = strdupz(recipient);
40 if(source) ae->source = strdupz(source);
41 if(units) ae->units = strdupz(units);
42 if(info) ae->info = strdupz(info);
44 ae->unique_id = host->health_log.next_log_id++;
45 ae->alarm_id = alarm_id;
46 ae->alarm_event_id = alarm_event_id;
48 ae->old_value = old_value;
49 ae->new_value = new_value;
50 ae->old_status = old_status;
51 ae->new_status = new_status;
52 ae->duration = duration;
54 ae->delay_up_to_timestamp = when + delay;
56 if(ae->old_status == RRDCALC_STATUS_WARNING || ae->old_status == RRDCALC_STATUS_CRITICAL)
57 ae->non_clear_duration += ae->duration;
60 pthread_rwlock_wrlock(&host->health_log.alarm_log_rwlock);
61 ae->next = host->health_log.alarms;
62 host->health_log.alarms = ae;
63 host->health_log.count++;
64 pthread_rwlock_unlock(&host->health_log.alarm_log_rwlock);
66 // match previous alarms
67 pthread_rwlock_rdlock(&host->health_log.alarm_log_rwlock);
69 for(t = host->health_log.alarms ; t ; t = t->next) {
70 if(t != ae && t->alarm_id == ae->alarm_id) {
71 if(!(t->notifications & HEALTH_ENTRY_NOTIFICATIONS_UPDATED) && !t->updated_by) {
72 t->notifications |= HEALTH_ENTRY_NOTIFICATIONS_UPDATED;
75 if((t->new_status == RRDCALC_STATUS_WARNING || t->new_status == RRDCALC_STATUS_CRITICAL) &&
76 (t->old_status == RRDCALC_STATUS_WARNING || t->old_status == RRDCALC_STATUS_CRITICAL))
77 ae->non_clear_duration += t->non_clear_duration;
80 // no need to continue
85 pthread_rwlock_unlock(&host->health_log.alarm_log_rwlock);
88 // ----------------------------------------------------------------------------
91 static inline int rrdvar_fix_name(char *variable) {
94 if (!isalnum(*variable) && *variable != '.' && *variable != '_') {
105 int rrdvar_compare(void* a, void* b) {
106 if(((RRDVAR *)a)->hash < ((RRDVAR *)b)->hash) return -1;
107 else if(((RRDVAR *)a)->hash > ((RRDVAR *)b)->hash) return 1;
108 else return strcmp(((RRDVAR *)a)->name, ((RRDVAR *)b)->name);
111 static inline RRDVAR *rrdvar_index_add(avl_tree_lock *tree, RRDVAR *rv) {
112 RRDVAR *ret = (RRDVAR *)avl_insert_lock(tree, (avl *)(rv));
114 debug(D_VARIABLES, "Request to insert RRDVAR '%s' into index failed. Already exists.", rv->name);
119 static inline RRDVAR *rrdvar_index_del(avl_tree_lock *tree, RRDVAR *rv) {
120 RRDVAR *ret = (RRDVAR *)avl_remove_lock(tree, (avl *)(rv));
122 error("Request to remove RRDVAR '%s' from index failed. Not Found.", rv->name);
127 static inline RRDVAR *rrdvar_index_find(avl_tree_lock *tree, const char *name, uint32_t hash) {
129 tmp.name = (char *)name;
130 tmp.hash = (hash)?hash:simple_hash(tmp.name);
132 return (RRDVAR *)avl_search_lock(tree, (avl *)&tmp);
135 static inline void rrdvar_free(RRDHOST *host, avl_tree_lock *tree, RRDVAR *rv) {
141 rrdvar_index_del(tree, rv);
147 static inline RRDVAR *rrdvar_create_and_index(const char *scope, avl_tree_lock *tree, const char *name, int type, calculated_number *value) {
148 char *variable = strdupz(name);
149 rrdvar_fix_name(variable);
150 uint32_t hash = simple_hash(variable);
152 RRDVAR *rv = rrdvar_index_find(tree, variable, hash);
154 debug(D_VARIABLES, "Variable '%s' not found in scope '%s'. Creating a new one.", variable, scope);
156 rv = callocz(1, sizeof(RRDVAR));
162 RRDVAR *ret = rrdvar_index_add(tree, rv);
163 if(unlikely(ret != rv)) {
164 debug(D_VARIABLES, "Variable '%s' in scope '%s' already exists", variable, scope);
165 rrdvar_free(NULL, NULL, rv);
169 debug(D_VARIABLES, "Variable '%s' created in scope '%s'", variable, scope);
180 // ----------------------------------------------------------------------------
183 calculated_number rrdvar2number(RRDVAR *rv) {
185 case RRDVAR_TYPE_CALCULATED: {
186 calculated_number *n = (calculated_number *)rv->value;
190 case RRDVAR_TYPE_TIME_T: {
191 time_t *n = (time_t *)rv->value;
195 case RRDVAR_TYPE_COLLECTED: {
196 collected_number *n = (collected_number *)rv->value;
200 case RRDVAR_TYPE_TOTAL: {
201 total_number *n = (total_number *)rv->value;
205 case RRDVAR_TYPE_INT: {
206 int *n = (int *)rv->value;
211 error("I don't know how to convert RRDVAR type %d to calculated_number", rv->type);
216 void dump_variable(void *data) {
217 RRDVAR *rv = (RRDVAR *)data;
218 debug(D_HEALTH, "%50s : %20.5Lf", rv->name, rrdvar2number(rv));
221 int health_variable_lookup(const char *variable, uint32_t hash, RRDCALC *rc, calculated_number *result) {
222 RRDSET *st = rc->rrdset;
227 rv = rrdvar_index_find(&st->variables_root_index, variable, hash);
229 *result = rrdvar2number(rv);
233 rv = rrdvar_index_find(&st->rrdfamily->variables_root_index, variable, hash);
235 *result = rrdvar2number(rv);
239 rv = rrdvar_index_find(&st->rrdhost->variables_root_index, variable, hash);
241 *result = rrdvar2number(rv);
245 debug(D_HEALTH, "Available local chart '%s' variables:", st->id);
246 avl_traverse_lock(&st->variables_root_index, dump_variable);
248 debug(D_HEALTH, "Available family '%s' variables:", st->rrdfamily->family);
249 avl_traverse_lock(&st->rrdfamily->variables_root_index, dump_variable);
251 debug(D_HEALTH, "Available host '%s' variables:", st->rrdhost->hostname);
252 avl_traverse_lock(&st->rrdhost->variables_root_index, dump_variable);
257 // ----------------------------------------------------------------------------
258 // RRDSETVAR management
260 RRDSETVAR *rrdsetvar_create(RRDSET *st, const char *variable, int type, void *value, uint32_t options) {
261 debug(D_VARIABLES, "RRDVARSET create for chart id '%s' name '%s' with variable name '%s'", st->id, st->name, variable);
262 RRDSETVAR *rs = (RRDSETVAR *)callocz(1, sizeof(RRDSETVAR));
264 char buffer[RRDVAR_MAX_LENGTH + 1];
265 snprintfz(buffer, RRDVAR_MAX_LENGTH, "%s.%s", st->id, variable);
266 rs->fullid = strdupz(buffer);
268 snprintfz(buffer, RRDVAR_MAX_LENGTH, "%s.%s", st->name, variable);
269 rs->fullname = strdupz(buffer);
271 rs->variable = strdupz(variable);
275 rs->options = options;
278 rs->local = rrdvar_create_and_index("local", &st->variables_root_index, rs->variable, rs->type, rs->value);
279 rs->family = rrdvar_create_and_index("family", &st->rrdfamily->variables_root_index, rs->fullid, rs->type, rs->value);
280 rs->host = rrdvar_create_and_index("host", &st->rrdhost->variables_root_index, rs->fullid, rs->type, rs->value);
281 rs->family_name = rrdvar_create_and_index("family", &st->rrdfamily->variables_root_index, rs->fullname, rs->type, rs->value);
282 rs->host_name = rrdvar_create_and_index("host", &st->rrdhost->variables_root_index, rs->fullname, rs->type, rs->value);
284 rs->next = st->variables;
290 void rrdsetvar_rename_all(RRDSET *st) {
291 debug(D_VARIABLES, "RRDSETVAR rename for chart id '%s' name '%s'", st->id, st->name);
293 // only these 2 can change name
297 char buffer[RRDVAR_MAX_LENGTH + 1];
298 RRDSETVAR *rs, *next = st->variables;
302 snprintfz(buffer, RRDVAR_MAX_LENGTH, "%s.%s", st->name, rs->variable);
304 if (strcmp(buffer, rs->fullname)) {
306 rrdvar_free(st->rrdhost, &st->rrdfamily->variables_root_index, rs->family_name);
307 rrdvar_free(st->rrdhost, &st->rrdhost->variables_root_index, rs->host_name);
310 rs->fullname = strdupz(st->name);
311 rs->family_name = rrdvar_create_and_index("family", &st->rrdfamily->variables_root_index, rs->fullname, rs->type, rs->value);
312 rs->host_name = rrdvar_create_and_index("host", &st->rrdhost->variables_root_index, rs->fullname, rs->type, rs->value);
316 rrdsetcalc_link_matching(st);
319 void rrdsetvar_free(RRDSETVAR *rs) {
320 RRDSET *st = rs->rrdset;
321 debug(D_VARIABLES, "RRDSETVAR free for chart id '%s' name '%s', variable '%s'", st->id, st->name, rs->variable);
323 rrdvar_free(st->rrdhost, &st->variables_root_index, rs->local);
324 rrdvar_free(st->rrdhost, &st->rrdfamily->variables_root_index, rs->family);
325 rrdvar_free(st->rrdhost, &st->rrdhost->variables_root_index, rs->host);
326 rrdvar_free(st->rrdhost, &st->rrdfamily->variables_root_index, rs->family_name);
327 rrdvar_free(st->rrdhost, &st->rrdhost->variables_root_index, rs->host_name);
329 if(st->variables == rs) {
330 st->variables = rs->next;
334 for (t = st->variables; t && t->next != rs; t = t->next);
335 if(!t) error("RRDSETVAR '%s' not found in chart '%s' variables linked list", rs->fullname, st->id);
336 else t->next = rs->next;
345 // ----------------------------------------------------------------------------
346 // RRDDIMVAR management
348 #define RRDDIMVAR_ID_MAX 1024
350 RRDDIMVAR *rrddimvar_create(RRDDIM *rd, int type, const char *prefix, const char *suffix, void *value, uint32_t options) {
351 RRDSET *st = rd->rrdset;
353 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:"");
355 if(!prefix) prefix = "";
356 if(!suffix) suffix = "";
358 char buffer[RRDDIMVAR_ID_MAX + 1];
359 RRDDIMVAR *rs = (RRDDIMVAR *)callocz(1, sizeof(RRDDIMVAR));
361 rs->prefix = strdupz(prefix);
362 rs->suffix = strdupz(suffix);
364 snprintfz(buffer, RRDDIMVAR_ID_MAX, "%s%s%s", rs->prefix, rd->id, rs->suffix);
365 rs->id = strdupz(buffer);
367 snprintfz(buffer, RRDDIMVAR_ID_MAX, "%s%s%s", rs->prefix, rd->name, rs->suffix);
368 rs->name = strdupz(buffer);
370 snprintfz(buffer, RRDDIMVAR_ID_MAX, "%s.%s", rd->rrdset->id, rs->id);
371 rs->fullidid = strdupz(buffer);
373 snprintfz(buffer, RRDDIMVAR_ID_MAX, "%s.%s", rd->rrdset->id, rs->name);
374 rs->fullidname = strdupz(buffer);
376 snprintfz(buffer, RRDDIMVAR_ID_MAX, "%s.%s", rd->rrdset->name, rs->id);
377 rs->fullnameid = strdupz(buffer);
379 snprintfz(buffer, RRDDIMVAR_ID_MAX, "%s.%s", rd->rrdset->name, rs->name);
380 rs->fullnamename = strdupz(buffer);
384 rs->options = options;
387 rs->local_id = rrdvar_create_and_index("local", &st->variables_root_index, rs->id, rs->type, rs->value);
388 rs->local_name = rrdvar_create_and_index("local", &st->variables_root_index, rs->name, rs->type, rs->value);
390 rs->family_id = rrdvar_create_and_index("family", &st->rrdfamily->variables_root_index, rs->id, rs->type, rs->value);
391 rs->family_name = rrdvar_create_and_index("family", &st->rrdfamily->variables_root_index, rs->name, rs->type, rs->value);
393 rs->host_fullidid = rrdvar_create_and_index("host", &st->rrdhost->variables_root_index, rs->fullidid, rs->type, rs->value);
394 rs->host_fullidname = rrdvar_create_and_index("host", &st->rrdhost->variables_root_index, rs->fullidname, rs->type, rs->value);
395 rs->host_fullnameid = rrdvar_create_and_index("host", &st->rrdhost->variables_root_index, rs->fullnameid, rs->type, rs->value);
396 rs->host_fullnamename = rrdvar_create_and_index("host", &st->rrdhost->variables_root_index, rs->fullnamename, rs->type, rs->value);
398 rs->next = rd->variables;
404 void rrddimvar_rename_all(RRDDIM *rd) {
405 RRDSET *st = rd->rrdset;
406 debug(D_VARIABLES, "RRDDIMSET rename for chart id '%s' name '%s', dimension id '%s', name '%s'", st->id, st->name, rd->id, rd->name);
408 RRDDIMVAR *rs, *next = rd->variables;
412 if (strcmp(rd->name, rs->name)) {
413 char buffer[RRDDIMVAR_ID_MAX + 1];
417 rrdvar_free(st->rrdhost, &st->variables_root_index, rs->local_name);
419 snprintfz(buffer, RRDDIMVAR_ID_MAX, "%s%s%s", rs->prefix, rd->name, rs->suffix);
420 rs->name = strdupz(buffer);
421 rs->local_name = rrdvar_create_and_index("local", &st->variables_root_index, rs->name, rs->type, rs->value);
423 rrdvar_free(st->rrdhost, &st->rrdhost->variables_root_index, rs->host_fullidname);
424 freez(rs->fullidname);
425 snprintfz(buffer, RRDDIMVAR_ID_MAX, "%s.%s", st->id, rs->name);
426 rs->fullidname = strdupz(buffer);
427 rs->host_fullidname = rrdvar_create_and_index("host", &st->rrdhost->variables_root_index,
428 rs->fullidname, rs->type, rs->value);
431 rrdvar_free(st->rrdhost, &st->rrdhost->variables_root_index, rs->host_fullnameid);
432 freez(rs->fullnameid);
433 snprintfz(buffer, RRDDIMVAR_ID_MAX, "%s.%s", st->name, rs->id);
434 rs->fullnameid = strdupz(buffer);
435 rs->host_fullnameid = rrdvar_create_and_index("host", &st->rrdhost->variables_root_index,
436 rs->fullnameid, rs->type, rs->value);
439 rrdvar_free(st->rrdhost, &st->rrdhost->variables_root_index, rs->host_fullnamename);
440 freez(rs->fullnamename);
441 snprintfz(buffer, RRDDIMVAR_ID_MAX, "%s.%s", st->name, rs->name);
442 rs->fullnamename = strdupz(buffer);
443 rs->host_fullnamename = rrdvar_create_and_index("host", &st->rrdhost->variables_root_index,
444 rs->fullnamename, rs->type, rs->value);
449 void rrddimvar_free(RRDDIMVAR *rs) {
450 RRDDIM *rd = rs->rrddim;
451 RRDSET *st = rd->rrdset;
452 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);
454 rrdvar_free(st->rrdhost, &st->variables_root_index, rs->local_id);
455 rrdvar_free(st->rrdhost, &st->variables_root_index, rs->local_name);
457 rrdvar_free(st->rrdhost, &st->rrdfamily->variables_root_index, rs->family_id);
458 rrdvar_free(st->rrdhost, &st->rrdfamily->variables_root_index, rs->family_name);
460 rrdvar_free(st->rrdhost, &st->rrdhost->variables_root_index, rs->host_fullidid);
461 rrdvar_free(st->rrdhost, &st->rrdhost->variables_root_index, rs->host_fullidname);
462 rrdvar_free(st->rrdhost, &st->rrdhost->variables_root_index, rs->host_fullnameid);
463 rrdvar_free(st->rrdhost, &st->rrdhost->variables_root_index, rs->host_fullnamename);
465 if(rd->variables == rs) {
466 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);
467 rd->variables = rs->next;
470 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);
472 for (t = rd->variables; t && t->next != rs; t = t->next) ;
473 if(!t) error("RRDDIMVAR '%s' not found in dimension '%s/%s' variables linked list", rs->name, st->id, rd->id);
474 else t->next = rs->next;
482 freez(rs->fullidname);
483 freez(rs->fullnameid);
484 freez(rs->fullnamename);
488 // ----------------------------------------------------------------------------
489 // RRDCALC management
491 static inline const char *rrdcalc_status2string(int status) {
493 case RRDCALC_STATUS_REMOVED:
496 case RRDCALC_STATUS_UNDEFINED:
499 case RRDCALC_STATUS_UNINITIALIZED:
500 return "UNINITIALIZED";
502 case RRDCALC_STATUS_CLEAR:
505 case RRDCALC_STATUS_RAISED:
508 case RRDCALC_STATUS_WARNING:
511 case RRDCALC_STATUS_CRITICAL:
515 error("Unknown alarm status %d", status);
520 static void rrdsetcalc_link(RRDSET *st, RRDCALC *rc) {
521 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);
523 rc->last_status_change = time(NULL);
526 rc->rrdset_next = st->alarms;
527 rc->rrdset_prev = NULL;
530 rc->rrdset_next->rrdset_prev = rc;
534 if(rc->update_every < rc->rrdset->update_every) {
535 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);
536 rc->update_every = rc->rrdset->update_every;
539 if(!isnan(rc->green) && isnan(st->green)) {
540 debug(D_HEALTH, "Health alarm '%s.%s' green threshold set from %Lf to %Lf.", rc->rrdset->id, rc->name, rc->rrdset->green, rc->green);
541 st->green = rc->green;
544 if(!isnan(rc->red) && isnan(st->red)) {
545 debug(D_HEALTH, "Health alarm '%s.%s' red threshold set from %Lf to %Lf.", rc->rrdset->id, rc->name, rc->rrdset->red, rc->red);
549 rc->local = rrdvar_create_and_index("local", &st->variables_root_index, rc->name, RRDVAR_TYPE_CALCULATED, &rc->value);
550 rc->family = rrdvar_create_and_index("family", &st->rrdfamily->variables_root_index, rc->name, RRDVAR_TYPE_CALCULATED, &rc->value);
552 char fullname[RRDVAR_MAX_LENGTH + 1];
553 snprintfz(fullname, RRDVAR_MAX_LENGTH, "%s.%s", st->id, rc->name);
554 rc->hostid = rrdvar_create_and_index("host", &st->rrdhost->variables_root_index, fullname, RRDVAR_TYPE_CALCULATED, &rc->value);
556 snprintfz(fullname, RRDVAR_MAX_LENGTH, "%s.%s", st->name, rc->name);
557 rc->hostname = rrdvar_create_and_index("host", &st->rrdhost->variables_root_index, fullname, RRDVAR_TYPE_CALCULATED, &rc->value);
559 if(!rc->units) rc->units = strdupz(st->units);
562 time_t now = time(NULL);
563 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);
567 static inline int rrdcalc_is_matching_this_rrdset(RRDCALC *rc, RRDSET *st) {
568 if( (rc->hash_chart == st->hash && !strcmp(rc->chart, st->id)) ||
569 (rc->hash_chart == st->hash_name && !strcmp(rc->chart, st->name)))
575 // this has to be called while the RRDHOST is locked
576 inline void rrdsetcalc_link_matching(RRDSET *st) {
577 // debug(D_HEALTH, "find matching alarms for chart '%s'", st->id);
580 for(rc = st->rrdhost->alarms; rc ; rc = rc->next) {
581 if(unlikely(rc->rrdset))
584 if(unlikely(rrdcalc_is_matching_this_rrdset(rc, st)))
585 rrdsetcalc_link(st, rc);
589 // this has to be called while the RRDHOST is locked
590 inline void rrdsetcalc_unlink(RRDCALC *rc) {
591 RRDSET *st = rc->rrdset;
594 debug(D_HEALTH, "Requested to unlink RRDCALC '%s.%s' which is not linked to any RRDSET", rc->chart?rc->chart:"NOCHART", rc->name);
595 error("Requested to unlink RRDCALC '%s.%s' which is not linked to any RRDSET", rc->chart?rc->chart:"NOCHART", rc->name);
600 time_t now = time(NULL);
601 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);
604 RRDHOST *host = st->rrdhost;
606 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);
610 rc->rrdset_prev->rrdset_next = rc->rrdset_next;
613 rc->rrdset_next->rrdset_prev = rc->rrdset_prev;
616 st->alarms = rc->rrdset_next;
618 rc->rrdset_prev = rc->rrdset_next = NULL;
620 rrdvar_free(st->rrdhost, &st->variables_root_index, rc->local);
623 rrdvar_free(st->rrdhost, &st->rrdfamily->variables_root_index, rc->family);
626 rrdvar_free(st->rrdhost, &st->rrdhost->variables_root_index, rc->hostid);
629 rrdvar_free(st->rrdhost, &st->rrdhost->variables_root_index, rc->hostname);
634 // RRDCALC will remain in RRDHOST
635 // so that if the matching chart is found in the future
636 // it will be applied automatically
639 RRDCALC *rrdcalc_find(RRDSET *st, const char *name) {
641 uint32_t hash = simple_hash(name);
643 for( rc = st->alarms; rc ; rc = rc->rrdset_next ) {
644 if(unlikely(rc->hash == hash && !strcmp(rc->name, name)))
651 static inline int rrdcalc_exists(RRDHOST *host, const char *chart, const char *name, uint32_t hash_chart, uint32_t hash_name) {
654 if(unlikely(!chart)) {
655 error("attempt to find RRDCALC '%s' without giving a chart name", name);
659 if(unlikely(!hash_chart)) hash_chart = simple_hash(chart);
660 if(unlikely(!hash_name)) hash_name = simple_hash(name);
662 // make sure it does not already exist
663 for(rc = host->alarms; rc ; rc = rc->next) {
664 if (unlikely(rc->chart && rc->hash == hash_name && rc->hash_chart == hash_chart && !strcmp(name, rc->name) && !strcmp(chart, rc->chart))) {
665 debug(D_HEALTH, "Health alarm '%s.%s' already exists in host '%s'.", chart, name, host->hostname);
666 error("Health alarm '%s.%s' already exists in host '%s'.", chart, name, host->hostname);
674 static inline uint32_t rrdcalc_get_unique_id(RRDHOST *host, const char *chart, const char *name, uint32_t *next_event_id) {
676 uint32_t hash_chart = simple_hash(chart);
677 uint32_t hash_name = simple_hash(name);
679 // re-use old IDs, by looking them up in the alarm log
681 for(ae = host->health_log.alarms; ae ;ae = ae->next) {
682 if(unlikely(ae->hash_name == hash_name && ae->hash_chart == hash_chart && !strcmp(name, ae->name) && !strcmp(chart, ae->chart))) {
683 if(next_event_id) *next_event_id = ae->alarm_event_id + 1;
689 return host->health_log.next_alarm_id++;
692 static inline void rrdcalc_create_part2(RRDHOST *host, RRDCALC *rc) {
693 rrdhost_check_rdlock(host);
695 if(rc->calculation) {
696 rc->calculation->this = &rc->value;
697 rc->calculation->after = &rc->db_after;
698 rc->calculation->before = &rc->db_before;
699 rc->calculation->rrdcalc = rc;
703 rc->warning->this = &rc->value;
704 rc->warning->after = &rc->db_after;
705 rc->warning->before = &rc->db_before;
706 rc->warning->rrdcalc = rc;
710 rc->critical->this = &rc->value;
711 rc->critical->after = &rc->db_after;
712 rc->critical->before = &rc->db_before;
713 rc->critical->rrdcalc = rc;
716 // link it to the host
717 if(likely(host->alarms)) {
720 for(t = host->alarms; t && t->next ; t = t->next) ;
727 // link it to its chart
729 for(st = host->rrdset_root; st ; st = st->next) {
730 if(rrdcalc_is_matching_this_rrdset(rc, st)) {
731 rrdsetcalc_link(st, rc);
737 static inline RRDCALC *rrdcalc_create(RRDHOST *host, RRDCALCTEMPLATE *rt, const char *chart) {
739 debug(D_HEALTH, "Health creating dynamic alarm (from template) '%s.%s'", chart, rt->name);
741 if(rrdcalc_exists(host, chart, rt->name, 0, 0))
744 RRDCALC *rc = callocz(1, sizeof(RRDCALC));
745 rc->next_event_id = 1;
746 rc->id = rrdcalc_get_unique_id(host, chart, rt->name, &rc->next_event_id);
747 rc->name = strdupz(rt->name);
748 rc->hash = simple_hash(rc->name);
749 rc->chart = strdupz(chart);
750 rc->hash_chart = simple_hash(rc->chart);
752 if(rt->dimensions) rc->dimensions = strdupz(rt->dimensions);
754 rc->green = rt->green;
759 rc->delay_up_duration = rt->delay_up_duration;
760 rc->delay_down_duration = rt->delay_down_duration;
761 rc->delay_max_duration = rt->delay_max_duration;
762 rc->delay_multiplier = rt->delay_multiplier;
764 rc->group = rt->group;
765 rc->after = rt->after;
766 rc->before = rt->before;
767 rc->update_every = rt->update_every;
768 rc->options = rt->options;
770 if(rt->exec) rc->exec = strdupz(rt->exec);
771 if(rt->recipient) rc->recipient = strdupz(rt->recipient);
772 if(rt->source) rc->source = strdupz(rt->source);
773 if(rt->units) rc->units = strdupz(rt->units);
774 if(rt->info) rc->info = strdupz(rt->info);
776 if(rt->calculation) {
777 rc->calculation = expression_parse(rt->calculation->source, NULL, NULL);
779 error("Health alarm '%s.%s': failed to parse calculation expression '%s'", chart, rt->name, rt->calculation->source);
782 rc->warning = expression_parse(rt->warning->source, NULL, NULL);
784 error("Health alarm '%s.%s': failed to re-parse warning expression '%s'", chart, rt->name, rt->warning->source);
787 rc->critical = expression_parse(rt->critical->source, NULL, NULL);
789 error("Health alarm '%s.%s': failed to re-parse critical expression '%s'", chart, rt->name, rt->critical->source);
792 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",
793 (rc->chart)?rc->chart:"NOCHART",
795 (rc->exec)?rc->exec:"DEFAULT",
796 (rc->recipient)?rc->recipient:"DEFAULT",
803 (rc->dimensions)?rc->dimensions:"NONE",
805 (rc->calculation)?rc->calculation->parsed_as:"NONE",
806 (rc->warning)?rc->warning->parsed_as:"NONE",
807 (rc->critical)?rc->critical->parsed_as:"NONE",
809 rc->delay_up_duration,
810 rc->delay_down_duration,
811 rc->delay_max_duration,
815 rrdcalc_create_part2(host, rc);
819 void rrdcalc_free(RRDHOST *host, RRDCALC *rc) {
822 debug(D_HEALTH, "Health removing alarm '%s.%s' of host '%s'", rc->chart?rc->chart:"NOCHART", rc->name, host->hostname);
824 // unlink it from RRDSET
825 if(rc->rrdset) rrdsetcalc_unlink(rc);
827 // unlink it from RRDHOST
828 if(unlikely(rc == host->alarms))
829 host->alarms = rc->next;
831 else if(likely(host->alarms)) {
832 RRDCALC *t, *last = host->alarms;
833 for(t = last->next; t && t != rc; last = t, t = t->next) ;
834 if(last && last->next == rc)
835 last->next = rc->next;
837 error("Cannot unlink alarm '%s.%s' from host '%s': not found", rc->chart?rc->chart:"NOCHART", rc->name, host->hostname);
840 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);
842 expression_free(rc->calculation);
843 expression_free(rc->warning);
844 expression_free(rc->critical);
849 freez(rc->dimensions);
851 freez(rc->recipient);
858 // ----------------------------------------------------------------------------
859 // RRDCALCTEMPLATE management
861 void rrdcalctemplate_link_matching(RRDSET *st) {
864 for(rt = st->rrdhost->templates; rt ; rt = rt->next) {
865 if(rt->hash_context == st->hash_context && !strcmp(rt->context, st->context)) {
866 RRDCALC *rc = rrdcalc_create(st->rrdhost, rt, st->id);
868 error("Health tried to create alarm from template '%s', but it failed", rt->name);
870 #ifdef NETDATA_INTERNAL_CHECKS
871 else if(rc->rrdset != st)
872 error("Health alarm '%s.%s' should be linked to chart '%s', but it is not", rc->chart?rc->chart:"NOCHART", rc->name, st->id);
878 static inline void rrdcalctemplate_free(RRDHOST *host, RRDCALCTEMPLATE *rt) {
879 debug(D_HEALTH, "Health removing template '%s' of host '%s'", rt->name, host->hostname);
881 if(host->templates) {
882 if(host->templates == rt) {
883 host->templates = rt->next;
886 RRDCALCTEMPLATE *t, *last = host->templates;
887 for (t = last->next; t && t != rt; last = t, t = t->next ) ;
888 if(last && last->next == rt) {
889 last->next = rt->next;
893 error("Cannot find RRDCALCTEMPLATE '%s' linked in host '%s'", rt->name, host->hostname);
897 expression_free(rt->calculation);
898 expression_free(rt->warning);
899 expression_free(rt->critical);
903 freez(rt->recipient);
908 freez(rt->dimensions);
912 // ----------------------------------------------------------------------------
913 // load health configuration
915 #define HEALTH_CONF_MAX_LINE 4096
917 #define HEALTH_ALARM_KEY "alarm"
918 #define HEALTH_TEMPLATE_KEY "template"
919 #define HEALTH_ON_KEY "on"
920 #define HEALTH_LOOKUP_KEY "lookup"
921 #define HEALTH_CALC_KEY "calc"
922 #define HEALTH_EVERY_KEY "every"
923 #define HEALTH_GREEN_KEY "green"
924 #define HEALTH_RED_KEY "red"
925 #define HEALTH_WARN_KEY "warn"
926 #define HEALTH_CRIT_KEY "crit"
927 #define HEALTH_EXEC_KEY "exec"
928 #define HEALTH_RECIPIENT_KEY "to"
929 #define HEALTH_UNITS_KEY "units"
930 #define HEALTH_INFO_KEY "info"
931 #define HEALTH_DELAY_KEY "delay"
933 static inline int rrdcalc_add_alarm_from_config(RRDHOST *host, RRDCALC *rc) {
935 error("Health configuration for alarm '%s' does not have a chart", rc->name);
939 if(!rc->update_every) {
940 error("Health configuration for alarm '%s.%s' has no frequency (parameter 'every'). Ignoring it.", rc->chart?rc->chart:"NOCHART", rc->name);
944 if(!RRDCALC_HAS_DB_LOOKUP(rc) && !rc->warning && !rc->critical) {
945 error("Health configuration for alarm '%s.%s' is useless (no calculation, no warning and no critical evaluation)", rc->chart?rc->chart:"NOCHART", rc->name);
949 if (rrdcalc_exists(host, rc->chart, rc->name, rc->hash_chart, rc->hash))
952 rc->id = rrdcalc_get_unique_id(&localhost, rc->chart, rc->name, &rc->next_event_id);
954 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",
955 rc->chart?rc->chart:"NOCHART",
958 (rc->exec)?rc->exec:"DEFAULT",
959 (rc->recipient)?rc->recipient:"DEFAULT",
966 (rc->dimensions)?rc->dimensions:"NONE",
968 (rc->calculation)?rc->calculation->parsed_as:"NONE",
969 (rc->warning)?rc->warning->parsed_as:"NONE",
970 (rc->critical)?rc->critical->parsed_as:"NONE",
972 rc->delay_up_duration,
973 rc->delay_down_duration,
974 rc->delay_max_duration,
978 rrdcalc_create_part2(host, rc);
982 static inline int rrdcalctemplate_add_template_from_config(RRDHOST *host, RRDCALCTEMPLATE *rt) {
983 if(unlikely(!rt->context)) {
984 error("Health configuration for template '%s' does not have a context", rt->name);
988 if(unlikely(!rt->update_every)) {
989 error("Health configuration for template '%s' has no frequency (parameter 'every'). Ignoring it.", rt->name);
993 if(unlikely(!RRDCALCTEMPLATE_HAS_CALCULATION(rt) && !rt->warning && !rt->critical)) {
994 error("Health configuration for template '%s' is useless (no calculation, no warning and no critical evaluation)", rt->name);
998 RRDCALCTEMPLATE *t, *last = NULL;
999 for (t = host->templates; t ; last = t, t = t->next) {
1000 if(unlikely(t->hash_name == rt->hash_name && !strcmp(t->name, rt->name))) {
1001 error("Health configuration template '%s' already exists for host '%s'.", rt->name, host->hostname);
1006 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",
1008 (rt->context)?rt->context:"NONE",
1009 (rt->exec)?rt->exec:"DEFAULT",
1010 (rt->recipient)?rt->recipient:"DEFAULT",
1017 (rt->dimensions)?rt->dimensions:"NONE",
1019 (rt->calculation)?rt->calculation->parsed_as:"NONE",
1020 (rt->warning)?rt->warning->parsed_as:"NONE",
1021 (rt->critical)?rt->critical->parsed_as:"NONE",
1023 rt->delay_up_duration,
1024 rt->delay_down_duration,
1025 rt->delay_max_duration,
1026 rt->delay_multiplier
1033 rt->next = host->templates;
1034 host->templates = rt;
1040 static inline int health_parse_duration(char *string, int *result) {
1041 // make sure it is a number
1042 if(!*string || !(isdigit(*string) || *string == '+' || *string == '-')) {
1048 calculated_number n = strtold(string, &e);
1052 *result = (int) (n * 86400 * 365);
1055 *result = (int) (n * 86400 * 30);
1058 *result = (int) (n * 86400 * 7);
1061 *result = (int) (n * 86400);
1064 *result = (int) (n * 3600);
1067 *result = (int) (n * 60);
1072 *result = (int) (n);
1082 static inline int health_parse_delay(
1083 size_t line, const char *path, const char *file, char *string,
1084 int *delay_up_duration,
1085 int *delay_down_duration,
1086 int *delay_max_duration,
1087 float *delay_multiplier) {
1090 char given_down = 0;
1092 char given_multiplier = 0;
1098 while(*s && !isspace(*s)) s++;
1099 while(*s && isspace(*s)) *s++ = '\0';
1104 while(*s && !isspace(*s)) s++;
1105 while(*s && isspace(*s)) *s++ = '\0';
1107 if(!strcasecmp(key, "up")) {
1108 if (!health_parse_duration(value, delay_up_duration)) {
1109 error("Health configuration at line %zu of file '%s/%s': invalid value '%s' for '%s' keyword",
1110 line, path, file, value, key);
1114 else if(!strcasecmp(key, "down")) {
1115 if (!health_parse_duration(value, delay_down_duration)) {
1116 error("Health configuration at line %zu of file '%s/%s': invalid value '%s' for '%s' keyword",
1117 line, path, file, value, key);
1119 else given_down = 1;
1121 else if(!strcasecmp(key, "multiplier")) {
1122 *delay_multiplier = strtof(value, NULL);
1123 if(isnan(*delay_multiplier) || isinf(*delay_multiplier) || islessequal(*delay_multiplier, 0)) {
1124 error("Health configuration at line %zu of file '%s/%s': invalid value '%s' for '%s' keyword",
1125 line, path, file, value, key);
1127 else given_multiplier = 1;
1129 else if(!strcasecmp(key, "max")) {
1130 if (!health_parse_duration(value, delay_max_duration)) {
1131 error("Health configuration at line %zu of file '%s/%s': invalid value '%s' for '%s' keyword",
1132 line, path, file, value, key);
1137 error("Health configuration at line %zu of file '%s/%s': unknown keyword '%s'",
1138 line, path, file, key);
1143 *delay_up_duration = 0;
1146 *delay_down_duration = 0;
1148 if(!given_multiplier)
1149 *delay_multiplier = 1.0;
1152 if((*delay_max_duration) < (*delay_up_duration) * (*delay_multiplier))
1153 *delay_max_duration = (*delay_up_duration) * (*delay_multiplier);
1155 if((*delay_max_duration) < (*delay_down_duration) * (*delay_multiplier))
1156 *delay_max_duration = (*delay_down_duration) * (*delay_multiplier);
1162 static inline int health_parse_db_lookup(
1163 size_t line, const char *path, const char *file, char *string,
1164 int *group_method, int *after, int *before, int *every,
1165 uint32_t *options, char **dimensions
1167 debug(D_HEALTH, "Health configuration parsing database lookup %zu@%s/%s: %s", line, path, file, string);
1169 if(*dimensions) freez(*dimensions);
1176 char *s = string, *key;
1178 // first is the group method
1180 while(*s && !isspace(*s)) s++;
1181 while(*s && isspace(*s)) *s++ = '\0';
1183 error("Health configuration invalid chart calculation at line %zu of file '%s/%s': expected group method followed by the 'after' time, but got '%s'",
1184 line, path, file, key);
1188 if((*group_method = web_client_api_request_v1_data_group(key, -1)) == -1) {
1189 error("Health configuration at line %zu of file '%s/%s': invalid group method '%s'",
1190 line, path, file, key);
1194 // then is the 'after' time
1196 while(*s && !isspace(*s)) s++;
1197 while(*s && isspace(*s)) *s++ = '\0';
1199 if(!health_parse_duration(key, after)) {
1200 error("Health configuration at line %zu of file '%s/%s': invalid duration '%s' after group method",
1201 line, path, file, key);
1206 *every = abs(*after);
1208 // now we may have optional parameters
1211 while(*s && !isspace(*s)) s++;
1212 while(*s && isspace(*s)) *s++ = '\0';
1215 if(!strcasecmp(key, "at")) {
1217 while(*s && !isspace(*s)) s++;
1218 while(*s && isspace(*s)) *s++ = '\0';
1220 if (!health_parse_duration(value, before)) {
1221 error("Health configuration at line %zu of file '%s/%s': invalid duration '%s' for '%s' keyword",
1222 line, path, file, value, key);
1225 else if(!strcasecmp(key, HEALTH_EVERY_KEY)) {
1227 while(*s && !isspace(*s)) s++;
1228 while(*s && isspace(*s)) *s++ = '\0';
1230 if (!health_parse_duration(value, every)) {
1231 error("Health configuration at line %zu of file '%s/%s': invalid duration '%s' for '%s' keyword",
1232 line, path, file, value, key);
1235 else if(!strcasecmp(key, "absolute") || !strcasecmp(key, "abs") || !strcasecmp(key, "absolute_sum")) {
1236 *options |= RRDR_OPTION_ABSOLUTE;
1238 else if(!strcasecmp(key, "min2max")) {
1239 *options |= RRDR_OPTION_MIN2MAX;
1241 else if(!strcasecmp(key, "null2zero")) {
1242 *options |= RRDR_OPTION_NULL2ZERO;
1244 else if(!strcasecmp(key, "percentage")) {
1245 *options |= RRDR_OPTION_PERCENTAGE;
1247 else if(!strcasecmp(key, "unaligned")) {
1248 *options |= RRDR_OPTION_NOT_ALIGNED;
1250 else if(!strcasecmp(key, "of")) {
1251 if(*s && strcasecmp(s, "all"))
1252 *dimensions = strdupz(s);
1256 error("Health configuration at line %zu of file '%s/%s': unknown keyword '%s'",
1257 line, path, file, key);
1264 static inline char *health_source_file(size_t line, const char *path, const char *filename) {
1265 char buffer[FILENAME_MAX + 1];
1266 snprintfz(buffer, FILENAME_MAX, "%zu@%s/%s", line, path, filename);
1267 return strdupz(buffer);
1270 static inline void strip_quotes(char *s) {
1272 if(*s == '\'' || *s == '"') *s = ' ';
1277 int health_readfile(const char *path, const char *filename) {
1278 debug(D_HEALTH, "Health configuration reading file '%s/%s'", path, filename);
1280 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;
1281 char buffer[HEALTH_CONF_MAX_LINE + 1];
1283 if(unlikely(!hash_alarm)) {
1284 hash_alarm = simple_uhash(HEALTH_ALARM_KEY);
1285 hash_template = simple_uhash(HEALTH_TEMPLATE_KEY);
1286 hash_on = simple_uhash(HEALTH_ON_KEY);
1287 hash_calc = simple_uhash(HEALTH_CALC_KEY);
1288 hash_lookup = simple_uhash(HEALTH_LOOKUP_KEY);
1289 hash_green = simple_uhash(HEALTH_GREEN_KEY);
1290 hash_red = simple_uhash(HEALTH_RED_KEY);
1291 hash_warn = simple_uhash(HEALTH_WARN_KEY);
1292 hash_crit = simple_uhash(HEALTH_CRIT_KEY);
1293 hash_exec = simple_uhash(HEALTH_EXEC_KEY);
1294 hash_every = simple_uhash(HEALTH_EVERY_KEY);
1295 hash_units = simple_hash(HEALTH_UNITS_KEY);
1296 hash_info = simple_hash(HEALTH_INFO_KEY);
1297 hash_recipient = simple_hash(HEALTH_RECIPIENT_KEY);
1298 hash_delay = simple_uhash(HEALTH_DELAY_KEY);
1301 snprintfz(buffer, HEALTH_CONF_MAX_LINE, "%s/%s", path, filename);
1302 FILE *fp = fopen(buffer, "r");
1304 error("Health configuration cannot read file '%s'.", buffer);
1309 RRDCALCTEMPLATE *rt = NULL;
1311 size_t line = 0, append = 0;
1313 while((s = fgets(&buffer[append], (int)(HEALTH_CONF_MAX_LINE - append), fp)) || append) {
1314 int stop_appending = !s;
1316 // info("Line %zu of file '%s/%s': '%s'", line, path, filename, s);
1319 // info("Trimmed line %zu of file '%s/%s': '%s'", line, path, filename, s);
1322 if(!stop_appending && s[append - 1] == '\\') {
1323 s[append - 1] = ' ';
1324 append = &s[append] - buffer;
1325 if(append < HEALTH_CONF_MAX_LINE)
1328 error("Health configuration has too long muli-line at line %zu of file '%s/%s'.", line, path, filename);
1334 while(*s && *s != ':') s++;
1336 error("Health configuration has invalid line %zu of file '%s/%s'. It does not contain a ':'. Ignoring it.", line, path, filename);
1344 value = trim(value);
1347 error("Health configuration has invalid line %zu of file '%s/%s'. Keyword is empty. Ignoring it.", line, path, filename);
1352 error("Health configuration has invalid line %zu of file '%s/%s'. value is empty. Ignoring it.", line, path, filename);
1356 // info("Health file '%s/%s', key '%s', value '%s'", path, filename, key, value);
1357 uint32_t hash = simple_uhash(key);
1359 if(hash == hash_alarm && !strcasecmp(key, HEALTH_ALARM_KEY)) {
1360 if(rc && !rrdcalc_add_alarm_from_config(&localhost, rc))
1361 rrdcalc_free(&localhost, rc);
1364 if (!rrdcalctemplate_add_template_from_config(&localhost, rt))
1365 rrdcalctemplate_free(&localhost, rt);
1369 rc = callocz(1, sizeof(RRDCALC));
1370 rc->next_event_id = 1;
1371 rc->name = strdupz(value);
1372 rc->hash = simple_hash(rc->name);
1373 rc->source = health_source_file(line, path, filename);
1377 rc->old_value = NAN;
1378 rc->delay_multiplier = 1.0;
1380 if(rrdvar_fix_name(rc->name))
1381 error("Health configuration renamed alarm '%s' to '%s'", value, rc->name);
1383 else if(hash == hash_template && !strcasecmp(key, HEALTH_TEMPLATE_KEY)) {
1385 if(!rrdcalc_add_alarm_from_config(&localhost, rc))
1386 rrdcalc_free(&localhost, rc);
1390 if(rt && !rrdcalctemplate_add_template_from_config(&localhost, rt))
1391 rrdcalctemplate_free(&localhost, rt);
1393 rt = callocz(1, sizeof(RRDCALCTEMPLATE));
1394 rt->name = strdupz(value);
1395 rt->hash_name = simple_hash(rt->name);
1396 rt->source = health_source_file(line, path, filename);
1399 rt->delay_multiplier = 1.0;
1401 if(rrdvar_fix_name(rt->name))
1402 error("Health configuration renamed template '%s' to '%s'", value, rt->name);
1405 if(hash == hash_on && !strcasecmp(key, HEALTH_ON_KEY)) {
1407 if(strcmp(rc->chart, value))
1408 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').",
1409 line, path, filename, rc->name, key, rc->chart, value, value);
1413 rc->chart = strdupz(value);
1414 rc->hash_chart = simple_hash(rc->chart);
1416 else if(hash == hash_lookup && !strcasecmp(key, HEALTH_LOOKUP_KEY)) {
1417 health_parse_db_lookup(line, path, filename, value, &rc->group, &rc->after, &rc->before,
1419 &rc->options, &rc->dimensions);
1421 else if(hash == hash_every && !strcasecmp(key, HEALTH_EVERY_KEY)) {
1422 if(!health_parse_duration(value, &rc->update_every))
1423 info("Health configuration at line %zu of file '%s/%s' for alarm '%s' at key '%s' cannot parse duration: '%s'.",
1424 line, path, filename, rc->name, key, value);
1426 else if(hash == hash_green && !strcasecmp(key, HEALTH_GREEN_KEY)) {
1428 rc->green = strtold(value, &e);
1430 info("Health configuration at line %zu of file '%s/%s' for alarm '%s' at key '%s' leaves this string unmatched: '%s'.",
1431 line, path, filename, rc->name, key, e);
1434 else if(hash == hash_red && !strcasecmp(key, HEALTH_RED_KEY)) {
1436 rc->red = strtold(value, &e);
1438 info("Health configuration at line %zu of file '%s/%s' for alarm '%s' at key '%s' leaves this string unmatched: '%s'.",
1439 line, path, filename, rc->name, key, e);
1442 else if(hash == hash_calc && !strcasecmp(key, HEALTH_CALC_KEY)) {
1443 const char *failed_at = NULL;
1445 rc->calculation = expression_parse(value, &failed_at, &error);
1446 if(!rc->calculation) {
1447 error("Health configuration at line %zu of file '%s/%s' for alarm '%s' at key '%s' has unparse-able expression '%s': %s at '%s'",
1448 line, path, filename, rc->name, key, value, expression_strerror(error), failed_at);
1451 else if(hash == hash_warn && !strcasecmp(key, HEALTH_WARN_KEY)) {
1452 const char *failed_at = NULL;
1454 rc->warning = expression_parse(value, &failed_at, &error);
1456 error("Health configuration at line %zu of file '%s/%s' for alarm '%s' at key '%s' has unparse-able expression '%s': %s at '%s'",
1457 line, path, filename, rc->name, key, value, expression_strerror(error), failed_at);
1460 else if(hash == hash_crit && !strcasecmp(key, HEALTH_CRIT_KEY)) {
1461 const char *failed_at = NULL;
1463 rc->critical = expression_parse(value, &failed_at, &error);
1465 error("Health configuration at line %zu of file '%s/%s' for alarm '%s' at key '%s' has unparse-able expression '%s': %s at '%s'",
1466 line, path, filename, rc->name, key, value, expression_strerror(error), failed_at);
1469 else if(hash == hash_exec && !strcasecmp(key, HEALTH_EXEC_KEY)) {
1471 if(strcmp(rc->exec, value))
1472 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').",
1473 line, path, filename, rc->name, key, rc->exec, value, value);
1477 rc->exec = strdupz(value);
1479 else if(hash == hash_recipient && !strcasecmp(key, HEALTH_RECIPIENT_KEY)) {
1481 if(strcmp(rc->recipient, value))
1482 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').",
1483 line, path, filename, rc->name, key, rc->recipient, value, value);
1485 freez(rc->recipient);
1487 rc->recipient = strdupz(value);
1489 else if(hash == hash_units && !strcasecmp(key, HEALTH_UNITS_KEY)) {
1491 if(strcmp(rc->units, value))
1492 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').",
1493 line, path, filename, rc->name, key, rc->units, value, value);
1497 rc->units = strdupz(value);
1498 strip_quotes(rc->units);
1500 else if(hash == hash_info && !strcasecmp(key, HEALTH_INFO_KEY)) {
1502 if(strcmp(rc->info, value))
1503 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').",
1504 line, path, filename, rc->name, key, rc->info, value, value);
1508 rc->info = strdupz(value);
1509 strip_quotes(rc->info);
1511 else if(hash == hash_delay && !strcasecmp(key, HEALTH_DELAY_KEY)) {
1512 health_parse_delay(line, path, filename, value, &rc->delay_up_duration, &rc->delay_down_duration, &rc->delay_max_duration, &rc->delay_multiplier);
1515 error("Health configuration at line %zu of file '%s/%s' for alarm '%s' has unknown key '%s'.",
1516 line, path, filename, rc->name, key);
1520 if(hash == hash_on && !strcasecmp(key, HEALTH_ON_KEY)) {
1522 if(strcmp(rt->context, value))
1523 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').",
1524 line, path, filename, rt->name, key, rt->context, value, value);
1528 rt->context = strdupz(value);
1529 rt->hash_context = simple_hash(rt->context);
1531 else if(hash == hash_lookup && !strcasecmp(key, HEALTH_LOOKUP_KEY)) {
1532 health_parse_db_lookup(line, path, filename, value, &rt->group, &rt->after, &rt->before,
1534 &rt->options, &rt->dimensions);
1536 else if(hash == hash_every && !strcasecmp(key, HEALTH_EVERY_KEY)) {
1537 if(!health_parse_duration(value, &rt->update_every))
1538 info("Health configuration at line %zu of file '%s/%s' for template '%s' at key '%s' cannot parse duration: '%s'.",
1539 line, path, filename, rt->name, key, value);
1541 else if(hash == hash_green && !strcasecmp(key, HEALTH_GREEN_KEY)) {
1543 rt->green = strtold(value, &e);
1545 info("Health configuration at line %zu of file '%s/%s' for template '%s' at key '%s' leaves this string unmatched: '%s'.",
1546 line, path, filename, rt->name, key, e);
1549 else if(hash == hash_red && !strcasecmp(key, HEALTH_RED_KEY)) {
1551 rt->red = strtold(value, &e);
1553 info("Health configuration at line %zu of file '%s/%s' for template '%s' at key '%s' leaves this string unmatched: '%s'.",
1554 line, path, filename, rt->name, key, e);
1557 else if(hash == hash_calc && !strcasecmp(key, HEALTH_CALC_KEY)) {
1558 const char *failed_at = NULL;
1560 rt->calculation = expression_parse(value, &failed_at, &error);
1561 if(!rt->calculation) {
1562 error("Health configuration at line %zu of file '%s/%s' for template '%s' at key '%s' has unparse-able expression '%s': %s at '%s'",
1563 line, path, filename, rt->name, key, value, expression_strerror(error), failed_at);
1566 else if(hash == hash_warn && !strcasecmp(key, HEALTH_WARN_KEY)) {
1567 const char *failed_at = NULL;
1569 rt->warning = expression_parse(value, &failed_at, &error);
1571 error("Health configuration at line %zu of file '%s/%s' for template '%s' at key '%s' has unparse-able expression '%s': %s at '%s'",
1572 line, path, filename, rt->name, key, value, expression_strerror(error), failed_at);
1575 else if(hash == hash_crit && !strcasecmp(key, HEALTH_CRIT_KEY)) {
1576 const char *failed_at = NULL;
1578 rt->critical = expression_parse(value, &failed_at, &error);
1580 error("Health configuration at line %zu of file '%s/%s' for template '%s' at key '%s' has unparse-able expression '%s': %s at '%s'",
1581 line, path, filename, rt->name, key, value, expression_strerror(error), failed_at);
1584 else if(hash == hash_exec && !strcasecmp(key, HEALTH_EXEC_KEY)) {
1586 if(strcmp(rt->exec, value))
1587 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').",
1588 line, path, filename, rt->name, key, rt->exec, value, value);
1592 rt->exec = strdupz(value);
1594 else if(hash == hash_recipient && !strcasecmp(key, HEALTH_RECIPIENT_KEY)) {
1596 if(strcmp(rt->recipient, value))
1597 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').",
1598 line, path, filename, rt->name, key, rt->recipient, value, value);
1600 freez(rt->recipient);
1602 rt->recipient = strdupz(value);
1604 else if(hash == hash_units && !strcasecmp(key, HEALTH_UNITS_KEY)) {
1606 if(strcmp(rt->units, value))
1607 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').",
1608 line, path, filename, rt->name, key, rt->units, value, value);
1612 rt->units = strdupz(value);
1613 strip_quotes(rt->units);
1615 else if(hash == hash_info && !strcasecmp(key, HEALTH_INFO_KEY)) {
1617 if(strcmp(rt->info, value))
1618 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').",
1619 line, path, filename, rt->name, key, rt->info, value, value);
1623 rt->info = strdupz(value);
1624 strip_quotes(rt->info);
1626 else if(hash == hash_delay && !strcasecmp(key, HEALTH_DELAY_KEY)) {
1627 health_parse_delay(line, path, filename, value, &rt->delay_up_duration, &rt->delay_down_duration, &rt->delay_max_duration, &rt->delay_multiplier);
1630 error("Health configuration at line %zu of file '%s/%s' for template '%s' has unknown key '%s'.",
1631 line, path, filename, rt->name, key);
1635 error("Health configuration at line %zu of file '%s/%s' has unknown key '%s'. Expected either '" HEALTH_ALARM_KEY "' or '" HEALTH_TEMPLATE_KEY "'.",
1636 line, path, filename, key);
1640 if(rc && !rrdcalc_add_alarm_from_config(&localhost, rc))
1641 rrdcalc_free(&localhost, rc);
1643 if(rt && !rrdcalctemplate_add_template_from_config(&localhost, rt))
1644 rrdcalctemplate_free(&localhost, rt);
1650 void health_readdir(const char *path) {
1651 size_t pathlen = strlen(path);
1653 debug(D_HEALTH, "Health configuration reading directory '%s'", path);
1655 DIR *dir = opendir(path);
1657 error("Health configuration cannot open directory '%s'.", path);
1661 struct dirent *de = NULL;
1662 while ((de = readdir(dir))) {
1663 size_t len = strlen(de->d_name);
1665 if(de->d_type == DT_DIR
1667 (de->d_name[0] == '.' && de->d_name[1] == '\0')
1668 || (de->d_name[0] == '.' && de->d_name[1] == '.' && de->d_name[2] == '\0')
1670 debug(D_HEALTH, "Ignoring directory '%s'", de->d_name);
1674 else if(de->d_type == DT_DIR) {
1675 char *s = mallocz(pathlen + strlen(de->d_name) + 2);
1678 strcat(s, de->d_name);
1684 else if((de->d_type == DT_LNK || de->d_type == DT_REG || de->d_type == DT_UNKNOWN) &&
1685 len > 5 && !strcmp(&de->d_name[len - 5], ".conf")) {
1686 health_readfile(path, de->d_name);
1689 else debug(D_HEALTH, "Ignoring file '%s'", de->d_name);
1695 static inline char *health_config_dir(void) {
1696 char buffer[FILENAME_MAX + 1];
1697 snprintfz(buffer, FILENAME_MAX, "%s/health.d", config_get("global", "config directory", CONFIG_DIR));
1698 return config_get("health", "health configuration directory", buffer);
1701 void health_init(void) {
1702 debug(D_HEALTH, "Health configuration initializing");
1704 if(!(health_enabled = config_get_boolean("health", "enabled", 1))) {
1705 debug(D_HEALTH, "Health is disabled.");
1709 char *path = health_config_dir();
1712 char buffer[FILENAME_MAX + 1];
1713 snprintfz(buffer, FILENAME_MAX, "%s/alarm-notify.sh", config_get("global", "plugins directory", PLUGINS_DIR));
1714 health_default_exec = config_get("health", "script to execute on alarm", buffer);
1717 long n = config_get_number("health", "in memory max health log entries", (long)localhost.health_log.max);
1719 error("Health configuration has invalid max log entries %ld. Using default %u", n, localhost.health_log.max);
1720 config_set_number("health", "in memory max health log entries", (long)localhost.health_log.max);
1722 else localhost.health_log.max = (unsigned int)n;
1724 rrdhost_rwlock(&localhost);
1725 health_readdir(path);
1726 rrdhost_unlock(&localhost);
1729 // ----------------------------------------------------------------------------
1732 static inline void health_string2json(BUFFER *wb, const char *prefix, const char *label, const char *value, const char *suffix) {
1734 buffer_sprintf(wb, "%s\"%s\":\"%s\"%s", prefix, label, value, suffix);
1736 buffer_sprintf(wb, "%s\"%s\":null%s", prefix, label, suffix);
1739 static inline void health_alarm_entry2json_nolock(BUFFER *wb, ALARM_ENTRY *ae, RRDHOST *host) {
1740 buffer_sprintf(wb, "\n\t{\n"
1741 "\t\t\"hostname\": \"%s\",\n"
1742 "\t\t\"unique_id\": %u,\n"
1743 "\t\t\"alarm_id\": %u,\n"
1744 "\t\t\"alarm_event_id\": %u,\n"
1745 "\t\t\"name\": \"%s\",\n"
1746 "\t\t\"chart\": \"%s\",\n"
1747 "\t\t\"family\": \"%s\",\n"
1748 "\t\t\"processed\": %s,\n"
1749 "\t\t\"updated\": %s,\n"
1750 "\t\t\"exec_run\": %lu,\n"
1751 "\t\t\"exec_failed\": %s,\n"
1752 "\t\t\"exec\": \"%s\",\n"
1753 "\t\t\"recipient\": \"%s\",\n"
1754 "\t\t\"exec_code\": %d,\n"
1755 "\t\t\"source\": \"%s\",\n"
1756 "\t\t\"units\": \"%s\",\n"
1757 "\t\t\"info\": \"%s\",\n"
1758 "\t\t\"when\": %lu,\n"
1759 "\t\t\"duration\": %lu,\n"
1760 "\t\t\"non_clear_duration\": %lu,\n"
1761 "\t\t\"status\": \"%s\",\n"
1762 "\t\t\"old_status\": \"%s\",\n"
1763 "\t\t\"delay\": %d,\n"
1764 "\t\t\"delay_up_to_timestamp\": %lu,\n",
1772 (ae->notifications & HEALTH_ENTRY_NOTIFICATIONS_PROCESSED)?"true":"false",
1773 (ae->notifications & HEALTH_ENTRY_NOTIFICATIONS_UPDATED)?"true":"false",
1774 (unsigned long)ae->exec_run_timestamp,
1775 (ae->notifications & HEALTH_ENTRY_NOTIFICATIONS_EXEC_FAILED)?"true":"false",
1776 ae->exec?ae->exec:health_default_exec,
1777 ae->recipient?ae->recipient:health_default_recipient,
1780 ae->units?ae->units:"",
1781 ae->info?ae->info:"",
1782 (unsigned long)ae->when,
1783 (unsigned long)ae->duration,
1784 (unsigned long)ae->non_clear_duration,
1785 rrdcalc_status2string(ae->new_status),
1786 rrdcalc_status2string(ae->old_status),
1788 (unsigned long)ae->delay_up_to_timestamp
1791 buffer_strcat(wb, "\t\t\"value\":");
1792 buffer_rrd_value(wb, ae->new_value);
1793 buffer_strcat(wb, ",\n");
1795 buffer_strcat(wb, "\t\t\"old_value\":");
1796 buffer_rrd_value(wb, ae->old_value);
1797 buffer_strcat(wb, "\n");
1799 buffer_strcat(wb, "\t}");
1802 void health_alarm_log2json(RRDHOST *host, BUFFER *wb, uint32_t after) {
1803 pthread_rwlock_rdlock(&host->health_log.alarm_log_rwlock);
1805 buffer_strcat(wb, "[");
1807 unsigned int max = host->health_log.max;
1808 unsigned int count = 0;
1810 for(ae = host->health_log.alarms; ae && count < max ; count++, ae = ae->next) {
1811 if(ae->unique_id > after) {
1812 if(likely(count)) buffer_strcat(wb, ",");
1813 health_alarm_entry2json_nolock(wb, ae, host);
1817 buffer_strcat(wb, "\n]\n");
1819 pthread_rwlock_unlock(&host->health_log.alarm_log_rwlock);
1822 static inline void health_rrdcalc2json_nolock(BUFFER *wb, RRDCALC *rc) {
1824 "\t\t\"%s.%s\": {\n"
1825 "\t\t\t\"name\": \"%s\",\n"
1826 "\t\t\t\"chart\": \"%s\",\n"
1827 "\t\t\t\"family\": \"%s\",\n"
1828 "\t\t\t\"active\": %s,\n"
1829 "\t\t\t\"exec\": \"%s\",\n"
1830 "\t\t\t\"recipient\": \"%s\",\n"
1831 "\t\t\t\"source\": \"%s\",\n"
1832 "\t\t\t\"units\": \"%s\",\n"
1833 "\t\t\t\"info\": \"%s\",\n"
1834 "\t\t\t\"status\": \"%s\",\n"
1835 "\t\t\t\"last_status_change\": %lu,\n"
1836 "\t\t\t\"last_updated\": %lu,\n"
1837 "\t\t\t\"next_update\": %lu,\n"
1838 "\t\t\t\"update_every\": %d,\n"
1839 "\t\t\t\"delay_up_duration\": %d,\n"
1840 "\t\t\t\"delay_down_duration\": %d,\n"
1841 "\t\t\t\"delay_max_duration\": %d,\n"
1842 "\t\t\t\"delay_multiplier\": %f,\n"
1843 "\t\t\t\"delay\": %d,\n"
1844 "\t\t\t\"delay_up_to_timestamp\": %lu,\n"
1845 , rc->chart, rc->name
1848 , (rc->rrdset && rc->rrdset->family)?rc->rrdset->family:""
1849 , (rc->rrdset)?"true":"false"
1850 , rc->exec?rc->exec:health_default_exec
1851 , rc->recipient?rc->recipient:health_default_recipient
1853 , rc->units?rc->units:""
1854 , rc->info?rc->info:""
1855 , rrdcalc_status2string(rc->status)
1856 , (unsigned long)rc->last_status_change
1857 , (unsigned long)rc->last_updated
1858 , (unsigned long)rc->next_update
1860 , rc->delay_up_duration
1861 , rc->delay_down_duration
1862 , rc->delay_max_duration
1863 , rc->delay_multiplier
1865 , (unsigned long)rc->delay_up_to_timestamp
1868 if(RRDCALC_HAS_DB_LOOKUP(rc)) {
1869 if(rc->dimensions && *rc->dimensions)
1870 health_string2json(wb, "\t\t\t", "lookup_dimensions", rc->dimensions, ",\n");
1873 "\t\t\t\"db_after\": %lu,\n"
1874 "\t\t\t\"db_before\": %lu,\n"
1875 "\t\t\t\"lookup_method\": \"%s\",\n"
1876 "\t\t\t\"lookup_after\": %d,\n"
1877 "\t\t\t\"lookup_before\": %d,\n"
1878 "\t\t\t\"lookup_options\": \"",
1879 (unsigned long) rc->db_after,
1880 (unsigned long) rc->db_before,
1881 group_method2string(rc->group),
1885 buffer_data_options2string(wb, rc->options);
1886 buffer_strcat(wb, "\",\n");
1889 if(rc->calculation) {
1890 health_string2json(wb, "\t\t\t", "calc", rc->calculation->source, ",\n");
1891 health_string2json(wb, "\t\t\t", "calc_parsed", rc->calculation->parsed_as, ",\n");
1895 health_string2json(wb, "\t\t\t", "warn", rc->warning->source, ",\n");
1896 health_string2json(wb, "\t\t\t", "warn_parsed", rc->warning->parsed_as, ",\n");
1900 health_string2json(wb, "\t\t\t", "crit", rc->critical->source, ",\n");
1901 health_string2json(wb, "\t\t\t", "crit_parsed", rc->critical->parsed_as, ",\n");
1904 buffer_strcat(wb, "\t\t\t\"green\":");
1905 buffer_rrd_value(wb, rc->green);
1906 buffer_strcat(wb, ",\n");
1908 buffer_strcat(wb, "\t\t\t\"red\":");
1909 buffer_rrd_value(wb, rc->red);
1910 buffer_strcat(wb, ",\n");
1912 buffer_strcat(wb, "\t\t\t\"value\":");
1913 buffer_rrd_value(wb, rc->value);
1914 buffer_strcat(wb, "\n");
1916 buffer_strcat(wb, "\t\t}");
1919 //void health_rrdcalctemplate2json_nolock(BUFFER *wb, RRDCALCTEMPLATE *rt) {
1923 void health_alarms2json(RRDHOST *host, BUFFER *wb, int all) {
1926 rrdhost_rdlock(&localhost);
1927 buffer_sprintf(wb, "{\n\t\"hostname\": \"%s\","
1928 "\n\t\"latest_alarm_log_unique_id\": %u,"
1929 "\n\t\"status\": %s,"
1931 "\n\t\"alarms\": {\n",
1933 (host->health_log.next_log_id > 0)?(host->health_log.next_log_id - 1):0,
1934 health_enabled?"true":"false",
1935 (unsigned long)time(NULL));
1938 for(i = 0, rc = host->alarms; rc ; rc = rc->next) {
1939 if(unlikely(!rc->rrdset || !rc->rrdset->last_collected_time.tv_sec))
1942 if(likely(!all && !(rc->status == RRDCALC_STATUS_WARNING || rc->status == RRDCALC_STATUS_CRITICAL)))
1945 if(likely(i)) buffer_strcat(wb, ",\n");
1946 health_rrdcalc2json_nolock(wb, rc);
1950 // buffer_strcat(wb, "\n\t},\n\t\"templates\": {");
1951 // RRDCALCTEMPLATE *rt;
1952 // for(rt = host->templates; rt ; rt = rt->next)
1953 // health_rrdcalctemplate2json_nolock(wb, rt);
1955 buffer_strcat(wb, "\n\t}\n}\n");
1956 rrdhost_unlock(&localhost);
1960 // ----------------------------------------------------------------------------
1961 // re-load health configuration
1963 static inline void health_free_all_nolock(RRDHOST *host) {
1964 while(host->templates)
1965 rrdcalctemplate_free(host, host->templates);
1968 rrdcalc_free(host, host->alarms);
1971 void health_reload(void) {
1972 if(!health_enabled) {
1973 error("Health reload is requested, but health is not enabled.");
1977 char *path = health_config_dir();
1979 // free all running alarms
1980 rrdhost_rwlock(&localhost);
1981 health_free_all_nolock(&localhost);
1982 rrdhost_unlock(&localhost);
1984 // invalidate all previous entries in the alarm log
1986 for(t = localhost.health_log.alarms ; t ; t = t->next) {
1987 if(t->new_status != RRDCALC_STATUS_REMOVED)
1988 t->notifications |= HEALTH_ENTRY_NOTIFICATIONS_UPDATED;
1991 // reset all thresholds to all charts
1993 for(st = localhost.rrdset_root; st ; st = st->next) {
1998 // load the new alarms
1999 rrdhost_rwlock(&localhost);
2000 health_readdir(path);
2001 rrdhost_unlock(&localhost);
2003 // link the loaded alarms to their charts
2004 for(st = localhost.rrdset_root; st ; st = st->next) {
2005 rrdhost_rwlock(&localhost);
2007 rrdsetcalc_link_matching(st);
2008 rrdcalctemplate_link_matching(st);
2010 rrdhost_unlock(&localhost);
2015 // ----------------------------------------------------------------------------
2016 // health main thread and friends
2018 static inline int rrdcalc_value2status(calculated_number n) {
2019 if(isnan(n)) return RRDCALC_STATUS_UNDEFINED;
2020 if(n) return RRDCALC_STATUS_RAISED;
2021 return RRDCALC_STATUS_CLEAR;
2024 static inline void health_alarm_execute(RRDHOST *host, ALARM_ENTRY *ae) {
2025 ae->notifications |= HEALTH_ENTRY_NOTIFICATIONS_PROCESSED;
2027 // find the previous notification for the same alarm
2029 for(t = ae->next; t ;t = t->next) {
2030 if(t->alarm_id == ae->alarm_id && t->notifications & HEALTH_ENTRY_NOTIFICATIONS_PROCESSED)
2034 if(t && t->new_status == ae->new_status) {
2035 // don't send the same notification again
2036 info("Health not sending again notification for alarm '%s.%s' status %s", ae->chart, ae->name, rrdcalc_status2string(ae->new_status));
2040 if(ae->old_status == RRDCALC_STATUS_UNINITIALIZED && ae->new_status == RRDCALC_STATUS_CLEAR) {
2041 info("Health not sending notification for first initialization of alarm '%s.%s' status %s", ae->chart, ae->name, rrdcalc_status2string(ae->new_status));
2045 char buffer[FILENAME_MAX + 1];
2048 const char *exec = ae->exec;
2049 if(!exec) exec = health_default_exec;
2051 const char *recipient = ae->recipient;
2052 if(!recipient) recipient = health_default_recipient;
2054 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'",
2061 (unsigned long)ae->when,
2063 ae->chart?ae->chart:"NOCAHRT",
2064 ae->family?ae->family:"NOFAMILY",
2065 rrdcalc_status2string(ae->new_status),
2066 rrdcalc_status2string(ae->old_status),
2069 ae->source?ae->source:"UNKNOWN",
2070 (uint32_t)ae->duration,
2071 (uint32_t)ae->non_clear_duration,
2072 ae->units?ae->units:"",
2073 ae->info?ae->info:""
2076 ae->notifications |= HEALTH_ENTRY_NOTIFICATIONS_EXEC_RUN;
2077 ae->exec_run_timestamp = time(NULL);
2079 debug(D_HEALTH, "executing command '%s'", buffer);
2080 FILE *fp = mypopen(buffer, &command_pid);
2082 error("HEALTH: Cannot popen(\"%s\", \"r\").", buffer);
2085 debug(D_HEALTH, "HEALTH reading from command");
2086 char *s = fgets(buffer, FILENAME_MAX, fp);
2088 ae->exec_code = mypclose(fp, command_pid);
2089 debug(D_HEALTH, "done executing command - returned with code %d", ae->exec_code);
2091 if(ae->exec_code != 0)
2092 ae->notifications |= HEALTH_ENTRY_NOTIFICATIONS_EXEC_FAILED;
2095 static inline void health_process_notifications(RRDHOST *host, ALARM_ENTRY *ae) {
2096 info("Health alarm '%s.%s' = %0.2Lf - changed status from %s to %s",
2097 ae->chart?ae->chart:"NOCHART", ae->name,
2099 rrdcalc_status2string(ae->old_status),
2100 rrdcalc_status2string(ae->new_status)
2103 health_alarm_execute(host, ae);
2106 static inline void health_alarm_log_process(RRDHOST *host) {
2107 static uint32_t stop_at_id = 0;
2108 uint32_t first_waiting = (host->health_log.alarms)?host->health_log.alarms->unique_id:0;
2109 time_t now = time(NULL);
2111 pthread_rwlock_rdlock(&host->health_log.alarm_log_rwlock);
2114 for(ae = host->health_log.alarms; ae && ae->unique_id >= stop_at_id ; ae = ae->next) {
2116 !(ae->notifications & HEALTH_ENTRY_NOTIFICATIONS_PROCESSED) &&
2117 !(ae->notifications & HEALTH_ENTRY_NOTIFICATIONS_UPDATED)
2120 if(unlikely(ae->unique_id < first_waiting))
2121 first_waiting = ae->unique_id;
2123 if(likely(now > ae->delay_up_to_timestamp))
2124 health_process_notifications(host, ae);
2128 // remember this for the next iteration
2129 stop_at_id = first_waiting;
2131 pthread_rwlock_unlock(&host->health_log.alarm_log_rwlock);
2133 if(host->health_log.count <= host->health_log.max)
2136 // cleanup excess entries in the log
2137 pthread_rwlock_wrlock(&host->health_log.alarm_log_rwlock);
2139 ALARM_ENTRY *last = NULL;
2140 unsigned int count = host->health_log.max * 2 / 3;
2141 for(ae = host->health_log.alarms; ae && count ; count--, last = ae, ae = ae->next) ;
2143 if(ae && last && last->next == ae)
2149 debug(D_HEALTH, "Health removing alarm log entry with id: %u", ae->unique_id);
2151 ALARM_ENTRY *t = ae->next;
2157 freez(ae->recipient);
2164 host->health_log.count--;
2167 pthread_rwlock_unlock(&host->health_log.alarm_log_rwlock);
2170 static inline int rrdcalc_isrunnable(RRDCALC *rc, time_t now, time_t *next_run) {
2171 if (unlikely(!rc->rrdset)) {
2172 debug(D_HEALTH, "Health not running alarm '%s.%s'. It is not linked to a chart.", rc->chart?rc->chart:"NOCHART", rc->name);
2176 if (unlikely(!rc->rrdset->last_collected_time.tv_sec)) {
2177 debug(D_HEALTH, "Health not running alarm '%s.%s'. Chart is not yet collected.", rc->chart?rc->chart:"NOCHART", rc->name);
2181 if (unlikely(!rc->update_every)) {
2182 debug(D_HEALTH, "Health not running alarm '%s.%s'. It does not have an update frequency", rc->chart?rc->chart:"NOCHART", rc->name);
2186 if (unlikely(rc->next_update > now)) {
2187 if (unlikely(*next_run > rc->next_update))
2188 *next_run = rc->next_update;
2190 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));
2195 // we should check that the DB lookup is possible
2197 // - the duration of the chart includes the required timeframe
2198 // we SHOULD NOT check the dimensions - there might be alarms that refer non-existing dimensions (e.g. cpu steal)
2203 void *health_main(void *ptr) {
2206 info("HEALTH thread created with task id %d", gettid());
2208 if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
2209 error("Cannot set pthread cancel type to DEFERRED.");
2211 if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
2212 error("Cannot set pthread cancel state to ENABLE.");
2214 int min_run_every = (int)config_get_number("health", "run at least every seconds", 10);
2215 if(min_run_every < 1) min_run_every = 1;
2217 BUFFER *wb = buffer_create(100);
2219 unsigned int loop = 0;
2220 while(health_enabled) {
2222 debug(D_HEALTH, "Health monitoring iteration no %u started", loop);
2224 int oldstate, runnable = 0;
2225 time_t now = time(NULL);
2226 time_t next_run = now + min_run_every;
2229 if (unlikely(pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldstate) != 0))
2230 error("Cannot set pthread cancel state to DISABLE.");
2232 rrdhost_rdlock(&localhost);
2234 // the first loop is to lookup values from the db
2235 for (rc = localhost.alarms; rc; rc = rc->next) {
2236 if (unlikely(!rrdcalc_isrunnable(rc, now, &next_run)))
2240 rc->old_value = rc->value;
2242 // 1. if there is database lookup, do it
2243 // 2. if there is calculation expression, run it
2245 if (unlikely(RRDCALC_HAS_DB_LOOKUP(rc))) {
2246 time_t old_db_timestamp = rc->db_before;
2247 int value_is_null = 0;
2249 int ret = rrd2value(rc->rrdset, wb, &rc->value,
2250 rc->dimensions, 1, rc->after, rc->before, rc->group,
2251 rc->options, &rc->db_after, &rc->db_before, &value_is_null);
2253 if (unlikely(ret != 200)) {
2254 // database lookup failed
2257 debug(D_HEALTH, "Health alarm '%s.%s': database lookup returned error %d", rc->chart?rc->chart:"NOCHART", rc->name, ret);
2259 if (unlikely(!(rc->rrdcalc_flags & RRDCALC_FLAG_DB_ERROR))) {
2260 rc->rrdcalc_flags |= RRDCALC_FLAG_DB_ERROR;
2261 error("Health alarm '%s.%s': database lookup returned error %d", rc->chart?rc->chart:"NOCHART", rc->name, ret);
2264 else if (unlikely(rc->rrdcalc_flags & RRDCALC_FLAG_DB_ERROR))
2265 rc->rrdcalc_flags &= ~RRDCALC_FLAG_DB_ERROR;
2267 if (unlikely(old_db_timestamp == rc->db_before)) {
2268 // database is stale
2270 debug(D_HEALTH, "Health alarm '%s.%s': database is stale", rc->chart?rc->chart:"NOCHART", rc->name);
2272 if (unlikely(!(rc->rrdcalc_flags & RRDCALC_FLAG_DB_STALE))) {
2273 rc->rrdcalc_flags |= RRDCALC_FLAG_DB_STALE;
2274 error("Health alarm '%s.%s': database is stale", rc->chart?rc->chart:"NOCHART", rc->name);
2277 else if (unlikely(rc->rrdcalc_flags & RRDCALC_FLAG_DB_STALE))
2278 rc->rrdcalc_flags &= ~RRDCALC_FLAG_DB_STALE;
2280 if (unlikely(value_is_null)) {
2281 // collected value is null
2285 debug(D_HEALTH, "Health alarm '%s.%s': database lookup returned empty value (possibly value is not collected yet)",
2286 rc->chart?rc->chart:"NOCHART", rc->name);
2288 if (unlikely(!(rc->rrdcalc_flags & RRDCALC_FLAG_DB_NAN))) {
2289 rc->rrdcalc_flags |= RRDCALC_FLAG_DB_NAN;
2290 error("Health alarm '%s.%s': database lookup returned empty value (possibly value is not collected yet)",
2291 rc->chart?rc->chart:"NOCHART", rc->name);
2294 else if (unlikely(rc->rrdcalc_flags & RRDCALC_FLAG_DB_NAN))
2295 rc->rrdcalc_flags &= ~RRDCALC_FLAG_DB_NAN;
2297 debug(D_HEALTH, "Health alarm '%s.%s': database lookup gave value "
2298 CALCULATED_NUMBER_FORMAT, rc->chart?rc->chart:"NOCHART", rc->name, rc->value);
2301 if(unlikely(rc->calculation)) {
2302 if (unlikely(!expression_evaluate(rc->calculation))) {
2303 // calculation failed
2307 debug(D_HEALTH, "Health alarm '%s.%s': failed to evaluate calculation with error: %s",
2308 rc->chart?rc->chart:"NOCHART", rc->name, buffer_tostring(rc->calculation->error_msg));
2310 if (unlikely(!(rc->rrdcalc_flags & RRDCALC_FLAG_CALC_ERROR))) {
2311 rc->rrdcalc_flags |= RRDCALC_FLAG_CALC_ERROR;
2312 error("Health alarm '%s.%s': failed to evaluate calculation with error: %s",
2313 rc->chart?rc->chart:"NOCHART", rc->name, buffer_tostring(rc->calculation->error_msg));
2317 if (unlikely(rc->rrdcalc_flags & RRDCALC_FLAG_CALC_ERROR))
2318 rc->rrdcalc_flags &= ~RRDCALC_FLAG_CALC_ERROR;
2320 debug(D_HEALTH, "Health alarm '%s.%s': calculation expression gave value "
2321 CALCULATED_NUMBER_FORMAT
2322 ": %s (source: %s)",
2323 rc->chart?rc->chart:"NOCHART", rc->name,
2324 rc->calculation->result,
2325 buffer_tostring(rc->calculation->error_msg),
2329 rc->value = rc->calculation->result;
2333 rrdhost_unlock(&localhost);
2336 rrdhost_rdlock(&localhost);
2338 for (rc = localhost.alarms; rc; rc = rc->next) {
2339 if (unlikely(!rrdcalc_isrunnable(rc, now, &next_run)))
2342 int warning_status = RRDCALC_STATUS_UNDEFINED;
2343 int critical_status = RRDCALC_STATUS_UNDEFINED;
2345 if(likely(rc->warning)) {
2346 if(unlikely(!expression_evaluate(rc->warning))) {
2347 // calculation failed
2349 debug(D_HEALTH, "Health alarm '%s.%s': warning expression failed with error: %s",
2350 rc->chart?rc->chart:"NOCHART", rc->name, buffer_tostring(rc->warning->error_msg));
2352 if (unlikely(!(rc->rrdcalc_flags & RRDCALC_FLAG_WARN_ERROR))) {
2353 rc->rrdcalc_flags |= RRDCALC_FLAG_WARN_ERROR;
2354 error("Health alarm '%s.%s': warning expression failed with error: %s",
2355 rc->chart?rc->chart:"NOCHART", rc->name, buffer_tostring(rc->warning->error_msg));
2359 if(unlikely(rc->rrdcalc_flags & RRDCALC_FLAG_WARN_ERROR))
2360 rc->rrdcalc_flags &= ~RRDCALC_FLAG_WARN_ERROR;
2362 debug(D_HEALTH, "Health alarm '%s.%s': warning expression gave value "
2363 CALCULATED_NUMBER_FORMAT
2364 ": %s (source: %s)",
2365 rc->chart?rc->chart:"NOCHART", rc->name,
2366 rc->warning->result,
2367 buffer_tostring(rc->warning->error_msg),
2371 warning_status = rrdcalc_value2status(rc->warning->result);
2375 if(likely(rc->critical)) {
2376 if(unlikely(!expression_evaluate(rc->critical))) {
2377 // calculation failed
2379 debug(D_HEALTH, "Health alarm '%s.%s': critical expression failed with error: %s",
2380 rc->chart?rc->chart:"NOCHART", rc->name, buffer_tostring(rc->critical->error_msg));
2382 if (unlikely(!(rc->rrdcalc_flags & RRDCALC_FLAG_CRIT_ERROR))) {
2383 rc->rrdcalc_flags |= RRDCALC_FLAG_CRIT_ERROR;
2384 error("Health alarm '%s.%s': critical expression failed with error: %s",
2385 rc->chart?rc->chart:"NOCHART", rc->name, buffer_tostring(rc->critical->error_msg));
2389 if(unlikely(rc->rrdcalc_flags & RRDCALC_FLAG_CRIT_ERROR))
2390 rc->rrdcalc_flags &= ~RRDCALC_FLAG_CRIT_ERROR;
2392 debug(D_HEALTH, "Health alarm '%s.%s': critical expression gave value "
2393 CALCULATED_NUMBER_FORMAT
2394 ": %s (source: %s)",
2395 rc->chart?rc->chart:"NOCHART", rc->name,
2396 rc->critical->result,
2397 buffer_tostring(rc->critical->error_msg),
2401 critical_status = rrdcalc_value2status(rc->critical->result);
2405 int status = RRDCALC_STATUS_UNDEFINED;
2407 switch(warning_status) {
2408 case RRDCALC_STATUS_CLEAR:
2409 status = RRDCALC_STATUS_CLEAR;
2412 case RRDCALC_STATUS_RAISED:
2413 status = RRDCALC_STATUS_WARNING;
2420 switch(critical_status) {
2421 case RRDCALC_STATUS_CLEAR:
2422 if(status == RRDCALC_STATUS_UNDEFINED)
2423 status = RRDCALC_STATUS_CLEAR;
2426 case RRDCALC_STATUS_RAISED:
2427 status = RRDCALC_STATUS_CRITICAL;
2434 if(status != rc->status) {
2437 if(now > rc->delay_up_to_timestamp) {
2438 rc->delay_up_current = rc->delay_up_duration;
2439 rc->delay_down_current = rc->delay_down_duration;
2441 rc->delay_up_to_timestamp = 0;
2444 rc->delay_up_current = (int)(rc->delay_up_current * rc->delay_multiplier);
2445 if(rc->delay_up_current > rc->delay_max_duration) rc->delay_up_current = rc->delay_max_duration;
2447 rc->delay_down_current = (int)(rc->delay_down_current * rc->delay_multiplier);
2448 if(rc->delay_down_current > rc->delay_max_duration) rc->delay_down_current = rc->delay_max_duration;
2451 if(status > rc->status)
2452 delay = rc->delay_up_current;
2454 delay = rc->delay_down_current;
2456 // COMMENTED: because we do need to send raising alarms
2457 // if(now + delay < rc->delay_up_to_timestamp)
2458 // delay = (int)(rc->delay_up_to_timestamp - now);
2460 rc->delay_last = delay;
2461 rc->delay_up_to_timestamp = now + delay;
2462 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);
2463 rc->last_status_change = now;
2464 rc->status = status;
2467 rc->last_updated = now;
2468 rc->next_update = now + rc->update_every;
2470 if (next_run > rc->next_update)
2471 next_run = rc->next_update;
2474 rrdhost_unlock(&localhost);
2477 if (unlikely(pthread_setcancelstate(oldstate, NULL) != 0))
2478 error("Cannot set pthread cancel state to RESTORE (%d).", oldstate);
2480 // execute notifications
2482 health_alarm_log_process(&localhost);
2485 if(now < next_run) {
2486 debug(D_HEALTH, "Health monitoring iteration no %u done. Next iteration in %d secs",
2487 loop, (int) (next_run - now));
2488 sleep_usec(1000000 * (unsigned long long) (next_run - now));
2491 debug(D_HEALTH, "Health monitoring iteration no %u done. Next iteration now", loop);
2497 info("HEALTH thread exiting");