From: Jonathan McDowell Date: Sun, 10 Apr 2011 17:47:35 +0000 (-0700) Subject: Add a stats command to keyd X-Git-Url: https://git.sommitrealweird.co.uk/onak.git/commitdiff_plain/7a0ffbb5910bd782ef569b63ce665659e3564e72?ds=sidebyside Add a stats command to keyd Add KEYD_CMD_STATS to obtain info about when keyd started, how many connects it has seen and how many instances of each command. --- diff --git a/keyd.c b/keyd.c index 1a349a6..5d5b7ba 100644 --- a/keyd.c +++ b/keyd.c @@ -17,6 +17,7 @@ #include #include #include +#include #include #include "charfuncs.h" @@ -31,6 +32,8 @@ #include "parsekey.h" #include "version.h" +static struct keyd_stats *stats; + void daemonize(void) { pid_t pid; @@ -150,6 +153,11 @@ int sock_do(int fd) } if (ret == 0) { + if (cmd < KEYD_CMD_LAST) { + stats->command_stats[cmd]++; + } else { + stats->command_stats[KEYD_CMD_UNKNOWN]++; + } switch (cmd) { case KEYD_CMD_VERSION: cmd = KEYD_REPLY_OK; @@ -341,6 +349,14 @@ int sock_do(int fd) ret = 1; trytocleanup(); break; + case KEYD_CMD_STATS: + cmd = KEYD_REPLY_OK; + write(fd, &cmd, sizeof(cmd)); + cmd = sizeof(*stats); + write(fd, &cmd, sizeof(cmd)); + write(fd, stats, + sizeof(*stats)); + break; default: logthing(LOGTHING_ERROR, "Got unknown command: %d", cmd); @@ -372,6 +388,7 @@ int sock_accept(int fd) } if (ret != -1) { + stats->connects++; while (!sock_do(srv)) ; sock_close(srv); } @@ -429,6 +446,15 @@ int main(int argc, char *argv[]) catchsignals(); signal(SIGPIPE, SIG_IGN); + + stats = calloc(1, sizeof(*stats)); + if (!stats) { + logthing(LOGTHING_ERROR, + "Couldn't allocate memory for stats structure."); + exit(EXIT_FAILURE); + } + stats->started = time(NULL); + snprintf(sockname, 1023, "%s/%s", config.db_dir, KEYD_SOCKET); fd = sock_init(sockname); @@ -449,6 +475,8 @@ int main(int argc, char *argv[]) unlink(sockname); } + free(stats); + cleanuplogthing(); cleanupconfig(); diff --git a/keyd.h b/keyd.h index 30af955..ce19d8d 100644 --- a/keyd.h +++ b/keyd.h @@ -24,6 +24,7 @@ enum keyd_ops { KEYD_CMD_KEYITER, KEYD_CMD_CLOSE, KEYD_CMD_QUIT, + KEYD_CMD_STATS, KEYD_CMD_LAST /* Placeholder */ }; @@ -34,4 +35,10 @@ enum keyd_reply { static uint32_t keyd_version = 2; +struct keyd_stats { + time_t started; + uint32_t connects; + uint32_t command_stats[KEYD_CMD_LAST]; +}; + #endif /* __KEYD_H__ */ diff --git a/keydctl.8 b/keydctl.8 index 44e7ba6..425d81e 100644 --- a/keydctl.8 +++ b/keydctl.8 @@ -32,8 +32,9 @@ otherwise. Outputs nothing to stdout/stderr. Request that keyd exits cleanly .TP .B status -Display the status of a running keyd. Currently just the protocol version -in use. +Display the status of a running keyd. Currently the protocol version in use, +when keyd was started, the number of client connections seen and a breakdown +of the commands seen. .SH FILES .br .nf diff --git a/keydctl.c b/keydctl.c index 96af7d2..c66ce28 100644 --- a/keydctl.c +++ b/keydctl.c @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include @@ -138,10 +139,37 @@ static void keyd_close(void) static void keyd_status(void) { uint32_t reply; + struct keyd_stats stats; keyd_do_command(KEYD_CMD_VERSION, &reply, sizeof(reply)); printf("Using keyd protocol version %d.\n", reply); + keyd_do_command(KEYD_CMD_STATS, &stats, sizeof(stats)); + printf("keyd running since %s", ctime(&stats.started)); + printf("%d client connections received\n", stats.connects); + + printf("Command statistics:\n"); + printf(" Version: %d\n", + stats.command_stats[KEYD_CMD_VERSION]); + printf(" Get key: %d\n", stats.command_stats[KEYD_CMD_GET]); + printf(" Store key: %d\n", + stats.command_stats[KEYD_CMD_STORE]); + printf(" Delete key: %d\n", + stats.command_stats[KEYD_CMD_DELETE]); + printf(" Search key: %d\n", + stats.command_stats[KEYD_CMD_GETTEXT]); + printf(" Get full keyid: %d\n", + stats.command_stats[KEYD_CMD_GETFULLKEYID]); + printf(" Iterate all keys: %d\n", + stats.command_stats[KEYD_CMD_KEYITER]); + printf(" Close: %d\n", + stats.command_stats[KEYD_CMD_CLOSE]); + printf(" Quit: %d\n", stats.command_stats[KEYD_CMD_QUIT]); + printf(" Get statistics: %d\n", + stats.command_stats[KEYD_CMD_STATS]); + printf(" Unknown: %d\n", + stats.command_stats[KEYD_CMD_UNKNOWN]); + return; }