Created
December 21, 2010 00:02
-
-
Save Igneous/749259 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/lua | |
require "socket" | |
require "LuaXml" | |
io.stdout:setvbuf "no" | |
zipcode = 38501 | |
prev_total = 0 | |
prev_idle = 0 | |
function round(number, decimal) | |
local multiplier = 10^(decimal or 0) | |
return math.floor(number * multiplier + 0.5) / multiplier | |
end | |
function sleep(sec) | |
socket.select(nil,nil,sec) | |
end | |
function getweather() | |
local http = require("socket.http") | |
local ltn12 = require("ltn12") | |
local io = require("io") | |
http.request { | |
url = "http://api.wunderground.com/auto/wui/geo/WXCurrentObXML/index.xml?query=" .. zipcode , | |
sink = ltn12.sink.file(io.open("weather.xml", "w")) | |
} | |
end | |
function parseweather() | |
local wxml = xml.load("weather.xml") | |
location = xml.find(wxml:find("display_location"),"full") | |
temp = wxml:find("temp_f") | |
weather = wxml:find("weather") | |
wicon = xml.find(wxml:find("icon_set","name","Mobile"),"icon_url") | |
end | |
function getcpupct() | |
local statfile = assert(io.open("/proc/stat", "r")) | |
--- Get the cpu line and insert each space-seperated value into a table | |
for line in statfile:lines() do | |
sfound = string.find(line, "cpu ", 1) | |
if sfound ~= nil then | |
cpu = {} | |
for sword in line:gmatch("%S+") do | |
table.insert(cpu,sword) | |
end | |
table.remove(cpu,1) | |
end | |
end | |
-- No need to keep this fd open any longer than we need to... | |
statfile:close() | |
-- Get the idle cpu time | |
idle = cpu[4] | |
-- Calculate the total cpu time | |
total = 0 | |
for row,cpuval in ipairs(cpu) do | |
total = total + cpuval | |
-- print("Loop run, total = ", total, " cpuval = ", cpuval) | |
end | |
-- Calculate the CPU usage since we last checked | |
diff_idle = idle - prev_idle | |
diff_total = total - prev_total | |
diff_usage = (1000 * (diff_total - diff_idle) / diff_total + 5) / 10 | |
-- Remember the total and idle CPU times for the next check | |
prev_total = total | |
prev_idle = idle | |
return round(diff_usage, 0) | |
end | |
function getmempct() | |
local memfile = assert(io.open("/proc/meminfo", "r")) | |
for line in memfile:lines() do | |
itr = 0 | |
if string.find(line, "MemTotal: ", 1) then | |
for mtword in line:gmatch("%S+") do | |
itr = itr + 1 | |
if itr == 2 then | |
mtotal = mtword | |
end | |
end | |
end | |
if string.find(line, "MemFree: ", 1) then | |
for mfword in line:gmatch("%S+") do | |
itr = itr + 1 | |
if itr == 2 then | |
mfree = mfword | |
end | |
end | |
end | |
if string.find(line, "Buffers: ", 1) then | |
for mbword in line:gmatch("%S+") do | |
itr = itr + 1 | |
if itr == 2 then | |
mbuf = mbword | |
end | |
end | |
end | |
if string.find(line, "Cached: ", 1) and not string.find(line, "SwapCached: ", 1) then | |
for mcword in line:gmatch("%S+") do | |
itr = itr + 1 | |
if itr == 2 then | |
mcache = mcword | |
end | |
end | |
end | |
end | |
memfile:close() | |
m_divisor = mtotal / 100 | |
m_used = ( mtotal - mfree - mbuf - mcache ) / m_divisor | |
return round(m_used, 0) | |
end | |
function gdbar(pct,fgcolor,bgcolor,x_dimension,y_dimension,maxpct) | |
gdbar_diff = maxpct / x_dimension | |
gdbar_div = pct / gdbar_diff | |
gdbar_rdiv = round(gdbar_div) | |
return("^fg(" .. fgcolor .. ")^r(" .. gdbar_rdiv .. "x" .. y_dimension .. ")^fg(" .. bgcolor .. ")^r(" .. x_dimension - gdbar_rdiv .. "x" .. y_dimension .. ")^fg()") | |
end | |
getweather() | |
parseweather() | |
main_loop_itr = 0 | |
while true do | |
main_loop_itr = main_loop_itr + 1 | |
date = os.date("%A, %b %d. %I:%M%p") | |
date_right_px = string.len(date) * 6 + 10 | |
cpu_pct = getcpupct() | |
mem_pct = getmempct() | |
if main_loop_itr == 200 then | |
main_loop_itr = 0 | |
getweather() | |
parseweather() | |
end | |
print("^p(_LEFT)^p(-120)^i(/home/igneous/.dzen/bitmaps/xbm8x8/cpu.xbm)^p(3)" .. gdbar(cpu_pct,"white","darkgrey",80,10,100) .. | |
"^p(10)^i(/home/igneous/.dzen/bitmaps/xbm8x8/mem.xbm)^p(3)" .. gdbar(mem_pct,"white","darkgrey",80,10,100) .. | |
"^p(10)^i(/home/igneous/.dzen/bitmaps/weather/clear.xbm) " .. location[1] .. ": (" .. weather[1] .. ")@" .. temp[1] .. "f " .. | |
"^p(_RIGHT)^p(-" .. date_right_px .. ")" .. date) | |
sleep(1.5) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment