CPU Governor/Freq. Widget for Linux (Pure Lua)

by Fredrik Ax 

It provides a textbox widget that shows your current governor and what frequence your cpu currently is using.

When left-clicked it opens a menu that allow you to select another governor (if you have the appropriate line in sudoers). Right-clicked it simply updates it self.

Prerequisites:
sudo and cpufreq-set must be in your path e.g. in Debian, as root do: apt-get install sudo cpufrequtils

You must be allowed to run the command "sudo cpufreq-set ..." passwordless, e.g. by adding the following line to your /etc/sudoers: username	ALL=(ALL) NOPASSWD:/usr/bin/cpufreq-set where username is ... guess what ... your user name.

Code to be inserted into your rc.lua
The widget must be defined before it is added to the wibox where it is going to be used. So using the mywibox as defined in the default rc.lua the code should be inserted before the loop (for s = 1, screen.count do) containing the statement starting with: mywibox[s].widgets = {

This is the code to be inserted: -- Create fraxcpumenu, and add all available governors to it fraxcpumenu = {} local fh= io.open("/sys/devices/system/cpu/cpu0/cpufreq/scaling_available_governors", "r") if fh ~= nil then govstr= fh:read fh:close local i= 1 for w in string.gmatch(govstr, "%a+") do      fraxcpumenu[i]= { w, "sudo cpufreq-set  --governor "..w}       i= i + 1 end end fraxcpumenu = awful.menu.new( { items= fraxcpumenu } ) -- Create fraxcpu widget fraxcpu= widget({ type = "textbox", name = "fraxcpu", align = "right" }) fraxcpu.text= 'fraxcpu' -- Function for updating the fraxcpu widget fraxcpuupd=1 function hook_fraxcpu (tbw) if not fraxcpuupd then return(nil) end local freq= '' local gov= '' local fh= io.open("/sys/devices/system/cpu/cpu0/cpufreq/scaling_governor", "r") if fh then gov= fh:read..':' fh:close end fh= io.open("/sys/devices/system/cpu/cpu0/cpufreq/scaling_cur_freq", "r") if fh then freq= fh:read fh:close freq= tostring(math.ceil(tonumber(freq)/1000)) else fraxcpuupd= nil fh= io.open("/proc/cpuinfo", "r") if fh then for l in fh:lines do 	   freq= string.match(l, '^%s*cpu MHz%s*:%s*([0-9]+)') if freq ~= nil then break end freq= '' end end end tbw.text= gov..freq end -- Mouse button bindings for fraxcpu widget fraxcpu.buttons(fraxcpu,{ button({ }, 1, function awful.menu.toggle(fraxcpumenu) end), 			  button({ }, 2, function  hook_fraxcpu(fraxcpu) end), 			  button({ }, 3, function  hook_fraxcpu(fraxcpu) end) })

Don't forget to make the widget visible, e.g. by adding it to the mywibox as defined by the default rc.lua, look for the line starting with mywibox[s].widgets = { and insert "fraxcpu," into the list of widgets.

Finally you need to register a timer that keeps the widget updated. For it to be informational in environments where the freq changes dynamically (e.g. when using the ondemand governor) it needs to be updated often.

To update it every second add the following at the end of your rc.lua: -- Update the fraxcpu widget every second awful.hooks.timer.register(1, function     hook_fraxcpu(fraxcpu)  end)