Tweak SIGUSR1 statistics totals logging

* Don't dynamically allocate the struct since that's not necessary.
* Use a more concise log message.
* Include current clients when counting the time.
* Print final statistics message *after* closing all clients.
* Don't include total connections in ACCEPT.
* Mention SIGUSR1 in the new man page, too.

Adjusts #24.
This commit is contained in:
Christopher Wellons 2019-04-12 18:46:53 -04:00
parent c50585f759
commit 6f621b90b1
3 changed files with 31 additions and 26 deletions

View File

@ -45,7 +45,7 @@ write a complete, consistent log.
A SIGHUP signal requests a reload of the configuration file (`-f`). A SIGHUP signal requests a reload of the configuration file (`-f`).
A SIGUSR1 signal will print connections stats to standard output. A SIGUSR1 signal will print connections stats to the log.
## Sample Configuration File ## Sample Configuration File

View File

@ -70,9 +70,9 @@ If
receives the SIGTERM signal it will gracefully shut receives the SIGTERM signal it will gracefully shut
down the daemon, allowing it to write a complete, consistent log. down the daemon, allowing it to write a complete, consistent log.
.Pp .Pp
A SIGHUP signal requests A SIGHUP signal requests a reload of its configuration file.
.Nm .Pp
a reload of its configuration file. A SIGUSR1 signal will print connections stats to the log.
.Sh FILES .Sh FILES
.Bl -tag -width /etc/endlessh/config -compact .Bl -tag -width /etc/endlessh/config -compact
.It Pa /etc/endlessh/config .It Pa /etc/endlessh/config

View File

@ -67,6 +67,12 @@ logmsg(enum loglevel level, const char *format, ...)
} }
} }
struct {
long long connects;
long long milliseconds;
long long bytes_sent;
} statistics;
struct client { struct client {
char ipaddr[INET6_ADDRSTRLEN]; char ipaddr[INET6_ADDRSTRLEN];
long long connect_time; long long connect_time;
@ -77,14 +83,6 @@ struct client {
int fd; int fd;
}; };
struct stats {
long long connects;
long long bytes_wasted;
long long time_wasted;
};
struct stats *s;
static struct client * static struct client *
client_new(int fd, long long send_next) client_new(int fd, long long send_next)
{ {
@ -138,11 +136,24 @@ client_destroy(struct client *client)
client->ipaddr, client->port, client->fd, client->ipaddr, client->port, client->fd,
dt / 1000, dt % 1000, dt / 1000, dt % 1000,
client->bytes_sent); client->bytes_sent);
s->time_wasted += dt / 1000; statistics.milliseconds += dt;
close(client->fd); close(client->fd);
free(client); free(client);
} }
static void
statistics_log_totals(struct client *clients)
{
long long milliseconds = statistics.milliseconds;
for (long long now = epochms(); clients; clients = clients->next)
milliseconds += now - clients->connect_time;
logmsg(LOG_INFO, "TOTALS connects=%lld seconds=%lld.%lld bytes=%lld",
statistics.connects,
milliseconds / 1000,
milliseconds % 1000,
statistics.bytes_sent);
}
struct fifo { struct fifo {
struct client *head; struct client *head;
struct client *tail; struct client *tail;
@ -569,7 +580,7 @@ sendline(struct client *client, int max_line_length, unsigned long *rng)
} }
} else { } else {
client->bytes_sent += out; client->bytes_sent += out;
s->bytes_wasted += out; statistics.bytes_sent += out;
return client; return client;
} }
} }
@ -583,11 +594,6 @@ main(int argc, char **argv)
const char *config_file = DEFAULT_CONFIG_FILE; const char *config_file = DEFAULT_CONFIG_FILE;
config_load(&config, config_file, 1); config_load(&config, config_file, 1);
s = malloc(sizeof(*s));
s->connects = 0;
s->bytes_wasted = 0;
s->time_wasted = 0;
int option; int option;
while ((option = getopt(argc, argv, "46d:f:hl:m:p:vV")) != -1) { while ((option = getopt(argc, argv, "46d:f:hl:m:p:vV")) != -1) {
switch (option) { switch (option) {
@ -682,8 +688,7 @@ main(int argc, char **argv)
} }
if (dumpstats) { if (dumpstats) {
/* print stats requested (SIGUSR1) */ /* print stats requested (SIGUSR1) */
logmsg(LOG_INFO, "Connections received in total: %lld\tWasted seconds total: %lld\tWasted bytes total: %lld", statistics_log_totals(fifo->head);
s->connects, s->time_wasted, s->bytes_wasted);
dumpstats = 0; dumpstats = 0;
} }
@ -724,7 +729,7 @@ main(int argc, char **argv)
if (fds.revents & POLLIN) { if (fds.revents & POLLIN) {
int fd = accept(server, 0, 0); int fd = accept(server, 0, 0);
logmsg(LOG_DEBUG, "accept() = %d", fd); logmsg(LOG_DEBUG, "accept() = %d", fd);
s->connects++; statistics.connects++;
if (fd == -1) { if (fd == -1) {
const char *msg = strerror(errno); const char *msg = strerror(errno);
switch (errno) { switch (errno) {
@ -756,13 +761,13 @@ main(int argc, char **argv)
close(fd); close(fd);
} }
fifo_append(fifo, client); fifo_append(fifo, client);
logmsg(LOG_INFO, "ACCEPT host=%s port=%d fd=%d n=%d/%d/%lld", logmsg(LOG_INFO, "ACCEPT host=%s port=%d fd=%d n=%d/%d",
client->ipaddr, client->port, client->fd, client->ipaddr, client->port, client->fd,
fifo->length, config.max_clients, s->connects); fifo->length, config.max_clients);
} }
} }
} }
logmsg(LOG_INFO, "Connections received in total: %lld\tWasted seconds total: %lld\tWasted bytes total: %lld",
s->connects, s->time_wasted, s->bytes_wasted);
fifo_destroy(fifo); fifo_destroy(fifo);
statistics_log_totals(0);
} }