summaryrefslogtreecommitdiffstats
path: root/utils/prometheus-node-exporter-lua/files/usr/lib/lua/prometheus-collectors/hwmon.lua
blob: 72688e7b422092db16d7f827c7e91592d611137d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
local fs = require "nixio.fs"

local function rtrim(s)
  return string.gsub(s, "\n$", "")
end

local function scrape()
  local metric_chip_names = metric("node_hwmon_chip_names", "gauge")
  local metric_sensor_label = metric("node_hwmon_sensor_label", "gauge")
  local metric_temp_celsius = metric("node_hwmon_temp_celsius", "gauge")
  local metric_pwm = metric("node_hwmon_pwm", "gauge")

  for hwmon_path in fs.glob("/sys/class/hwmon/hwmon*") do
    -- Produce node_hwmon_chip_names
    -- See https://github.com/prometheus/node_exporter/blob/7c564bcbeffade3dacac43b07c2eeca4957ca71d/collector/hwmon_linux.go#L415
    local chip_name = rtrim(get_contents(hwmon_path .. "/name"))
    if chip_name == "" then
      chip_name = fs.basename(hwmon_path)
    end

    -- See https://github.com/prometheus/node_exporter/blob/7c564bcbeffade3dacac43b07c2eeca4957ca71d/collector/hwmon_linux.go#L355
    local chip = chip_name
    local real_dev_path, status = fs.realpath(hwmon_path .. "/device")
    if not status then
      local dev_name = fs.basename(real_dev_path)
      local dev_type = fs.basename(fs.dirname(real_dev_path))
      chip = dev_type .. "_" .. dev_name
    end
    metric_chip_names({chip=chip, chip_name=chip_name}, 1)

    -- Produce node_hwmon_sensor_label
    for sensor_path in fs.glob(hwmon_path .. "/*_label") do
      local sensor = string.gsub(fs.basename(sensor_path), "_label$", "")
      local sensor_label = rtrim(get_contents(sensor_path))
      metric_sensor_label({chip=chip, sensor=sensor, label=sensor_label}, 1)
    end

    -- Produce node_hwmon_temp_celsius
    for sensor_path in fs.glob(hwmon_path .. "/temp*_input") do
      local sensor = string.gsub(fs.basename(sensor_path), "_input$", "")
      local temp = get_contents(sensor_path) / 1000
      metric_temp_celsius({chip=chip, sensor=sensor}, temp)
    end

    -- Produce node_hwmon_pwm
    for sensor_path in fs.glob(hwmon_path .. "/pwm*") do
      local sensor = fs.basename(sensor_path)
      if string.match(sensor, "^pwm[0-9]+$") then
        local pwm = rtrim(get_contents(sensor_path))
        metric_pwm({chip=chip, sensor=sensor}, pwm)
      end
    end
  end
end

return { scrape = scrape }