1
0

Merge pull request #25 from jodizzle/dev/swap

Add functions for measuring swap
This commit is contained in:
Aaron Marcher 2016-10-28 16:27:19 +02:00 committed by GitHub
commit bcd5732b04
3 changed files with 155 additions and 7 deletions

View File

@ -19,6 +19,7 @@ The following information is included:
- IP addresses - IP addresses
- Load averages - Load averages
- Memory status (free memory, percentage, total memory and used memory) - Memory status (free memory, percentage, total memory and used memory)
- Swap status (free swap, percentage, total swap and used swap)
- Temperature - Temperature
- Uptime - Uptime
- Volume percentage (ALSA) - Volume percentage (ALSA)

View File

@ -11,21 +11,25 @@
- battery_state (battery charging state) [argument: battery name] - battery_state (battery charging state) [argument: battery name]
- cpu_perc (cpu usage in percent) [argument: NULL] - cpu_perc (cpu usage in percent) [argument: NULL]
- datetime (date and time) [argument: format] - 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_perc (disk usage in percent) [argument: mountpoint]
- disk_total (disk usage in percent) [argument: mountpoint] - disk_total (total disk space in GB) [argument: mountpoint]
- disk_used (disk usage in percent) [argument: mountpoint] - disk_used (used disk space in GB) [argument: mountpoint]
- entropy (available entropy) [argument: NULL] - entropy (available entropy) [argument: NULL]
- gid (gid of current user) [argument: NULL] - gid (gid of current user) [argument: NULL]
- hostname [argument: NULL] - hostname [argument: NULL]
- ip (ip address) [argument: interface] - ip (ip address) [argument: interface]
- load_avg (load average) [argument: NULL] - 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_perc (ram usage in percent) [argument: NULL]
- ram_total (ram usage in percent) [argument: NULL] - ram_total (total ram in GB) [argument: NULL]
- ram_used (ram usage in percent) [argument: NULL] - ram_used (used ram in GB) [argument: NULL]
- run_command (run custom shell command) [argument: command] - 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] - uid (uid of current user) [argument: NULL]
- uptime (uptime) [argument: NULL] - uptime (uptime) [argument: NULL]
- username (username of current user) [argument: NULL] - username (username of current user) [argument: NULL]

View File

@ -57,6 +57,10 @@ static char *ram_perc(void);
static char *ram_used(void); static char *ram_used(void);
static char *ram_total(void); static char *ram_total(void);
static char *run_command(const char *cmd); 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 *temp(const char *file);
static char *uid(void); static char *uid(void);
static char *uptime(void); static char *uptime(void);
@ -417,6 +421,145 @@ run_command(const char *cmd)
return smprintf("%s", buf); 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 * static char *
temp(const char *file) temp(const char *file)
{ {