Created
February 8, 2023 11:19
Revisions
-
cloverstd created this gist
Feb 8, 2023 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,86 @@ --[[ 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 }