comparison mod_munin/mod_munin.lua @ 4553:7e5c8186f121

mod_munin: Port to new OpenMetrics based statistics module
author Jonas Schäfer <jonas@wielicki.name>
date Wed, 28 Apr 2021 08:20:15 +0200
parents c47cd8dbd310
children
comparison
equal deleted inserted replaced
4552:c87181a98f29 4553:7e5c8186f121
1 module:set_global(); 1 module:set_global();
2 2
3 local s_format = string.format; 3 local s_format = string.format;
4 local t_insert = table.insert;
5 local t_concat = table.concat;
4 local array = require"util.array"; 6 local array = require"util.array";
5 local it = require"util.iterators"; 7 local it = require"util.iterators";
6 local mt = require"util.multitable"; 8 local mt = require"util.multitable";
7 9
8 local meta = mt.new(); meta.data = module:shared"meta"; 10 local meta = mt.new(); meta.data = module:shared"meta";
117 data:set(key, name, value); 119 data:set(key, name, value);
118 end 120 end
119 end 121 end
120 end); 122 end);
121 123
124 local function openmetrics_handler(event)
125 local registry = event.metric_registry
126 local host, sect, name, typ, key;
127 for family_name, metric_family in pairs(registry:get_metric_families()) do
128 if not ignore_stats:contains(family_name) then
129 -- module:log("debug", "changed_stats[%q] = %s", stat, tostring(value));
130 local host_key
131 if metric_family.label_keys[1] == "host" then
132 host_key = 1
133 end
134 if family_name:sub(1, 12) == "prosody_mod_" then
135 sect, name = family_name:match("^prosody_mod_([^/]+)/(.+)$")
136 else
137 sect, name = family_name:match("^([^_]+)_(.+)$")
138 end
139 name = clean_fieldname(name)
140
141 local metric_type = metric_family.type_
142 if metric_type == "gauge" or metric_type == "unknown" then
143 typ = "GAUGE"
144 else
145 typ = "DCOUNTER"
146 end
147
148 for labelset, metric in metric_family:iter_metrics() do
149 host = host_key and labelset[host_key] or "global"
150 local name_parts = {}
151 for i, label in ipairs(labelset) do
152 if i ~= host_key then
153 t_insert(name_parts, label)
154 end
155 end
156 local full_name = t_concat(name_parts, "_")
157 local display_name = #name_parts > 0 and full_name or name
158 key = clean_fieldname(s_format("%s_%s_%s", host or "global", sect, name));
159
160 local unit
161 local factor = 1
162 unit = metric_family.unit
163 if unit == "seconds" and typ == "DCOUNTER" then
164 factor = 100
165 unit = "%time"
166 elseif typ == "DCOUNTER" then
167 unit = unit .. "/s"
168 end
169
170 if not meta:get(key, "") then
171 meta:set(key, "", "graph_title", s_format(metric_family.description));
172 if unit ~= "" then
173 meta:set(key, "", "graph_vlabel", unit);
174 end
175 meta:set(key, "", "graph_category", sect);
176 end
177 if not meta:get(key, display_name) then
178 meta:set(key, display_name, "label", display_name);
179 meta:set(key, display_name, "type", typ)
180 end
181
182 for suffix, extra_labels, value in metric:iter_samples() do
183 if metric_type == "histogram" or metric_type == "summary" then
184 if suffix == "_sum" then
185 data:set(key, display_name, value * factor)
186 end
187 elseif suffix == "_total" or suffix == "" then
188 data:set(key, display_name, value * factor)
189 end
190 end
191 end
192 end
193 end
194 end
195
196 module:hook("openmetrics-updated", openmetrics_handler);
197
122 module:provides("net", { 198 module:provides("net", {
123 listener = munin_listener; 199 listener = munin_listener;
124 default_mode = "*l"; 200 default_mode = "*l";
125 default_port = 4949; 201 default_port = 4949;
126 }); 202 });