From c60af317f3cb49381258c2480636a7b390b9ff3b Mon Sep 17 00:00:00 2001 From: Jody Leonard Date: Fri, 14 Oct 2016 05:44:46 -0400 Subject: [PATCH 1/4] Add functions for measuring swap --- slstatus.c | 128 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 128 insertions(+) diff --git a/slstatus.c b/slstatus.c index efdc9b0..f39a2dc 100644 --- a/slstatus.c +++ b/slstatus.c @@ -57,6 +57,10 @@ static char *ram_perc(void); static char *ram_used(void); static char *ram_total(void); static char *run_command(const char *cmd); +static char *swap_free(void); +static char *swap_perc(void); +static char *swap_used(void); +static char *swap_total(void); static char *temp(const char *file); static char *uid(void); static char *uptime(void); @@ -417,6 +421,130 @@ run_command(const char *cmd) return smprintf("%s", buf); } +static char * +swap_free(void) +{ + long free; + FILE *fp; + char buf[2048]; + size_t bytes_read; + char *match; + + fp = fopen("/proc/meminfo", "r"); + if (fp == NULL) { + warn("Failed to open file /proc/meminfo"); + return smprintf(UNKNOWN_STR); + } + bytes_read = fread(buf, sizeof(char), sizeof(buf), fp); + buf[bytes_read] = '\0'; + fclose(fp); + if (bytes_read == 0 || bytes_read == sizeof(buf)) { + warn("Failed to read /proc/meminfo\n"); + return smprintf(UNKNOWN_STR); + } + + match = strstr(buf, "SwapFree"); + sscanf(match, "SwapFree: %ld kB\n", &free); + + return smprintf("%f", (float)free / 1024 / 1024); +} + +static char * +swap_perc(void) +{ + long total, free, cached; + FILE *fp; + char buf[2048]; + size_t bytes_read; + char *match; + + fp = fopen("/proc/meminfo", "r"); + if (fp == NULL) { + warn("Failed to open file /proc/meminfo"); + return smprintf(UNKNOWN_STR); + } + bytes_read = fread(buf, sizeof(char), sizeof(buf), fp); + buf[bytes_read] = '\0'; + fclose(fp); + if (bytes_read == 0 || bytes_read == sizeof(buf)) { + warn("Failed to read /proc/meminfo\n"); + return smprintf(UNKNOWN_STR); + } + + match = strstr(buf, "SwapCached"); + sscanf(match, "SwapCached: %ld kB\n", &cached); + + match = strstr(buf, "SwapTotal"); + sscanf(match, "SwapTotal: %ld kB\n", &total); + + match = strstr(buf, "SwapFree"); + sscanf(match, "SwapFree: %ld kB\n", &free); + + return smprintf("%d%%", 100 * (total - free - cached) / total); +} + +static char * +swap_total(void) +{ + long total; + FILE *fp; + char buf[2048]; + size_t bytes_read; + char *match; + + fp = fopen("/proc/meminfo", "r"); + if (fp == NULL) { + warn("Failed to open file /proc/meminfo"); + return smprintf(UNKNOWN_STR); + } + bytes_read = fread(buf, sizeof(char), sizeof(buf), fp); + buf[bytes_read] = '\0'; + fclose(fp); + if (bytes_read == 0 || bytes_read == sizeof(buf)) { + warn("Failed to read /proc/meminfo\n"); + return smprintf(UNKNOWN_STR); + } + + match = strstr(buf, "SwapTotal"); + sscanf(match, "SwapTotal: %ld kB\n", &total); + + return smprintf("%f", (float)total / 1024 / 1024); +} + +static char * +swap_used(void) +{ + long total, free, cached; + FILE *fp; + char buf[2048]; + size_t bytes_read; + char *match; + + fp = fopen("/proc/meminfo", "r"); + if (fp == NULL) { + warn("Failed to open file /proc/meminfo"); + return smprintf(UNKNOWN_STR); + } + bytes_read = fread(buf, sizeof(char), sizeof(buf), fp); + buf[bytes_read] = '\0'; + fclose(fp); + if (bytes_read == 0 || bytes_read == sizeof(buf)) { + warn("Failed to read /proc/meminfo\n"); + return smprintf(UNKNOWN_STR); + } + + match = strstr(buf, "SwapCached"); + sscanf(match, "SwapCached: %ld kB\n", &cached); + + match = strstr(buf, "SwapTotal"); + sscanf(match, "SwapTotal: %ld kB\n", &total); + + match = strstr(buf, "SwapFree"); + sscanf(match, "SwapFree: %ld kB\n", &free); + + return smprintf("%f", (float)(total - free - cached) / 1024 / 1024); +} + static char * temp(const char *file) { From 46fb6abdc04cf5833ca9eb1d78e24f505edf142c Mon Sep 17 00:00:00 2001 From: Jody Leonard Date: Sat, 15 Oct 2016 06:36:21 -0400 Subject: [PATCH 2/4] Edit config.def.h function descriptions --- config.def.h | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/config.def.h b/config.def.h index 853adb6..bc2835c 100644 --- a/config.def.h +++ b/config.def.h @@ -11,21 +11,25 @@ - battery_state (battery charging state) [argument: battery name] - cpu_perc (cpu usage in percent) [argument: NULL] - datetime (date and time) [argument: format] -- disk_free (disk usage in percent) [argument: mountpoint] +- disk_free (free disk space in GB) [argument: mountpoint] - disk_perc (disk usage in percent) [argument: mountpoint] -- disk_total (disk usage in percent) [argument: mountpoint] -- disk_used (disk usage in percent) [argument: mountpoint] +- disk_total (total disk space in GB) [argument: mountpoint] +- disk_used (used disk space in GB) [argument: mountpoint] - entropy (available entropy) [argument: NULL] - gid (gid of current user) [argument: NULL] - hostname [argument: NULL] - ip (ip address) [argument: interface] - load_avg (load average) [argument: NULL] -- ram_free (ram usage in percent) [argument: NULL] +- ram_free (free ram in GB) [argument: NULL] - ram_perc (ram usage in percent) [argument: NULL] -- ram_total (ram usage in percent) [argument: NULL] -- ram_used (ram usage in percent) [argument: NULL] +- ram_total (total ram in GB) [argument: NULL] +- ram_used (used ram in GB) [argument: NULL] - run_command (run custom shell command) [argument: command] -- temp (temperature in degrees) [argument: temperature file] +- swap_free (free swap in GB) [argument: NULL] +- swap_perc (swap usage in percent) [argument: NULL] +- swap_total (total swap in GB) [argument: NULL] +- swap_used (used swap in GB) [argument: NULL] +- temp (temperature in celsius) [argument: temperature file] - uid (uid of current user) [argument: NULL] - uptime (uptime) [argument: NULL] - username (username of current user) [argument: NULL] From 4d6761e0c70942d5cab055bf12ebc6ac920d39b5 Mon Sep 17 00:00:00 2001 From: Jody Leonard Date: Sat, 15 Oct 2016 06:49:33 -0400 Subject: [PATCH 3/4] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 7bb3dc3..d2de582 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,7 @@ The following information is included: - IP addresses - Load averages - Memory status (free memory, percentage, total memory and used memory) +- Swap status (free swap, percentage, total swap and used swap) - Temperature - Uptime - Volume percentage (ALSA) From 9bef4e889a547ed909a23bb57489bc71da80e472 Mon Sep 17 00:00:00 2001 From: Jody Leonard Date: Sat, 15 Oct 2016 19:50:02 -0400 Subject: [PATCH 4/4] swap: Print UNKNOWN_STR if no swap is allocated --- slstatus.c | 29 ++++++++++++++++++++++------- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/slstatus.c b/slstatus.c index f39a2dc..9559226 100644 --- a/slstatus.c +++ b/slstatus.c @@ -424,7 +424,7 @@ run_command(const char *cmd) static char * swap_free(void) { - long free; + long total, free; FILE *fp; char buf[2048]; size_t bytes_read; @@ -443,6 +443,12 @@ swap_free(void) return smprintf(UNKNOWN_STR); } + match = strstr(buf, "SwapTotal"); + sscanf(match, "SwapTotal: %ld kB\n", &total); + if (total == 0) { + return smprintf(UNKNOWN_STR); + } + match = strstr(buf, "SwapFree"); sscanf(match, "SwapFree: %ld kB\n", &free); @@ -471,11 +477,14 @@ swap_perc(void) return smprintf(UNKNOWN_STR); } - match = strstr(buf, "SwapCached"); - sscanf(match, "SwapCached: %ld kB\n", &cached); - match = strstr(buf, "SwapTotal"); sscanf(match, "SwapTotal: %ld kB\n", &total); + if (total == 0) { + return smprintf(UNKNOWN_STR); + } + + match = strstr(buf, "SwapCached"); + sscanf(match, "SwapCached: %ld kB\n", &cached); match = strstr(buf, "SwapFree"); sscanf(match, "SwapFree: %ld kB\n", &free); @@ -507,6 +516,9 @@ swap_total(void) match = strstr(buf, "SwapTotal"); sscanf(match, "SwapTotal: %ld kB\n", &total); + if (total == 0) { + return smprintf(UNKNOWN_STR); + } return smprintf("%f", (float)total / 1024 / 1024); } @@ -533,11 +545,14 @@ swap_used(void) return smprintf(UNKNOWN_STR); } - match = strstr(buf, "SwapCached"); - sscanf(match, "SwapCached: %ld kB\n", &cached); - match = strstr(buf, "SwapTotal"); sscanf(match, "SwapTotal: %ld kB\n", &total); + if (total == 0) { + return smprintf(UNKNOWN_STR); + } + + match = strstr(buf, "SwapCached"); + sscanf(match, "SwapCached: %ld kB\n", &cached); match = strstr(buf, "SwapFree"); sscanf(match, "SwapFree: %ld kB\n", &free);