1 #define NETDATA_HEALTH_INTERNALS
4 // ----------------------------------------------------------------------------
5 // health alarm log load/save
6 // no need for locking - only one thread is reading / writing the alarms log
8 inline int health_alarm_log_open(RRDHOST *host) {
9 if(host->health_log_fp)
10 fclose(host->health_log_fp);
12 host->health_log_fp = fopen(host->health_log_filename, "a");
14 if(host->health_log_fp) {
15 if (setvbuf(host->health_log_fp, NULL, _IOLBF, 0) != 0)
16 error("HEALTH [%s]: cannot set line buffering on health log file '%s'.", host->hostname, host->health_log_filename);
20 error("HEALTH [%s]: cannot open health log file '%s'. Health data will be lost in case of netdata or server crash.", host->hostname, host->health_log_filename);
24 inline void health_alarm_log_close(RRDHOST *host) {
25 if(host->health_log_fp) {
26 fclose(host->health_log_fp);
27 host->health_log_fp = NULL;
31 inline void health_log_rotate(RRDHOST *host) {
32 static size_t rotate_every = 0;
34 if(unlikely(rotate_every == 0)) {
35 rotate_every = (size_t)config_get_number(CONFIG_SECTION_HEALTH, "rotate log every lines", 2000);
36 if(rotate_every < 100) rotate_every = 100;
39 if(unlikely(host->health_log_entries_written > rotate_every)) {
40 health_alarm_log_close(host);
42 char old_filename[FILENAME_MAX + 1];
43 snprintfz(old_filename, FILENAME_MAX, "%s.old", host->health_log_filename);
45 if(unlink(old_filename) == -1 && errno != ENOENT)
46 error("HEALTH [%s]: cannot remove old alarms log file '%s'", host->hostname, old_filename);
48 if(link(host->health_log_filename, old_filename) == -1 && errno != ENOENT)
49 error("HEALTH [%s]: cannot move file '%s' to '%s'.", host->hostname, host->health_log_filename, old_filename);
51 if(unlink(host->health_log_filename) == -1 && errno != ENOENT)
52 error("HEALTH [%s]: cannot remove old alarms log file '%s'", host->hostname, host->health_log_filename);
54 // open it with truncate
55 host->health_log_fp = fopen(host->health_log_filename, "w");
57 if(host->health_log_fp)
58 fclose(host->health_log_fp);
60 error("HEALTH [%s]: cannot truncate health log '%s'", host->hostname, host->health_log_filename);
62 host->health_log_fp = NULL;
64 host->health_log_entries_written = 0;
65 health_alarm_log_open(host);
69 inline void health_alarm_log_save(RRDHOST *host, ALARM_ENTRY *ae) {
70 health_log_rotate(host);
72 if(likely(host->health_log_fp)) {
73 if(unlikely(fprintf(host->health_log_fp
75 "\t%08x\t%08x\t%08x\t%08x\t%08x"
78 "\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s"
82 , (ae->flags & HEALTH_ENTRY_FLAG_SAVED)?'U':'A'
92 , (uint32_t)ae->duration
93 , (uint32_t)ae->non_clear_duration
95 , (uint32_t)ae->exec_run_timestamp
96 , (uint32_t)ae->delay_up_to_timestamp
98 , (ae->name)?ae->name:""
99 , (ae->chart)?ae->chart:""
100 , (ae->family)?ae->family:""
101 , (ae->exec)?ae->exec:""
102 , (ae->recipient)?ae->recipient:""
103 , (ae->source)?ae->source:""
104 , (ae->units)?ae->units:""
105 , (ae->info)?ae->info:""
112 , (long double)ae->new_value
113 , (long double)ae->old_value
115 error("HEALTH [%s]: failed to save alarm log entry to '%s'. Health data may be lost in case of abnormal restart.", host->hostname, host->health_log_filename);
117 ae->flags |= HEALTH_ENTRY_FLAG_SAVED;
118 host->health_log_entries_written++;
123 inline ssize_t health_alarm_log_read(RRDHOST *host, FILE *fp, const char *filename) {
124 static uint32_t max_unique_id = 0, max_alarm_id = 0;
128 char *s, *buf = mallocz(65536 + 1);
129 size_t line = 0, len = 0;
130 ssize_t loaded = 0, updated = 0, errored = 0, duplicate = 0;
132 pthread_rwlock_rdlock(&host->health_log.alarm_log_rwlock);
134 while((s = fgets_trim_len(buf, 65536, fp, &len))) {
135 host->health_log_entries_written++;
138 int max_entries = 30, entries = 0;
139 char *pointers[max_entries];
141 pointers[entries++] = s++;
143 if(unlikely(*s == '\t')) {
145 pointers[entries++] = ++s;
146 if(entries >= max_entries) {
147 error("HEALTH [%s]: line %zu of file '%s' has more than %d entries. Ignoring excessive entries.", host->hostname, line, filename, max_entries);
154 if(likely(*pointers[0] == 'U' || *pointers[0] == 'A')) {
155 ALARM_ENTRY *ae = NULL;
158 error("HEALTH [%s]: line %zu of file '%s' should have at least 26 entries, but it has %d. Ignoring it.", host->hostname, line, filename, entries);
163 // check that we have valid ids
164 uint32_t unique_id = (uint32_t)strtoul(pointers[2], NULL, 16);
166 error("HEALTH [%s]: line %zu of file '%s' states alarm entry with invalid unique id %u (%s). Ignoring it.", host->hostname, line, filename, unique_id, pointers[2]);
171 uint32_t alarm_id = (uint32_t)strtoul(pointers[3], NULL, 16);
173 error("HEALTH [%s]: line %zu of file '%s' states alarm entry for invalid alarm id %u (%s). Ignoring it.", host->hostname, line, filename, alarm_id, pointers[3]);
178 if(unlikely(*pointers[0] == 'A')) {
179 // make sure it is properly numbered
180 if(unlikely(host->health_log.alarms && unique_id < host->health_log.alarms->unique_id)) {
181 error("HEALTH [%s]: line %zu of file '%s' has alarm log entry %u in wrong order. Ignoring it.", host->hostname, line, filename, unique_id);
186 ae = callocz(1, sizeof(ALARM_ENTRY));
188 else if(unlikely(*pointers[0] == 'U')) {
190 for(ae = host->health_log.alarms; ae; ae = ae->next) {
191 if(unlikely(unique_id == ae->unique_id)) {
192 if(unlikely(*pointers[0] == 'A')) {
193 error("HEALTH [%s]: line %zu of file '%s' adds duplicate alarm log entry %u. Using the later."
194 , host->hostname, line, filename, unique_id);
200 else if(unlikely(unique_id > ae->unique_id)) {
201 // no need to continue
202 // the linked list is sorted
209 // if not found, skip this line
211 // error("HEALTH [%s]: line %zu of file '%s' updates alarm log entry with unique id %u, but it is not found.", host->hostname, line, filename, unique_id);
215 // check for a possible host missmatch
216 //if(strcmp(pointers[1], host->hostname))
217 // error("HEALTH [%s]: line %zu of file '%s' provides an alarm for host '%s' but this is named '%s'.", host->hostname, line, filename, pointers[1], host->hostname);
219 ae->unique_id = unique_id;
220 ae->alarm_id = alarm_id;
221 ae->alarm_event_id = (uint32_t)strtoul(pointers[4], NULL, 16);
222 ae->updated_by_id = (uint32_t)strtoul(pointers[5], NULL, 16);
223 ae->updates_id = (uint32_t)strtoul(pointers[6], NULL, 16);
225 ae->when = (uint32_t)strtoul(pointers[7], NULL, 16);
226 ae->duration = (uint32_t)strtoul(pointers[8], NULL, 16);
227 ae->non_clear_duration = (uint32_t)strtoul(pointers[9], NULL, 16);
229 ae->flags = (uint32_t)strtoul(pointers[10], NULL, 16);
230 ae->flags |= HEALTH_ENTRY_FLAG_SAVED;
232 ae->exec_run_timestamp = (uint32_t)strtoul(pointers[11], NULL, 16);
233 ae->delay_up_to_timestamp = (uint32_t)strtoul(pointers[12], NULL, 16);
236 ae->name = strdupz(pointers[13]);
237 ae->hash_name = simple_hash(ae->name);
240 ae->chart = strdupz(pointers[14]);
241 ae->hash_chart = simple_hash(ae->chart);
244 ae->family = strdupz(pointers[15]);
247 ae->exec = strdupz(pointers[16]);
248 if(!*ae->exec) { freez(ae->exec); ae->exec = NULL; }
250 freez(ae->recipient);
251 ae->recipient = strdupz(pointers[17]);
252 if(!*ae->recipient) { freez(ae->recipient); ae->recipient = NULL; }
255 ae->source = strdupz(pointers[18]);
256 if(!*ae->source) { freez(ae->source); ae->source = NULL; }
259 ae->units = strdupz(pointers[19]);
260 if(!*ae->units) { freez(ae->units); ae->units = NULL; }
263 ae->info = strdupz(pointers[20]);
264 if(!*ae->info) { freez(ae->info); ae->info = NULL; }
266 ae->exec_code = str2i(pointers[21]);
267 ae->new_status = str2i(pointers[22]);
268 ae->old_status = str2i(pointers[23]);
269 ae->delay = str2i(pointers[24]);
271 ae->new_value = str2l(pointers[25]);
272 ae->old_value = str2l(pointers[26]);
274 static char value_string[100 + 1];
275 freez(ae->old_value_string);
276 freez(ae->new_value_string);
277 ae->old_value_string = strdupz(format_value_and_unit(value_string, 100, ae->old_value, ae->units, -1));
278 ae->new_value_string = strdupz(format_value_and_unit(value_string, 100, ae->new_value, ae->units, -1));
280 // add it to host if not already there
281 if(unlikely(*pointers[0] == 'A')) {
282 ae->next = host->health_log.alarms;
283 host->health_log.alarms = ae;
288 if(unlikely(ae->unique_id > max_unique_id))
289 max_unique_id = ae->unique_id;
291 if(unlikely(ae->alarm_id >= max_alarm_id))
292 max_alarm_id = ae->alarm_id;
295 error("HEALTH [%s]: line %zu of file '%s' is invalid (unrecognized entry type '%s').", host->hostname, line, filename, pointers[0]);
300 pthread_rwlock_unlock(&host->health_log.alarm_log_rwlock);
304 if(!max_unique_id) max_unique_id = (uint32_t)now_realtime_sec();
305 if(!max_alarm_id) max_alarm_id = (uint32_t)now_realtime_sec();
307 host->health_log.next_log_id = max_unique_id + 1;
308 host->health_log.next_alarm_id = max_alarm_id + 1;
310 debug(D_HEALTH, "HEALTH [%s]: loaded file '%s' with %zd new alarm entries, updated %zd alarms, errors %zd entries, duplicate %zd", host->hostname, filename, loaded, updated, errored, duplicate);
314 inline void health_alarm_log_load(RRDHOST *host) {
315 health_alarm_log_close(host);
317 char filename[FILENAME_MAX + 1];
318 snprintfz(filename, FILENAME_MAX, "%s.old", host->health_log_filename);
319 FILE *fp = fopen(filename, "r");
321 error("HEALTH [%s]: cannot open health file: %s", host->hostname, filename);
323 health_alarm_log_read(host, fp, filename);
327 host->health_log_entries_written = 0;
328 fp = fopen(host->health_log_filename, "r");
330 error("HEALTH [%s]: cannot open health file: %s", host->hostname, host->health_log_filename);
332 health_alarm_log_read(host, fp, host->health_log_filename);
336 health_alarm_log_open(host);
340 // ----------------------------------------------------------------------------
341 // health alarm log management
343 inline void health_alarm_log(
346 uint32_t alarm_event_id,
352 const char *recipient,
354 calculated_number old_value,
355 calculated_number new_value,
364 debug(D_HEALTH, "Health adding alarm log entry with id: %u", host->health_log.next_log_id);
366 ALARM_ENTRY *ae = callocz(1, sizeof(ALARM_ENTRY));
367 ae->name = strdupz(name);
368 ae->hash_name = simple_hash(ae->name);
371 ae->chart = strdupz(chart);
372 ae->hash_chart = simple_hash(ae->chart);
376 ae->family = strdupz(family);
378 if(exec) ae->exec = strdupz(exec);
379 if(recipient) ae->recipient = strdupz(recipient);
380 if(source) ae->source = strdupz(source);
381 if(units) ae->units = strdupz(units);
382 if(info) ae->info = strdupz(info);
384 ae->unique_id = host->health_log.next_log_id++;
385 ae->alarm_id = alarm_id;
386 ae->alarm_event_id = alarm_event_id;
388 ae->old_value = old_value;
389 ae->new_value = new_value;
391 static char value_string[100 + 1];
392 ae->old_value_string = strdupz(format_value_and_unit(value_string, 100, ae->old_value, ae->units, -1));
393 ae->new_value_string = strdupz(format_value_and_unit(value_string, 100, ae->new_value, ae->units, -1));
395 ae->old_status = old_status;
396 ae->new_status = new_status;
397 ae->duration = duration;
399 ae->delay_up_to_timestamp = when + delay;
403 if(ae->old_status == RRDCALC_STATUS_WARNING || ae->old_status == RRDCALC_STATUS_CRITICAL)
404 ae->non_clear_duration += ae->duration;
407 pthread_rwlock_wrlock(&host->health_log.alarm_log_rwlock);
408 ae->next = host->health_log.alarms;
409 host->health_log.alarms = ae;
410 host->health_log.count++;
411 pthread_rwlock_unlock(&host->health_log.alarm_log_rwlock);
413 // match previous alarms
414 pthread_rwlock_rdlock(&host->health_log.alarm_log_rwlock);
416 for(t = host->health_log.alarms ; t ; t = t->next) {
417 if(t != ae && t->alarm_id == ae->alarm_id) {
418 if(!(t->flags & HEALTH_ENTRY_FLAG_UPDATED) && !t->updated_by_id) {
419 t->flags |= HEALTH_ENTRY_FLAG_UPDATED;
420 t->updated_by_id = ae->unique_id;
421 ae->updates_id = t->unique_id;
423 if((t->new_status == RRDCALC_STATUS_WARNING || t->new_status == RRDCALC_STATUS_CRITICAL) &&
424 (t->old_status == RRDCALC_STATUS_WARNING || t->old_status == RRDCALC_STATUS_CRITICAL))
425 ae->non_clear_duration += t->non_clear_duration;
427 health_alarm_log_save(host, t);
430 // no need to continue
434 pthread_rwlock_unlock(&host->health_log.alarm_log_rwlock);
436 health_alarm_log_save(host, ae);
439 inline void health_alarm_log_free_one_nochecks_nounlink(ALARM_ENTRY *ae) {
444 freez(ae->recipient);
448 freez(ae->old_value_string);
449 freez(ae->new_value_string);
453 inline void health_alarm_log_free(RRDHOST *host) {
454 rrdhost_check_wrlock(host);
456 pthread_rwlock_wrlock(&host->health_log.alarm_log_rwlock);
459 while((ae = host->health_log.alarms)) {
460 host->health_log.alarms = ae->next;
461 health_alarm_log_free_one_nochecks_nounlink(ae);
464 pthread_rwlock_unlock(&host->health_log.alarm_log_rwlock);