all repos — slstatus @ bcd5732b04d5b8b6572b2d1f122a2762316ea476

my build of slstatus (tools.suckless.org/slstatus/)

Merge pull request #25 from jodizzle/dev/swap

Add functions for measuring swap
Aaron Marcher info@nulltime.net
Fri, 28 Oct 2016 16:27:19 +0200
commit

bcd5732b04d5b8b6572b2d1f122a2762316ea476

parent

59ed3b6d8d594e00d72ecd3beadb512f797146ee

3 files changed, 155 insertions(+), 7 deletions(-)

jump to
M README.mdREADME.md

@@ -19,6 +19,7 @@ - Hostname

- 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)
M config.def.hconfig.def.h

@@ -11,21 +11,25 @@ - battery_perc (battery percentage) [argument: battery name]

- 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]
M slstatus.cslstatus.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);

@@ -415,6 +419,145 @@ buf[strlen(buf)] = '\0';

strtok(buf, "\n"); return smprintf("%s", buf); +} + +static char * +swap_free(void) +{ + long total, 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, "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); + + 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, "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); + + 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); + if (total == 0) { + return smprintf(UNKNOWN_STR); + } + + 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, "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); + + return smprintf("%f", (float)(total - free - cached) / 1024 / 1024); } static char *