Annotate

mod_munin/mod_munin.lua @ 1713:01e9465f8f80

mod_delegation: delegated features/identities removal for disco info requests on bare jid
author Goffi <goffi@goffi.org>
date Fri, 17 Apr 2015 21:06:51 +0200
parent 1648:648ce9087902
child 1676:accbf0db0246
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
648ce9087902 mod_munin: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1624
diff changeset
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
648ce9087902 mod_munin: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1624
diff changeset
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
648ce9087902 mod_munin: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1624
diff changeset
60 for _, name, k, value in meta:iter(stat, nil, nil) do
1624
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
61 if name ~= "" then
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
648ce9087902 mod_munin: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1624
diff changeset
71 for _, name, value in data:iter(stat, nil) do
1624
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
72 conn:write(s_format("%s.value %s\n", name, tostring(value)));
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
73 end
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
74 conn:write(".\n");
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
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
77 function munin_commands.quit(conn)
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
78 conn:close();
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
79 end
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
80
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
81 module:hook("stats-updated", function (event)
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
82 local all_stats, this = event.stats_extra;
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
83 local host, sect, name, typ, key;
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
84 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
85 if not ignore_stats:contains(stat) then
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
86 this = all_stats[stat];
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
87 -- 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
88 host, sect, name, typ = stat:match("^/([^/]+)/([^/]+)/(.+):(%a+)$");
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
89 if host == nil then
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
90 sect, name, typ, host = stat:match("^([^.]+)%.([^:]+):(%a+)$");
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
91 elseif host == "*" then
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
92 host = nil;
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
93 end
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
94 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
95
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
96 if not meta:get(key) then
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
97 if host then
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
98 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
99 else
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
100 meta:set(key, "", "graph_title", s_format("Global %s %s", sect, typ, host));
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
101 end
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
102 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
103 meta:set(key, "", "graph_category", sect);
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
104
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
105 meta:set(key, name, "label", name);
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
106 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
107 meta:set(key, name, "label", name);
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
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
110 data:set(key, name, value);
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
111 end
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
112 end
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
113 end);
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
114
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
115 module:provides("net", {
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
116 listener = munin_listener;
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
117 default_mode = "*l";
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
118 default_port = 4949;
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
119 });
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
120