Skip to content

Instantly share code, notes, and snippets.

@cloverstd
Created February 8, 2023 11:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cloverstd/3bc46312e59516239f0275e8d81ec26d to your computer and use it in GitHub Desktop.
Save cloverstd/3bc46312e59516239f0275e8d81ec26d to your computer and use it in GitHub Desktop.
OpenWrt temperature monitor for prometheus-collectors
--[[
function print_metric(metric, labels, value)
local label_string = ""
if labels then
for label,value in pairs(labels) do
label_string = label_string .. label .. '="' .. value .. '",'
end
label_string = "{" .. string.sub(label_string, 1, -2) .. "}"
end
io.write(string.format("%s%s %s", metric, label_string, value))
end
function metric(name, mtype, labels, value)
local outputter = function(labels, value)
print_metric(name, labels, value)
end
if value then
outputter(labels, value)
end
return outputter
end
--]]
local function scrape()
local json = require "luci.jsonc"
local fs = require "nixio.fs"
local sysHwmon = "/sys/class/hwmon"
local sysThermal = "/sys/class/thermal"
local function readFile(path)
local s = fs.readfile(path)
return s and (s:gsub("^%s+", ""):gsub("%s+$", ""))
end
local function list()
io.write('{"getTempStatus":{}}')
end
local temp_metric = metric("sensor_temperature", "gauge")
-- /sys/class/hwmon
local hwmonDir = fs.dir(sysHwmon)
if hwmonDir then
local hwmon = {}
for item in hwmonDir do
if item:match("^hwmon[0-9]+$") then
local deviceDirPath = string.format("%s/%s", sysHwmon, item)
local deviceDir = fs.dir(deviceDirPath)
if deviceDir then
local dNumber = tonumber(item:match("[0-9]+"))
local title = readFile(deviceDirPath .. "/name")
for source in deviceDir do
if source:match("^temp[0-9]+_input$") then
local sNumber = tonumber(source:match("[0-9]+"))
local temp = readFile(
string.format("%s/%s", deviceDirPath, source))
-- local label = readFile(
-- string.format("%s/%s", deviceDirPath, source:gsub("_input$", "_label")))
if sNumber ~= nil and temp ~= nil then
temp_metric({name="hwmon_"..title..sNumber}, tonumber(temp))
end
end
end
end
end
end
end
-- /sys/class/thermal
local dir = fs.dir(sysThermal)
if dir then
for item in dir do
if item:match("^thermal_zone[0-9]+$") then
local number = tonumber(item:match("[0-9]+"))
local temp = readFile(string.format("%s/%s/temp", sysThermal, item))
local title = readFile(string.format("%s/%s/type", sysThermal, item))
if number ~= nil and temp ~= nil then
temp_metric({name=title..number}, tonumber(temp))
end
end
end
end
end
-- scrape()
return { scrape = scrape }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment