mirror of https://github.com/pikami/slstatus.git
Merge pull request #25 from jodizzle/dev/swap
Add functions for measuring swap
This commit is contained in:
commit
bcd5732b04
|
@ -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)
|
||||
|
|
18
config.def.h
18
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]
|
||||
|
|
143
slstatus.c
143
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,145 @@ run_command(const char *cmd)
|
|||
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 *
|
||||
temp(const char *file)
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue