Add support for path_exists directive.

This commit is contained in:
Kinware AB 2013-11-12 20:51:23 +01:00 committed by Michael Stapelberg
parent 98595f9f28
commit 717484184f
5 changed files with 69 additions and 2 deletions

View File

@ -210,6 +210,13 @@ int main(int argc, char *argv[]) {
CFG_END()
};
cfg_opt_t path_exists_opts[] = {
CFG_STR("path", NULL, CFGF_NONE),
CFG_STR("format", "%title: %status", CFGF_NONE),
CFG_CUSTOM_COLOR_OPTS,
CFG_END()
};
cfg_opt_t wireless_opts[] = {
CFG_STR("format_up", "W: (%quality at %essid, %bitrate) %ip", CFGF_NONE),
CFG_STR("format_down", "W: down", CFGF_NONE),
@ -298,6 +305,7 @@ int main(int argc, char *argv[]) {
CFG_STR_LIST("order", "{}", CFGF_NONE),
CFG_SEC("general", general_opts, CFGF_NONE),
CFG_SEC("run_watch", run_watch_opts, CFGF_TITLE | CFGF_MULTI),
CFG_SEC("path_exists", path_exists_opts, CFGF_TITLE | CFGF_MULTI),
CFG_SEC("wireless", wireless_opts, CFGF_TITLE | CFGF_MULTI),
CFG_SEC("ethernet", ethernet_opts, CFGF_TITLE | CFGF_MULTI),
CFG_SEC("battery", battery_opts, CFGF_TITLE | CFGF_MULTI),
@ -481,6 +489,12 @@ int main(int argc, char *argv[]) {
SEC_CLOSE_MAP;
}
CASE_SEC_TITLE("path_exists") {
SEC_OPEN_MAP("path_exists");
print_path_exists(json_gen, buffer, title, cfg_getstr(sec, "path"), cfg_getstr(sec, "format"));
SEC_CLOSE_MAP;
}
CASE_SEC_TITLE("disk") {
SEC_OPEN_MAP("disk_info");
print_disk_info(json_gen, buffer, title, cfg_getstr(sec, "format"));

View File

@ -152,6 +152,7 @@ void print_ddate(yajl_gen json_gen, char *buffer, const char *format, time_t t);
const char *get_ip_addr();
void print_wireless_info(yajl_gen json_gen, char *buffer, const char *interface, const char *format_up, const char *format_down);
void print_run_watch(yajl_gen json_gen, char *buffer, const char *title, const char *pidfile, const char *format);
void print_path_exists(yajl_gen json_gen, char *buffer, const char *title, const char *path, const char *format);
void print_cpu_temperature_info(yajl_gen json_gen, char *buffer, int zone, const char *path, const char *format, int);
void print_cpu_usage(yajl_gen json_gen, char *buffer, const char *format);
void print_eth_info(yajl_gen json_gen, char *buffer, const char *interface, const char *format_up, const char *format_down);

View File

@ -50,7 +50,8 @@ general {
order += "ipv6"
order += "disk /"
order += "run_watch DHCP"
order += "run_watch VPN"
order += "run_watch VPNC"
order += "path_exists VPN"
order += "wireless wlan0"
order += "ethernet eth0"
order += "battery 0"
@ -81,10 +82,16 @@ run_watch DHCP {
pidfile = "/var/run/dhclient*.pid"
}
run_watch VPN {
run_watch VPNC {
# file containing the PID of a vpnc process
pidfile = "/var/run/vpnc/pid"
}
path_exists VPN {
# path exists when a VPN tunnel launched by nmcli/nm-applet is active
path = "/proc/sys/net/ipv4/conf/tun0"
}
tztime local {
format = "%Y-%m-%d %H:%M:%S"
}
@ -193,6 +200,15 @@ a specific application, such as a VPN client or your DHCP client is running.
*Example format*: +%title: %status+
=== Path-exists
Checks if the given path exists in the filesystem. You can use this to check if
something is active, like for example a VPN tunnel managed by NetworkManager.
*Example order*: +path_exists VPN+
*Example format*: +%title: %status+
=== Wireless
Gets the link quality and ESSID of the given wireless network interface. You

View File

@ -6,6 +6,7 @@
#include <stdlib.h>
#include <unistd.h>
#include <sys/fcntl.h>
#include <sys/stat.h>
#include "i3status.h"

35
src/print_path_exists.c Normal file
View File

@ -0,0 +1,35 @@
#include <stdio.h>
#include <string.h>
#include <yajl/yajl_gen.h>
#include <yajl/yajl_version.h>
#include <sys/stat.h>
#include "i3status.h"
void print_path_exists(yajl_gen json_gen, char *buffer, const char *title, const char *path, const char *format) {
const char *walk;
char *outwalk = buffer;
struct stat st;
const bool exists = (stat(path, &st) == 0);
INSTANCE(path);
START_COLOR((exists ? "color_good" : "color_bad"));
for (walk = format; *walk != '\0'; walk++) {
if (*walk != '%') {
*(outwalk++) = *walk;
continue;
}
if (strncmp(walk+1, "title", strlen("title")) == 0) {
outwalk += sprintf(outwalk, "%s", title);
walk += strlen("title");
} else if (strncmp(walk+1, "status", strlen("status")) == 0) {
outwalk += sprintf(outwalk, "%s", (exists ? "yes" : "no"));
walk += strlen("status");
}
}
END_COLOR;
OUTPUT_FULL_TEXT(buffer);
}