Gather statistics and LOG_INFO them upon SIGUSR1

Merges and closes #24.
This commit is contained in:
Felix Kronlage 2019-04-12 13:16:04 +02:00 committed by Christopher Wellons
parent f8398093b1
commit 3473beb868
2 changed files with 44 additions and 3 deletions

View File

@ -45,6 +45,8 @@ 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.
## Sample Configuration File ## Sample Configuration File
The configuration file has similar syntax to OpenSSH. The configuration file has similar syntax to OpenSSH.

View File

@ -77,6 +77,14 @@ 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)
{ {
@ -130,6 +138,7 @@ 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;
close(client->fd); close(client->fd);
free(client); free(client);
} }
@ -228,6 +237,15 @@ sighup_handler(int signal)
reload = 1; reload = 1;
} }
static volatile sig_atomic_t dumpstats = 0;
static void
sigusr1_handler(int signal)
{
(void)signal;
dumpstats = 1;
}
struct config { struct config {
int port; int port;
int delay; int delay;
@ -551,11 +569,13 @@ 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;
return client; return client;
} }
} }
} }
int int
main(int argc, char **argv) main(int argc, char **argv)
{ {
@ -563,6 +583,11 @@ 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) {
@ -628,6 +653,12 @@ main(int argc, char **argv)
if (r == -1) if (r == -1)
die(); die();
} }
{
struct sigaction sa = {.sa_handler = sigusr1_handler};
int r = sigaction(SIGUSR1, &sa, 0);
if (r == -1)
die();
}
struct fifo fifo[1]; struct fifo fifo[1];
fifo_init(fifo); fifo_init(fifo);
@ -649,6 +680,12 @@ main(int argc, char **argv)
} }
reload = 0; 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 */ /* Enqueue clients that are due for another message */
int timeout = -1; int timeout = -1;
@ -687,6 +724,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++;
if (fd == -1) { if (fd == -1) {
const char *msg = strerror(errno); const char *msg = strerror(errno);
switch (errno) { switch (errno) {
@ -718,12 +756,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", logmsg(LOG_INFO, "ACCEPT host=%s port=%d fd=%d n=%d/%d/%lld",
client->ipaddr, client->port, client->fd, 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); fifo_destroy(fifo);
} }