Log service uptime in status

This commit is contained in:
C. McEnroe 2020-08-17 22:39:54 -04:00
parent 533c95c321
commit e548219ebf

View File

@ -127,21 +127,46 @@ void serviceDrop(size_t index) {
services.ptr[index] = services.ptr[--services.len]; 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) { void serviceStatus(struct Service *service) {
struct timespec now;
clock_gettime(CLOCK_MONOTONIC, &now);
if (service->state == Stop && service->intent == Stop) { if (service->state == Stop && service->intent == Stop) {
syslog(LOG_NOTICE, "%s[] is stopped", service->name); syslog(LOG_NOTICE, "%s[] is stopped", service->name);
} else if (service->state == Stop && service->intent == Start) { } else if (service->state == Stop && service->intent == Start) {
struct timespec now, timeleft; struct timespec timeleft;
clock_gettime(CLOCK_MONOTONIC, &now);
timespecsub(&service->restartDeadline, &now, &timeleft); timespecsub(&service->restartDeadline, &now, &timeleft);
syslog( syslog(
LOG_NOTICE, "%s[] is restarting in %lds", LOG_NOTICE, "%s[] is restarting in %ds",
service->name, (long)timeleft.tv_sec service->name, (int)timeleft.tv_sec
); );
} else if (service->state == Stop && service->intent == Restart) { } else if (service->state == Stop && service->intent == Restart) {
syslog(LOG_NOTICE, "%s[] is restarting", service->name); syslog(LOG_NOTICE, "%s[] is restarting", service->name);
} else if (service->state == Start && service->intent == Start) { } 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) { } else if (service->state == Start && service->intent == Stop) {
syslog(LOG_NOTICE, "%s[%d] is stopping", service->name, service->pid); syslog(LOG_NOTICE, "%s[%d] is stopping", service->name, service->pid);
} else if (service->state == Start && service->intent == Restart) { } else if (service->state == Start && service->intent == Restart) {
@ -324,8 +349,8 @@ void serviceReap(pid_t pid, int status) {
} }
setDeadline(service); setDeadline(service);
syslog( syslog(
LOG_NOTICE, "%s[%d] restarting in %lds", LOG_NOTICE, "%s[%d] restarting in %ds",
service->name, pid, (long)service->restartInterval.tv_sec service->name, pid, (int)service->restartInterval.tv_sec
); );
} else { } else {
syslog(LOG_NOTICE, "%s[%d] stopped", service->name, pid); syslog(LOG_NOTICE, "%s[%d] stopped", service->name, pid);