summaryrefslogtreecommitdiff
path: root/slstatus.c
diff options
context:
space:
mode:
authorAaron Marcher <info@nulltime.net>2016-10-28 16:27:19 +0200
committerGitHub <noreply@github.com>2016-10-28 16:27:19 +0200
commitbcd5732b04d5b8b6572b2d1f122a2762316ea476 (patch)
tree51430783ba27a7e7a7fc7768946b1c873387e4f8 /slstatus.c
parent59ed3b6d8d594e00d72ecd3beadb512f797146ee (diff)
parent9bef4e889a547ed909a23bb57489bc71da80e472 (diff)
Merge pull request #25 from jodizzle/dev/swap
Add functions for measuring swap
Diffstat (limited to 'slstatus.c')
-rw-r--r--slstatus.c143
1 files changed, 143 insertions, 0 deletions
diff --git a/slstatus.c b/slstatus.c
index efdc9b0..9559226 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);
@@ -418,6 +422,145 @@ run_command(const char *cmd)
}
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 *
temp(const char *file)
{
int temp;