diff options
author | Oliver Smith <osmith@sysmocom.de> | 2021-07-09 17:19:32 +0200 |
---|---|---|
committer | Oliver Smith <osmith@sysmocom.de> | 2021-07-15 12:50:24 +0200 |
commit | d243c2ad738b12db8d6d06c0d37cb66a6045a3dd (patch) | |
tree | 0994643a71e6af670fdaa4153ed609f7b2f08c01 /src | |
parent | a140d44a4f1c362f8dc0bc25d876852a455eed6a (diff) |
vty: add "show uptime"
Related: OS#4609
Change-Id: Ic7d9d5d3c8a9abd06a7e626aed7679f68406040c
Diffstat (limited to 'src')
-rw-r--r-- | src/vty/command.c | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/src/vty/command.c b/src/vty/command.c index de89337d..67b4001d 100644 --- a/src/vty/command.c +++ b/src/vty/command.c @@ -46,6 +46,7 @@ Boston, MA 02110-1301, USA. */ #include <osmocom/vty/command.h> #include <osmocom/core/talloc.h> +#include <osmocom/core/timer.h> #include <osmocom/core/utils.h> #ifndef MAXPATHLEN @@ -63,6 +64,9 @@ Boston, MA 02110-1301, USA. */ void *tall_vty_cmd_ctx; +/* Set by on_dso_load_starttime() for "show uptime". */ +static struct timespec starttime; + /* Command vector which includes some level of command lists. Normally each daemon maintains each own cmdvec. */ vector cmdvec; @@ -3037,6 +3041,23 @@ DEFUN(show_online_help, return CMD_SUCCESS; } +DEFUN(show_uptime, + show_uptime_cmd, "show uptime", SHOW_STR "Displays how long the program has been running\n") +{ + struct timespec now; + osmo_clock_gettime(CLOCK_MONOTONIC, &now); + + time_t uptime = now.tv_sec - starttime.tv_sec; + int d = uptime / (3600 * 24); + int h = uptime / 3600 % 24; + int m = uptime / 60 % 60; + int s = uptime % 60; + + vty_out(vty, "%s has been running for %dd %dh %dm %ds%s", host.app_info->name, d, h, m, s, VTY_NEWLINE); + + return CMD_SUCCESS; +} + /* Help display function for all node. */ gDEFUN(config_help, config_help_cmd, "help", "Description of the interactive help system\n") @@ -4346,6 +4367,7 @@ void cmd_init(int terminal) install_node(&config_node, config_write_host); /* Each node's basic commands. */ + install_lib_element(VIEW_NODE, &show_uptime_cmd); install_lib_element(VIEW_NODE, &show_version_cmd); install_lib_element(VIEW_NODE, &show_online_help_cmd); if (terminal) { @@ -4399,6 +4421,11 @@ void cmd_init(int terminal) srand(time(NULL)); } +static __attribute__((constructor)) void on_dso_load_starttime(void) +{ + osmo_clock_gettime(CLOCK_MONOTONIC, &starttime); +} + /* FIXME: execute this section in the unit test instead */ static __attribute__((constructor)) void on_dso_load(void) { |