forked from otakup0pe/luup-prometheus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathL_PrometheusMetrics.lua
More file actions
248 lines (211 loc) · 9.02 KB
/
Copy pathL_PrometheusMetrics.lua
File metadata and controls
248 lines (211 loc) · 9.02 KB
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
--[[
This file is part of luup-prometheus
luup-prometheus is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
luup-prometheus is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with luup-prometheus. If not, see <http://www.gnu.org/licenses/>.
--]]
local DATA = {
battery_level={
{{4}, 'urn:micasaverde-com:serviceId:HaDevice1', 'BatteryLevel'},
{'battery_level_percent', 'Battery level as a percentage of its capacity'}
},
energy_sensor_counter={
{{3, 21}, 'urn:micasaverde-com:serviceId:EnergyMetering1', 'KWH'},
{'electricity_used_kwh', 'Electricity used by this device in KWh', 'counter'}
},
energy_sensor_gauge={
{{3, 21}, 'urn:micasaverde-com:serviceId:EnergyMetering1', 'Watts'},
{'electricity_usage_w', 'Currently reported electricity draw in Watts'}
},
humidity_sensor={
{{16}, 'urn:micasaverde-com:serviceId:HumiditySensor1', 'CurrentLevel'},
{'humidity_relative', 'Relative humidity (0..100)'}
},
light_sensor={
{{18}, 'urn:micasaverde-com:serviceId:LightSensor1', 'CurrentLevel'},
{'light_lux', 'Light level in lux'}
},
security_sensor={
{{4}, 'urn:micasaverde-com:serviceId:SecuritySensor1', 'Tripped'},
{'security_sensor_tripped', 'Integer 1/0 indicating sensor trip'}
},
temperature_sensor={
{{5, 17}, 'urn:upnp-org:serviceId:TemperatureSensor1', 'CurrentTemperature'},
{'temperature_c', 'Temperature in degrees Celsius'}
},
dimmer_state={
{{2}, 'urn:upnp-org:serviceId:Dimming1', 'LoadLevelStatus'},
{'dimmable_load_percent', 'Load level of a dimmable device (0..100)'}
},
}
local DATA_BY_CAT = {}
for key, data in pairs(DATA) do
for _, cat in ipairs(data[1][1]) do
DATA_BY_CAT[cat] = DATA_BY_CAT[cat] or {}
table.insert(DATA_BY_CAT[cat], key)
end
end
function pm_metric(name, attributes, value)
-- Format a metric for Prometheus text output
local attrs = ''
if attributes then
local parts = {}
for k, v in pairs(attributes) do
table.insert(parts, k .. '="' .. tostring(v) .. '"')
end
attrs = '{' .. table.concat(parts, ',') .. '}'
end
return name .. attrs .. ' ' .. tostring(value) .. "\n"
end
function pm_attributes(device_num, device)
-- Return a basic set of attributes for a device
return {
description=device.description,
device=device_num,
room=luup.rooms[device.room_num]
}
end
function pm_device_metrics()
local output = ''
-- Sort devices by device_num
local sorted_devices = {}
for device_num, device in pairs(luup.devices) do
table.insert(sorted_devices, device_num)
end
table.sort(sorted_devices)
local metrics = {}
for _, device_num in ipairs(sorted_devices) do
device = luup.devices[device_num]
for _, data_key in ipairs(DATA_BY_CAT[device.category_num] or {}) do
local d = DATA[data_key]
local value = luup.variable_get(d[1][2], d[1][3], device_num)
value = tonumber(value)
if value then
metrics[data_key] = metrics[data_key] or {}
if device.device_num_parent then
metrics[data_key][device.device_num_parent] = nil
end
metrics[data_key][device_num] = {
luup.variable_get(d[1][2], d[1][3], device_num),
pm_attributes(device_num, device)
}
end
end
end
local output = ''
for data_key, values in pairs(metrics) do
local mname = DATA[data_key][2][1]
local mhelp = DATA[data_key][2][2]
local mtype = DATA[data_key][2][3] or 'gauge'
output = output .. '# HELP ' .. mname .. ' ' .. mhelp .. "\n"
output = output .. '# TYPE ' .. mname .. ' ' .. mtype .. "\n"
for _, v in pairs(values) do
output = output .. pm_metric(mname, v[2], v[1])
end
end
return output
end
function pm_procstat_one(line, expected, mname, help, mtype)
local output = ''
local label = ''
local value = ''
label, value = line:match('(' .. expected .. ') (%d+)')
if label ~= expected then return '' end
mname = 'node_' .. mname
value = tonumber(value)
output = output .. '# HELP ' .. mname .. ' ' .. help .. '\n'
output = output .. '# TYPE ' .. mname .. ' ' .. mtype .. '\n'
output = output .. pm_metric(mname, nil, value)
return output
end
function pm_procstat()
local f = io.open('/proc/stat', 'rb')
if not f then return '' end
local output = '# HELP node_cpu Seconds the cpus spent in each mode.\n'
output = output .. '# TYPE node_cpu counter\n'
-- First line is overall CPU stats - ignore
_ = f:read()
local cpu_attrs = {'user', 'nice', 'system', 'idle', 'iowait', 'irq',
'softirq', 'steal', 'guest', 'guest_nice'}
local _sc_clk_tck = 100 -- Should be read from sysconf
local line = f:read()
while string.sub(line, 1, 3) == 'cpu' do
local stats = {line:match('(cpu%d+) (%d+) (%d+) (%d+) (%d+)( ?%d*)( ?%d*)( ?%d*)( ?%d*)( ?%d*)( ?%d*)')}
for k, mode in pairs(cpu_attrs) do
local v = tonumber(stats[k + 1])
if not v then break end
local attrs = {cpu=stats[1], mode=mode}
output = output .. pm_metric('node_cpu', attrs, v / _sc_clk_tck)
end
line = f:read()
end
output = output .. pm_procstat_one(line, 'intr', 'intr', 'Total number of interrupts serviced.', 'counter')
line = f:read()
output = output .. pm_procstat_one(line, 'ctxt', 'context_switches', 'Total number of context switches.', 'counter')
line = f:read()
output = output .. pm_procstat_one(line, 'btime', 'boot_time', 'Node boot time, in unixtime.', 'gauge')
line = f:read()
output = output .. pm_procstat_one(line, 'processes', 'forks', 'Total number of forks.', 'counter')
line = f:read()
output = output .. pm_procstat_one(line, 'procs_running', 'procs_running', 'Number of processes in runnable state.', 'gauge')
line = f:read()
output = output .. pm_procstat_one(line, 'procs_blocked', 'procs_blocked', 'Number of processes blocked waiting for I/O to complete.', 'gauge')
f:close()
return output
end
function pm_meminfo()
local f = io.open('/proc/meminfo', 'rb')
if not f then return '' end
local output = ''
for line in f:lines() do
local field, value, unit = line:match('(.+): +(%d+)( ?k?B?)')
if not field and not value then break end
value = tonumber(value)
if unit == ' kB' then value = value * 1024 end
field = field:gsub('%((.+)%)', '_%1')
local mname = 'node_memory_' .. field
output = output .. '# HELP ' .. mname .. ' Memory information field ' .. field .. '\n'
output = output .. '# TYPE ' .. mname .. ' gauge\n'
output = output .. pm_metric(mname, nil, value)
end
return output
end
function pm_process_procstat()
-- Metrics about this process
local f = io.open('/proc/self/stat', 'rb')
if not f then return '' end
local contents = f:read()
local pattern = '(%d+) %((.-)%) (.)' .. string.rep(" ([-%d]+)", 21)
local data = {contents:match(pattern)}
local fields = {utime=14, stime=15, vsize=23, rss=24}
local cputime = (data[fields.utime] + data[fields.stime]) / 100
local output = '# HELP process_cpu_seconds_total Total user and system CPU time spent in seconds.\n'
output = output .. '# TYPE process_cpu_seconds_total counter\n'
output = output .. pm_metric('process_cpu_seconds_total', nil, cputime)
local rss = data[fields.rss] * 4096
output = output .. '# HELP process_resident_memory_bytes Resident memory size in bytes.\n'
output = output .. '# TYPE process_resident_memory_bytes gauge\n'
output = output .. pm_metric('process_resident_memory_bytes', nil, rss)
output = output .. '# HELP process_virtual_memory_bytes Virtual memory size in bytes.\n'
output = output .. '# TYPE process_virtual_memory_bytes gauge\n'
output = output .. pm_metric('process_virtual_memory_bytes', nil, data[fields.vsize])
return output
end
function prometheus_metrics_handler(lul_request, lul_parameters, lul_outputformat)
local output = pm_device_metrics()
output = output .. pm_procstat()
output = output .. pm_meminfo()
output = output .. pm_process_procstat()
return output, 'text/plain'
end
function initstatus(lul_device)
luup.log("PrometheusMetrics initstatus("..lul_device..") starting")
luup.register_handler("prometheus_metrics_handler", "prometheus_metrics")
end