Mercurial > prosody-modules
annotate mod_munin/mod_munin.lua @ 4529:c6d44e1fb4d9
mod_rest: Document the /echo endpoint in the openapi spec
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Tue, 23 Mar 2021 22:08:38 +0100 |
parents | c47cd8dbd310 |
children | 7e5c8186f121 |
rev | line source |
---|---|
1624
0f20390f6fa5
mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
1 module:set_global(); |
0f20390f6fa5
mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
2 |
0f20390f6fa5
mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
3 local s_format = string.format; |
0f20390f6fa5
mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
4 local array = require"util.array"; |
0f20390f6fa5
mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
5 local it = require"util.iterators"; |
0f20390f6fa5
mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
6 local mt = require"util.multitable"; |
0f20390f6fa5
mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
7 |
0f20390f6fa5
mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
8 local meta = mt.new(); meta.data = module:shared"meta"; |
0f20390f6fa5
mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
9 local data = mt.new(); data.data = module:shared"data"; |
0f20390f6fa5
mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
10 |
0f20390f6fa5
mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
11 local munin_listener = {}; |
0f20390f6fa5
mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
12 local munin_commands = {}; |
0f20390f6fa5
mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
13 |
0f20390f6fa5
mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
14 local node_name = module:get_option_string("munin_node_name", "localhost"); |
0f20390f6fa5
mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
15 local ignore_stats = module:get_option_set("munin_ignored_stats", { }); |
0f20390f6fa5
mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
16 |
0f20390f6fa5
mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
17 local function clean_fieldname(name) |
0f20390f6fa5
mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
18 return (name:gsub("[^A-Za-z0-9_]", "_"):gsub("^[^A-Za-z_]", "_%1")); |
0f20390f6fa5
mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
19 end |
0f20390f6fa5
mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
20 |
0f20390f6fa5
mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
21 function munin_listener.onconnect(conn) |
0f20390f6fa5
mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
22 -- require"core.statsmanager".collect(); |
0f20390f6fa5
mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
23 conn:write("# munin node at "..node_name.."\n"); |
0f20390f6fa5
mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
24 end |
0f20390f6fa5
mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
25 |
0f20390f6fa5
mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
26 function munin_listener.onincoming(conn, line) |
0f20390f6fa5
mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
27 line = line and line:match("^[^\r\n]+"); |
0f20390f6fa5
mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
28 if type(line) ~= "string" then return end |
0f20390f6fa5
mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
29 -- module:log("debug", "incoming: %q", line); |
0f20390f6fa5
mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
30 local command = line:match"^%w+"; |
0f20390f6fa5
mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
31 command = munin_commands[command]; |
0f20390f6fa5
mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
32 if not command then |
0f20390f6fa5
mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
33 conn:write("# Unknown command.\n"); |
0f20390f6fa5
mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
34 return; |
0f20390f6fa5
mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
35 end |
0f20390f6fa5
mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
36 local ok, err = pcall(command, conn, line); |
0f20390f6fa5
mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
37 if not ok then |
0f20390f6fa5
mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
38 module:log("error", "Error running %q: %s", line, err); |
0f20390f6fa5
mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
39 conn:close(); |
0f20390f6fa5
mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
40 end |
0f20390f6fa5
mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
41 end |
0f20390f6fa5
mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
42 |
0f20390f6fa5
mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
43 function munin_listener.ondisconnect() end |
0f20390f6fa5
mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
44 |
0f20390f6fa5
mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
45 function munin_commands.cap(conn) |
0f20390f6fa5
mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
46 conn:write("cap\n"); |
0f20390f6fa5
mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
47 end |
0f20390f6fa5
mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
48 |
1648 | 49 function munin_commands.list(conn) |
1624
0f20390f6fa5
mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
50 conn:write(array(it.keys(data.data)):concat(" ") .. "\n"); |
0f20390f6fa5
mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
51 end |
0f20390f6fa5
mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
52 |
0f20390f6fa5
mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
53 function munin_commands.config(conn, line) |
0f20390f6fa5
mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
54 -- TODO what exactly? |
0f20390f6fa5
mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
55 local stat = line:match("%s(%S+)"); |
0f20390f6fa5
mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
56 if not stat then conn:write("# Unknown service\n.\n"); return end |
1648 | 57 for _, _, k, value in meta:iter(stat, "", nil) do |
1624
0f20390f6fa5
mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
58 conn:write(s_format("%s %s\n", k, value)); |
0f20390f6fa5
mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
59 end |
1648 | 60 for _, name, k, value in meta:iter(stat, nil, nil) do |
1676
accbf0db0246
mod_munin: Exclude ignored stats even if they happen to be included in data
Kim Alvefur <zash@zash.se>
parents:
1648
diff
changeset
|
61 if name ~= "" and not ignore_stats:contains(name) then |
1624
0f20390f6fa5
mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
62 conn:write(s_format("%s.%s %s\n", name, k, value)); |
0f20390f6fa5
mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
63 end |
0f20390f6fa5
mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
64 end |
0f20390f6fa5
mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
65 conn:write(".\n"); |
0f20390f6fa5
mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
66 end |
0f20390f6fa5
mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
67 |
0f20390f6fa5
mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
68 function munin_commands.fetch(conn, line) |
0f20390f6fa5
mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
69 local stat = line:match("%s(%S+)"); |
0f20390f6fa5
mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
70 if not stat then conn:write("# Unknown service\n.\n"); return end |
1648 | 71 for _, name, value in data:iter(stat, nil) do |
1676
accbf0db0246
mod_munin: Exclude ignored stats even if they happen to be included in data
Kim Alvefur <zash@zash.se>
parents:
1648
diff
changeset
|
72 if not ignore_stats:contains(name) then |
1692
44ddec97ad82
mod_munin: Use string.format flag instead of tostring when reporting values
Kim Alvefur <zash@zash.se>
parents:
1676
diff
changeset
|
73 conn:write(s_format("%s.value %.12f\n", name, value)); |
1676
accbf0db0246
mod_munin: Exclude ignored stats even if they happen to be included in data
Kim Alvefur <zash@zash.se>
parents:
1648
diff
changeset
|
74 end |
1624
0f20390f6fa5
mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
75 end |
0f20390f6fa5
mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
76 conn:write(".\n"); |
0f20390f6fa5
mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
77 end |
0f20390f6fa5
mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
78 |
0f20390f6fa5
mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
79 function munin_commands.quit(conn) |
0f20390f6fa5
mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
80 conn:close(); |
0f20390f6fa5
mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
81 end |
0f20390f6fa5
mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
82 |
0f20390f6fa5
mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
83 module:hook("stats-updated", function (event) |
0f20390f6fa5
mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
84 local all_stats, this = event.stats_extra; |
0f20390f6fa5
mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
85 local host, sect, name, typ, key; |
0f20390f6fa5
mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
86 for stat, value in pairs(event.changed_stats) do |
0f20390f6fa5
mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
87 if not ignore_stats:contains(stat) then |
0f20390f6fa5
mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
88 this = all_stats[stat]; |
0f20390f6fa5
mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
89 -- module:log("debug", "changed_stats[%q] = %s", stat, tostring(value)); |
0f20390f6fa5
mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
90 host, sect, name, typ = stat:match("^/([^/]+)/([^/]+)/(.+):(%a+)$"); |
0f20390f6fa5
mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
91 if host == nil then |
3130
c47cd8dbd310
mod_munin: Allow names containing any number of “:”.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
3129
diff
changeset
|
92 sect, name, typ = stat:match("^([^.]+)%.(.+):(%a+)$"); |
1624
0f20390f6fa5
mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
93 elseif host == "*" then |
0f20390f6fa5
mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
94 host = nil; |
0f20390f6fa5
mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
95 end |
1898 | 96 if sect:find("^mod_measure_.") then |
1897
effd909d05b0
mod_munin: Strip mod_measure_ and mod_statistics_ from section names
Kim Alvefur <zash@zash.se>
parents:
1692
diff
changeset
|
97 sect = sect:sub(13); |
1898 | 98 elseif sect:find("^mod_statistics_.") then |
1897
effd909d05b0
mod_munin: Strip mod_measure_ and mod_statistics_ from section names
Kim Alvefur <zash@zash.se>
parents:
1692
diff
changeset
|
99 sect = sect:sub(16); |
effd909d05b0
mod_munin: Strip mod_measure_ and mod_statistics_ from section names
Kim Alvefur <zash@zash.se>
parents:
1692
diff
changeset
|
100 end |
1624
0f20390f6fa5
mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
101 key = clean_fieldname(s_format("%s_%s_%s", host or "global", sect, typ)); |
0f20390f6fa5
mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
102 |
0f20390f6fa5
mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
103 if not meta:get(key) then |
0f20390f6fa5
mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
104 if host then |
0f20390f6fa5
mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
105 meta:set(key, "", "graph_title", s_format("%s %s on %s", sect, typ, host)); |
0f20390f6fa5
mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
106 else |
3129
2ffc268ba2fa
mod_munin: Don’t use host when it is nil.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
1898
diff
changeset
|
107 meta:set(key, "", "graph_title", s_format("Global %s %s", sect, typ)); |
1624
0f20390f6fa5
mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
108 end |
0f20390f6fa5
mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
109 meta:set(key, "", "graph_vlabel", this and this.units or typ); |
0f20390f6fa5
mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
110 meta:set(key, "", "graph_category", sect); |
0f20390f6fa5
mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
111 |
0f20390f6fa5
mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
112 meta:set(key, name, "label", name); |
0f20390f6fa5
mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
113 elseif not meta:get(key, name, "label") then |
0f20390f6fa5
mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
114 meta:set(key, name, "label", name); |
0f20390f6fa5
mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
115 end |
0f20390f6fa5
mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
116 |
0f20390f6fa5
mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
117 data:set(key, name, value); |
0f20390f6fa5
mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
118 end |
0f20390f6fa5
mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
119 end |
0f20390f6fa5
mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
120 end); |
0f20390f6fa5
mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
121 |
0f20390f6fa5
mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
122 module:provides("net", { |
0f20390f6fa5
mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
123 listener = munin_listener; |
0f20390f6fa5
mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
124 default_mode = "*l"; |
0f20390f6fa5
mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
125 default_port = 4949; |
0f20390f6fa5
mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
126 }); |
0f20390f6fa5
mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
127 |