From e548219ebfdc772389bf5dbc9d90440badf12421 Mon Sep 17 00:00:00 2001 From: "C. McEnroe" Date: Mon, 17 Aug 2020 22:39:54 -0400 Subject: Log service uptime in status --- service.c | 39 ++++++++++++++++++++++++++++++++------- 1 file changed, 32 insertions(+), 7 deletions(-) diff --git a/service.c b/service.c index 39ce60e..0607f80 100644 --- a/service.c +++ b/service.c @@ -127,21 +127,46 @@ void serviceDrop(size_t index) { services.ptr[index] = services.ptr[--services.len]; } +static const char *humanize(struct timespec uptime) { + static char buf[256]; + int days = uptime.tv_sec / (24 * 60 * 60); + uptime.tv_sec %= (24 * 60 * 60); + int hours = uptime.tv_sec / (60 * 60); + uptime.tv_sec %= (60 * 60); + int mins = uptime.tv_sec / 60; + uptime.tv_sec %= 60; + int d, h, m, s; + snprintf( + buf, sizeof(buf), "%n%dd %n%dh %n%dm %n%ds", + &d, days, &h, hours, &m, mins, &s, (int)uptime.tv_sec + ); + if (days) return &buf[d]; + if (hours) return &buf[h]; + if (mins) return &buf[m]; + return &buf[s]; +} + void serviceStatus(struct Service *service) { + struct timespec now; + clock_gettime(CLOCK_MONOTONIC, &now); if (service->state == Stop && service->intent == Stop) { syslog(LOG_NOTICE, "%s[] is stopped", service->name); } else if (service->state == Stop && service->intent == Start) { - struct timespec now, timeleft; - clock_gettime(CLOCK_MONOTONIC, &now); + struct timespec timeleft; timespecsub(&service->restartDeadline, &now, &timeleft); syslog( - LOG_NOTICE, "%s[] is restarting in %lds", - service->name, (long)timeleft.tv_sec + LOG_NOTICE, "%s[] is restarting in %ds", + service->name, (int)timeleft.tv_sec ); } else if (service->state == Stop && service->intent == Restart) { syslog(LOG_NOTICE, "%s[] is restarting", service->name); } else if (service->state == Start && service->intent == Start) { - syslog(LOG_NOTICE, "%s[%d] is started", service->name, service->pid); + struct timespec uptime; + timespecsub(&now, &service->startTime, &uptime); + syslog( + LOG_NOTICE, "%s[%d] is started (up %s)", + service->name, service->pid, humanize(uptime) + ); } else if (service->state == Start && service->intent == Stop) { syslog(LOG_NOTICE, "%s[%d] is stopping", service->name, service->pid); } else if (service->state == Start && service->intent == Restart) { @@ -324,8 +349,8 @@ void serviceReap(pid_t pid, int status) { } setDeadline(service); syslog( - LOG_NOTICE, "%s[%d] restarting in %lds", - service->name, pid, (long)service->restartInterval.tv_sec + LOG_NOTICE, "%s[%d] restarting in %ds", + service->name, pid, (int)service->restartInterval.tv_sec ); } else { syslog(LOG_NOTICE, "%s[%d] stopped", service->name, pid); -- cgit 1.4.1