From: James Bunton Date: Thu, 6 Nov 2014 06:38:42 +0000 (+1100) Subject: xmonad: use xfce4-genmon instead of xmobar for cpu/mem X-Git-Url: https://code.delx.au/dotfiles/commitdiff_plain/121ffab72309f32c2d9f9088d63546953603e5ec xmonad: use xfce4-genmon instead of xmobar for cpu/mem --- diff --git a/.xmonad/xmobar.hs b/.xmonad/xmobar.hs index a84cd4b..55e7862 100644 --- a/.xmonad/xmobar.hs +++ b/.xmonad/xmobar.hs @@ -2,15 +2,9 @@ Config { font = "xft:sans-serif:size=10", bgColor = "#edeceb", fgColor = "black", - position = TopSize L 80 20, - commands = [ - Run Cpu ["-t","Cpu:%","-L","3","-H","50","--high","red","-p","3"] 10, - Run Memory ["-t","Mem:%","-p","3"] 10, - Run BatteryP ["BAT1"] ["-t","Batt: %","-L","25","--low","red","--","-O","+","-o","-","-f","ADP1/online"] 10, - Run StdinReader - ], + position = TopSize L 70 20, + commands = [Run StdinReader], sepChar = "%", - alignSep = "}{", - template = "%StdinReader% }{ %cpu% | %memory% | %battery% ", - lowerOnStart = False + template = "%StdinReader%", + lowerOnStart = False, } diff --git a/bin/xfce4-genmon-script b/bin/xfce4-genmon-script new file mode 100755 index 0000000..4a3667c --- /dev/null +++ b/bin/xfce4-genmon-script @@ -0,0 +1,86 @@ +#!/usr/bin/env python3 + +import time +import sys + +def read_file(filename): + with open(filename) as f: + return f.read() + +def read_cpu_idle_jiffys(procstat): + line = procstat.split("\n")[0] + fields = line.split() + return int(fields[4]) + +def count_cpus(procstat): + count = -1 + for line in procstat.split("\n"): + if line.startswith("cpu"): + count += 1 + return count + +def read_cpu_percent(): + procstat = read_file("/proc/stat") + num_cpus = count_cpus(procstat) + + tstart = time.time() + idle_jiffy1 = read_cpu_idle_jiffys(procstat) + time.sleep(0.1) + idle_jiffy2 = read_cpu_idle_jiffys(read_file("/proc/stat")) + tend = time.time() + + duration = tend - tstart + idle_jiffys_per_second = (idle_jiffy2 - idle_jiffy1) / duration + + idle_jiffys_per_cpu_second = idle_jiffys_per_second / num_cpus + + # One jiffy is 10ms, so we can get the percentage very easily! + return 100 - idle_jiffys_per_cpu_second + +def read_mem_percent(): + meminfo = read_file("/proc/meminfo") + + d = {} + for line in meminfo.strip().split("\n"): + key, value = line.split()[:2] + d[key] = int(value) + + mem_total = d["MemTotal:"] + mem_free = d["MemAvailable:"] + mem_used = mem_total - mem_free + + return mem_used / mem_total * 100 + +def write(s): + sys.stdout.write(s) + +def print_percent(name, units, value, red_threshold): + if value > red_threshold: + color = "red" + else: + color = "black" + + write( + "%s %s%s " % + (name, color, value, units) + ) + +def main(): + write("") + + print_percent( + "cpu", "%", + round(read_cpu_percent()), + red_threshold=50, + ) + + print_percent( + "mem", "%", + round(read_mem_percent()), + red_threshold=70, + ) + + write("") + +if __name__ == "__main__": + main()