diff options
-rwxr-xr-x | files/common/openwrt/sensors-init.lua | 2 | ||||
-rw-r--r-- | files/common/openwrt/sensors.module_lua | 75 | ||||
-rw-r--r-- | files/common/openwrt/sensors_prometheus-node-exporter.lua | 3 |
3 files changed, 62 insertions, 18 deletions
diff --git a/files/common/openwrt/sensors-init.lua b/files/common/openwrt/sensors-init.lua index a283bfc7..5e2cd0ee 100755 --- a/files/common/openwrt/sensors-init.lua +++ b/files/common/openwrt/sensors-init.lua @@ -6,5 +6,5 @@ local config, err = sensors.read_config('/etc/sensors.json') if not config then error(err) end local num_sensors, err = sensors.setup(config) -if not num_sensors then error(err) end +if num_sensors == nil then error(err) end print(string.format("successfully initialized %d sensor(s)", num_sensors)) diff --git a/files/common/openwrt/sensors.module_lua b/files/common/openwrt/sensors.module_lua index 499886b0..dbae81ea 100644 --- a/files/common/openwrt/sensors.module_lua +++ b/files/common/openwrt/sensors.module_lua @@ -84,18 +84,18 @@ end function _M.i2c_add_devices_recursive(bus, devices) - local num_devices = 0 + local num_sensors = 0 local device for _, device in base.ipairs(devices) do local ret, err = _M.i2c_add_device(bus, device.address, device.type) if not ret then return nil, err end - num_devices = num_devices + 1 local setup_function = _internal_.setup[device.type] if setup_function ~= nil then time.nanosleep({tv_sec = 0, tv_nsec = 100000000}) local ret, err = setup_function(bus, device.address) - if not ret then return nil, err end + if ret == nil then return nil, err end + num_sensors = num_sensors + ret end if device.channels then @@ -106,13 +106,13 @@ function _M.i2c_add_devices_recursive(bus, devices) for _, channel in base.ipairs(device.channels) do if not mux_channels[channel.number] then return nil, string.format("i2c-mux %s has no channel %d", _M.i2c_device_name(bus, device.address), channel.number) end local tmp, err = _M.i2c_add_devices_recursive(mux_channels[channel.number], channel.devices) - if not tmp then return nil, err end - num_devices = num_devices + tmp + if ret == nil then return nil, err end + num_sensors = num_sensors + tmp end end end - return num_devices + return num_sensors end function _M.i2c_read_sensors_recursive(bus, devices) @@ -160,6 +160,24 @@ function _M.w1_device_family_and_serial(address) end +function _M.w1_setup_devices(devices) + local num_sensors = 0 + local device + for _, device in base.ipairs(devices) do + local family, _ = _M.w1_device_family_and_serial(device.address) + if family ~= nil then + local setup_function = _internal_.setup["w1-" .. family] + if setup_function ~= nil then + local tmp, err = setup_function(device.address) + if tmp == nil then return nil, err end + num_sensors = num_sensors + tmp + end + end + end + + return num_sensors +end + function _M.w1_read_sensors(devices) local readings = {} local device @@ -209,7 +227,7 @@ function _M.gpio_setup_pins(pins) local pin for _, pin in base.ipairs(pins) do local ret, err = _M.gpio_export_pin(pin.number) - if not ret then return nil, err end + if ret == nil then return nil, err end num_pins = num_pins + 1 end @@ -253,7 +271,10 @@ function _M.iio_setup_bmp280(bus, address) local ret, err = _internal_.write_to_file(iio_device .. '/in_pressure_oversampling_ratio', '1') if not ret then return nil, err end - return _internal_.write_to_file(iio_device .. '/in_temp_oversampling_ratio', '1') + local ret, err = _internal_.write_to_file(iio_device .. '/in_temp_oversampling_ratio', '1') + if not ret then return nil, err end + + return 2 end _internal_.setup['bmp280'] = _M.iio_setup_bmp280 @@ -286,7 +307,10 @@ function _M.iio_setup_bme280(bus, address) if not ret then return nil, err end local ret, err = _internal_.write_to_file(iio_device .. '/in_pressure_oversampling_ratio', '1') if not ret then return nil, err end - return _internal_.write_to_file(iio_device .. '/in_temp_oversampling_ratio', '1') + local ret, err = _internal_.write_to_file(iio_device .. '/in_temp_oversampling_ratio', '1') + if not ret then return nil, err end + + return 3 end _internal_.setup['bme280'] = _M.iio_setup_bme280 @@ -316,6 +340,11 @@ end _internal_.read['bme280'] = _M.iio_read_bme280 +function _M.iio_setup_am2315(bus, address) + return 2 +end +_internal_.setup['am2315'] = _M.iio_setup_am2315 + function _M.iio_read_am2315(bus, address) local iio_device, err = _M.iio_device_path_from_i2c_device(bus, address) if not iio_device then return end @@ -354,6 +383,15 @@ function _M.hwmon_device_path_from_w1_device(address) end +function _M.hwmon_setup_w1_therm(address) + return 1 +end +_internal_.setup['w1-10'] = _M.hwmon_setup_w1_therm +_internal_.setup['w1-22'] = _M.hwmon_setup_w1_therm +_internal_.setup['w1-28'] = _M.hwmon_setup_w1_therm +_internal_.setup['w1-3B'] = _M.hwmon_setup_w1_therm +_internal_.setup['w1-42'] = _M.hwmon_setup_w1_therm + function _M.hwmon_read_w1_therm(address) local hwmon_device, err = _M.hwmon_device_path_from_w1_device(address) if not hwmon_device then return end @@ -385,22 +423,27 @@ function _M.read_config(path) end function _M.setup(config) - local num_devices = 0 + local num_sensors = 0 if config.i2c then local i2c_bus for _, i2c_bus in base.ipairs(config.i2c) do local tmp, err = _M.i2c_add_devices_recursive(i2c_bus.number, i2c_bus.devices) - if not tmp then return nil, err end - num_devices = num_devices + tmp + if tmp == nil then return nil, err end + num_sensors = num_sensors + tmp end end - -- for now the only supported 1-wire master device is i2c based and will be initialized above - -- also sensors on 1-wire busses are discvored automatically and for now no supported sensor needs any setup + if config.w1 then + local tmp, err = _M.w1_setup_devices(config.w1) + if tmp == nil then return nil, err end + num_sensors = num_sensors + tmp + end if config.gpio then local tmp, err = _M.gpio_setup_pins(config.gpio) - if not tmp then return nil, err end - num_devices = num_devices + tmp + if tmp == nil then return nil, err end + num_sensors = num_sensors + tmp end + + return num_sensors end function _M.read(config) diff --git a/files/common/openwrt/sensors_prometheus-node-exporter.lua b/files/common/openwrt/sensors_prometheus-node-exporter.lua index 1d32fa0d..9aa0af71 100644 --- a/files/common/openwrt/sensors_prometheus-node-exporter.lua +++ b/files/common/openwrt/sensors_prometheus-node-exporter.lua @@ -1,7 +1,7 @@ local sensors = require "sensors" local config, _ = sensors.read_config('/etc/sensors.json') -sensors.setup(config) +local num_sensors, _ = sensors.setup(config) local units = { temperature = "celsius", humidity = "percent", @@ -13,6 +13,7 @@ local function scrape() local readings, err = sensors.read(config) if not readings then return end + metric("sensors_count_total", "gauge", nil, num_sensors) for name, values in pairs(readings) do labels = { name = name, kind = values._kind_ } for t, v in pairs(values) do |