From 3473beb868274bacea93e69036313d7ce9a9d55b Mon Sep 17 00:00:00 2001 From: Felix Kronlage Date: Fri, 12 Apr 2019 13:16:04 +0200 Subject: [PATCH] Gather statistics and LOG_INFO them upon SIGUSR1 Merges and closes #24. --- README.md | 2 ++ endlessh.c | 45 ++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 44 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index b13d12c..d12e902 100644 --- a/README.md +++ b/README.md @@ -45,6 +45,8 @@ write a complete, consistent log. A SIGHUP signal requests a reload of the configuration file (`-f`). +A SIGUSR1 signal will print connections stats to standard output. + ## Sample Configuration File The configuration file has similar syntax to OpenSSH. diff --git a/endlessh.c b/endlessh.c index 2ca11cf..80b4f16 100644 --- a/endlessh.c +++ b/endlessh.c @@ -77,6 +77,14 @@ struct client { int fd; }; +struct stats { + long long connects; + long long bytes_wasted; + long long time_wasted; +}; + +struct stats *s; + static struct client * client_new(int fd, long long send_next) { @@ -130,6 +138,7 @@ client_destroy(struct client *client) client->ipaddr, client->port, client->fd, dt / 1000, dt % 1000, client->bytes_sent); + s->time_wasted += dt / 1000; close(client->fd); free(client); } @@ -228,6 +237,15 @@ sighup_handler(int signal) reload = 1; } +static volatile sig_atomic_t dumpstats = 0; + +static void +sigusr1_handler(int signal) +{ + (void)signal; + dumpstats = 1; +} + struct config { int port; int delay; @@ -551,11 +569,13 @@ sendline(struct client *client, int max_line_length, unsigned long *rng) } } else { client->bytes_sent += out; + s->bytes_wasted += out; return client; } } } + int main(int argc, char **argv) { @@ -563,6 +583,11 @@ main(int argc, char **argv) const char *config_file = DEFAULT_CONFIG_FILE; config_load(&config, config_file, 1); + s = malloc(sizeof(*s)); + s->connects = 0; + s->bytes_wasted = 0; + s->time_wasted = 0; + int option; while ((option = getopt(argc, argv, "46d:f:hl:m:p:vV")) != -1) { switch (option) { @@ -628,6 +653,12 @@ main(int argc, char **argv) if (r == -1) die(); } + { + struct sigaction sa = {.sa_handler = sigusr1_handler}; + int r = sigaction(SIGUSR1, &sa, 0); + if (r == -1) + die(); + } struct fifo fifo[1]; fifo_init(fifo); @@ -649,6 +680,12 @@ main(int argc, char **argv) } reload = 0; } + if (dumpstats) { + /* print stats requested (SIGUSR1) */ + logmsg(LOG_INFO, "Connections received in total: %lld\tWasted seconds total: %lld\tWasted bytes total: %lld", + s->connects, s->time_wasted, s->bytes_wasted); + dumpstats = 0; + } /* Enqueue clients that are due for another message */ int timeout = -1; @@ -687,6 +724,7 @@ main(int argc, char **argv) if (fds.revents & POLLIN) { int fd = accept(server, 0, 0); logmsg(LOG_DEBUG, "accept() = %d", fd); + s->connects++; if (fd == -1) { const char *msg = strerror(errno); switch (errno) { @@ -718,12 +756,13 @@ main(int argc, char **argv) close(fd); } fifo_append(fifo, client); - logmsg(LOG_INFO, "ACCEPT host=%s port=%d fd=%d n=%d/%d", + logmsg(LOG_INFO, "ACCEPT host=%s port=%d fd=%d n=%d/%d/%lld", client->ipaddr, client->port, client->fd, - fifo->length, config.max_clients); + fifo->length, config.max_clients, s->connects); } } } - + 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); }