From 3bce73818e00e58f01d8394194c6850ce2363da6 Mon Sep 17 00:00:00 2001 From: Christian Pointner Date: Tue, 4 Jan 2022 14:37:50 +0100 Subject: sensor: i2c bus detection using regex for name --- files/common/openwrt/sensors.module_lua | 32 ++++++++++++++-------- inventory/host_vars/ch-phoebe.yml | 2 +- .../prometheus/exporter/node/defaults/main.yml | 4 +++ 3 files changed, 26 insertions(+), 12 deletions(-) diff --git a/files/common/openwrt/sensors.module_lua b/files/common/openwrt/sensors.module_lua index d5d869d5..4f3ecc23 100644 --- a/files/common/openwrt/sensors.module_lua +++ b/files/common/openwrt/sensors.module_lua @@ -96,7 +96,11 @@ function _M.i2c_add_or_replace_device(bus, address, name) return false, nil end -function _M.i2c_get_bus_number_from_name(name) +function _M.i2c_get_bus_number_from_name(name, name_regex) + if name == nil and name_regex == nil then + return nil, "unable to find i2c bus: please specifiy either 'number', 'name' or 'name_regex'" + end + local bus_paths, glob_result = _internal_.glob("/sys/bus/i2c/devices/i2c-*", 0) if not bus_paths then if glob_result == glob.GLOB_NOMATCH then return {} end @@ -110,9 +114,15 @@ function _M.i2c_get_bus_number_from_name(name) if not num then return nil, "unable to parse bus number from path: " .. bus_path end local contents, err = _internal_.read_line_from_file(bus_path .. "/name") - if contents ~= nil and contents == name then return num end + if contents ~= nil then + if contents == name then return num end + if name_regex ~= nil and contents:match(name_regex) then return num end + end + end + if name ~= nil then + return nil, "unable to find i2c bus with name: " .. name end - return nil, "unable to find i2c bus with name: " .. name + return nil, "unable to find i2c bus with name matching: " .. name_regex end function _M.i2c_get_mux_channels(parent, address) @@ -140,10 +150,10 @@ function _M.i2c_get_mux_channels(parent, address) end -function _M.i2c_setup_devices_recursive(bus_num, bus_name, devices) +function _M.i2c_setup_devices_recursive(bus_num, bus_name, bus_name_regex, devices) if bus_num == nil then local err - bus_num, err = _M.i2c_get_bus_number_from_name(bus_name) + bus_num, err = _M.i2c_get_bus_number_from_name(bus_name, bus_name_regex) if bus_num == nil then return nil, err end end @@ -168,7 +178,7 @@ function _M.i2c_setup_devices_recursive(bus_num, bus_name, devices) local channel 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_num, device.address), channel.number) end - local tmp, err = _M.i2c_setup_devices_recursive(mux_channels[channel.number], nil, channel.devices) + local tmp, err = _M.i2c_setup_devices_recursive(mux_channels[channel.number], nil, nil, channel.devices) if tmp == nil then return nil, err end num_sensors = num_sensors + tmp end @@ -178,11 +188,11 @@ function _M.i2c_setup_devices_recursive(bus_num, bus_name, devices) return num_sensors end -function _M.i2c_read_sensors_recursive(bus_num, bus_name, devices) +function _M.i2c_read_sensors_recursive(bus_num, bus_name, bus_name_regex, devices) local readings = {} if bus_num == nil then local err - bus_num, err = _M.i2c_get_bus_number_from_name(bus_name) + bus_num, err = _M.i2c_get_bus_number_from_name(bus_name, bus_name_regex) if bus_num == nil then return readings end end @@ -204,7 +214,7 @@ function _M.i2c_read_sensors_recursive(bus_num, bus_name, devices) local channel for _, channel in base.ipairs(device.channels) do if mux_channels[channel.number] then - local tmp, err = _M.i2c_read_sensors_recursive(mux_channels[channel.number], nil, channel.devices) + local tmp, err = _M.i2c_read_sensors_recursive(mux_channels[channel.number], nil, nil, channel.devices) if tmp ~= nil then for k,v in base.pairs(tmp) do readings[k] = v end end end end @@ -506,7 +516,7 @@ function _M.setup(config) if config.i2c then local i2c_bus for _, i2c_bus in base.ipairs(config.i2c) do - local tmp, err = _M.i2c_setup_devices_recursive(i2c_bus.number, i2c_bus.name, i2c_bus.devices) + local tmp, err = _M.i2c_setup_devices_recursive(i2c_bus.number, i2c_bus.name, i2c_bus.name_regex, i2c_bus.devices) if tmp == nil then return nil, err end num_sensors = num_sensors + tmp end @@ -530,7 +540,7 @@ function _M.read(config) if config.i2c then local i2c_bus for _, i2c_bus in base.ipairs(config.i2c) do - local tmp, err = _M.i2c_read_sensors_recursive(i2c_bus.number, i2c_bus.name, i2c_bus.devices) + local tmp, err = _M.i2c_read_sensors_recursive(i2c_bus.number, i2c_bus.name, i2c_bus.name_regex, i2c_bus.devices) if tmp ~= nil then for k,v in base.pairs(tmp) do readings[k] = v end end end end diff --git a/inventory/host_vars/ch-phoebe.yml b/inventory/host_vars/ch-phoebe.yml index 9e90a5f6..99a20b40 100644 --- a/inventory/host_vars/ch-phoebe.yml +++ b/inventory/host_vars/ch-phoebe.yml @@ -39,7 +39,7 @@ prometheus_exporter_node_textfile_collector_scripts: prometheus_exporter_node_textfile_collector__sensors: i2c: - - name: "i2c-tiny-usb at bus 001 device 004" + - name_regex: "^i2c%-tiny%-usb at bus 001 device %d+$" devices: - address: 0x18 type: ds2482 diff --git a/roles/monitoring/prometheus/exporter/node/defaults/main.yml b/roles/monitoring/prometheus/exporter/node/defaults/main.yml index 9e8bcf8b..38bcc080 100644 --- a/roles/monitoring/prometheus/exporter/node/defaults/main.yml +++ b/roles/monitoring/prometheus/exporter/node/defaults/main.yml @@ -42,6 +42,10 @@ prometheus_exporter_node_textfile_collector_scripts: # - name: bar # address: 0x77 # type: bmp280 +# - name_regex: "^i2c%-tiny%-usb at bus 001 device %d+$" ## this uses lua pattern maching +# devices: +# - address: 0x77 +# type: bmp280 # w1: # - name: temp1 # address: 28-987654321098 -- cgit v1.2.3