From 76f27975a9ebcbff3fbaeeda0d83bba6573cf578 Mon Sep 17 00:00:00 2001 From: Nick Hainke Date: Sun, 3 Apr 2022 12:07:40 +0200 Subject: [PATCH] prometheus-node-exporter-lua: add ubnt-manager collector Ubnt-manager-collector collects statistics from airos devices configured in ubnt-manager. Signed-off-by: Nick Hainke --- utils/prometheus-node-exporter-lua/Makefile | 11 ++ .../prometheus-collectors/ubnt-manager.lua | 141 ++++++++++++++++++ 2 files changed, 152 insertions(+) create mode 100644 utils/prometheus-node-exporter-lua/files/usr/lib/lua/prometheus-collectors/ubnt-manager.lua diff --git a/utils/prometheus-node-exporter-lua/Makefile b/utils/prometheus-node-exporter-lua/Makefile index 846bb4c71f..52fb23a39d 100644 --- a/utils/prometheus-node-exporter-lua/Makefile +++ b/utils/prometheus-node-exporter-lua/Makefile @@ -168,6 +168,17 @@ define Package/prometheus-node-exporter-lua-textfile/install $(INSTALL_BIN) ./files/usr/lib/lua/prometheus-collectors/textfile.lua $(1)/usr/lib/lua/prometheus-collectors/ endef +define Package/prometheus-node-exporter-lua-ubnt-manager + $(call Package/prometheus-node-exporter-lua/Default) + TITLE+= (ubnt-manager collector) + DEPENDS:=prometheus-node-exporter-lua +ubnt-manager +lua-cjson +endef + +define Package/prometheus-node-exporter-lua-ubnt-manager/install + $(INSTALL_DIR) $(1)/usr/lib/lua/prometheus-collectors + $(INSTALL_BIN) ./files/usr/lib/lua/prometheus-collectors/ubnt-manager.lua $(1)/usr/lib/lua/prometheus-collectors/ +endef + define Package/prometheus-node-exporter-lua-uci_dhcp_host $(call Package/prometheus-node-exporter-lua/Default) TITLE+= (uci_dhcp_host collector) diff --git a/utils/prometheus-node-exporter-lua/files/usr/lib/lua/prometheus-collectors/ubnt-manager.lua b/utils/prometheus-node-exporter-lua/files/usr/lib/lua/prometheus-collectors/ubnt-manager.lua new file mode 100644 index 0000000000..4cd17aac73 --- /dev/null +++ b/utils/prometheus-node-exporter-lua/files/usr/lib/lua/prometheus-collectors/ubnt-manager.lua @@ -0,0 +1,141 @@ +local function get_devices() + local handle = io.popen("ubnt-manager -l") + local result = handle:read("*a") + handle:close() + + local devices = {} + for device in result:gmatch("%S+") do table.insert(devices, device) end + return devices +end + +local function get_metric_airos6(device_data, label) + -- host + metric("ubnt_uptime", "counter", label, device_data['host']['uptime']) + metric("ubnt_totalram", "gauge", label, device_data['host']['totalram']) + metric("ubnt_freeram", "gauge", label, device_data['host']['freeram']) + metric("ubnt_cpuload", "gauge", label, device_data['host']['cpuload']) + metric("ubnt_cpubusy", "gauge", label, device_data['host']['cpubusy']) + metric("ubnt_cputotal", "gauge", label, device_data['host']['cputotal']) + + -- wireless + metric("ubnt_channel", "gauge", label, device_data['wireless']['channel']) + local freqstring = {} + for freq in device_data['wireless']['frequency']:gmatch("%S+") do + table.insert(freqstring, freq) + end + if freqstring[1] then + metric("ubnt_frequency", "gauge", label, tonumber(freqstring[1])) + end + + metric("ubnt_dfs", "gauge", label, tonumber(device_data['wireless']['dfs'])) + metric("ubnt_signal", "gauge", label, device_data['wireless']['signal']) + metric("ubnt_rssi", "gauge", label, device_data['wireless']['rssi']) + metric("ubnt_noisef", "gauge", label, device_data['wireless']['noisef']) + metric("ubnt_txpower", "gauge", label, device_data['wireless']['txpower']) + metric("ubnt_distance", "gauge", label, device_data['wireless']['distance']) + metric("ubnt_txrate", "gauge", label, + tonumber(device_data['wireless']['txrate'])) + metric("ubnt_rxrate", "gauge", label, + tonumber(device_data['wireless']['rxrate'])) + metric("ubnt_count", "gauge", label, device_data['wireless']['count']) +end + +local function get_metric_airos8(device_data, label) + -- host + metric("ubnt_uptime", "counter", label, device_data['host']['uptime']) + metric("ubnt_loadavg", "gauge", label, device_data['host']['loadavg']) + metric("ubnt_totalram", "gauge", label, device_data['host']['totalram']) + metric("ubnt_freeram", "gauge", label, device_data['host']['freeram']) + metric("ubnt_temperature", "gauge", label, + device_data['host']['temperature']) + metric("ubnt_cpuload", "gauge", label, device_data['host']['cpuload']) + metric("ubnt_timestamp", "counter", label, device_data['host']['timestamp']) + + -- wireless + metric("ubnt_band", "gauge", label, device_data['wireless']['band']) + metric("ubnt_frequency", "gauge", label, + device_data['wireless']['frequency']) + metric("ubnt_center1_freq", "gauge", label, + device_data['wireless']['center1_freq']) + metric("ubnt_dfs", "gauge", label, device_data['wireless']['dfs']) + metric("ubnt_distance", "gauge", label, device_data['wireless']['distance']) + metric("ubnt_noisef", "gauge", label, device_data['wireless']['noisef']) + metric("ubnt_txpower", "gauge", label, device_data['wireless']['txpower']) + metric("ubnt_aprepeater", "gauge", label, + device_data['wireless']['aprepeater']) + metric("ubnt_rstatus", "gauge", label, device_data['wireless']['rstatus']) + metric("ubnt_chanbw", "gauge", label, device_data['wireless']['chanbw']) + metric("ubnt_rx_chainmask", "gauge", label, + device_data['wireless']['rx_chainmask']) + metric("ubnt_tx_chainmask", "gauge", label, + device_data['wireless']['tx_chainmask']) + metric("ubnt_cac_state", "gauge", label, + device_data['wireless']['cac_state']) + metric("ubnt_cac_timeout", "gauge", label, + device_data['wireless']['cac_timeout']) + metric("ubnt_rx_idx", "gauge", label, device_data['wireless']['rx_idx']) + metric("ubnt_rx_nss", "gauge", label, device_data['wireless']['rx_nss']) + metric("ubnt_tx_idx", "gauge", label, device_data['wireless']['tx_idx']) + metric("ubnt_tx_nss", "gauge", label, device_data['wireless']['tx_nss']) + metric("ubnt_count", "gauge", label, device_data['wireless']['count']) + + -- wireless throughput + metric("ubnt_throughput_tx", "gauge", label, + device_data['wireless']['throughput']['tx']) + metric("ubnt_throughput_rx", "gauge", label, + device_data['wireless']['throughput']['rx']) + + -- wireless polling + metric("ubnt_polling_cb_capacity", "gauge", label, + device_data['wireless']['polling']['cb_capacity']) + metric("ubnt_polling_dl_capacity", "gauge", label, + device_data['wireless']['polling']['dl_capacity']) + metric("ubnt_polling_ul_capacity", "gauge", label, + device_data['wireless']['polling']['ul_capacity']) + metric("ubnt_use", "gauge", label, device_data['wireless']['polling']['use']) + metric("ubnt_tx_use", "gauge", label, + device_data['wireless']['polling']['tx_use']) + metric("ubnt_rx_use", "gauge", label, + device_data['wireless']['polling']['rx_use']) + metric("ubnt_atpc_status", "gauge", label, + device_data['wireless']['polling']['atpc_status']) + metric("ubnt_atpc_status", "gauge", label, + device_data['wireless']['polling']['atpc_status']) +end + +local function get_metric(device) + local json = require('cjson') + local handle = io.popen("ubnt-manager -j -t " .. device) + local result = handle:read("*a") + handle:close() + local device_data = json.decode(result) + + if not device_data['host'] then return end + if not device_data['wireless'] then return end + + local hostname = device_data['host']['hostname'] + local devmodel = device_data['host']['devmodel'] + local fwversion = device_data['host']['fwversion'] + local essid = device_data['wireless']['essid'] + + local label = { + device = device, + hostname = hostname, + devmodel = devmodel, + fwversion = fwversion, + essid = essid + } + + -- v6. vs v8. + if fwversion:find("v8.", 1, true) then + get_metric_airos8(device_data, label) + elseif fwversion:find("v6.", 1, true) then + get_metric_airos6(device_data, label) + end +end + +local function scrape() + for _, device in ipairs(get_devices()) do get_metric(device) end +end + +return {scrape = scrape} -- 2.30.2