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];
}
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);