diff --git a/utils/prometheus-node-exporter-lua/Makefile b/utils/prometheus-node-exporter-lua/Makefile index 6662eb060..d079e6071 100644 --- a/utils/prometheus-node-exporter-lua/Makefile +++ b/utils/prometheus-node-exporter-lua/Makefile @@ -16,7 +16,7 @@ define Package/prometheus-node-exporter-lua SECTION:=utils CATEGORY:=Utilities TITLE:=Provides system statistics as Prometheus scraping endpoint - DEPENDS:=+luasocket + DEPENDS:=+luasocket +libiwinfo-lua +libubus-lua +lua URL:=https://github.com/rbo/openwrt_exporter PKGARCH:=all endef diff --git a/utils/prometheus-node-exporter-lua/files/usr/bin/prometheus-node-exporter-lua b/utils/prometheus-node-exporter-lua/files/usr/bin/prometheus-node-exporter-lua index 488374ddd..f0435ad66 100755 --- a/utils/prometheus-node-exporter-lua/files/usr/bin/prometheus-node-exporter-lua +++ b/utils/prometheus-node-exporter-lua/files/usr/bin/prometheus-node-exporter-lua @@ -69,11 +69,10 @@ function metric(name, mtype, labels, value) return outputter end -function scraper_wifi() - local rv = { } - local ntm = require "luci.model.network".init() +local ubus = require "ubus" +local iwinfo = require "iwinfo" - local metric_wifi_network_up = metric("wifi_network_up","gauge") +function scraper_wifi() local metric_wifi_network_quality = metric("wifi_network_quality","gauge") local metric_wifi_network_bitrate = metric("wifi_network_bitrate","gauge") local metric_wifi_network_noise = metric("wifi_network_noise","gauge") @@ -83,53 +82,47 @@ function scraper_wifi() local metric_wifi_station_tx_packets = metric("wifi_station_tx_packets","gauge") local metric_wifi_station_rx_packets = metric("wifi_station_rx_packets","gauge") - local dev - for _, dev in ipairs(ntm:get_wifidevs()) do - local rd = { - up = dev:is_up(), - device = dev:name(), - name = dev:get_i18n(), - networks = { } - } - - local net - for _, net in ipairs(dev:get_wifinets()) do + local u = ubus.connect() + local status = u:call("network.wireless", "status", {}) + + for dev, dev_table in pairs(status) do + for _, intf in ipairs(dev_table['interfaces']) do + local ifname = intf['ifname'] + local iw = iwinfo[iwinfo.type(ifname)] local labels = { - channel = net:channel(), - ssid = net:active_ssid(), - bssid = net:active_bssid(), - mode = net:active_mode(), - ifname = net:ifname(), - country = net:country(), - frequency = net:frequency(), + channel = iw.channel(ifname), + ssid = iw.ssid(ifname), + bssid = iw.bssid(ifname), + mode = iw.mode(ifname), + ifname = ifname, + country = iw.country(ifname), + frequency = iw.frequency(ifname), + device = dev, } - if net:is_up() then - metric_wifi_network_up(labels, 1) - local signal = net:signal_percent() - if signal ~= 0 then - metric_wifi_network_quality(labels, net:signal_percent()) - end - metric_wifi_network_noise(labels, net:noise()) - local bitrate = net:bitrate() - if bitrate then - metric_wifi_network_bitrate(labels, bitrate) - end - - local assoclist = net:assoclist() - for mac, station in pairs(assoclist) do - local labels = { - ifname = net:ifname(), - mac = mac, - } - metric_wifi_station_signal(labels, station.signal) - metric_wifi_station_tx_packets(labels, station.tx_packets) - metric_wifi_station_rx_packets(labels, station.rx_packets) - end - else - metric_wifi_network_up(labels, 0) + + local qc = iw.quality(ifname) or 0 + local qm = iw.quality_max(ifname) or 0 + local quality = 0 + if qc > 0 and qm > 0 then + quality = math.floor((100 / qm) * qc) + end + + metric_wifi_network_quality(labels, quality) + metric_wifi_network_noise(labels, iw.noise(ifname) or 0) + metric_wifi_network_bitrate(labels, iw.bitrate(ifname) or 0) + metric_wifi_network_signal(labels, iw.signal(ifname) or -255) + + local assoclist = iw.assoclist(ifname) + for mac, station in pairs(assoclist) do + local labels = { + ifname = ifname, + mac = mac, + } + metric_wifi_station_signal(labels, station.signal) + metric_wifi_station_tx_packets(labels, station.tx_packets) + metric_wifi_station_rx_packets(labels, station.rx_packets) end end - rv[#rv+1] = rd end end