# Copyright (c) 2006 Andy Gimblett # http://gimbo.org.uk/ # Licensed under the same terms as Ruby (see LICENSE). # # =========================================================================== # #{{{ wmii FreeBSD monitors plugin # # This file must be placed in $HOME/.wmii-3/plugins. # # Thus plugin contains a number of status bar monitors for wmii under # FreeBSD. FreeBSD reports on uptime, etc. somewhat differently than # Linux, and in particular makes extensive use of the sysctl utility. Plugin.define "wmii-freebsd@gimbo.org.uk" do author '"Andy Gimblett" ' # {{{ CPU info - report on temperature and CPU speed. Different hosts # might have more than one thermal zone, but mine only has one, so I # report that. bar_applet("cpuinfo", 800) do |wmii, bar| Thread.new do cpu_path = "/compat/linux/proc/cpuinfo" temp_cmd = '/sbin/sysctl hw.acpi.thermal.tz0.temperature' loop do cpuinfo = IO.readlines(cpu_path)[6].chomp cpuinfo = cpuinfo.split[-1].sub(/\..*$/,'') IO.popen(temp_cmd) do |f| temp = f.read.chomp.scan(/: (.*C)/) bar.data = "#{temp} #{cpuinfo}Mhz" end sleep 10 end end end # {{{ Load average - straight from sysctl, basically. bar_applet("loadavg", 875) do |wmii, bar| Thread.new do loop do IO.popen('/sbin/sysctl vm.loadavg') do |f| bar.data = f.read.chomp.scan(/\{ (.*) \}/)[0][0] end sleep 5 end end end # {{{ Uptime - calculated as time elapsed since boot time. bar_applet("uptime", 880) do |wmii, bar| Thread.new do boottime = '' loop do IO.popen('/sbin/sysctl kern.boottime') do |f| boottime = f.read.chomp end bootsecs = boottime.scan(/.* sec = ([0-9+].*)\,.*/)[0][0].to_i (mins, secs) = (Time.now - Time.at(bootsecs)).to_i.divmod(60) if mins > 60 (hours, mins) = mins.divmod(60) bar.data = "%d:%02d" % [hours, mins] else bar.data = "#{mins}m" end sleep 30 end end end # {{{ Volume monitor - uses the 'mixer' utility, reports on "vol" # (master volume). bar_applet("volume", 910) do |wmii, bar| Thread.new do line = '' loop do IO.popen('/usr/sbin/mixer') do |f| line = f.readline().chomp f.close end (l, r) = line.scan(/(\d+):(\d+)/)[0] if l == r bar.data = l else bar.data = "#{l}:#{r}" end sleep 5 end end end # {{{ Battery - get status, percentage, and time remaining if # discharging. If at 0% and discharging, halt the machine. If less # than 5% and discharging, issue an unmissable on-screen warning using # nbosd (in ports as sysutils/nbosd). bar_applet("battery", 890) do |wmii, bar| Thread.new do cmd = '/sbin/sysctl hw.acpi.acline hw.acpi.battery.life hw.acpi.battery.time' battery = '' loop do IO.popen(cmd) do |f| battery = f.readlines end acline = battery[0].scan(/hw.acpi.acline: (.*)/)[0][0] if acline == "1" status = "+" elsif acline == "0" status = "-" else status = "?" end life = battery[1].scan(/hw.acpi.battery.life: (.*)/)[0][0].to_i if status != "+" if life == 0 Thread.new do `sudo /sbin/shutdown -p now` end elsif life <= 5 Thread.new do `nbosd -t 11000 -x 100` end end end mins = battery[2].scan(/hw.acpi.battery.time: (.*)/)[0][0].to_i if mins == 0 || mins == -1 time = "" else (hh, mm) = mins.divmod(60) time = "%d:%02d" % [hh, mm] end bar.data = "#{status}#{life}% #{time}".strip sleep 10 end end end # {{{ Date/time - this isn't actually FreeBSD specific, but I define # it here because I've basically stopped using the standard monitor # plugin completely. bar_applet("datetime", 950) do |wmii, bar| Thread.new do loop do bar.data = Time.new.strftime("%Y-%m-%d %H:%M:%S") sleep 1 end end end end