diff --git a/bumblebee/modules/disk.py b/bumblebee/modules/disk.py new file mode 100644 index 0000000..5f4739a --- /dev/null +++ b/bumblebee/modules/disk.py @@ -0,0 +1,25 @@ +import os +import bumblebee.util +import bumblebee.module + +class Module(bumblebee.module.Module): + def __init__(self, args): + super(Module, self).__init__(args) + self._path = args[0] if args else "/" + + def data(self): + st = os.statvfs(self._path) + + self._size = st.f_frsize*st.f_blocks + self._free = st.f_frsize*st.f_bavail + self._perc = 100.0*self._free/self._size + + return "{} {}/{} ({:05.02f}%)".format(self._path, bumblebee.util.bytefmt(self._free), bumblebee.util.bytefmt(self._size), self._perc) + + def warning(self): + return self._perc < 20 + + def critical(self): + return self._perc < 10 + +# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 diff --git a/bumblebee/modules/memory.py b/bumblebee/modules/memory.py index 350effd..89f96a1 100644 --- a/bumblebee/modules/memory.py +++ b/bumblebee/modules/memory.py @@ -1,12 +1,6 @@ -import bumblebee.module import psutil - -def fmt(num, suffix='B'): - for unit in [ "", "Ki", "Mi", "Gi" ]: - if num < 1024.0: - return "{:.2f}{}{}".format(num, unit, suffix) - num /= 1024.0 - return "{:05.2f%}{}{}".format(num, "Gi", suffix) +import bumblebee.module +import bumblebee.util class Module(bumblebee.module.Module): def __init__(self, args): @@ -19,7 +13,7 @@ class Module(bumblebee.module.Module): free = self._mem.available total = self._mem.total - return "{}/{} ({:05.02f}%)".format(fmt(self._mem.available), fmt(self._mem.total), 100.0 - self._mem.percent) + return "{}/{} ({:05.02f}%)".format(bumblebee.util.bytefmt(self._mem.available), bumblebee.util.bytefmt(self._mem.total), 100.0 - self._mem.percent) def warning(self): return self._mem.percent < 20 diff --git a/bumblebee/modules/nic.py b/bumblebee/modules/nic.py index 5d23483..f5b341c 100644 --- a/bumblebee/modules/nic.py +++ b/bumblebee/modules/nic.py @@ -19,11 +19,15 @@ class Module(bumblebee.module.Module): self._state = "down" addr = [] - if netifaces.AF_INET in netifaces.ifaddresses(self._intf): - for ip in netifaces.ifaddresses(self._intf)[netifaces.AF_INET]: - if "addr" in ip and ip["addr"] != "": - addr.append(ip["addr"]) - self._state = "up" + try: + if netifaces.AF_INET in netifaces.ifaddresses(self._intf): + for ip in netifaces.ifaddresses(self._intf)[netifaces.AF_INET]: + if "addr" in ip and ip["addr"] != "": + addr.append(ip["addr"]) + self._state = "up" + except Exception as e: + self._state = "down" + addr = [] return "{} {} {}".format(self._intf, self._state, ", ".join(addr)) @@ -48,9 +52,14 @@ class Module(bumblebee.module.Module): self._cache["wlan{}".format(intf)] = False return self._cache["wlan{}".format(intf)] + def _istunnel(self, intf): + return intf.startswith("tun") + def state(self): t = "wireless" if self._iswlan(self._intf) else "wired" + t = "tunnel" if self._istunnel(self._intf) else t + return "{}-{}".format(t, self._state) def warning(self): diff --git a/bumblebee/themes/solarized-powerline.json b/bumblebee/themes/solarized-powerline.json index 4f39704..aa5ca06 100644 --- a/bumblebee/themes/solarized-powerline.json +++ b/bumblebee/themes/solarized-powerline.json @@ -32,6 +32,9 @@ "cpu": { "prefix": "  " }, + "disk": { + "prefix": "  " + }, "nic": { "states": { "wireless-up": { @@ -45,6 +48,12 @@ }, "wired-down": { "prefix": "  " + }, + "tunnel-up": { + "prefix": "  " + }, + "tunnel-down": { + "prefix": "  " } } }, diff --git a/bumblebee/util.py b/bumblebee/util.py new file mode 100644 index 0000000..01bd63e --- /dev/null +++ b/bumblebee/util.py @@ -0,0 +1,8 @@ + +def bytefmt(num): + for unit in [ "", "Ki", "Mi", "Gi" ]: + if num < 1024.0: + return "{:.2f}{}B".format(num, unit) + num /= 1024.0 + return "{:05.2f%}{}GiB".format(num) +