2017-12-01 00:21:33 -06:00
|
|
|
---------------------------------------------------
|
|
|
|
-- Licensed under the GNU General Public License v2
|
|
|
|
-- * (c) 2010, Greg D. <jabbas@jabbas.pl>
|
|
|
|
---------------------------------------------------
|
|
|
|
|
|
|
|
-- {{{ Grab environment
|
|
|
|
local tonumber = tonumber
|
|
|
|
local io = { popen = io.popen }
|
|
|
|
local setmetatable = setmetatable
|
|
|
|
local table = { insert = table.insert }
|
|
|
|
local string = {
|
2017-12-17 12:06:39 -06:00
|
|
|
gsub = string.gsub,
|
|
|
|
match = string.match
|
2017-12-01 00:21:33 -06:00
|
|
|
}
|
|
|
|
-- }}}
|
|
|
|
|
|
|
|
|
|
|
|
-- Sensors: provides access to lm_sensors data
|
|
|
|
-- vicious.contrib.sensors
|
|
|
|
local sensors = {}
|
|
|
|
|
|
|
|
|
|
|
|
-- {{{ Split helper function
|
|
|
|
local function datasplit(str)
|
2017-12-17 12:06:39 -06:00
|
|
|
-- Splitting strings into associative array
|
|
|
|
-- with some magic to get the values right.
|
|
|
|
str = string.gsub(str, "\n", ":")
|
2017-12-01 00:21:33 -06:00
|
|
|
|
2017-12-17 12:06:39 -06:00
|
|
|
local tbl = {}
|
|
|
|
string.gsub(str, "([^:]*)", function (v)
|
|
|
|
if string.match(v, ".") then
|
|
|
|
table.insert(tbl, v)
|
|
|
|
end
|
|
|
|
end)
|
2017-12-01 00:21:33 -06:00
|
|
|
|
2017-12-17 12:06:39 -06:00
|
|
|
local assoc = {}
|
|
|
|
for c = 1, #tbl, 2 do
|
|
|
|
local k = string.gsub(tbl[c], ".*_", "")
|
|
|
|
local v = tonumber(string.match(tbl[c+1], "[%d]+"))
|
|
|
|
assoc[k] = v
|
|
|
|
end
|
2017-12-01 00:21:33 -06:00
|
|
|
|
2017-12-17 12:06:39 -06:00
|
|
|
return assoc
|
2017-12-01 00:21:33 -06:00
|
|
|
end
|
|
|
|
-- }}}
|
|
|
|
|
|
|
|
-- {{{ Sensors widget type
|
|
|
|
local function worker(format, warg)
|
2017-12-17 12:06:39 -06:00
|
|
|
-- Get data from all sensors
|
|
|
|
local f = io.popen("LANG=C sensors -uA")
|
|
|
|
local lm_sensors = f:read("*all")
|
|
|
|
f:close()
|
2017-12-01 00:21:33 -06:00
|
|
|
|
2017-12-17 12:06:39 -06:00
|
|
|
local sensor_data = string.gsub(
|
|
|
|
string.match(lm_sensors, warg..":\n(%s%s.-)\n[^ ]"), " ", "")
|
2017-12-01 00:21:33 -06:00
|
|
|
|
2017-12-17 12:06:39 -06:00
|
|
|
-- One of: crit, max
|
|
|
|
local divisor = "crit"
|
|
|
|
local s_data = datasplit(sensor_data)
|
2017-12-01 00:21:33 -06:00
|
|
|
|
2017-12-17 12:06:39 -06:00
|
|
|
if s_data[divisor] and s_data[divisor] > 0 then
|
|
|
|
s_data.percent = s_data.input / s_data[divisor] * 100
|
|
|
|
end
|
2017-12-01 00:21:33 -06:00
|
|
|
|
2017-12-17 12:06:39 -06:00
|
|
|
return {s_data.input, tonumber(s_data.percent)}
|
2017-12-01 00:21:33 -06:00
|
|
|
end
|
|
|
|
-- }}}
|
|
|
|
|
|
|
|
return setmetatable(sensors, { __call = function(_, ...) return worker(...) end })
|