aboutsummaryrefslogtreecommitdiff
path: root/utils/collectd/files/lua-scripts/dsl.lua
blob: 29a4be11b39124490b7f3f9036a557c0f68f6a33 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
-- Copyright 2019 Florian Eckert <fe@dev.tdt.de>
-- Copyright 2021 Jeroen Peelaerts <jeroen@steganos.dev>
-- 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)