From 533c072ee09e3ebbd5a8be4786c1e86db92f5c5e Mon Sep 17 00:00:00 2001 From: Jeroen Peelaerts Date: Fri, 15 May 2020 19:42:48 +0200 Subject: [PATCH] collectd: Add lua and exec collectors for lantiq dsl metrics Signed-off-by: Jeroen Peelaerts --- utils/collectd/Makefile | 29 +++- utils/collectd/files/lua-scripts/dsl.lua | 170 +++++++++++++++++++++++ 2 files changed, 198 insertions(+), 1 deletion(-) create mode 100644 utils/collectd/files/lua-scripts/dsl.lua diff --git a/utils/collectd/Makefile b/utils/collectd/Makefile index 10bd70ef1..11f467dc5 100644 --- a/utils/collectd/Makefile +++ b/utils/collectd/Makefile @@ -9,7 +9,7 @@ include $(TOPDIR)/rules.mk PKG_NAME:=collectd PKG_VERSION:=5.12.0 -PKG_RELEASE:=13 +PKG_RELEASE:=15 PKG_SOURCE:=$(PKG_NAME)-$(PKG_VERSION).tar.bz2 PKG_SOURCE_URL:=https://collectd.org/files/ \ @@ -397,6 +397,32 @@ define BuildPlugin endef +define BuildScriptLuaPlugin + PKG_CONFIG_DEPENDS+= CONFIG_PACKAGE_collectd-mod-$(1) + + define Package/collectd-mod-$(1) + $$(call Package/collectd/Default) + TITLE:=$(2) plugin + DEPENDS:= $(4) + endef + + define Package/collectd-mod-$(1)/install + $(INSTALL_DIR) $$(1)/usr/lib/collectd-mod-lua + for m in $(3); do \ + ${INSTALL_DATA} \ + ./files/lua-scripts/$$$$$$$${m}.lua $$(1)/usr/lib/collectd-mod-lua/ ; \ + done + if [ -f ./files/usr/share/collectd/plugin/$(1).json ]; then \ + $(INSTALL_DIR) $$(1)/usr/share/collectd/plugin ; \ + $(INSTALL_DATA) ./files/usr/share/collectd/plugin/$(1).json \ + $$(1)/usr/share/collectd/plugin/$(1).json ; \ + fi + endef + + $$(eval $$(call BuildPackage,collectd-mod-$(1))) + +endef + define BuildScriptPlugin PKG_CONFIG_DEPENDS+= CONFIG_PACKAGE_collectd-mod-$(1) @@ -503,3 +529,4 @@ $(eval $(call BuildPlugin,write-graphite,Carbon/Graphite output,write_graphite,+ $(eval $(call BuildPlugin,write-http,HTTP POST output,write_http,+PACKAGE_collectd-mod-write-http:libcurl)) $(eval $(call BuildScriptPlugin,sqm,SQM/qdisc collection,sqm_collectd,+PACKAGE_collectd-mod-sqm:collectd-mod-exec)) +$(eval $(call BuildScriptLuaPlugin,ltq-dsl,Lantiq DSL collection,dsl,@TARGET_lantiq_xrx200 +PACKAGE_collectd-mod-ltq-dsl:collectd-mod-lua +libubus-lua)) diff --git a/utils/collectd/files/lua-scripts/dsl.lua b/utils/collectd/files/lua-scripts/dsl.lua new file mode 100644 index 000000000..29a4be11b --- /dev/null +++ b/utils/collectd/files/lua-scripts/dsl.lua @@ -0,0 +1,170 @@ +-- Copyright 2019 Florian Eckert +-- Copyright 2021 Jeroen Peelaerts +-- Licensed to the public under the GNU General Public License v2. + +local ubus = require("ubus") + +local hostname_file = "/proc/sys/kernel/hostname" + +local line_vars = { + { + name = "vector", + type = "bool" + }, + { + name = "trellis", + type = "bool" + }, + { + name = "bitswap", + type = "bool" + }, + { + name = "retx", + type = "bool" + }, + { + name = "satn", + type = "snr" + }, + { + name = "latn", + type = "snr" + }, + { + name = "attndr", + type = "bitrate" + }, + { + name = "snr", + type = "snr" + }, + { + name = "data_rate", + type = "bitrate" + }, + { + name = "latn", + type = "latency" + } +} + +local errors = { + { + name = "uas", + type = "gauge" + }, + { + name = "rx_corrupted", + type = "gauge" + }, + { + name = "rx_retransmitted", + type = "gauge" + }, + { + name = "tx_retransmitted", + type = "gauge" + } +} + +local erb_vars = { + { + name = "sent", + type = "gauge" + }, + { + name = "discarded", + type = "gauge" + } +} + +local general_vars = { + { + name = "profile", + type = "gauge" + }, + { + name = "mode", + type = "gauge" + }, + { + name = "state_num", + type = "gauge" + }, + { + name = "power_state_num", + type = "gauge" + }, + { + name = "uptime", + type = "uptime" + } +} + +local function build_metric(name, direction) + if direction ~= '' then + return string.format("%s_%s", name, direction) + else + return name + end +end + +local function get_values(hostname, variables, metrics, direction) + for _, information in pairs(variables) do + local name = information["name"] + + if metrics and metrics[name] ~= nil then + local value = metrics[name] + local metric = build_metric(name, direction) + + local t = { + host = host, + plugin = 'dsl', + type = information["type"], + type_instance = metric, + values = {value} + } + collectd.log_debug(string.format("%s: %s=%s", "collectd-mod-dsl(lua)", metric, tostring(value))) + collectd.dispatch_values(t) + else + collectd.log_info(string.format("%s: Unable to get %s", "collectd-mod-dsl(lua)", name)) + end + end +end + +local function read() + local lines = io.lines(hostname_file) + local hostname = lines() + + local conn = ubus.connect() + if not conn then + collectd.log_error("collectd-mod-dsl(lua): Failed to connect to ubus") + return 0 + end + + local metrics = conn:call("dsl", "metrics", {}) + + if metrics then + if metrics["up"] then + local near_errors = metrics["errors"]["near"] + local far_errors = metrics["errors"]["far"] + local down_line = metrics["downstream"] + local up_line = metrics["upstream"] + local erb = metrics["erb"] + + get_values(hostname, errors, near_errors, "near") + get_values(hostname, errors, far_errors, "far") + get_values(hostname, line_vars, down_line, "down") + get_values(hostname, line_vars, up_line, "up") + get_values(hostname, erb_vars, erb, "") + end + get_values(hostname, general_vars, metrics, "") + return 0 + end + + collectd.log_error("collectd-mod-dsl(lua): No ubus dsl object found") + return 0 +end + +collectd.register_read(read)