@cryptotaxi247 / netdata-1 / commits / 22cfb9507

better detection of system suspension, to prevent alarms after wake up; fixes #1225, fixes #1296

Costa Tsaousis (ktsaou) committed Aug 13, 2017 at 10:59 UTC 22cfb95079eaea988c148ca55e0bc0a1e03bd35f
2 files changed +38 -21
src/health.c
+29 -19
@@ -323,6 +323,23 @@ static inline int rrdcalc_isrunnable(RRDCALC *rc, time_t now, time_t *next_run)
323 return 1;
324 }
325
326 +static inline int check_if_resumed_from_suspention(void) {
327 + static usec_t last_realtime = 0, last_monotonic = 0;
328 + usec_t realtime = now_realtime_usec(), monotonic = now_monotonic_usec();
329 + int ret = 0;
330 +
331 + // detect if monotonic and realtime have twice the difference
332 + // in which case we assume the system was just waken from hibernation
333 +
334 + if(last_realtime && last_monotonic && realtime - last_realtime > 2 * (monotonic - last_monotonic))
335 + ret = 1;
336 +
337 + last_realtime = realtime;
338 + last_monotonic = monotonic;
339 +
340 + return ret;
341 +}
342 +
343 void *health_main(void *ptr) {
344 struct netdata_static_thread *static_thread = (struct netdata_static_thread *)ptr;
345
@@ -339,11 +356,8 @@ void *health_main(void *ptr) {
356
357 BUFFER *wb = buffer_create(100);
358
342 - time_t now = now_realtime_sec();
343 - time_t now_boottime = now_boottime_sec();
344 - time_t last_now = now;
345 - time_t last_now_boottime = now_boottime;
346 - time_t hibernation_delay = config_get_number(CONFIG_SECTION_HEALTH, "postpone alarms during hibernation for seconds", 60);
359 + time_t now = now_realtime_sec();
360 + time_t hibernation_delay = config_get_number(CONFIG_SECTION_HEALTH, "postpone alarms during hibernation for seconds", 60);
361
362 unsigned int loop = 0;
363 while(!netdata_exit) {
@@ -354,21 +368,14 @@ void *health_main(void *ptr) {
368 time_t next_run = now + min_run_every;
369 RRDCALC *rc;
370
357 - // detect if boottime and realtime have twice the difference
358 - // in which case we assume the system was just waken from hibernation
359 - if(unlikely(now - last_now > 2 * (now_boottime - last_now_boottime))) {
371 + if(unlikely(check_if_resumed_from_suspention())) {
372 apply_hibernation_delay = 1;
373
362 - info("Postponing alarm checks for %ld seconds, due to boottime discrepancy (realtime dt: %ld, boottime dt: %ld)."
374 + info("Postponing alarm checks for %ld seconds, because it seems that the system was just resumed from suspension."
375 , hibernation_delay
364 - , (long)(now - last_now)
365 - , (long)(now_boottime - last_now_boottime)
376 );
377 }
378
369 - last_now = now;
370 - last_now_boottime = now_boottime;
371 -
379 if(unlikely(pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &oldstate) != 0))
380 error("Cannot set pthread cancel state to DISABLE.");
381
@@ -381,7 +388,7 @@ void *health_main(void *ptr) {
388
389 if(unlikely(apply_hibernation_delay)) {
390
384 - info("Postponing alarm checks for %ld seconds, on host '%s'."
391 + info("Postponing health checks for %ld seconds, on host '%s'."
392 , hibernation_delay
393 , host->hostname
394 );
@@ -389,8 +396,13 @@ void *health_main(void *ptr) {
396 host->health_delay_up_to = now + hibernation_delay;
397 }
398
392 - if(unlikely(!host->health_enabled || now < host->health_delay_up_to))
393 - continue;
399 + if(unlikely(host->health_delay_up_to)) {
400 + if(unlikely(now < host->health_delay_up_to))
401 + continue;
402 +
403 + info("Resuming health checks on host '%s'.", host->hostname);
404 + host->health_delay_up_to = 0;
405 + }
406
407 rrdhost_rdlock(host);
408
@@ -726,8 +738,6 @@ void *health_main(void *ptr) {
738 else
739 debug(D_HEALTH, "Health monitoring iteration no %u done. Next iteration now", loop);
740
729 - now_boottime = now_boottime_sec();
730 -
741 } // forever
742
743 buffer_free(wb);
src/rrdpush.c
+9 -2
@@ -648,8 +648,15 @@ static int rrdpush_receive(int fd, const char *key, const char *hostname, const
648
649 host->connected_senders++;
650 rrdhost_flag_clear(host, RRDHOST_ORPHAN);
651 - if(health_enabled != CONFIG_BOOLEAN_NO)
652 - host->health_delay_up_to = now_realtime_sec() + alarms_delay;
651 + if(health_enabled != CONFIG_BOOLEAN_NO) {
652 + if(alarms_delay > 0) {
653 + host->health_delay_up_to = now_realtime_sec() + alarms_delay;
654 + info("Postponing health checks for %ld seconds, on host '%s', because it was just connected."
655 + , alarms_delay
656 + , host->hostname
657 + );
658 + }
659 + }
660 rrdhost_unlock(host);
661
662 // call the plugins.d processor to receive the metrics