diff options
4 files changed, 37 insertions, 18 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 496d4dc1..99a20b40 100644 --- a/inventory/host_vars/ch-phoebe.yml +++ b/inventory/host_vars/ch-phoebe.yml @@ -35,21 +35,21 @@ spreadspace_apt_repo_components: prometheus_exporter_node_textfile_collector_scripts: - deleted-libraries - smartmon -# - sensors + - sensors prometheus_exporter_node_textfile_collector__sensors: i2c: - - name: "i2c-tiny-usb" + - name_regex: "^i2c%-tiny%-usb at bus 001 device %d+$" devices: - address: 0x18 type: ds2482 w1: - name: rack-center - address: 28.3c01d607db23 + address: 28-3c01d607db23 - name: rack-intake - address: 28-???????????? - - name: ceiling - address: 28-???????????? + address: 28-012112eedaa7 + - name: rack-outlet + address: 28-012113b14676 prometheus_exporters_extra: - ssl 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 diff --git a/roles/monitoring/prometheus/exporter/node/templates/textfile-collector-scripts/sensors.j2 b/roles/monitoring/prometheus/exporter/node/templates/textfile-collector-scripts/sensors.j2 index 1e769389..0ab806c9 100644 --- a/roles/monitoring/prometheus/exporter/node/templates/textfile-collector-scripts/sensors.j2 +++ b/roles/monitoring/prometheus/exporter/node/templates/textfile-collector-scripts/sensors.j2 @@ -11,8 +11,13 @@ local units = { gpio = "status", } +local _typestring_printed = {} + local function metric(name, mtype, labels, value) - print("# TYPE " .. name .. " " .. mtype) + if nil == _typestring_printed[name] then + print("# TYPE " .. name .. " " .. mtype) + _typestring_printed[name] = 1 + end local label_string = "" if labels then |