]> arthur.barton.de Git - netdata.git/blob - src/health.c
Fix occasional crashes in python plugins
[netdata.git] / src / health.c
1 #include "common.h"
2
3 #define RRDVAR_MAX_LENGTH 1024
4
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;
10     FILE *log_fp;
11 };
12
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,
18     .log_fp = NULL
19 };
20
21 int health_enabled = 1;
22
23 // ----------------------------------------------------------------------------
24 // health alarm log load/save
25 // no need for locking - only one thread is reading / writing the alarms log
26
27 static inline int health_alarm_log_open(void) {
28     if(health.log_fp)
29         fclose(health.log_fp);
30
31     health.log_fp = fopen(health.log_filename, "a");
32
33     if(health.log_fp) {
34         if (setvbuf(health.log_fp, NULL, _IOLBF, 0) != 0)
35             error("Health: cannot set line buffering on health log file.");
36         return 0;
37     }
38
39     error("Health: cannot open health log file '%s'. Health data will be lost in case of netdata or server crash.", health.log_filename);
40     return -1;
41 }
42
43 static inline void health_alarm_log_close(void) {
44     if(health.log_fp) {
45         fclose(health.log_fp);
46         health.log_fp = NULL;
47     }
48 }
49
50 static inline void health_log_rotate(void) {
51     static size_t rotate_every = 0;
52
53     if(unlikely(rotate_every == 0)) {
54         rotate_every = (size_t)config_get_number("health", "rotate log every lines", 2000);
55         if(rotate_every < 100) rotate_every = 100;
56     }
57
58     if(unlikely(health.log_entries_written > rotate_every)) {
59         health_alarm_log_close();
60
61         char old_filename[FILENAME_MAX + 1];
62         snprintfz(old_filename, FILENAME_MAX, "%s.old", health.log_filename);
63
64         if(unlink(old_filename) == -1 && errno != ENOENT)
65             error("Health: cannot remove old alarms log file '%s'", old_filename);
66
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);
69
70         if(unlink(health.log_filename) == -1 && errno != ENOENT)
71             error("Health: cannot remove old alarms log file '%s'", health.log_filename);
72
73         // open it with truncate
74         health.log_fp = fopen(health.log_filename, "w");
75
76         if(health.log_fp)
77             fclose(health.log_fp);
78         else
79             error("Health: cannot truncate health log '%s'", health.log_filename);
80
81         health.log_fp = NULL;
82
83         health.log_entries_written = 0;
84         health_alarm_log_open();
85     }
86 }
87
88 static inline void health_alarm_log_save(RRDHOST *host, ALARM_ENTRY *ae) {
89     health_log_rotate();
90
91     if(likely(health.log_fp)) {
92         if(unlikely(fprintf(health.log_fp
93                 , "%c\t%s"
94                   "\t%08x\t%08x\t%08x\t%08x\t%08x"
95                   "\t%08x\t%08x\t%08x"
96                   "\t%08x\t%08x\t%08x"
97                   "\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s"
98                   "\t%d\t%d\t%d\t%d"
99                   "\t%Lf\t%Lf"
100                   "\n"
101                 , (ae->flags & HEALTH_ENTRY_FLAG_SAVED)?'U':'A'
102                 , host->hostname
103
104                 , ae->unique_id
105                 , ae->alarm_id
106                 , ae->alarm_event_id
107                 , ae->updated_by_id
108                 , ae->updates_id
109
110                 , (uint32_t)ae->when
111                 , (uint32_t)ae->duration
112                 , (uint32_t)ae->non_clear_duration
113                 , (uint32_t)ae->flags
114                 , (uint32_t)ae->exec_run_timestamp
115                 , (uint32_t)ae->delay_up_to_timestamp
116
117                 , (ae->name)?ae->name:""
118                 , (ae->chart)?ae->chart:""
119                 , (ae->family)?ae->family:""
120                 , (ae->exec)?ae->exec:""
121                 , (ae->recipient)?ae->recipient:""
122                 , (ae->source)?ae->source:""
123                 , (ae->units)?ae->units:""
124                 , (ae->info)?ae->info:""
125
126                 , ae->exec_code
127                 , ae->new_status
128                 , ae->old_status
129                 , ae->delay
130
131                 , (long double)ae->new_value
132                 , (long double)ae->old_value
133         ) < 0))
134             error("Health: failed to save alarm log entry. Health data may be lost in case of abnormal restart.");
135         else {
136             ae->flags |= HEALTH_ENTRY_FLAG_SAVED;
137             health.log_entries_written++;
138         }
139     }
140 }
141
142 static inline ssize_t health_alarm_log_read(RRDHOST *host, FILE *fp, const char *filename) {
143     static uint32_t max_unique_id = 0, max_alarm_id = 0;
144     ssize_t loaded = -1, updated = -1, errored = -1, duplicate = -1;
145
146     errno = 0;
147
148     char *s, *buf = mallocz(65536 + 1);
149     size_t line = 0, len = 0;
150     loaded = updated = errored = duplicate = 0;
151
152     pthread_rwlock_rdlock(&host->health_log.alarm_log_rwlock);
153
154     while((s = fgets_trim_len(buf, 65536, fp, &len))) {
155         health.log_entries_written++;
156         line++;
157
158         int max_entries = 30, entries = 0;
159         char *pointers[max_entries];
160
161         pointers[entries++] = s++;
162         while(*s) {
163             if(unlikely(*s == '\t')) {
164                 *s = '\0';
165                 pointers[entries++] = ++s;
166                 if(entries >= max_entries) {
167                     error("Health: line %zu of file '%s' has more than %d entries. Ignoring excessive entries.", line, filename, max_entries);
168                     break;
169                 }
170             }
171             else s++;
172         }
173
174         if(likely(*pointers[0] == 'U' || *pointers[0] == 'A')) {
175             ALARM_ENTRY *ae = NULL;
176
177             if(entries < 26) {
178                 error("Health: line %zu of file '%s' should have at least 26 entries, but it has %d. Ignoring it.", line, filename, entries);
179                 errored++;
180                 continue;
181             }
182
183             // check that we have valid ids
184             uint32_t unique_id = (uint32_t)strtoul(pointers[2], NULL, 16);
185             if(!unique_id) {
186                 error("Health: line %zu of file '%s' states alarm entry with invalid unique id %u (%s). Ignoring it.", line, filename, unique_id, pointers[2]);
187                 errored++;
188                 continue;
189             }
190
191             uint32_t alarm_id = (uint32_t)strtoul(pointers[3], NULL, 16);
192             if(!alarm_id) {
193                 error("Health: line %zu of file '%s' states alarm entry for invalid alarm id %u (%s). Ignoring it.", line, filename, alarm_id, pointers[3]);
194                 errored++;
195                 continue;
196             }
197
198             if(unlikely(*pointers[0] == 'A')) {
199                 // make sure it is properly numbered
200                 if(unlikely(host->health_log.alarms && unique_id < host->health_log.alarms->unique_id)) {
201                     error("Health: line %zu of file '%s' has alarm log entry with %u in wrong order. Ignoring it.", line, filename, unique_id);
202                     errored++;
203                     continue;
204                 }
205
206                 ae = callocz(1, sizeof(ALARM_ENTRY));
207             }
208             else if(unlikely(*pointers[0] == 'U')) {
209                 // find the original
210                 for(ae = host->health_log.alarms; ae; ae = ae->next) {
211                     if(unlikely(unique_id == ae->unique_id)) {
212                         if(unlikely(*pointers[0] == 'A')) {
213                             error("Health: line %zu of file '%s' adds duplicate alarm log entry with unique id %u. Using the later."
214                                   , line, filename, unique_id);
215                             *pointers[0] = 'U';
216                             duplicate++;
217                         }
218                         break;
219                     }
220                     else if(unlikely(unique_id > ae->unique_id)) {
221                         // no need to continue
222                         // the linked list is sorted
223                         ae = NULL;
224                         break;
225                     }
226                 }
227
228                 // if not found, skip this line
229                 if(!ae) {
230                     // error("Health: line %zu of file '%s' updates alarm log entry with unique id %u, but it is not found.", line, filename, unique_id);
231                     continue;
232                 }
233             }
234
235             // check for a possible host missmatch
236             //if(strcmp(pointers[1], host->hostname))
237             //    error("Health: line %zu of file '%s' provides an alarm for host '%s' but this is named '%s'.", line, filename, pointers[1], host->hostname);
238
239             ae->unique_id               = unique_id;
240             ae->alarm_id                = alarm_id;
241             ae->alarm_event_id          = (uint32_t)strtoul(pointers[4], NULL, 16);
242             ae->updated_by_id           = (uint32_t)strtoul(pointers[5], NULL, 16);
243             ae->updates_id              = (uint32_t)strtoul(pointers[6], NULL, 16);
244
245             ae->when                    = (uint32_t)strtoul(pointers[7], NULL, 16);
246             ae->duration                = (uint32_t)strtoul(pointers[8], NULL, 16);
247             ae->non_clear_duration      = (uint32_t)strtoul(pointers[9], NULL, 16);
248
249             ae->flags                   = (uint32_t)strtoul(pointers[10], NULL, 16);
250             ae->flags |= HEALTH_ENTRY_FLAG_SAVED;
251
252             ae->exec_run_timestamp      = (uint32_t)strtoul(pointers[11], NULL, 16);
253             ae->delay_up_to_timestamp   = (uint32_t)strtoul(pointers[12], NULL, 16);
254
255             if(unlikely(ae->name)) freez(ae->name);
256             ae->name = strdupz(pointers[13]);
257             ae->hash_name = simple_hash(ae->name);
258
259             if(unlikely(ae->chart)) freez(ae->chart);
260             ae->chart = strdupz(pointers[14]);
261             ae->hash_chart = simple_hash(ae->chart);
262
263             if(unlikely(ae->family)) freez(ae->family);
264             ae->family = strdupz(pointers[15]);
265
266             if(unlikely(ae->exec)) freez(ae->exec);
267             ae->exec = strdupz(pointers[16]);
268             if(!*ae->exec) { freez(ae->exec); ae->exec = NULL; }
269
270             if(unlikely(ae->recipient)) freez(ae->recipient);
271             ae->recipient = strdupz(pointers[17]);
272             if(!*ae->recipient) { freez(ae->recipient); ae->recipient = NULL; }
273
274             if(unlikely(ae->source)) freez(ae->source);
275             ae->source = strdupz(pointers[18]);
276             if(!*ae->source) { freez(ae->source); ae->source = NULL; }
277
278             if(unlikely(ae->units)) freez(ae->units);
279             ae->units = strdupz(pointers[19]);
280             if(!*ae->units) { freez(ae->units); ae->units = NULL; }
281
282             if(unlikely(ae->info)) freez(ae->info);
283             ae->info = strdupz(pointers[20]);
284             if(!*ae->info) { freez(ae->info); ae->info = NULL; }
285
286             ae->exec_code   = atoi(pointers[21]);
287             ae->new_status  = atoi(pointers[22]);
288             ae->old_status  = atoi(pointers[23]);
289             ae->delay       = atoi(pointers[24]);
290
291             ae->new_value   = strtold(pointers[25], NULL);
292             ae->old_value   = strtold(pointers[26], NULL);
293
294             // add it to host if not already there
295             if(unlikely(*pointers[0] == 'A')) {
296                 ae->next = host->health_log.alarms;
297                 host->health_log.alarms = ae;
298                 loaded++;
299             }
300             else updated++;
301
302             if(unlikely(ae->unique_id > max_unique_id))
303                 max_unique_id = ae->unique_id;
304
305             if(unlikely(ae->alarm_id >= max_alarm_id))
306                 max_alarm_id = ae->alarm_id;
307         }
308         else {
309             error("Health: line %zu of file '%s' is invalid (unrecognized entry type '%s').", line, filename, pointers[0]);
310             errored++;
311         }
312     }
313
314     pthread_rwlock_unlock(&host->health_log.alarm_log_rwlock);
315
316     freez(buf);
317
318     if(!max_unique_id) max_unique_id = (uint32_t)time(NULL);
319     if(!max_alarm_id)  max_alarm_id  = (uint32_t)time(NULL);
320
321     host->health_log.next_log_id = max_unique_id + 1;
322     host->health_log.next_alarm_id = max_alarm_id + 1;
323
324     debug(D_HEALTH, "Health: loaded file '%s' with %zd new alarm entries, updated %zd alarms, errors %zd entries, duplicate %zd", filename, loaded, updated, errored, duplicate);
325     return loaded;
326 }
327
328 static inline void health_alarm_log_load(RRDHOST *host) {
329     health_alarm_log_close();
330
331     char filename[FILENAME_MAX + 1];
332     snprintfz(filename, FILENAME_MAX, "%s.old", health.log_filename);
333     FILE *fp = fopen(filename, "r");
334     if(!fp)
335         error("Health: cannot open health file: %s", filename);
336     else {
337         health_alarm_log_read(host, fp, filename);
338         fclose(fp);
339     }
340
341     health.log_entries_written = 0;
342     fp = fopen(health.log_filename, "r");
343     if(!fp)
344         error("Health: cannot open health file: %s", health.log_filename);
345     else {
346         health_alarm_log_read(host, fp, health.log_filename);
347         fclose(fp);
348     }
349
350     health_alarm_log_open();
351 }
352
353
354 // ----------------------------------------------------------------------------
355 // health alarm log management
356
357 static inline void health_alarm_log(RRDHOST *host,
358                 uint32_t alarm_id, uint32_t alarm_event_id,
359                 time_t when,
360                 const char *name, const char *chart, const char *family,
361                 const char *exec, const char *recipient, time_t duration,
362                 calculated_number old_value, calculated_number new_value,
363                 int old_status, int new_status,
364                 const char *source,
365                 const char *units,
366                 const char *info,
367                 int delay
368 ) {
369     debug(D_HEALTH, "Health adding alarm log entry with id: %u", host->health_log.next_log_id);
370
371     ALARM_ENTRY *ae = callocz(1, sizeof(ALARM_ENTRY));
372     ae->name = strdupz(name);
373     ae->hash_name = simple_hash(ae->name);
374
375     if(chart) {
376         ae->chart = strdupz(chart);
377         ae->hash_chart = simple_hash(ae->chart);
378     }
379
380     if(family)
381         ae->family = strdupz(family);
382
383     if(exec) ae->exec = strdupz(exec);
384     if(recipient) ae->recipient = strdupz(recipient);
385     if(source) ae->source = strdupz(source);
386     if(units) ae->units = strdupz(units);
387     if(info) ae->info = strdupz(info);
388
389     ae->unique_id = host->health_log.next_log_id++;
390     ae->alarm_id = alarm_id;
391     ae->alarm_event_id = alarm_event_id;
392     ae->when = when;
393     ae->old_value = old_value;
394     ae->new_value = new_value;
395     ae->old_status = old_status;
396     ae->new_status = new_status;
397     ae->duration = duration;
398     ae->delay = delay;
399     ae->delay_up_to_timestamp = when + delay;
400
401     if(ae->old_status == RRDCALC_STATUS_WARNING || ae->old_status == RRDCALC_STATUS_CRITICAL)
402         ae->non_clear_duration += ae->duration;
403
404     // link it
405     pthread_rwlock_wrlock(&host->health_log.alarm_log_rwlock);
406     ae->next = host->health_log.alarms;
407     host->health_log.alarms = ae;
408     host->health_log.count++;
409     pthread_rwlock_unlock(&host->health_log.alarm_log_rwlock);
410
411     // match previous alarms
412     pthread_rwlock_rdlock(&host->health_log.alarm_log_rwlock);
413     ALARM_ENTRY *t;
414     for(t = host->health_log.alarms ; t ; t = t->next) {
415         if(t != ae && t->alarm_id == ae->alarm_id) {
416             if(!(t->flags & HEALTH_ENTRY_FLAG_UPDATED) && !t->updated_by_id) {
417                 t->flags |= HEALTH_ENTRY_FLAG_UPDATED;
418                 t->updated_by_id = ae->unique_id;
419                 ae->updates_id = t->unique_id;
420
421                 if((t->new_status == RRDCALC_STATUS_WARNING || t->new_status == RRDCALC_STATUS_CRITICAL) &&
422                    (t->old_status == RRDCALC_STATUS_WARNING || t->old_status == RRDCALC_STATUS_CRITICAL))
423                     ae->non_clear_duration += t->non_clear_duration;
424
425                 health_alarm_log_save(host, t);
426             }
427
428             // no need to continue
429             break;
430         }
431     }
432     pthread_rwlock_unlock(&host->health_log.alarm_log_rwlock);
433
434     health_alarm_log_save(host, ae);
435 }
436
437 // ----------------------------------------------------------------------------
438 // RRDVAR management
439
440 static inline int rrdvar_fix_name(char *variable) {
441     int fixed = 0;
442     while(*variable) {
443         if (!isalnum(*variable) && *variable != '.' && *variable != '_') {
444             *variable++ = '_';
445             fixed++;
446         }
447         else
448             variable++;
449     }
450
451     return fixed;
452 }
453
454 int rrdvar_compare(void* a, void* b) {
455     if(((RRDVAR *)a)->hash < ((RRDVAR *)b)->hash) return -1;
456     else if(((RRDVAR *)a)->hash > ((RRDVAR *)b)->hash) return 1;
457     else return strcmp(((RRDVAR *)a)->name, ((RRDVAR *)b)->name);
458 }
459
460 static inline RRDVAR *rrdvar_index_add(avl_tree_lock *tree, RRDVAR *rv) {
461     RRDVAR *ret = (RRDVAR *)avl_insert_lock(tree, (avl *)(rv));
462     if(ret != rv)
463         debug(D_VARIABLES, "Request to insert RRDVAR '%s' into index failed. Already exists.", rv->name);
464
465     return ret;
466 }
467
468 static inline RRDVAR *rrdvar_index_del(avl_tree_lock *tree, RRDVAR *rv) {
469     RRDVAR *ret = (RRDVAR *)avl_remove_lock(tree, (avl *)(rv));
470     if(!ret)
471         error("Request to remove RRDVAR '%s' from index failed. Not Found.", rv->name);
472
473     return ret;
474 }
475
476 static inline RRDVAR *rrdvar_index_find(avl_tree_lock *tree, const char *name, uint32_t hash) {
477     RRDVAR tmp;
478     tmp.name = (char *)name;
479     tmp.hash = (hash)?hash:simple_hash(tmp.name);
480
481     return (RRDVAR *)avl_search_lock(tree, (avl *)&tmp);
482 }
483
484 static inline void rrdvar_free(RRDHOST *host, avl_tree_lock *tree, RRDVAR *rv) {
485     (void)host;
486
487     if(!rv) return;
488
489     if(tree)
490         rrdvar_index_del(tree, rv);
491
492     freez(rv->name);
493     freez(rv);
494 }
495
496 static inline RRDVAR *rrdvar_create_and_index(const char *scope, avl_tree_lock *tree, const char *name, int type, calculated_number *value) {
497     char *variable = strdupz(name);
498     rrdvar_fix_name(variable);
499     uint32_t hash = simple_hash(variable);
500
501     RRDVAR *rv = rrdvar_index_find(tree, variable, hash);
502     if(unlikely(!rv)) {
503         debug(D_VARIABLES, "Variable '%s' not found in scope '%s'. Creating a new one.", variable, scope);
504
505         rv = callocz(1, sizeof(RRDVAR));
506         rv->name = variable;
507         rv->hash = hash;
508         rv->type = type;
509         rv->value = value;
510
511         RRDVAR *ret = rrdvar_index_add(tree, rv);
512         if(unlikely(ret != rv)) {
513             debug(D_VARIABLES, "Variable '%s' in scope '%s' already exists", variable, scope);
514             rrdvar_free(NULL, NULL, rv);
515             rv = NULL;
516         }
517         else
518             debug(D_VARIABLES, "Variable '%s' created in scope '%s'", variable, scope);
519     }
520     else {
521         // already exists
522         freez(variable);
523         rv = NULL;
524     }
525
526     return rv;
527 }
528
529 // ----------------------------------------------------------------------------
530 // RRDVAR lookup
531
532 calculated_number rrdvar2number(RRDVAR *rv) {
533     switch(rv->type) {
534         case RRDVAR_TYPE_CALCULATED: {
535             calculated_number *n = (calculated_number *)rv->value;
536             return *n;
537         }
538
539         case RRDVAR_TYPE_TIME_T: {
540             time_t *n = (time_t *)rv->value;
541             return *n;
542         }
543
544         case RRDVAR_TYPE_COLLECTED: {
545             collected_number *n = (collected_number *)rv->value;
546             return *n;
547         }
548
549         case RRDVAR_TYPE_TOTAL: {
550             total_number *n = (total_number *)rv->value;
551             return *n;
552         }
553
554         case RRDVAR_TYPE_INT: {
555             int *n = (int *)rv->value;
556             return *n;
557         }
558
559         default:
560             error("I don't know how to convert RRDVAR type %d to calculated_number", rv->type);
561             return NAN;
562     }
563 }
564
565 void dump_variable(void *data) {
566     RRDVAR *rv = (RRDVAR *)data;
567     debug(D_HEALTH, "%50s : %20.5Lf", rv->name, rrdvar2number(rv));
568 }
569
570 int health_variable_lookup(const char *variable, uint32_t hash, RRDCALC *rc, calculated_number *result) {
571     RRDSET *st = rc->rrdset;
572     RRDVAR *rv;
573
574     if(!st) return 0;
575
576     rv = rrdvar_index_find(&st->variables_root_index, variable, hash);
577     if(rv) {
578         *result = rrdvar2number(rv);
579         return 1;
580     }
581
582     rv = rrdvar_index_find(&st->rrdfamily->variables_root_index, variable, hash);
583     if(rv) {
584         *result = rrdvar2number(rv);
585         return 1;
586     }
587
588     rv = rrdvar_index_find(&st->rrdhost->variables_root_index, variable, hash);
589     if(rv) {
590         *result = rrdvar2number(rv);
591         return 1;
592     }
593
594     debug(D_HEALTH, "Available local chart '%s' variables:", st->id);
595     avl_traverse_lock(&st->variables_root_index, dump_variable);
596
597     debug(D_HEALTH, "Available family '%s' variables:", st->rrdfamily->family);
598     avl_traverse_lock(&st->rrdfamily->variables_root_index, dump_variable);
599
600     debug(D_HEALTH, "Available host '%s' variables:", st->rrdhost->hostname);
601     avl_traverse_lock(&st->rrdhost->variables_root_index, dump_variable);
602
603     return 0;
604 }
605
606 // ----------------------------------------------------------------------------
607 // RRDSETVAR management
608
609 RRDSETVAR *rrdsetvar_create(RRDSET *st, const char *variable, int type, void *value, uint32_t options) {
610     debug(D_VARIABLES, "RRDVARSET create for chart id '%s' name '%s' with variable name '%s'", st->id, st->name, variable);
611     RRDSETVAR *rs = (RRDSETVAR *)callocz(1, sizeof(RRDSETVAR));
612
613     char buffer[RRDVAR_MAX_LENGTH + 1];
614     snprintfz(buffer, RRDVAR_MAX_LENGTH, "%s.%s", st->id, variable);
615     rs->fullid = strdupz(buffer);
616
617     snprintfz(buffer, RRDVAR_MAX_LENGTH, "%s.%s", st->name, variable);
618     rs->fullname = strdupz(buffer);
619
620     rs->variable = strdupz(variable);
621
622     rs->type = type;
623     rs->value = value;
624     rs->options = options;
625     rs->rrdset = st;
626
627     rs->local       = rrdvar_create_and_index("local",  &st->variables_root_index, rs->variable, rs->type, rs->value);
628     rs->family      = rrdvar_create_and_index("family", &st->rrdfamily->variables_root_index, rs->fullid, rs->type, rs->value);
629     rs->host        = rrdvar_create_and_index("host",   &st->rrdhost->variables_root_index, rs->fullid, rs->type, rs->value);
630     rs->family_name = rrdvar_create_and_index("family", &st->rrdfamily->variables_root_index, rs->fullname, rs->type, rs->value);
631     rs->host_name   = rrdvar_create_and_index("host",   &st->rrdhost->variables_root_index, rs->fullname, rs->type, rs->value);
632
633     rs->next = st->variables;
634     st->variables = rs;
635
636     return rs;
637 }
638
639 void rrdsetvar_rename_all(RRDSET *st) {
640     debug(D_VARIABLES, "RRDSETVAR rename for chart id '%s' name '%s'", st->id, st->name);
641
642     // only these 2 can change name
643     // rs->family_name
644     // rs->host_name
645
646     char buffer[RRDVAR_MAX_LENGTH + 1];
647     RRDSETVAR *rs, *next = st->variables;
648     while((rs = next)) {
649         next = rs->next;
650
651         snprintfz(buffer, RRDVAR_MAX_LENGTH, "%s.%s", st->name, rs->variable);
652
653         if (strcmp(buffer, rs->fullname)) {
654             // name changed
655             rrdvar_free(st->rrdhost, &st->rrdfamily->variables_root_index, rs->family_name);
656             rrdvar_free(st->rrdhost, &st->rrdhost->variables_root_index, rs->host_name);
657
658             freez(rs->fullname);
659             rs->fullname = strdupz(st->name);
660             rs->family_name = rrdvar_create_and_index("family", &st->rrdfamily->variables_root_index, rs->fullname, rs->type, rs->value);
661             rs->host_name   = rrdvar_create_and_index("host",   &st->rrdhost->variables_root_index, rs->fullname, rs->type, rs->value);
662         }
663     }
664
665     rrdsetcalc_link_matching(st);
666 }
667
668 void rrdsetvar_free(RRDSETVAR *rs) {
669     RRDSET *st = rs->rrdset;
670     debug(D_VARIABLES, "RRDSETVAR free for chart id '%s' name '%s', variable '%s'", st->id, st->name, rs->variable);
671
672     rrdvar_free(st->rrdhost, &st->variables_root_index, rs->local);
673     rrdvar_free(st->rrdhost, &st->rrdfamily->variables_root_index, rs->family);
674     rrdvar_free(st->rrdhost, &st->rrdhost->variables_root_index, rs->host);
675     rrdvar_free(st->rrdhost, &st->rrdfamily->variables_root_index, rs->family_name);
676     rrdvar_free(st->rrdhost, &st->rrdhost->variables_root_index, rs->host_name);
677
678     if(st->variables == rs) {
679         st->variables = rs->next;
680     }
681     else {
682         RRDSETVAR *t;
683         for (t = st->variables; t && t->next != rs; t = t->next);
684         if(!t) error("RRDSETVAR '%s' not found in chart '%s' variables linked list", rs->fullname, st->id);
685         else t->next = rs->next;
686     }
687
688     freez(rs->fullid);
689     freez(rs->fullname);
690     freez(rs->variable);
691     freez(rs);
692 }
693
694 // ----------------------------------------------------------------------------
695 // RRDDIMVAR management
696
697 #define RRDDIMVAR_ID_MAX 1024
698
699 RRDDIMVAR *rrddimvar_create(RRDDIM *rd, int type, const char *prefix, const char *suffix, void *value, uint32_t options) {
700     RRDSET *st = rd->rrdset;
701
702     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:"");
703
704     if(!prefix) prefix = "";
705     if(!suffix) suffix = "";
706
707     char buffer[RRDDIMVAR_ID_MAX + 1];
708     RRDDIMVAR *rs = (RRDDIMVAR *)callocz(1, sizeof(RRDDIMVAR));
709
710     rs->prefix = strdupz(prefix);
711     rs->suffix = strdupz(suffix);
712
713     snprintfz(buffer, RRDDIMVAR_ID_MAX, "%s%s%s", rs->prefix, rd->id, rs->suffix);
714     rs->id = strdupz(buffer);
715
716     snprintfz(buffer, RRDDIMVAR_ID_MAX, "%s%s%s", rs->prefix, rd->name, rs->suffix);
717     rs->name = strdupz(buffer);
718
719     snprintfz(buffer, RRDDIMVAR_ID_MAX, "%s.%s", rd->rrdset->id, rs->id);
720     rs->fullidid = strdupz(buffer);
721
722     snprintfz(buffer, RRDDIMVAR_ID_MAX, "%s.%s", rd->rrdset->id, rs->name);
723     rs->fullidname = strdupz(buffer);
724
725     snprintfz(buffer, RRDDIMVAR_ID_MAX, "%s.%s", rd->rrdset->name, rs->id);
726     rs->fullnameid = strdupz(buffer);
727
728     snprintfz(buffer, RRDDIMVAR_ID_MAX, "%s.%s", rd->rrdset->name, rs->name);
729     rs->fullnamename = strdupz(buffer);
730
731     rs->type = type;
732     rs->value = value;
733     rs->options = options;
734     rs->rrddim = rd;
735
736     rs->local_id     = rrdvar_create_and_index("local", &st->variables_root_index, rs->id, rs->type, rs->value);
737     rs->local_name   = rrdvar_create_and_index("local", &st->variables_root_index, rs->name, rs->type, rs->value);
738
739     rs->family_id    = rrdvar_create_and_index("family", &st->rrdfamily->variables_root_index, rs->id, rs->type, rs->value);
740     rs->family_name  = rrdvar_create_and_index("family", &st->rrdfamily->variables_root_index, rs->name, rs->type, rs->value);
741
742     rs->host_fullidid     = rrdvar_create_and_index("host", &st->rrdhost->variables_root_index, rs->fullidid, rs->type, rs->value);
743     rs->host_fullidname   = rrdvar_create_and_index("host", &st->rrdhost->variables_root_index, rs->fullidname, rs->type, rs->value);
744     rs->host_fullnameid   = rrdvar_create_and_index("host", &st->rrdhost->variables_root_index, rs->fullnameid, rs->type, rs->value);
745     rs->host_fullnamename = rrdvar_create_and_index("host", &st->rrdhost->variables_root_index, rs->fullnamename, rs->type, rs->value);
746
747     rs->next = rd->variables;
748     rd->variables = rs;
749
750     return rs;
751 }
752
753 void rrddimvar_rename_all(RRDDIM *rd) {
754     RRDSET *st = rd->rrdset;
755     debug(D_VARIABLES, "RRDDIMSET rename for chart id '%s' name '%s', dimension id '%s', name '%s'", st->id, st->name, rd->id, rd->name);
756
757     RRDDIMVAR *rs, *next = rd->variables;
758     while((rs = next)) {
759         next = rs->next;
760
761         if (strcmp(rd->name, rs->name)) {
762             char buffer[RRDDIMVAR_ID_MAX + 1];
763             // name changed
764
765             // name
766             rrdvar_free(st->rrdhost, &st->variables_root_index, rs->local_name);
767             freez(rs->name);
768             snprintfz(buffer, RRDDIMVAR_ID_MAX, "%s%s%s", rs->prefix, rd->name, rs->suffix);
769             rs->name = strdupz(buffer);
770             rs->local_name = rrdvar_create_and_index("local", &st->variables_root_index, rs->name, rs->type, rs->value);
771
772             rrdvar_free(st->rrdhost, &st->rrdhost->variables_root_index, rs->host_fullidname);
773             freez(rs->fullidname);
774             snprintfz(buffer, RRDDIMVAR_ID_MAX, "%s.%s", st->id, rs->name);
775             rs->fullidname = strdupz(buffer);
776             rs->host_fullidname = rrdvar_create_and_index("host", &st->rrdhost->variables_root_index,
777                                                              rs->fullidname, rs->type, rs->value);
778
779             // fullnameid
780             rrdvar_free(st->rrdhost, &st->rrdhost->variables_root_index, rs->host_fullnameid);
781             freez(rs->fullnameid);
782             snprintfz(buffer, RRDDIMVAR_ID_MAX, "%s.%s", st->name, rs->id);
783             rs->fullnameid = strdupz(buffer);
784             rs->host_fullnameid = rrdvar_create_and_index("host", &st->rrdhost->variables_root_index,
785                                                           rs->fullnameid, rs->type, rs->value);
786
787             // fullnamename
788             rrdvar_free(st->rrdhost, &st->rrdhost->variables_root_index, rs->host_fullnamename);
789             freez(rs->fullnamename);
790             snprintfz(buffer, RRDDIMVAR_ID_MAX, "%s.%s", st->name, rs->name);
791             rs->fullnamename = strdupz(buffer);
792             rs->host_fullnamename = rrdvar_create_and_index("host", &st->rrdhost->variables_root_index,
793                                                           rs->fullnamename, rs->type, rs->value);
794         }
795     }
796 }
797
798 void rrddimvar_free(RRDDIMVAR *rs) {
799     RRDDIM *rd = rs->rrddim;
800     RRDSET *st = rd->rrdset;
801     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);
802
803     rrdvar_free(st->rrdhost, &st->variables_root_index, rs->local_id);
804     rrdvar_free(st->rrdhost, &st->variables_root_index, rs->local_name);
805
806     rrdvar_free(st->rrdhost, &st->rrdfamily->variables_root_index, rs->family_id);
807     rrdvar_free(st->rrdhost, &st->rrdfamily->variables_root_index, rs->family_name);
808
809     rrdvar_free(st->rrdhost, &st->rrdhost->variables_root_index, rs->host_fullidid);
810     rrdvar_free(st->rrdhost, &st->rrdhost->variables_root_index, rs->host_fullidname);
811     rrdvar_free(st->rrdhost, &st->rrdhost->variables_root_index, rs->host_fullnameid);
812     rrdvar_free(st->rrdhost, &st->rrdhost->variables_root_index, rs->host_fullnamename);
813
814     if(rd->variables == rs) {
815         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);
816         rd->variables = rs->next;
817     }
818     else {
819         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);
820         RRDDIMVAR *t;
821         for (t = rd->variables; t && t->next != rs; t = t->next) ;
822         if(!t) error("RRDDIMVAR '%s' not found in dimension '%s/%s' variables linked list", rs->name, st->id, rd->id);
823         else t->next = rs->next;
824     }
825
826     freez(rs->prefix);
827     freez(rs->suffix);
828     freez(rs->id);
829     freez(rs->name);
830     freez(rs->fullidid);
831     freez(rs->fullidname);
832     freez(rs->fullnameid);
833     freez(rs->fullnamename);
834     freez(rs);
835 }
836
837 // ----------------------------------------------------------------------------
838 // RRDCALC management
839
840 static inline const char *rrdcalc_status2string(int status) {
841     switch(status) {
842         case RRDCALC_STATUS_REMOVED:
843             return "REMOVED";
844
845         case RRDCALC_STATUS_UNDEFINED:
846             return "UNDEFINED";
847
848         case RRDCALC_STATUS_UNINITIALIZED:
849             return "UNINITIALIZED";
850
851         case RRDCALC_STATUS_CLEAR:
852             return "CLEAR";
853
854         case RRDCALC_STATUS_RAISED:
855             return "RAISED";
856
857         case RRDCALC_STATUS_WARNING:
858             return "WARNING";
859
860         case RRDCALC_STATUS_CRITICAL:
861             return "CRITICAL";
862
863         default:
864             error("Unknown alarm status %d", status);
865             return "UNKNOWN";
866     }
867 }
868
869 static void rrdsetcalc_link(RRDSET *st, RRDCALC *rc) {
870     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);
871
872     rc->last_status_change = time(NULL);
873     rc->rrdset = st;
874
875     rc->rrdset_next = st->alarms;
876     rc->rrdset_prev = NULL;
877     
878     if(rc->rrdset_next)
879         rc->rrdset_next->rrdset_prev = rc;
880
881     st->alarms = rc;
882
883     if(rc->update_every < rc->rrdset->update_every) {
884         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);
885         rc->update_every = rc->rrdset->update_every;
886     }
887
888     if(!isnan(rc->green) && isnan(st->green)) {
889         debug(D_HEALTH, "Health alarm '%s.%s' green threshold set from %Lf to %Lf.", rc->rrdset->id, rc->name, rc->rrdset->green, rc->green);
890         st->green = rc->green;
891     }
892
893     if(!isnan(rc->red) && isnan(st->red)) {
894         debug(D_HEALTH, "Health alarm '%s.%s' red threshold set from %Lf to %Lf.", rc->rrdset->id, rc->name, rc->rrdset->red, rc->red);
895         st->red = rc->red;
896     }
897
898     rc->local  = rrdvar_create_and_index("local",  &st->variables_root_index, rc->name, RRDVAR_TYPE_CALCULATED, &rc->value);
899     rc->family = rrdvar_create_and_index("family", &st->rrdfamily->variables_root_index, rc->name, RRDVAR_TYPE_CALCULATED, &rc->value);
900
901     char fullname[RRDVAR_MAX_LENGTH + 1];
902     snprintfz(fullname, RRDVAR_MAX_LENGTH, "%s.%s", st->id, rc->name);
903     rc->hostid   = rrdvar_create_and_index("host", &st->rrdhost->variables_root_index, fullname, RRDVAR_TYPE_CALCULATED, &rc->value);
904
905     snprintfz(fullname, RRDVAR_MAX_LENGTH, "%s.%s", st->name, rc->name);
906     rc->hostname = rrdvar_create_and_index("host", &st->rrdhost->variables_root_index, fullname, RRDVAR_TYPE_CALCULATED, &rc->value);
907
908         if(!rc->units) rc->units = strdupz(st->units);
909
910     {
911         time_t now = time(NULL);
912         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);
913     }
914 }
915
916 static inline int rrdcalc_is_matching_this_rrdset(RRDCALC *rc, RRDSET *st) {
917     if(     (rc->hash_chart == st->hash      && !strcmp(rc->chart, st->id)) ||
918             (rc->hash_chart == st->hash_name && !strcmp(rc->chart, st->name)))
919         return 1;
920
921     return 0;
922 }
923
924 // this has to be called while the RRDHOST is locked
925 inline void rrdsetcalc_link_matching(RRDSET *st) {
926     // debug(D_HEALTH, "find matching alarms for chart '%s'", st->id);
927
928     RRDCALC *rc;
929     for(rc = st->rrdhost->alarms; rc ; rc = rc->next) {
930         if(unlikely(rc->rrdset))
931             continue;
932
933         if(unlikely(rrdcalc_is_matching_this_rrdset(rc, st)))
934             rrdsetcalc_link(st, rc);
935     }
936 }
937
938 // this has to be called while the RRDHOST is locked
939 inline void rrdsetcalc_unlink(RRDCALC *rc) {
940     RRDSET *st = rc->rrdset;
941
942     if(!st) {
943         debug(D_HEALTH, "Requested to unlink RRDCALC '%s.%s' which is not linked to any RRDSET", rc->chart?rc->chart:"NOCHART", rc->name);
944         error("Requested to unlink RRDCALC '%s.%s' which is not linked to any RRDSET", rc->chart?rc->chart:"NOCHART", rc->name);
945         return;
946     }
947
948     {
949         time_t now = time(NULL);
950         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);
951     }
952
953     RRDHOST *host = st->rrdhost;
954
955     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);
956
957     // unlink it
958     if(rc->rrdset_prev)
959         rc->rrdset_prev->rrdset_next = rc->rrdset_next;
960
961     if(rc->rrdset_next)
962         rc->rrdset_next->rrdset_prev = rc->rrdset_prev;
963
964     if(st->alarms == rc)
965         st->alarms = rc->rrdset_next;
966
967     rc->rrdset_prev = rc->rrdset_next = NULL;
968
969     rrdvar_free(st->rrdhost, &st->variables_root_index, rc->local);
970     rc->local = NULL;
971
972     rrdvar_free(st->rrdhost, &st->rrdfamily->variables_root_index, rc->family);
973     rc->family = NULL;
974
975     rrdvar_free(st->rrdhost, &st->rrdhost->variables_root_index, rc->hostid);
976     rc->hostid = NULL;
977
978     rrdvar_free(st->rrdhost, &st->rrdhost->variables_root_index, rc->hostname);
979     rc->hostname = NULL;
980
981     rc->rrdset = NULL;
982
983     // RRDCALC will remain in RRDHOST
984     // so that if the matching chart is found in the future
985     // it will be applied automatically
986 }
987
988 RRDCALC *rrdcalc_find(RRDSET *st, const char *name) {
989     RRDCALC *rc;
990     uint32_t hash = simple_hash(name);
991
992     for( rc = st->alarms; rc ; rc = rc->rrdset_next ) {
993         if(unlikely(rc->hash == hash && !strcmp(rc->name, name)))
994             return rc;
995     }
996
997     return NULL;
998 }
999
1000 static inline int rrdcalc_exists(RRDHOST *host, const char *chart, const char *name, uint32_t hash_chart, uint32_t hash_name) {
1001     RRDCALC *rc;
1002
1003     if(unlikely(!chart)) {
1004         error("attempt to find RRDCALC '%s' without giving a chart name", name);
1005         return 1;
1006     }
1007
1008     if(unlikely(!hash_chart)) hash_chart = simple_hash(chart);
1009     if(unlikely(!hash_name))  hash_name  = simple_hash(name);
1010
1011     // make sure it does not already exist
1012     for(rc = host->alarms; rc ; rc = rc->next) {
1013         if (unlikely(rc->chart && rc->hash == hash_name && rc->hash_chart == hash_chart && !strcmp(name, rc->name) && !strcmp(chart, rc->chart))) {
1014             debug(D_HEALTH, "Health alarm '%s.%s' already exists in host '%s'.", chart, name, host->hostname);
1015             error("Health alarm '%s.%s' already exists in host '%s'.", chart, name, host->hostname);
1016             return 1;
1017         }
1018     }
1019
1020     return 0;
1021 }
1022
1023 static inline uint32_t rrdcalc_get_unique_id(RRDHOST *host, const char *chart, const char *name, uint32_t *next_event_id) {
1024     if(chart && name) {
1025         uint32_t hash_chart = simple_hash(chart);
1026         uint32_t hash_name = simple_hash(name);
1027
1028         // re-use old IDs, by looking them up in the alarm log
1029         ALARM_ENTRY *ae;
1030         for(ae = host->health_log.alarms; ae ;ae = ae->next) {
1031             if(unlikely(ae->hash_name == hash_name && ae->hash_chart == hash_chart && !strcmp(name, ae->name) && !strcmp(chart, ae->chart))) {
1032                 if(next_event_id) *next_event_id = ae->alarm_event_id + 1;
1033                 return ae->alarm_id;
1034             }
1035         }
1036     }
1037
1038     return host->health_log.next_alarm_id++;
1039 }
1040
1041 static inline void rrdcalc_create_part2(RRDHOST *host, RRDCALC *rc) {
1042     rrdhost_check_rdlock(host);
1043
1044     if(rc->calculation) {
1045         rc->calculation->status = &rc->status;
1046         rc->calculation->this = &rc->value;
1047         rc->calculation->after = &rc->db_after;
1048         rc->calculation->before = &rc->db_before;
1049         rc->calculation->rrdcalc = rc;
1050     }
1051
1052     if(rc->warning) {
1053         rc->warning->status = &rc->status;
1054         rc->warning->this = &rc->value;
1055         rc->warning->after = &rc->db_after;
1056         rc->warning->before = &rc->db_before;
1057         rc->warning->rrdcalc = rc;
1058     }
1059
1060     if(rc->critical) {
1061         rc->critical->status = &rc->status;
1062         rc->critical->this = &rc->value;
1063         rc->critical->after = &rc->db_after;
1064         rc->critical->before = &rc->db_before;
1065         rc->critical->rrdcalc = rc;
1066     }
1067
1068     // link it to the host
1069     if(likely(host->alarms)) {
1070         // append it
1071         RRDCALC *t;
1072         for(t = host->alarms; t && t->next ; t = t->next) ;
1073         t->next = rc;
1074     }
1075     else {
1076         host->alarms = rc;
1077     }
1078
1079     // link it to its chart
1080     RRDSET *st;
1081     for(st = host->rrdset_root; st ; st = st->next) {
1082         if(rrdcalc_is_matching_this_rrdset(rc, st)) {
1083             rrdsetcalc_link(st, rc);
1084             break;
1085         }
1086     }
1087 }
1088
1089 static inline RRDCALC *rrdcalc_create(RRDHOST *host, RRDCALCTEMPLATE *rt, const char *chart) {
1090
1091     debug(D_HEALTH, "Health creating dynamic alarm (from template) '%s.%s'", chart, rt->name);
1092
1093     if(rrdcalc_exists(host, chart, rt->name, 0, 0))
1094         return NULL;
1095
1096     RRDCALC *rc = callocz(1, sizeof(RRDCALC));
1097     rc->next_event_id = 1;
1098     rc->id = rrdcalc_get_unique_id(host, chart, rt->name, &rc->next_event_id);
1099     rc->name = strdupz(rt->name);
1100     rc->hash = simple_hash(rc->name);
1101     rc->chart = strdupz(chart);
1102     rc->hash_chart = simple_hash(rc->chart);
1103
1104     if(rt->dimensions) rc->dimensions = strdupz(rt->dimensions);
1105
1106     rc->green = rt->green;
1107     rc->red = rt->red;
1108     rc->value = NAN;
1109     rc->old_value = NAN;
1110
1111     rc->delay_up_duration = rt->delay_up_duration;
1112     rc->delay_down_duration = rt->delay_down_duration;
1113     rc->delay_max_duration = rt->delay_max_duration;
1114     rc->delay_multiplier = rt->delay_multiplier;
1115
1116     rc->group = rt->group;
1117     rc->after = rt->after;
1118     rc->before = rt->before;
1119     rc->update_every = rt->update_every;
1120     rc->options = rt->options;
1121
1122     if(rt->exec) rc->exec = strdupz(rt->exec);
1123     if(rt->recipient) rc->recipient = strdupz(rt->recipient);
1124     if(rt->source) rc->source = strdupz(rt->source);
1125     if(rt->units) rc->units = strdupz(rt->units);
1126     if(rt->info) rc->info = strdupz(rt->info);
1127
1128     if(rt->calculation) {
1129         rc->calculation = expression_parse(rt->calculation->source, NULL, NULL);
1130         if(!rc->calculation)
1131             error("Health alarm '%s.%s': failed to parse calculation expression '%s'", chart, rt->name, rt->calculation->source);
1132     }
1133     if(rt->warning) {
1134         rc->warning = expression_parse(rt->warning->source, NULL, NULL);
1135         if(!rc->warning)
1136             error("Health alarm '%s.%s': failed to re-parse warning expression '%s'", chart, rt->name, rt->warning->source);
1137     }
1138     if(rt->critical) {
1139         rc->critical = expression_parse(rt->critical->source, NULL, NULL);
1140         if(!rc->critical)
1141             error("Health alarm '%s.%s': failed to re-parse critical expression '%s'", chart, rt->name, rt->critical->source);
1142     }
1143
1144     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",
1145           (rc->chart)?rc->chart:"NOCHART",
1146           rc->name,
1147           (rc->exec)?rc->exec:"DEFAULT",
1148           (rc->recipient)?rc->recipient:"DEFAULT",
1149           rc->green,
1150           rc->red,
1151           rc->group,
1152           rc->after,
1153           rc->before,
1154           rc->options,
1155           (rc->dimensions)?rc->dimensions:"NONE",
1156           rc->update_every,
1157           (rc->calculation)?rc->calculation->parsed_as:"NONE",
1158           (rc->warning)?rc->warning->parsed_as:"NONE",
1159           (rc->critical)?rc->critical->parsed_as:"NONE",
1160           rc->source,
1161           rc->delay_up_duration,
1162           rc->delay_down_duration,
1163           rc->delay_max_duration,
1164           rc->delay_multiplier
1165     );
1166
1167     rrdcalc_create_part2(host, rc);
1168     return rc;
1169 }
1170
1171 void rrdcalc_free(RRDHOST *host, RRDCALC *rc) {
1172     if(!rc) return;
1173
1174     debug(D_HEALTH, "Health removing alarm '%s.%s' of host '%s'", rc->chart?rc->chart:"NOCHART", rc->name, host->hostname);
1175
1176     // unlink it from RRDSET
1177     if(rc->rrdset) rrdsetcalc_unlink(rc);
1178
1179     // unlink it from RRDHOST
1180     if(unlikely(rc == host->alarms))
1181         host->alarms = rc->next;
1182
1183     else if(likely(host->alarms)) {
1184         RRDCALC *t, *last = host->alarms;
1185         for(t = last->next; t && t != rc; last = t, t = t->next) ;
1186         if(last->next == rc)
1187             last->next = rc->next;
1188         else
1189             error("Cannot unlink alarm '%s.%s' from host '%s': not found", rc->chart?rc->chart:"NOCHART", rc->name, host->hostname);
1190     }
1191     else
1192         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);
1193
1194     expression_free(rc->calculation);
1195     expression_free(rc->warning);
1196     expression_free(rc->critical);
1197
1198     freez(rc->name);
1199     freez(rc->chart);
1200     freez(rc->family);
1201     freez(rc->dimensions);
1202     freez(rc->exec);
1203     freez(rc->recipient);
1204     freez(rc->source);
1205     freez(rc->units);
1206     freez(rc->info);
1207     freez(rc);
1208 }
1209
1210 // ----------------------------------------------------------------------------
1211 // RRDCALCTEMPLATE management
1212
1213 void rrdcalctemplate_link_matching(RRDSET *st) {
1214     RRDCALCTEMPLATE *rt;
1215
1216     for(rt = st->rrdhost->templates; rt ; rt = rt->next) {
1217         if(rt->hash_context == st->hash_context && !strcmp(rt->context, st->context)) {
1218             RRDCALC *rc = rrdcalc_create(st->rrdhost, rt, st->id);
1219             if(unlikely(!rc))
1220                 error("Health tried to create alarm from template '%s', but it failed", rt->name);
1221
1222 #ifdef NETDATA_INTERNAL_CHECKS
1223             else if(rc->rrdset != st)
1224                 error("Health alarm '%s.%s' should be linked to chart '%s', but it is not", rc->chart?rc->chart:"NOCHART", rc->name, st->id);
1225 #endif
1226         }
1227     }
1228 }
1229
1230 static inline void rrdcalctemplate_free(RRDHOST *host, RRDCALCTEMPLATE *rt) {
1231     debug(D_HEALTH, "Health removing template '%s' of host '%s'", rt->name, host->hostname);
1232
1233     if(host->templates) {
1234         if(host->templates == rt) {
1235             host->templates = rt->next;
1236         }
1237         else {
1238             RRDCALCTEMPLATE *t, *last = host->templates;
1239             for (t = last->next; t && t != rt; last = t, t = t->next ) ;
1240             if(last && last->next == rt) {
1241                 last->next = rt->next;
1242                 rt->next = NULL;
1243             }
1244             else
1245                 error("Cannot find RRDCALCTEMPLATE '%s' linked in host '%s'", rt->name, host->hostname);
1246         }
1247     }
1248
1249     expression_free(rt->calculation);
1250     expression_free(rt->warning);
1251     expression_free(rt->critical);
1252
1253     freez(rt->name);
1254     freez(rt->exec);
1255     freez(rt->recipient);
1256     freez(rt->context);
1257     freez(rt->source);
1258     freez(rt->units);
1259     freez(rt->info);
1260     freez(rt->dimensions);
1261     freez(rt);
1262 }
1263
1264 // ----------------------------------------------------------------------------
1265 // load health configuration
1266
1267 #define HEALTH_CONF_MAX_LINE 4096
1268
1269 #define HEALTH_ALARM_KEY "alarm"
1270 #define HEALTH_TEMPLATE_KEY "template"
1271 #define HEALTH_ON_KEY "on"
1272 #define HEALTH_LOOKUP_KEY "lookup"
1273 #define HEALTH_CALC_KEY "calc"
1274 #define HEALTH_EVERY_KEY "every"
1275 #define HEALTH_GREEN_KEY "green"
1276 #define HEALTH_RED_KEY "red"
1277 #define HEALTH_WARN_KEY "warn"
1278 #define HEALTH_CRIT_KEY "crit"
1279 #define HEALTH_EXEC_KEY "exec"
1280 #define HEALTH_RECIPIENT_KEY "to"
1281 #define HEALTH_UNITS_KEY "units"
1282 #define HEALTH_INFO_KEY "info"
1283 #define HEALTH_DELAY_KEY "delay"
1284
1285 static inline int rrdcalc_add_alarm_from_config(RRDHOST *host, RRDCALC *rc) {
1286     if(!rc->chart) {
1287         error("Health configuration for alarm '%s' does not have a chart", rc->name);
1288         return 0;
1289     }
1290
1291     if(!rc->update_every) {
1292         error("Health configuration for alarm '%s.%s' has no frequency (parameter 'every'). Ignoring it.", rc->chart?rc->chart:"NOCHART", rc->name);
1293         return 0;
1294     }
1295
1296     if(!RRDCALC_HAS_DB_LOOKUP(rc) && !rc->warning && !rc->critical) {
1297         error("Health configuration for alarm '%s.%s' is useless (no calculation, no warning and no critical evaluation)", rc->chart?rc->chart:"NOCHART", rc->name);
1298         return 0;
1299     }
1300
1301     if (rrdcalc_exists(host, rc->chart, rc->name, rc->hash_chart, rc->hash))
1302         return 0;
1303
1304     rc->id = rrdcalc_get_unique_id(&localhost, rc->chart, rc->name, &rc->next_event_id);
1305
1306     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",
1307           rc->chart?rc->chart:"NOCHART",
1308           rc->name,
1309           rc->id,
1310           (rc->exec)?rc->exec:"DEFAULT",
1311           (rc->recipient)?rc->recipient:"DEFAULT",
1312           rc->green,
1313           rc->red,
1314           rc->group,
1315           rc->after,
1316           rc->before,
1317           rc->options,
1318           (rc->dimensions)?rc->dimensions:"NONE",
1319           rc->update_every,
1320           (rc->calculation)?rc->calculation->parsed_as:"NONE",
1321           (rc->warning)?rc->warning->parsed_as:"NONE",
1322           (rc->critical)?rc->critical->parsed_as:"NONE",
1323           rc->source,
1324           rc->delay_up_duration,
1325           rc->delay_down_duration,
1326           rc->delay_max_duration,
1327           rc->delay_multiplier
1328     );
1329
1330     rrdcalc_create_part2(host, rc);
1331     return 1;
1332 }
1333
1334 static inline int rrdcalctemplate_add_template_from_config(RRDHOST *host, RRDCALCTEMPLATE *rt) {
1335     if(unlikely(!rt->context)) {
1336         error("Health configuration for template '%s' does not have a context", rt->name);
1337         return 0;
1338     }
1339
1340     if(unlikely(!rt->update_every)) {
1341         error("Health configuration for template '%s' has no frequency (parameter 'every'). Ignoring it.", rt->name);
1342         return 0;
1343     }
1344
1345     if(unlikely(!RRDCALCTEMPLATE_HAS_CALCULATION(rt) && !rt->warning && !rt->critical)) {
1346         error("Health configuration for template '%s' is useless (no calculation, no warning and no critical evaluation)", rt->name);
1347         return 0;
1348     }
1349
1350     RRDCALCTEMPLATE *t, *last = NULL;
1351     for (t = host->templates; t ; last = t, t = t->next) {
1352         if(unlikely(t->hash_name == rt->hash_name && !strcmp(t->name, rt->name))) {
1353             error("Health configuration template '%s' already exists for host '%s'.", rt->name, host->hostname);
1354             return 0;
1355         }
1356     }
1357
1358     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",
1359           rt->name,
1360           (rt->context)?rt->context:"NONE",
1361           (rt->exec)?rt->exec:"DEFAULT",
1362           (rt->recipient)?rt->recipient:"DEFAULT",
1363           rt->green,
1364           rt->red,
1365           rt->group,
1366           rt->after,
1367           rt->before,
1368           rt->options,
1369           (rt->dimensions)?rt->dimensions:"NONE",
1370           rt->update_every,
1371           (rt->calculation)?rt->calculation->parsed_as:"NONE",
1372           (rt->warning)?rt->warning->parsed_as:"NONE",
1373           (rt->critical)?rt->critical->parsed_as:"NONE",
1374           rt->source,
1375           rt->delay_up_duration,
1376           rt->delay_down_duration,
1377           rt->delay_max_duration,
1378           rt->delay_multiplier
1379     );
1380
1381     if(likely(last)) {
1382         last->next = rt;
1383     }
1384     else {
1385         rt->next = host->templates;
1386         host->templates = rt;
1387     }
1388
1389     return 1;
1390 }
1391
1392 static inline int health_parse_duration(char *string, int *result) {
1393     // make sure it is a number
1394     if(!*string || !(isdigit(*string) || *string == '+' || *string == '-')) {
1395         *result = 0;
1396         return 0;
1397     }
1398
1399     char *e = NULL;
1400     calculated_number n = strtold(string, &e);
1401     if(e && *e) {
1402         switch (*e) {
1403             case 'Y':
1404                 *result = (int) (n * 86400 * 365);
1405                 break;
1406             case 'M':
1407                 *result = (int) (n * 86400 * 30);
1408                 break;
1409             case 'w':
1410                 *result = (int) (n * 86400 * 7);
1411                 break;
1412             case 'd':
1413                 *result = (int) (n * 86400);
1414                 break;
1415             case 'h':
1416                 *result = (int) (n * 3600);
1417                 break;
1418             case 'm':
1419                 *result = (int) (n * 60);
1420                 break;
1421
1422             default:
1423             case 's':
1424                 *result = (int) (n);
1425                 break;
1426         }
1427     }
1428     else
1429        *result = (int)(n);
1430
1431     return 1;
1432 }
1433
1434 static inline int health_parse_delay(
1435         size_t line, const char *path, const char *file, char *string,
1436         int *delay_up_duration,
1437         int *delay_down_duration,
1438         int *delay_max_duration,
1439         float *delay_multiplier) {
1440
1441     char given_up = 0;
1442     char given_down = 0;
1443     char given_max = 0;
1444     char given_multiplier = 0;
1445
1446     char *s = string;
1447     while(*s) {
1448         char *key = s;
1449
1450         while(*s && !isspace(*s)) s++;
1451         while(*s && isspace(*s)) *s++ = '\0';
1452
1453         if(!*key) break;
1454
1455         char *value = s;
1456         while(*s && !isspace(*s)) s++;
1457         while(*s && isspace(*s)) *s++ = '\0';
1458
1459         if(!strcasecmp(key, "up")) {
1460             if (!health_parse_duration(value, delay_up_duration)) {
1461                 error("Health configuration at line %zu of file '%s/%s': invalid value '%s' for '%s' keyword",
1462                       line, path, file, value, key);
1463             }
1464             else given_up = 1;
1465         }
1466         else if(!strcasecmp(key, "down")) {
1467             if (!health_parse_duration(value, delay_down_duration)) {
1468                 error("Health configuration at line %zu of file '%s/%s': invalid value '%s' for '%s' keyword",
1469                       line, path, file, value, key);
1470             }
1471             else given_down = 1;
1472         }
1473         else if(!strcasecmp(key, "multiplier")) {
1474             *delay_multiplier = strtof(value, NULL);
1475             if(isnan(*delay_multiplier) || isinf(*delay_multiplier) || islessequal(*delay_multiplier, 0)) {
1476                 error("Health configuration at line %zu of file '%s/%s': invalid value '%s' for '%s' keyword",
1477                       line, path, file, value, key);
1478             }
1479             else given_multiplier = 1;
1480         }
1481         else if(!strcasecmp(key, "max")) {
1482             if (!health_parse_duration(value, delay_max_duration)) {
1483                 error("Health configuration at line %zu of file '%s/%s': invalid value '%s' for '%s' keyword",
1484                       line, path, file, value, key);
1485             }
1486             else given_max = 1;
1487         }
1488         else {
1489             error("Health configuration at line %zu of file '%s/%s': unknown keyword '%s'",
1490                   line, path, file, key);
1491         }
1492     }
1493
1494     if(!given_up)
1495         *delay_up_duration = 0;
1496
1497     if(!given_down)
1498         *delay_down_duration = 0;
1499
1500     if(!given_multiplier)
1501         *delay_multiplier = 1.0;
1502
1503     if(!given_max) {
1504         if((*delay_max_duration) < (*delay_up_duration) * (*delay_multiplier))
1505             *delay_max_duration = (*delay_up_duration) * (*delay_multiplier);
1506
1507         if((*delay_max_duration) < (*delay_down_duration) * (*delay_multiplier))
1508             *delay_max_duration = (*delay_down_duration) * (*delay_multiplier);
1509     }
1510
1511     return 1;
1512 }
1513
1514 static inline int health_parse_db_lookup(
1515         size_t line, const char *path, const char *file, char *string,
1516         int *group_method, int *after, int *before, int *every,
1517         uint32_t *options, char **dimensions
1518 ) {
1519     debug(D_HEALTH, "Health configuration parsing database lookup %zu@%s/%s: %s", line, path, file, string);
1520
1521     if(*dimensions) freez(*dimensions);
1522     *dimensions = NULL;
1523     *after = 0;
1524     *before = 0;
1525     *every = 0;
1526     *options = 0;
1527
1528     char *s = string, *key;
1529
1530     // first is the group method
1531     key = s;
1532     while(*s && !isspace(*s)) s++;
1533     while(*s && isspace(*s)) *s++ = '\0';
1534     if(!*s) {
1535         error("Health configuration invalid chart calculation at line %zu of file '%s/%s': expected group method followed by the 'after' time, but got '%s'",
1536               line, path, file, key);
1537         return 0;
1538     }
1539
1540     if((*group_method = web_client_api_request_v1_data_group(key, -1)) == -1) {
1541         error("Health configuration at line %zu of file '%s/%s': invalid group method '%s'",
1542               line, path, file, key);
1543         return 0;
1544     }
1545
1546     // then is the 'after' time
1547     key = s;
1548     while(*s && !isspace(*s)) s++;
1549     while(*s && isspace(*s)) *s++ = '\0';
1550
1551     if(!health_parse_duration(key, after)) {
1552         error("Health configuration at line %zu of file '%s/%s': invalid duration '%s' after group method",
1553               line, path, file, key);
1554         return 0;
1555     }
1556
1557     // sane defaults
1558     *every = abs(*after);
1559
1560     // now we may have optional parameters
1561     while(*s) {
1562         key = s;
1563         while(*s && !isspace(*s)) s++;
1564         while(*s && isspace(*s)) *s++ = '\0';
1565         if(!*key) break;
1566
1567         if(!strcasecmp(key, "at")) {
1568             char *value = s;
1569             while(*s && !isspace(*s)) s++;
1570             while(*s && isspace(*s)) *s++ = '\0';
1571
1572             if (!health_parse_duration(value, before)) {
1573                 error("Health configuration at line %zu of file '%s/%s': invalid duration '%s' for '%s' keyword",
1574                       line, path, file, value, key);
1575             }
1576         }
1577         else if(!strcasecmp(key, HEALTH_EVERY_KEY)) {
1578             char *value = s;
1579             while(*s && !isspace(*s)) s++;
1580             while(*s && isspace(*s)) *s++ = '\0';
1581
1582             if (!health_parse_duration(value, every)) {
1583                 error("Health configuration at line %zu of file '%s/%s': invalid duration '%s' for '%s' keyword",
1584                       line, path, file, value, key);
1585             }
1586         }
1587         else if(!strcasecmp(key, "absolute") || !strcasecmp(key, "abs") || !strcasecmp(key, "absolute_sum")) {
1588             *options |= RRDR_OPTION_ABSOLUTE;
1589         }
1590         else if(!strcasecmp(key, "min2max")) {
1591             *options |= RRDR_OPTION_MIN2MAX;
1592         }
1593         else if(!strcasecmp(key, "null2zero")) {
1594             *options |= RRDR_OPTION_NULL2ZERO;
1595         }
1596         else if(!strcasecmp(key, "percentage")) {
1597             *options |= RRDR_OPTION_PERCENTAGE;
1598         }
1599         else if(!strcasecmp(key, "unaligned")) {
1600             *options |= RRDR_OPTION_NOT_ALIGNED;
1601         }
1602         else if(!strcasecmp(key, "of")) {
1603             if(*s && strcasecmp(s, "all"))
1604                *dimensions = strdupz(s);
1605             break;
1606         }
1607         else {
1608             error("Health configuration at line %zu of file '%s/%s': unknown keyword '%s'",
1609                   line, path, file, key);
1610         }
1611     }
1612
1613     return 1;
1614 }
1615
1616 static inline char *tabs2spaces(char *s) {
1617     char *t = s;
1618     while(*t) {
1619         if(unlikely(*t == '\t')) *t = ' ';
1620         t++;
1621     }
1622
1623     return s;
1624 }
1625
1626 static inline char *health_source_file(size_t line, const char *path, const char *filename) {
1627     char buffer[FILENAME_MAX + 1];
1628     snprintfz(buffer, FILENAME_MAX, "%zu@%s/%s", line, path, filename);
1629     return strdupz(buffer);
1630 }
1631
1632 static inline void strip_quotes(char *s) {
1633     while(*s) {
1634         if(*s == '\'' || *s == '"') *s = ' ';
1635         s++;
1636     }
1637 }
1638
1639 int health_readfile(const char *path, const char *filename) {
1640     debug(D_HEALTH, "Health configuration reading file '%s/%s'", path, filename);
1641
1642     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;
1643     char buffer[HEALTH_CONF_MAX_LINE + 1];
1644
1645     if(unlikely(!hash_alarm)) {
1646         hash_alarm = simple_uhash(HEALTH_ALARM_KEY);
1647         hash_template = simple_uhash(HEALTH_TEMPLATE_KEY);
1648         hash_on = simple_uhash(HEALTH_ON_KEY);
1649         hash_calc = simple_uhash(HEALTH_CALC_KEY);
1650         hash_lookup = simple_uhash(HEALTH_LOOKUP_KEY);
1651         hash_green = simple_uhash(HEALTH_GREEN_KEY);
1652         hash_red = simple_uhash(HEALTH_RED_KEY);
1653         hash_warn = simple_uhash(HEALTH_WARN_KEY);
1654         hash_crit = simple_uhash(HEALTH_CRIT_KEY);
1655         hash_exec = simple_uhash(HEALTH_EXEC_KEY);
1656         hash_every = simple_uhash(HEALTH_EVERY_KEY);
1657         hash_units = simple_hash(HEALTH_UNITS_KEY);
1658         hash_info = simple_hash(HEALTH_INFO_KEY);
1659         hash_recipient = simple_hash(HEALTH_RECIPIENT_KEY);
1660         hash_delay = simple_uhash(HEALTH_DELAY_KEY);
1661     }
1662
1663     snprintfz(buffer, HEALTH_CONF_MAX_LINE, "%s/%s", path, filename);
1664     FILE *fp = fopen(buffer, "r");
1665     if(!fp) {
1666         error("Health configuration cannot read file '%s'.", buffer);
1667         return 0;
1668     }
1669
1670     RRDCALC *rc = NULL;
1671     RRDCALCTEMPLATE *rt = NULL;
1672
1673     size_t line = 0, append = 0;
1674     char *s;
1675     while((s = fgets(&buffer[append], (int)(HEALTH_CONF_MAX_LINE - append), fp)) || append) {
1676         int stop_appending = !s;
1677         line++;
1678         s = trim(buffer);
1679         if(!s) continue;
1680
1681         append = strlen(s);
1682         if(!stop_appending && s[append - 1] == '\\') {
1683             s[append - 1] = ' ';
1684             append = &s[append] - buffer;
1685             if(append < HEALTH_CONF_MAX_LINE)
1686                 continue;
1687             else {
1688                 error("Health configuration has too long muli-line at line %zu of file '%s/%s'.", line, path, filename);
1689             }
1690         }
1691         append = 0;
1692
1693         char *key = s;
1694         while(*s && *s != ':') s++;
1695         if(!*s) {
1696             error("Health configuration has invalid line %zu of file '%s/%s'. It does not contain a ':'. Ignoring it.", line, path, filename);
1697             continue;
1698         }
1699         *s = '\0';
1700         s++;
1701
1702         char *value = s;
1703         key = trim(key);
1704         value = trim(value);
1705
1706         if(!key) {
1707             error("Health configuration has invalid line %zu of file '%s/%s'. Keyword is empty. Ignoring it.", line, path, filename);
1708             continue;
1709         }
1710
1711         if(!value) {
1712             error("Health configuration has invalid line %zu of file '%s/%s'. value is empty. Ignoring it.", line, path, filename);
1713             continue;
1714         }
1715
1716         uint32_t hash = simple_uhash(key);
1717
1718         if(hash == hash_alarm && !strcasecmp(key, HEALTH_ALARM_KEY)) {
1719             if(rc && !rrdcalc_add_alarm_from_config(&localhost, rc))
1720                 rrdcalc_free(&localhost, rc);
1721
1722             if(rt) {
1723                 if (!rrdcalctemplate_add_template_from_config(&localhost, rt))
1724                     rrdcalctemplate_free(&localhost, rt);
1725                 rt = NULL;
1726             }
1727
1728             rc = callocz(1, sizeof(RRDCALC));
1729             rc->next_event_id = 1;
1730             rc->name = tabs2spaces(strdupz(value));
1731             rc->hash = simple_hash(rc->name);
1732             rc->source = health_source_file(line, path, filename);
1733             rc->green = NAN;
1734             rc->red = NAN;
1735             rc->value = NAN;
1736             rc->old_value = NAN;
1737             rc->delay_multiplier = 1.0;
1738
1739             if(rrdvar_fix_name(rc->name))
1740                 error("Health configuration renamed alarm '%s' to '%s'", value, rc->name);
1741         }
1742         else if(hash == hash_template && !strcasecmp(key, HEALTH_TEMPLATE_KEY)) {
1743             if(rc) {
1744                 if(!rrdcalc_add_alarm_from_config(&localhost, rc))
1745                     rrdcalc_free(&localhost, rc);
1746                 rc = NULL;
1747             }
1748
1749             if(rt && !rrdcalctemplate_add_template_from_config(&localhost, rt))
1750                 rrdcalctemplate_free(&localhost, rt);
1751
1752             rt = callocz(1, sizeof(RRDCALCTEMPLATE));
1753             rt->name = tabs2spaces(strdupz(value));
1754             rt->hash_name = simple_hash(rt->name);
1755             rt->source = health_source_file(line, path, filename);
1756             rt->green = NAN;
1757             rt->red = NAN;
1758             rt->delay_multiplier = 1.0;
1759
1760             if(rrdvar_fix_name(rt->name))
1761                 error("Health configuration renamed template '%s' to '%s'", value, rt->name);
1762         }
1763         else if(rc) {
1764             if(hash == hash_on && !strcasecmp(key, HEALTH_ON_KEY)) {
1765                 if(rc->chart) {
1766                     if(strcmp(rc->chart, value))
1767                         error("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').",
1768                              line, path, filename, rc->name, key, rc->chart, value, value);
1769
1770                     freez(rc->chart);
1771                 }
1772                 rc->chart = tabs2spaces(strdupz(value));
1773                 rc->hash_chart = simple_hash(rc->chart);
1774             }
1775             else if(hash == hash_lookup && !strcasecmp(key, HEALTH_LOOKUP_KEY)) {
1776                 health_parse_db_lookup(line, path, filename, value, &rc->group, &rc->after, &rc->before,
1777                                        &rc->update_every,
1778                                        &rc->options, &rc->dimensions);
1779             }
1780             else if(hash == hash_every && !strcasecmp(key, HEALTH_EVERY_KEY)) {
1781                 if(!health_parse_duration(value, &rc->update_every))
1782                     error("Health configuration at line %zu of file '%s/%s' for alarm '%s' at key '%s' cannot parse duration: '%s'.",
1783                          line, path, filename, rc->name, key, value);
1784             }
1785             else if(hash == hash_green && !strcasecmp(key, HEALTH_GREEN_KEY)) {
1786                 char *e;
1787                 rc->green = strtold(value, &e);
1788                 if(e && *e) {
1789                     error("Health configuration at line %zu of file '%s/%s' for alarm '%s' at key '%s' leaves this string unmatched: '%s'.",
1790                          line, path, filename, rc->name, key, e);
1791                 }
1792             }
1793             else if(hash == hash_red && !strcasecmp(key, HEALTH_RED_KEY)) {
1794                 char *e;
1795                 rc->red = strtold(value, &e);
1796                 if(e && *e) {
1797                     error("Health configuration at line %zu of file '%s/%s' for alarm '%s' at key '%s' leaves this string unmatched: '%s'.",
1798                          line, path, filename, rc->name, key, e);
1799                 }
1800             }
1801             else if(hash == hash_calc && !strcasecmp(key, HEALTH_CALC_KEY)) {
1802                 const char *failed_at = NULL;
1803                 int error = 0;
1804                 rc->calculation = expression_parse(value, &failed_at, &error);
1805                 if(!rc->calculation) {
1806                     error("Health configuration at line %zu of file '%s/%s' for alarm '%s' at key '%s' has unparse-able expression '%s': %s at '%s'",
1807                           line, path, filename, rc->name, key, value, expression_strerror(error), failed_at);
1808                 }
1809             }
1810             else if(hash == hash_warn && !strcasecmp(key, HEALTH_WARN_KEY)) {
1811                 const char *failed_at = NULL;
1812                 int error = 0;
1813                 rc->warning = expression_parse(value, &failed_at, &error);
1814                 if(!rc->warning) {
1815                     error("Health configuration at line %zu of file '%s/%s' for alarm '%s' at key '%s' has unparse-able expression '%s': %s at '%s'",
1816                           line, path, filename, rc->name, key, value, expression_strerror(error), failed_at);
1817                 }
1818             }
1819             else if(hash == hash_crit && !strcasecmp(key, HEALTH_CRIT_KEY)) {
1820                 const char *failed_at = NULL;
1821                 int error = 0;
1822                 rc->critical = expression_parse(value, &failed_at, &error);
1823                 if(!rc->critical) {
1824                     error("Health configuration at line %zu of file '%s/%s' for alarm '%s' at key '%s' has unparse-able expression '%s': %s at '%s'",
1825                           line, path, filename, rc->name, key, value, expression_strerror(error), failed_at);
1826                 }
1827             }
1828             else if(hash == hash_exec && !strcasecmp(key, HEALTH_EXEC_KEY)) {
1829                 if(rc->exec) {
1830                     if(strcmp(rc->exec, value))
1831                         error("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').",
1832                              line, path, filename, rc->name, key, rc->exec, value, value);
1833
1834                     freez(rc->exec);
1835                 }
1836                 rc->exec = tabs2spaces(strdupz(value));
1837             }
1838             else if(hash == hash_recipient && !strcasecmp(key, HEALTH_RECIPIENT_KEY)) {
1839                 if(rc->recipient) {
1840                     if(strcmp(rc->recipient, value))
1841                         error("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').",
1842                              line, path, filename, rc->name, key, rc->recipient, value, value);
1843
1844                     freez(rc->recipient);
1845                 }
1846                 rc->recipient = tabs2spaces(strdupz(value));
1847             }
1848             else if(hash == hash_units && !strcasecmp(key, HEALTH_UNITS_KEY)) {
1849                 if(rc->units) {
1850                     if(strcmp(rc->units, value))
1851                         error("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').",
1852                              line, path, filename, rc->name, key, rc->units, value, value);
1853
1854                     freez(rc->units);
1855                 }
1856                 rc->units = tabs2spaces(strdupz(value));
1857                 strip_quotes(rc->units);
1858             }
1859             else if(hash == hash_info && !strcasecmp(key, HEALTH_INFO_KEY)) {
1860                 if(rc->info) {
1861                     if(strcmp(rc->info, value))
1862                         error("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').",
1863                              line, path, filename, rc->name, key, rc->info, value, value);
1864
1865                     freez(rc->info);
1866                 }
1867                 rc->info = tabs2spaces(strdupz(value));
1868                 strip_quotes(rc->info);
1869             }
1870             else if(hash == hash_delay && !strcasecmp(key, HEALTH_DELAY_KEY)) {
1871                 health_parse_delay(line, path, filename, value, &rc->delay_up_duration, &rc->delay_down_duration, &rc->delay_max_duration, &rc->delay_multiplier);
1872             }
1873             else {
1874                 error("Health configuration at line %zu of file '%s/%s' for alarm '%s' has unknown key '%s'.",
1875                      line, path, filename, rc->name, key);
1876             }
1877         }
1878         else if(rt) {
1879             if(hash == hash_on && !strcasecmp(key, HEALTH_ON_KEY)) {
1880                 if(rt->context) {
1881                     if(strcmp(rt->context, value))
1882                         error("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').",
1883                              line, path, filename, rt->name, key, rt->context, value, value);
1884
1885                     freez(rt->context);
1886                 }
1887                 rt->context = tabs2spaces(strdupz(value));
1888                 rt->hash_context = simple_hash(rt->context);
1889             }
1890             else if(hash == hash_lookup && !strcasecmp(key, HEALTH_LOOKUP_KEY)) {
1891                 health_parse_db_lookup(line, path, filename, value, &rt->group, &rt->after, &rt->before,
1892                                        &rt->update_every,
1893                                        &rt->options, &rt->dimensions);
1894             }
1895             else if(hash == hash_every && !strcasecmp(key, HEALTH_EVERY_KEY)) {
1896                 if(!health_parse_duration(value, &rt->update_every))
1897                     error("Health configuration at line %zu of file '%s/%s' for template '%s' at key '%s' cannot parse duration: '%s'.",
1898                          line, path, filename, rt->name, key, value);
1899             }
1900             else if(hash == hash_green && !strcasecmp(key, HEALTH_GREEN_KEY)) {
1901                 char *e;
1902                 rt->green = strtold(value, &e);
1903                 if(e && *e) {
1904                     error("Health configuration at line %zu of file '%s/%s' for template '%s' at key '%s' leaves this string unmatched: '%s'.",
1905                          line, path, filename, rt->name, key, e);
1906                 }
1907             }
1908             else if(hash == hash_red && !strcasecmp(key, HEALTH_RED_KEY)) {
1909                 char *e;
1910                 rt->red = strtold(value, &e);
1911                 if(e && *e) {
1912                     error("Health configuration at line %zu of file '%s/%s' for template '%s' at key '%s' leaves this string unmatched: '%s'.",
1913                          line, path, filename, rt->name, key, e);
1914                 }
1915             }
1916             else if(hash == hash_calc && !strcasecmp(key, HEALTH_CALC_KEY)) {
1917                 const char *failed_at = NULL;
1918                 int error = 0;
1919                 rt->calculation = expression_parse(value, &failed_at, &error);
1920                 if(!rt->calculation) {
1921                     error("Health configuration at line %zu of file '%s/%s' for template '%s' at key '%s' has unparse-able expression '%s': %s at '%s'",
1922                           line, path, filename, rt->name, key, value, expression_strerror(error), failed_at);
1923                 }
1924             }
1925             else if(hash == hash_warn && !strcasecmp(key, HEALTH_WARN_KEY)) {
1926                 const char *failed_at = NULL;
1927                 int error = 0;
1928                 rt->warning = expression_parse(value, &failed_at, &error);
1929                 if(!rt->warning) {
1930                     error("Health configuration at line %zu of file '%s/%s' for template '%s' at key '%s' has unparse-able expression '%s': %s at '%s'",
1931                           line, path, filename, rt->name, key, value, expression_strerror(error), failed_at);
1932                 }
1933             }
1934             else if(hash == hash_crit && !strcasecmp(key, HEALTH_CRIT_KEY)) {
1935                 const char *failed_at = NULL;
1936                 int error = 0;
1937                 rt->critical = expression_parse(value, &failed_at, &error);
1938                 if(!rt->critical) {
1939                     error("Health configuration at line %zu of file '%s/%s' for template '%s' at key '%s' has unparse-able expression '%s': %s at '%s'",
1940                           line, path, filename, rt->name, key, value, expression_strerror(error), failed_at);
1941                 }
1942             }
1943             else if(hash == hash_exec && !strcasecmp(key, HEALTH_EXEC_KEY)) {
1944                 if(rt->exec) {
1945                     if(strcmp(rt->exec, value))
1946                         error("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').",
1947                              line, path, filename, rt->name, key, rt->exec, value, value);
1948
1949                     freez(rt->exec);
1950                 }
1951                 rt->exec = tabs2spaces(strdupz(value));
1952             }
1953             else if(hash == hash_recipient && !strcasecmp(key, HEALTH_RECIPIENT_KEY)) {
1954                 if(rt->recipient) {
1955                     if(strcmp(rt->recipient, value))
1956                         error("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').",
1957                              line, path, filename, rt->name, key, rt->recipient, value, value);
1958
1959                     freez(rt->recipient);
1960                 }
1961                 rt->recipient = tabs2spaces(strdupz(value));
1962             }
1963             else if(hash == hash_units && !strcasecmp(key, HEALTH_UNITS_KEY)) {
1964                 if(rt->units) {
1965                     if(strcmp(rt->units, value))
1966                         error("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').",
1967                              line, path, filename, rt->name, key, rt->units, value, value);
1968
1969                     freez(rt->units);
1970                 }
1971                 rt->units = tabs2spaces(strdupz(value));
1972                 strip_quotes(rt->units);
1973             }
1974             else if(hash == hash_info && !strcasecmp(key, HEALTH_INFO_KEY)) {
1975                 if(rt->info) {
1976                     if(strcmp(rt->info, value))
1977                         error("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').",
1978                              line, path, filename, rt->name, key, rt->info, value, value);
1979
1980                     freez(rt->info);
1981                 }
1982                 rt->info = tabs2spaces(strdupz(value));
1983                 strip_quotes(rt->info);
1984             }
1985             else if(hash == hash_delay && !strcasecmp(key, HEALTH_DELAY_KEY)) {
1986                 health_parse_delay(line, path, filename, value, &rt->delay_up_duration, &rt->delay_down_duration, &rt->delay_max_duration, &rt->delay_multiplier);
1987             }
1988             else {
1989                 error("Health configuration at line %zu of file '%s/%s' for template '%s' has unknown key '%s'.",
1990                       line, path, filename, rt->name, key);
1991             }
1992         }
1993         else {
1994             error("Health configuration at line %zu of file '%s/%s' has unknown key '%s'. Expected either '" HEALTH_ALARM_KEY "' or '" HEALTH_TEMPLATE_KEY "'.",
1995                   line, path, filename, key);
1996         }
1997     }
1998
1999     if(rc && !rrdcalc_add_alarm_from_config(&localhost, rc))
2000         rrdcalc_free(&localhost, rc);
2001
2002     if(rt && !rrdcalctemplate_add_template_from_config(&localhost, rt))
2003         rrdcalctemplate_free(&localhost, rt);
2004
2005     fclose(fp);
2006     return 1;
2007 }
2008
2009 void health_readdir(const char *path) {
2010     size_t pathlen = strlen(path);
2011
2012     debug(D_HEALTH, "Health configuration reading directory '%s'", path);
2013
2014     DIR *dir = opendir(path);
2015     if (!dir) {
2016         error("Health configuration cannot open directory '%s'.", path);
2017         return;
2018     }
2019
2020     struct dirent *de = NULL;
2021     while ((de = readdir(dir))) {
2022         size_t len = strlen(de->d_name);
2023
2024         if(de->d_type == DT_DIR
2025            && (
2026                    (de->d_name[0] == '.' && de->d_name[1] == '\0')
2027                    || (de->d_name[0] == '.' && de->d_name[1] == '.' && de->d_name[2] == '\0')
2028            )) {
2029             debug(D_HEALTH, "Ignoring directory '%s'", de->d_name);
2030             continue;
2031         }
2032
2033         else if(de->d_type == DT_DIR) {
2034             char *s = mallocz(pathlen + strlen(de->d_name) + 2);
2035             strcpy(s, path);
2036             strcat(s, "/");
2037             strcat(s, de->d_name);
2038             health_readdir(s);
2039             freez(s);
2040             continue;
2041         }
2042
2043         else if((de->d_type == DT_LNK || de->d_type == DT_REG || de->d_type == DT_UNKNOWN) &&
2044                 len > 5 && !strcmp(&de->d_name[len - 5], ".conf")) {
2045             health_readfile(path, de->d_name);
2046         }
2047
2048         else debug(D_HEALTH, "Ignoring file '%s'", de->d_name);
2049     }
2050
2051     closedir(dir);
2052 }
2053
2054 static inline char *health_config_dir(void) {
2055     char buffer[FILENAME_MAX + 1];
2056     snprintfz(buffer, FILENAME_MAX, "%s/health.d", config_get("global", "config directory", CONFIG_DIR));
2057     return config_get("health", "health configuration directory", buffer);
2058 }
2059
2060 void health_init(void) {
2061     debug(D_HEALTH, "Health configuration initializing");
2062
2063     if(!(health_enabled = config_get_boolean("health", "enabled", 1))) {
2064         debug(D_HEALTH, "Health is disabled.");
2065         return;
2066     }
2067
2068     char *pathname = config_get("health", "health db directory", VARLIB_DIR "/health");
2069     if(mkdir(pathname, 0770) == -1 && errno != EEXIST)
2070         fatal("Cannot create directory '%s'.", pathname);
2071
2072     char filename[FILENAME_MAX + 1];
2073     snprintfz(filename, FILENAME_MAX, "%s/health-log.db", pathname);
2074     health.log_filename = config_get("health", "health db file", filename);
2075
2076     health_alarm_log_load(&localhost);
2077     health_alarm_log_open();
2078
2079     char *path = health_config_dir();
2080
2081     {
2082         char buffer[FILENAME_MAX + 1];
2083         snprintfz(buffer, FILENAME_MAX, "%s/alarm-notify.sh", config_get("global", "plugins directory", PLUGINS_DIR));
2084         health.health_default_exec = config_get("health", "script to execute on alarm", buffer);
2085     }
2086
2087     long n = config_get_number("health", "in memory max health log entries", (long)localhost.health_log.max);
2088     if(n < 10) {
2089         error("Health configuration has invalid max log entries %ld. Using default %u", n, localhost.health_log.max);
2090         config_set_number("health", "in memory max health log entries", (long)localhost.health_log.max);
2091     }
2092     else localhost.health_log.max = (unsigned int)n;
2093
2094     rrdhost_rwlock(&localhost);
2095     health_readdir(path);
2096     rrdhost_unlock(&localhost);
2097 }
2098
2099 // ----------------------------------------------------------------------------
2100 // JSON generation
2101
2102 static inline void health_string2json(BUFFER *wb, const char *prefix, const char *label, const char *value, const char *suffix) {
2103     if(value && *value)
2104         buffer_sprintf(wb, "%s\"%s\":\"%s\"%s", prefix, label, value, suffix);
2105     else
2106         buffer_sprintf(wb, "%s\"%s\":null%s", prefix, label, suffix);
2107 }
2108
2109 static inline void health_alarm_entry2json_nolock(BUFFER *wb, ALARM_ENTRY *ae, RRDHOST *host) {
2110     buffer_sprintf(wb, "\n\t{\n"
2111                            "\t\t\"hostname\": \"%s\",\n"
2112                            "\t\t\"unique_id\": %u,\n"
2113                            "\t\t\"alarm_id\": %u,\n"
2114                            "\t\t\"alarm_event_id\": %u,\n"
2115                            "\t\t\"name\": \"%s\",\n"
2116                            "\t\t\"chart\": \"%s\",\n"
2117                            "\t\t\"family\": \"%s\",\n"
2118                            "\t\t\"processed\": %s,\n"
2119                            "\t\t\"updated\": %s,\n"
2120                            "\t\t\"exec_run\": %lu,\n"
2121                            "\t\t\"exec_failed\": %s,\n"
2122                            "\t\t\"exec\": \"%s\",\n"
2123                            "\t\t\"recipient\": \"%s\",\n"
2124                            "\t\t\"exec_code\": %d,\n"
2125                            "\t\t\"source\": \"%s\",\n"
2126                            "\t\t\"units\": \"%s\",\n"
2127                            "\t\t\"info\": \"%s\",\n"
2128                            "\t\t\"when\": %lu,\n"
2129                            "\t\t\"duration\": %lu,\n"
2130                            "\t\t\"non_clear_duration\": %lu,\n"
2131                            "\t\t\"status\": \"%s\",\n"
2132                            "\t\t\"old_status\": \"%s\",\n"
2133                            "\t\t\"delay\": %d,\n"
2134                            "\t\t\"delay_up_to_timestamp\": %lu,\n"
2135                            "\t\t\"updated_by_id\": %u,\n"
2136                            "\t\t\"updates_id\": %u,\n",
2137                    host->hostname,
2138                    ae->unique_id,
2139                    ae->alarm_id,
2140                    ae->alarm_event_id,
2141                    ae->name,
2142                    ae->chart,
2143                    ae->family,
2144                    (ae->flags & HEALTH_ENTRY_FLAG_PROCESSED)?"true":"false",
2145                    (ae->flags & HEALTH_ENTRY_FLAG_UPDATED)?"true":"false",
2146                    (unsigned long)ae->exec_run_timestamp,
2147                    (ae->flags & HEALTH_ENTRY_FLAG_EXEC_FAILED)?"true":"false",
2148                    ae->exec?ae->exec:health.health_default_exec,
2149                    ae->recipient?ae->recipient:health.health_default_recipient,
2150                    ae->exec_code,
2151                    ae->source,
2152                    ae->units?ae->units:"",
2153                    ae->info?ae->info:"",
2154                    (unsigned long)ae->when,
2155                    (unsigned long)ae->duration,
2156                    (unsigned long)ae->non_clear_duration,
2157                    rrdcalc_status2string(ae->new_status),
2158                    rrdcalc_status2string(ae->old_status),
2159                    ae->delay,
2160                    (unsigned long)ae->delay_up_to_timestamp,
2161                    ae->updated_by_id,
2162                    ae->updates_id
2163     );
2164
2165     buffer_strcat(wb, "\t\t\"value\":");
2166     buffer_rrd_value(wb, ae->new_value);
2167     buffer_strcat(wb, ",\n");
2168
2169     buffer_strcat(wb, "\t\t\"old_value\":");
2170     buffer_rrd_value(wb, ae->old_value);
2171     buffer_strcat(wb, "\n");
2172
2173     buffer_strcat(wb, "\t}");
2174 }
2175
2176 void health_alarm_log2json(RRDHOST *host, BUFFER *wb, uint32_t after) {
2177     pthread_rwlock_rdlock(&host->health_log.alarm_log_rwlock);
2178
2179     buffer_strcat(wb, "[");
2180
2181     unsigned int max = host->health_log.max;
2182     unsigned int count = 0;
2183     ALARM_ENTRY *ae;
2184     for(ae = host->health_log.alarms; ae && count < max ; count++, ae = ae->next) {
2185         if(ae->unique_id > after) {
2186             if(likely(count)) buffer_strcat(wb, ",");
2187             health_alarm_entry2json_nolock(wb, ae, host);
2188         }
2189     }
2190
2191     buffer_strcat(wb, "\n]\n");
2192
2193     pthread_rwlock_unlock(&host->health_log.alarm_log_rwlock);
2194 }
2195
2196 static inline void health_rrdcalc2json_nolock(BUFFER *wb, RRDCALC *rc) {
2197     buffer_sprintf(wb,
2198            "\t\t\"%s.%s\": {\n"
2199                    "\t\t\t\"id\": %lu,\n"
2200                    "\t\t\t\"name\": \"%s\",\n"
2201                    "\t\t\t\"chart\": \"%s\",\n"
2202                    "\t\t\t\"family\": \"%s\",\n"
2203                    "\t\t\t\"active\": %s,\n"
2204                    "\t\t\t\"exec\": \"%s\",\n"
2205                    "\t\t\t\"recipient\": \"%s\",\n"
2206                    "\t\t\t\"source\": \"%s\",\n"
2207                    "\t\t\t\"units\": \"%s\",\n"
2208                    "\t\t\t\"info\": \"%s\",\n"
2209                                    "\t\t\t\"status\": \"%s\",\n"
2210                    "\t\t\t\"last_status_change\": %lu,\n"
2211                    "\t\t\t\"last_updated\": %lu,\n"
2212                    "\t\t\t\"next_update\": %lu,\n"
2213                    "\t\t\t\"update_every\": %d,\n"
2214                    "\t\t\t\"delay_up_duration\": %d,\n"
2215                    "\t\t\t\"delay_down_duration\": %d,\n"
2216                    "\t\t\t\"delay_max_duration\": %d,\n"
2217                    "\t\t\t\"delay_multiplier\": %f,\n"
2218                    "\t\t\t\"delay\": %d,\n"
2219                    "\t\t\t\"delay_up_to_timestamp\": %lu,\n"
2220             , rc->chart, rc->name
2221             , (unsigned long)rc->id
2222             , rc->name
2223             , rc->chart
2224             , (rc->rrdset && rc->rrdset->family)?rc->rrdset->family:""
2225             , (rc->rrdset)?"true":"false"
2226             , rc->exec?rc->exec:health.health_default_exec
2227             , rc->recipient?rc->recipient:health.health_default_recipient
2228             , rc->source
2229             , rc->units?rc->units:""
2230             , rc->info?rc->info:""
2231             , rrdcalc_status2string(rc->status)
2232             , (unsigned long)rc->last_status_change
2233             , (unsigned long)rc->last_updated
2234             , (unsigned long)rc->next_update
2235             , rc->update_every
2236             , rc->delay_up_duration
2237             , rc->delay_down_duration
2238             , rc->delay_max_duration
2239             , rc->delay_multiplier
2240             , rc->delay_last
2241             , (unsigned long)rc->delay_up_to_timestamp
2242     );
2243
2244     if(RRDCALC_HAS_DB_LOOKUP(rc)) {
2245         if(rc->dimensions && *rc->dimensions)
2246             health_string2json(wb, "\t\t\t", "lookup_dimensions", rc->dimensions, ",\n");
2247
2248         buffer_sprintf(wb,
2249                        "\t\t\t\"db_after\": %lu,\n"
2250                        "\t\t\t\"db_before\": %lu,\n"
2251                        "\t\t\t\"lookup_method\": \"%s\",\n"
2252                        "\t\t\t\"lookup_after\": %d,\n"
2253                        "\t\t\t\"lookup_before\": %d,\n"
2254                        "\t\t\t\"lookup_options\": \"",
2255                        (unsigned long) rc->db_after,
2256                        (unsigned long) rc->db_before,
2257                        group_method2string(rc->group),
2258                        rc->after,
2259                        rc->before
2260         );
2261         buffer_data_options2string(wb, rc->options);
2262         buffer_strcat(wb, "\",\n");
2263     }
2264
2265     if(rc->calculation) {
2266         health_string2json(wb, "\t\t\t", "calc", rc->calculation->source, ",\n");
2267         health_string2json(wb, "\t\t\t", "calc_parsed", rc->calculation->parsed_as, ",\n");
2268     }
2269
2270     if(rc->warning) {
2271         health_string2json(wb, "\t\t\t", "warn", rc->warning->source, ",\n");
2272         health_string2json(wb, "\t\t\t", "warn_parsed", rc->warning->parsed_as, ",\n");
2273     }
2274
2275     if(rc->critical) {
2276         health_string2json(wb, "\t\t\t", "crit", rc->critical->source, ",\n");
2277         health_string2json(wb, "\t\t\t", "crit_parsed", rc->critical->parsed_as, ",\n");
2278     }
2279
2280     buffer_strcat(wb, "\t\t\t\"green\":");
2281     buffer_rrd_value(wb, rc->green);
2282     buffer_strcat(wb, ",\n");
2283
2284     buffer_strcat(wb, "\t\t\t\"red\":");
2285     buffer_rrd_value(wb, rc->red);
2286     buffer_strcat(wb, ",\n");
2287
2288     buffer_strcat(wb, "\t\t\t\"value\":");
2289     buffer_rrd_value(wb, rc->value);
2290     buffer_strcat(wb, "\n");
2291
2292     buffer_strcat(wb, "\t\t}");
2293 }
2294
2295 //void health_rrdcalctemplate2json_nolock(BUFFER *wb, RRDCALCTEMPLATE *rt) {
2296 //
2297 //}
2298
2299 void health_alarms2json(RRDHOST *host, BUFFER *wb, int all) {
2300     int i;
2301
2302     rrdhost_rdlock(&localhost);
2303     buffer_sprintf(wb, "{\n\t\"hostname\": \"%s\","
2304                         "\n\t\"latest_alarm_log_unique_id\": %u,"
2305                         "\n\t\"status\": %s,"
2306                         "\n\t\"now\": %lu,"
2307                         "\n\t\"alarms\": {\n",
2308                         host->hostname,
2309                         (host->health_log.next_log_id > 0)?(host->health_log.next_log_id - 1):0,
2310                         health_enabled?"true":"false",
2311                         (unsigned long)time(NULL));
2312
2313     RRDCALC *rc;
2314     for(i = 0, rc = host->alarms; rc ; rc = rc->next) {
2315         if(unlikely(!rc->rrdset || !rc->rrdset->last_collected_time.tv_sec))
2316             continue;
2317
2318         if(likely(!all && !(rc->status == RRDCALC_STATUS_WARNING || rc->status == RRDCALC_STATUS_CRITICAL)))
2319             continue;
2320
2321         if(likely(i)) buffer_strcat(wb, ",\n");
2322         health_rrdcalc2json_nolock(wb, rc);
2323         i++;
2324     }
2325
2326 //    buffer_strcat(wb, "\n\t},\n\t\"templates\": {");
2327 //    RRDCALCTEMPLATE *rt;
2328 //    for(rt = host->templates; rt ; rt = rt->next)
2329 //        health_rrdcalctemplate2json_nolock(wb, rt);
2330
2331     buffer_strcat(wb, "\n\t}\n}\n");
2332     rrdhost_unlock(&localhost);
2333 }
2334
2335
2336 // ----------------------------------------------------------------------------
2337 // re-load health configuration
2338
2339 static inline void health_free_all_nolock(RRDHOST *host) {
2340     while(host->templates)
2341         rrdcalctemplate_free(host, host->templates);
2342
2343     while(host->alarms)
2344         rrdcalc_free(host, host->alarms);
2345 }
2346
2347 void health_reload(void) {
2348     if(!health_enabled) {
2349         error("Health reload is requested, but health is not enabled.");
2350         return;
2351     }
2352
2353     char *path = health_config_dir();
2354
2355     // free all running alarms
2356     rrdhost_rwlock(&localhost);
2357     health_free_all_nolock(&localhost);
2358     rrdhost_unlock(&localhost);
2359
2360     // invalidate all previous entries in the alarm log
2361     ALARM_ENTRY *t;
2362     for(t = localhost.health_log.alarms ; t ; t = t->next) {
2363         if(t->new_status != RRDCALC_STATUS_REMOVED)
2364             t->flags |= HEALTH_ENTRY_FLAG_UPDATED;
2365     }
2366
2367     // reset all thresholds to all charts
2368     RRDSET *st;
2369     for(st = localhost.rrdset_root; st ; st = st->next) {
2370         st->green = NAN;
2371         st->red = NAN;
2372     }
2373
2374     // load the new alarms
2375     rrdhost_rwlock(&localhost);
2376     health_readdir(path);
2377     rrdhost_unlock(&localhost);
2378
2379     // link the loaded alarms to their charts
2380     for(st = localhost.rrdset_root; st ; st = st->next) {
2381         rrdhost_rwlock(&localhost);
2382
2383         rrdsetcalc_link_matching(st);
2384         rrdcalctemplate_link_matching(st);
2385
2386         rrdhost_unlock(&localhost);
2387     }
2388 }
2389
2390 // ----------------------------------------------------------------------------
2391 // health main thread and friends
2392
2393 static inline int rrdcalc_value2status(calculated_number n) {
2394     if(isnan(n)) return RRDCALC_STATUS_UNDEFINED;
2395     if(n) return RRDCALC_STATUS_RAISED;
2396     return RRDCALC_STATUS_CLEAR;
2397 }
2398
2399 static inline void health_alarm_execute(RRDHOST *host, ALARM_ENTRY *ae) {
2400     ae->flags |= HEALTH_ENTRY_FLAG_PROCESSED;
2401
2402     if(unlikely(ae->new_status < RRDCALC_STATUS_CLEAR)) {
2403         // do not send notifications for internal statuses
2404         goto done;
2405     }
2406
2407     // find the previous notification for the same alarm
2408     // which we have run the exec script
2409     ALARM_ENTRY *t;
2410     for(t = ae->next; t ;t = t->next) {
2411         if(t->alarm_id == ae->alarm_id && t->flags & HEALTH_ENTRY_FLAG_EXEC_RUN)
2412             break;
2413     }
2414
2415     if(likely(t)) {
2416         // we have executed this alarm notification in the past
2417         if (t && t->new_status == ae->new_status) {
2418             // don't send the same notification again
2419             debug(D_HEALTH, "Health not sending again notification for alarm '%s.%s' status %s", ae->chart, ae->name,
2420                  rrdcalc_status2string(ae->new_status));
2421             goto done;
2422         }
2423     }
2424     else {
2425         // we have not executed this alarm notification in the past
2426         if(unlikely(ae->old_status == RRDCALC_STATUS_UNINITIALIZED && ae->new_status == RRDCALC_STATUS_CLEAR)) {
2427             debug(D_HEALTH, "Health not sending notification for first initialization of alarm '%s.%s' status %s", ae->chart, ae->name, rrdcalc_status2string(ae->new_status));
2428             goto done;
2429         }
2430     }
2431
2432     char buffer[FILENAME_MAX + 1];
2433     pid_t command_pid;
2434
2435     const char *exec = ae->exec;
2436     if(!exec) exec = health.health_default_exec;
2437
2438     const char *recipient = ae->recipient;
2439     if(!recipient) recipient = health.health_default_recipient;
2440
2441     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'",
2442               exec,
2443               recipient,
2444               host->hostname,
2445               ae->unique_id,
2446               ae->alarm_id,
2447               ae->alarm_event_id,
2448               (unsigned long)ae->when,
2449               ae->name,
2450               ae->chart?ae->chart:"NOCAHRT",
2451               ae->family?ae->family:"NOFAMILY",
2452               rrdcalc_status2string(ae->new_status),
2453               rrdcalc_status2string(ae->old_status),
2454               ae->new_value,
2455               ae->old_value,
2456               ae->source?ae->source:"UNKNOWN",
2457               (uint32_t)ae->duration,
2458               (uint32_t)ae->non_clear_duration,
2459               ae->units?ae->units:"",
2460               ae->info?ae->info:""
2461     );
2462
2463     ae->flags |= HEALTH_ENTRY_FLAG_EXEC_RUN;
2464     ae->exec_run_timestamp = time(NULL);
2465
2466     debug(D_HEALTH, "executing command '%s'", buffer);
2467     FILE *fp = mypopen(buffer, &command_pid);
2468     if(!fp) {
2469         error("HEALTH: Cannot popen(\"%s\", \"r\").", buffer);
2470         goto done;
2471     }
2472     debug(D_HEALTH, "HEALTH reading from command");
2473     char *s = fgets(buffer, FILENAME_MAX, fp);
2474     (void)s;
2475     ae->exec_code = mypclose(fp, command_pid);
2476     debug(D_HEALTH, "done executing command - returned with code %d", ae->exec_code);
2477
2478     if(ae->exec_code != 0)
2479         ae->flags |= HEALTH_ENTRY_FLAG_EXEC_FAILED;
2480
2481 done:
2482     health_alarm_log_save(host, ae);
2483     return;
2484 }
2485
2486 static inline void health_process_notifications(RRDHOST *host, ALARM_ENTRY *ae) {
2487     debug(D_HEALTH, "Health alarm '%s.%s' = %0.2Lf - changed status from %s to %s",
2488          ae->chart?ae->chart:"NOCHART", ae->name,
2489          ae->new_value,
2490          rrdcalc_status2string(ae->old_status),
2491          rrdcalc_status2string(ae->new_status)
2492     );
2493
2494     health_alarm_execute(host, ae);
2495 }
2496
2497 static inline void health_alarm_log_process(RRDHOST *host) {
2498     static uint32_t stop_at_id = 0;
2499     uint32_t first_waiting = (host->health_log.alarms)?host->health_log.alarms->unique_id:0;
2500     time_t now = time(NULL);
2501
2502     pthread_rwlock_rdlock(&host->health_log.alarm_log_rwlock);
2503
2504     ALARM_ENTRY *ae;
2505     for(ae = host->health_log.alarms; ae && ae->unique_id >= stop_at_id ; ae = ae->next) {
2506         if(unlikely(
2507             !(ae->flags & HEALTH_ENTRY_FLAG_PROCESSED) &&
2508             !(ae->flags & HEALTH_ENTRY_FLAG_UPDATED)
2509             )) {
2510
2511             if(unlikely(ae->unique_id < first_waiting))
2512                 first_waiting = ae->unique_id;
2513
2514             if(likely(now >= ae->delay_up_to_timestamp))
2515                 health_process_notifications(host, ae);
2516         }
2517     }
2518
2519     // remember this for the next iteration
2520     stop_at_id = first_waiting;
2521
2522     pthread_rwlock_unlock(&host->health_log.alarm_log_rwlock);
2523
2524     if(host->health_log.count <= host->health_log.max)
2525         return;
2526
2527     // cleanup excess entries in the log
2528     pthread_rwlock_wrlock(&host->health_log.alarm_log_rwlock);
2529
2530     ALARM_ENTRY *last = NULL;
2531     unsigned int count = host->health_log.max * 2 / 3;
2532     for(ae = host->health_log.alarms; ae && count ; count--, last = ae, ae = ae->next) ;
2533
2534     if(ae && last && last->next == ae)
2535         last->next = NULL;
2536     else
2537         ae = NULL;
2538
2539     while(ae) {
2540         debug(D_HEALTH, "Health removing alarm log entry with id: %u", ae->unique_id);
2541
2542         ALARM_ENTRY *t = ae->next;
2543
2544         freez(ae->name);
2545         freez(ae->chart);
2546         freez(ae->family);
2547         freez(ae->exec);
2548         freez(ae->recipient);
2549         freez(ae->source);
2550         freez(ae->units);
2551         freez(ae->info);
2552         freez(ae);
2553
2554         ae = t;
2555         host->health_log.count--;
2556     }
2557
2558     pthread_rwlock_unlock(&host->health_log.alarm_log_rwlock);
2559 }
2560
2561 static inline int rrdcalc_isrunnable(RRDCALC *rc, time_t now, time_t *next_run) {
2562     if (unlikely(!rc->rrdset)) {
2563         debug(D_HEALTH, "Health not running alarm '%s.%s'. It is not linked to a chart.", rc->chart?rc->chart:"NOCHART", rc->name);
2564         return 0;
2565     }
2566
2567     if (unlikely(!rc->rrdset->last_collected_time.tv_sec)) {
2568         debug(D_HEALTH, "Health not running alarm '%s.%s'. Chart is not yet collected.", rc->chart?rc->chart:"NOCHART", rc->name);
2569         return 0;
2570     }
2571
2572     if (unlikely(!rc->update_every)) {
2573         debug(D_HEALTH, "Health not running alarm '%s.%s'. It does not have an update frequency", rc->chart?rc->chart:"NOCHART", rc->name);
2574         return 0;
2575     }
2576
2577     if (unlikely(rc->next_update > now)) {
2578         if (unlikely(*next_run > rc->next_update))
2579             *next_run = rc->next_update;
2580
2581         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));
2582         return 0;
2583     }
2584
2585     // FIXME
2586     // we should check that the DB lookup is possible
2587     // i.e.
2588     // - the duration of the chart includes the required timeframe
2589     // we SHOULD NOT check the dimensions - there might be alarms that refer non-existing dimensions (e.g. cpu steal)
2590
2591     return 1;
2592 }
2593
2594 void *health_main(void *ptr) {
2595     (void)ptr;
2596
2597     info("HEALTH thread created with task id %d", gettid());
2598
2599     if(pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL) != 0)
2600         error("Cannot set pthread cancel type to DEFERRED.");
2601
2602     if(pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL) != 0)
2603         error("Cannot set pthread cancel state to ENABLE.");
2604
2605     int min_run_every = (int)config_get_number("health", "run at least every seconds", 10);
2606     if(min_run_every < 1) min_run_every = 1;
2607
2608     BUFFER *wb = buffer_create(100);
2609
2610     unsigned int loop = 0;
2611     while(health_enabled && !netdata_exit) {
2612         loop++;
2613         debug(D_HEALTH, "Health monitoring iteration no %u started", loop);
2614
2615         int oldstate, runnable = 0;
2616         time_t now = time(NULL);
2617         time_t next_run = now + min_run_every;
2618         RRDCALC *rc;
2619
2620         if (unlikely(pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldstate) != 0))
2621             error("Cannot set pthread cancel state to DISABLE.");
2622
2623         rrdhost_rdlock(&localhost);
2624
2625         // the first loop is to lookup values from the db
2626         for (rc = localhost.alarms; rc; rc = rc->next) {
2627             if (unlikely(!rrdcalc_isrunnable(rc, now, &next_run)))
2628                 continue;
2629
2630             runnable++;
2631             rc->old_value = rc->value;
2632
2633             // 1. if there is database lookup, do it
2634             // 2. if there is calculation expression, run it
2635
2636             if (unlikely(RRDCALC_HAS_DB_LOOKUP(rc))) {
2637                 time_t old_db_timestamp = rc->db_before;
2638                 int value_is_null = 0;
2639
2640                 int ret = rrd2value(rc->rrdset, wb, &rc->value,
2641                                     rc->dimensions, 1, rc->after, rc->before, rc->group,
2642                                     rc->options, &rc->db_after, &rc->db_before, &value_is_null);
2643
2644                 if (unlikely(ret != 200)) {
2645                     // database lookup failed
2646                     rc->value = NAN;
2647
2648                     debug(D_HEALTH, "Health alarm '%s.%s': database lookup returned error %d", rc->chart?rc->chart:"NOCHART", rc->name, ret);
2649
2650                     if (unlikely(!(rc->rrdcalc_flags & RRDCALC_FLAG_DB_ERROR))) {
2651                         rc->rrdcalc_flags |= RRDCALC_FLAG_DB_ERROR;
2652                         error("Health alarm '%s.%s': database lookup returned error %d", rc->chart?rc->chart:"NOCHART", rc->name, ret);
2653                     }
2654                 }
2655                 else if (unlikely(rc->rrdcalc_flags & RRDCALC_FLAG_DB_ERROR))
2656                     rc->rrdcalc_flags &= ~RRDCALC_FLAG_DB_ERROR;
2657
2658                 if (unlikely(old_db_timestamp == rc->db_before)) {
2659                     // database is stale
2660
2661                     debug(D_HEALTH, "Health alarm '%s.%s': database is stale", rc->chart?rc->chart:"NOCHART", rc->name);
2662
2663                     if (unlikely(!(rc->rrdcalc_flags & RRDCALC_FLAG_DB_STALE))) {
2664                         rc->rrdcalc_flags |= RRDCALC_FLAG_DB_STALE;
2665                         error("Health alarm '%s.%s': database is stale", rc->chart?rc->chart:"NOCHART", rc->name);
2666                     }
2667                 }
2668                 else if (unlikely(rc->rrdcalc_flags & RRDCALC_FLAG_DB_STALE))
2669                     rc->rrdcalc_flags &= ~RRDCALC_FLAG_DB_STALE;
2670
2671                 if (unlikely(value_is_null)) {
2672                     // collected value is null
2673
2674                     rc->value = NAN;
2675
2676                     debug(D_HEALTH, "Health alarm '%s.%s': database lookup returned empty value (possibly value is not collected yet)",
2677                           rc->chart?rc->chart:"NOCHART", rc->name);
2678
2679                     if (unlikely(!(rc->rrdcalc_flags & RRDCALC_FLAG_DB_NAN))) {
2680                         rc->rrdcalc_flags |= RRDCALC_FLAG_DB_NAN;
2681                         error("Health alarm '%s.%s': database lookup returned empty value (possibly value is not collected yet)",
2682                               rc->chart?rc->chart:"NOCHART", rc->name);
2683                     }
2684                 }
2685                 else if (unlikely(rc->rrdcalc_flags & RRDCALC_FLAG_DB_NAN))
2686                     rc->rrdcalc_flags &= ~RRDCALC_FLAG_DB_NAN;
2687
2688                 debug(D_HEALTH, "Health alarm '%s.%s': database lookup gave value "
2689                         CALCULATED_NUMBER_FORMAT, rc->chart?rc->chart:"NOCHART", rc->name, rc->value);
2690             }
2691
2692             if(unlikely(rc->calculation)) {
2693                 if (unlikely(!expression_evaluate(rc->calculation))) {
2694                     // calculation failed
2695
2696                     rc->value = NAN;
2697
2698                     debug(D_HEALTH, "Health alarm '%s.%s': failed to evaluate calculation with error: %s",
2699                           rc->chart?rc->chart:"NOCHART", rc->name, buffer_tostring(rc->calculation->error_msg));
2700
2701                     if (unlikely(!(rc->rrdcalc_flags & RRDCALC_FLAG_CALC_ERROR))) {
2702                         rc->rrdcalc_flags |= RRDCALC_FLAG_CALC_ERROR;
2703                         error("Health alarm '%s.%s': failed to evaluate calculation with error: %s",
2704                               rc->chart?rc->chart:"NOCHART", rc->name, buffer_tostring(rc->calculation->error_msg));
2705                     }
2706                 }
2707                 else {
2708                     if (unlikely(rc->rrdcalc_flags & RRDCALC_FLAG_CALC_ERROR))
2709                         rc->rrdcalc_flags &= ~RRDCALC_FLAG_CALC_ERROR;
2710
2711                     debug(D_HEALTH, "Health alarm '%s.%s': calculation expression gave value "
2712                             CALCULATED_NUMBER_FORMAT
2713                             ": %s (source: %s)",
2714                           rc->chart?rc->chart:"NOCHART", rc->name,
2715                           rc->calculation->result,
2716                           buffer_tostring(rc->calculation->error_msg),
2717                           rc->source
2718                     );
2719
2720                     rc->value = rc->calculation->result;
2721                 }
2722             }
2723         }
2724         rrdhost_unlock(&localhost);
2725
2726         if (unlikely(runnable && !netdata_exit)) {
2727             rrdhost_rdlock(&localhost);
2728
2729             for (rc = localhost.alarms; rc; rc = rc->next) {
2730                 if (unlikely(!rrdcalc_isrunnable(rc, now, &next_run)))
2731                     continue;
2732
2733                 int warning_status  = RRDCALC_STATUS_UNDEFINED;
2734                 int critical_status = RRDCALC_STATUS_UNDEFINED;
2735
2736                 if(likely(rc->warning)) {
2737                     if(unlikely(!expression_evaluate(rc->warning))) {
2738                         // calculation failed
2739
2740                         debug(D_HEALTH, "Health alarm '%s.%s': warning expression failed with error: %s",
2741                               rc->chart?rc->chart:"NOCHART", rc->name, buffer_tostring(rc->warning->error_msg));
2742
2743                         if (unlikely(!(rc->rrdcalc_flags & RRDCALC_FLAG_WARN_ERROR))) {
2744                             rc->rrdcalc_flags |= RRDCALC_FLAG_WARN_ERROR;
2745                             error("Health alarm '%s.%s': warning expression failed with error: %s",
2746                                   rc->chart?rc->chart:"NOCHART", rc->name, buffer_tostring(rc->warning->error_msg));
2747                         }
2748                     }
2749                     else {
2750                         if(unlikely(rc->rrdcalc_flags & RRDCALC_FLAG_WARN_ERROR))
2751                             rc->rrdcalc_flags &= ~RRDCALC_FLAG_WARN_ERROR;
2752
2753                         debug(D_HEALTH, "Health alarm '%s.%s': warning expression gave value "
2754                                 CALCULATED_NUMBER_FORMAT
2755                                 ": %s (source: %s)",
2756                               rc->chart?rc->chart:"NOCHART", rc->name,
2757                               rc->warning->result,
2758                               buffer_tostring(rc->warning->error_msg),
2759                               rc->source
2760                         );
2761
2762                         warning_status = rrdcalc_value2status(rc->warning->result);
2763                     }
2764                 }
2765
2766                 if(likely(rc->critical)) {
2767                     if(unlikely(!expression_evaluate(rc->critical))) {
2768                         // calculation failed
2769
2770                         debug(D_HEALTH, "Health alarm '%s.%s': critical expression failed with error: %s",
2771                               rc->chart?rc->chart:"NOCHART", rc->name, buffer_tostring(rc->critical->error_msg));
2772
2773                         if (unlikely(!(rc->rrdcalc_flags & RRDCALC_FLAG_CRIT_ERROR))) {
2774                             rc->rrdcalc_flags |= RRDCALC_FLAG_CRIT_ERROR;
2775                             error("Health alarm '%s.%s': critical expression failed with error: %s",
2776                                   rc->chart?rc->chart:"NOCHART", rc->name, buffer_tostring(rc->critical->error_msg));
2777                         }
2778                     }
2779                     else {
2780                         if(unlikely(rc->rrdcalc_flags & RRDCALC_FLAG_CRIT_ERROR))
2781                             rc->rrdcalc_flags &= ~RRDCALC_FLAG_CRIT_ERROR;
2782
2783                         debug(D_HEALTH, "Health alarm '%s.%s': critical expression gave value "
2784                                 CALCULATED_NUMBER_FORMAT
2785                                 ": %s (source: %s)",
2786                               rc->chart?rc->chart:"NOCHART", rc->name,
2787                               rc->critical->result,
2788                               buffer_tostring(rc->critical->error_msg),
2789                               rc->source
2790                         );
2791
2792                         critical_status = rrdcalc_value2status(rc->critical->result);
2793                     }
2794                 }
2795
2796                 int status = RRDCALC_STATUS_UNDEFINED;
2797
2798                 switch(warning_status) {
2799                     case RRDCALC_STATUS_CLEAR:
2800                         status = RRDCALC_STATUS_CLEAR;
2801                         break;
2802
2803                     case RRDCALC_STATUS_RAISED:
2804                         status = RRDCALC_STATUS_WARNING;
2805                         break;
2806
2807                     default:
2808                         break;
2809                 }
2810
2811                 switch(critical_status) {
2812                     case RRDCALC_STATUS_CLEAR:
2813                         if(status == RRDCALC_STATUS_UNDEFINED)
2814                             status = RRDCALC_STATUS_CLEAR;
2815                         break;
2816
2817                     case RRDCALC_STATUS_RAISED:
2818                         status = RRDCALC_STATUS_CRITICAL;
2819                         break;
2820
2821                     default:
2822                         break;
2823                 }
2824
2825                 if(status != rc->status) {
2826                     int delay = 0;
2827
2828                     if(now > rc->delay_up_to_timestamp) {
2829                         rc->delay_up_current = rc->delay_up_duration;
2830                         rc->delay_down_current = rc->delay_down_duration;
2831                         rc->delay_last = 0;
2832                         rc->delay_up_to_timestamp = 0;
2833                     }
2834                     else {
2835                         rc->delay_up_current = (int)(rc->delay_up_current * rc->delay_multiplier);
2836                         if(rc->delay_up_current > rc->delay_max_duration) rc->delay_up_current = rc->delay_max_duration;
2837
2838                         rc->delay_down_current = (int)(rc->delay_down_current * rc->delay_multiplier);
2839                         if(rc->delay_down_current > rc->delay_max_duration) rc->delay_down_current = rc->delay_max_duration;
2840                     }
2841
2842                     if(status > rc->status)
2843                         delay = rc->delay_up_current;
2844                     else
2845                         delay = rc->delay_down_current;
2846
2847                     // COMMENTED: because we do need to send raising alarms
2848                     // if(now + delay < rc->delay_up_to_timestamp)
2849                     //    delay = (int)(rc->delay_up_to_timestamp - now);
2850
2851                     rc->delay_last = delay;
2852                     rc->delay_up_to_timestamp = now + delay;
2853                     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);
2854                     rc->last_status_change = now;
2855                     rc->status = status;
2856                 }
2857
2858                 rc->last_updated = now;
2859                 rc->next_update = now + rc->update_every;
2860
2861                 if (next_run > rc->next_update)
2862                     next_run = rc->next_update;
2863             }
2864
2865             rrdhost_unlock(&localhost);
2866         }
2867
2868         if (unlikely(pthread_setcancelstate(oldstate, NULL) != 0))
2869             error("Cannot set pthread cancel state to RESTORE (%d).", oldstate);
2870
2871         if(unlikely(netdata_exit))
2872             break;
2873
2874         // execute notifications
2875         // and cleanup
2876         health_alarm_log_process(&localhost);
2877
2878         if(unlikely(netdata_exit))
2879             break;
2880         
2881         now = time(NULL);
2882         if(now < next_run) {
2883             debug(D_HEALTH, "Health monitoring iteration no %u done. Next iteration in %d secs",
2884                   loop, (int) (next_run - now));
2885             sleep_usec(1000000 * (unsigned long long) (next_run - now));
2886         }
2887         else {
2888             debug(D_HEALTH, "Health monitoring iteration no %u done. Next iteration now", loop);
2889         }
2890     }
2891
2892     buffer_free(wb);
2893
2894     info("HEALTH thread exiting");
2895     pthread_exit(NULL);
2896     return NULL;
2897 }