annotate mod_munin/mod_munin.lua @ 6299:5cf5ee23b361

mod_voipms: New Module to send and receive SMS/MMS via VoIP.ms APIs. diff --git a/mod_voipms/README.md b/mod_voipms/README.md new file mode 100644 --- /dev/null +++ b/mod_voipms/README.md @@ -0,0 +1,51 @@ +--- +labels: +- 'Stage-Alpha' +- 'Type-Web' +summary: Send and receive SMS/MMS via VoIP.ms APIs. +rockspec: + build: + modules: + mod_voipms: mod_voipms.lua +... + +Introduction +============ + +This is a Prosody module to map JIDs to DIDs on VoIP.ms and support sending/receiving SMS/MMS. + +Configuration +============= + +| option | type | default | +|-----------------------|--------|---------| +| voipms\_api\_username | string | nil | +| voipms\_api\_password | string | nil | +| voipms\_query\_key | string | nil | +| voipms\_jid\_map | table | nil | +``` +VirtualHost "sms.example.com" +modules_enabled = { + "voipms"; +} +voipms_api_username = john@example.com -- E-mail registered with VoIP.ms +voipms_api_password = abcd1234 -- API password configured in VoIP.ms +voipms_query_key = some_query_key -- query param 'key' part of your URL callback +voipms_jid_map = { + ["your_jid@your_domain.com"] = "+1234567890" +} +``` + +HTTP +==== + +The module is served on Prosody's default HTTP ports at the path /voipms. More details on configuring HTTP modules in Prosody can be found in the HTTP documentation. + +VoIP.ms Webhook URL +=================== + +This module receives the VoIP.ms Webhook URL (POST) at the /voipms endpoint. It uses the sendSMS/sendMMS GET methods against the VoIP.ms APIs. This is an example webhook to use in VoIP.ms: + +``` +https://sms.example.com/voipms?key=some_query_key +``` diff --git a/mod_voipms/mod_voipms.lua b/mod_voipms/mod_voipms.lua new file mode 100644 --- /dev/null +++ b/mod_voipms/mod_voipms.lua @@ -0,0 +1,166 @@ +local http = require "net.http" +local json = require "util.json" +local st = require "util.stanza" + +local api_username = module:get_option_string("voipms_api_username") +local api_password = module:get_option_string("voipms_api_password") +local query_key = module:get_option("voipms_query_key") +local jid_map = module:get_option("voipms_jid_map") or {} +local rest_endpoint = "https://voip.ms/api/v1/rest.php" + +if not api_username or not api_password or not query_key then + module:log("error", "Missing required config values (voipms_api_username, voipms_api_password, voipms_query_key)") + return +end + +module:depends("http") + +local function normalize_number(num) + if not num then return nil end + if num:sub(1, 1) ~= "+" then + return "+1" .. num + end + return num +end + +local function extract_query_key(event) + return (event.request.url.query or ""):match("key=([^&]+)") +end + +module:provides("http", { + route = { + ["POST"] = function(event) + local req = event.request + local body = req.body or "" + + if extract_query_key(event) ~= query_key then + module:log("warn", "Unauthorized webhook: missing or invalid key") + return { status_code = 403 } + end + + local json_payload, err = json.decode(body) + if not json_payload then + module:log("warn", "Invalid JSON: %s", err or "unknown error") + return { status_code = 400 } + end + + local payload = json_payload.data and json_payload.data.payload + if not payload then + module:log("warn", "Missing payload in JSON") + return { status_code = 400 } + end + + local from = payload.from and payload.from.phone_number + local to_list = payload.to or {} + local to = #to_list > 0 and to_list[1].phone_number or nil + + if not from or not to then + module:log("warn", "Missing phone numbers (from: %s, to: %s)", tostring(from), tostring(to)) + return { status_code = 400 } + end + + local normalized_to = normalize_number(to) + local target_jid = nil + + for jid, did in pairs(jid_map) do + if normalize_number(did) == normalized_to then + target_jid = jid + break + end + end + + if not target_jid then + module:log("warn", "No JID mapping for DID %s", normalized_to) + return { status_code = 404 } + end + + local normalized_from = normalize_number(from) + local message_text = payload.text or "" + + local message = st.message({ + from = normalized_from .. "@" .. module.host, + to = target_jid, + type = "chat" + }):tag("body"):text(message_text):up() + + if payload.media and #payload.media > 0 then + for _, media_item in ipairs(payload.media) do + if media_item.url then + message_text = message_text .. "\n" .. media_item.url + message:tag("x", { xmlns = "jabber:x:oob" }) + :tag("url"):text(media_item.url):up():up() + end + end + end + + module:send(message) + module:log("info", "Delivered SMS from %s to %s", normalized_from, target_jid) + + return { status_code = 204 } + end + } +}) + +module:hook("message/bare", function(event) + local stanza = event.stanza + if stanza.attr.type ~= "chat" then return end + + local from_jid = stanza.attr.from + local to_jid = stanza.attr.to + local body = stanza:get_child_text("body") + if not body or body == "" then return end + + local from_number = jid_map[from_jid] + if not from_number then + module:log("warn", "No DID mapping for JID %s", from_jid) + return + end + + local to_number = to_jid:match("^([^@]+)") + if not to_number then + module:log("warn", "Malformed JID in message.to: %s", to_jid) + return + end + + to_number = normalize_number(to_number) + + local media_urls = {} + for line in body:gmatch("[^\r\n]+") do + if line:match("^https?://") then + table.insert(media_urls, line) + end + end + + local method = (#media_urls > 0) and "sendMMS" or "sendSMS" + local query = { + api_username = api_username, + api_password = api_password, + method = method, + did = from_number, + dst = to_number, + message = body + } + + if method == "sendMMS" then + for i, url in ipairs(media_urls) do + query["media_url[" .. (i - 1) .. "]"] = url + end + end + + local query_str = http.formencode(query) + + http.request(rest_endpoint .. "?" .. query_str, { + method = "GET"; + }, function(response_body, code) + if code == 200 then + local resp, err = json.decode(response_body) + if not resp or resp.status ~= "success" then + module:log("error", "Failed to send %s: %s", method, err or (resp and resp.status) or "unknown") + else + module:log("info", "Sent %s from %s to %s", method, from_number, to_number) + end + else + module:log("error", "HTTP error sending %s: code %s", method, tostring(code)) + end + end) +end)
author Chaz <menel@snikket.de>
date Mon, 21 Jul 2025 23:57:37 +0200
parents 7e5c8186f121
children
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;
4553
7e5c8186f121 mod_munin: Port to new OpenMetrics based statistics module
Jonas Schäfer <jonas@wielicki.name>
parents: 3130
diff changeset
4 local t_insert = table.insert;
7e5c8186f121 mod_munin: Port to new OpenMetrics based statistics module
Jonas Schäfer <jonas@wielicki.name>
parents: 3130
diff changeset
5 local t_concat = table.concat;
1624
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
6 local array = require"util.array";
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
7 local it = require"util.iterators";
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
8 local mt = require"util.multitable";
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
9
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
10 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
11 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
12
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
13 local munin_listener = {};
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
14 local munin_commands = {};
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
15
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
16 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
17 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
18
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
19 local function clean_fieldname(name)
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
20 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
21 end
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
22
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
23 function munin_listener.onconnect(conn)
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
24 -- require"core.statsmanager".collect();
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
25 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
26 end
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
27
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
28 function munin_listener.onincoming(conn, line)
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
29 line = line and line:match("^[^\r\n]+");
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
30 if type(line) ~= "string" then return end
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
31 -- module:log("debug", "incoming: %q", line);
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
32 local command = line:match"^%w+";
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
33 command = munin_commands[command];
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
34 if not command then
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
35 conn:write("# Unknown command.\n");
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
36 return;
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
37 end
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
38 local ok, err = pcall(command, conn, line);
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
39 if not ok then
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
40 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
41 conn:close();
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
42 end
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
43 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_listener.ondisconnect() end
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
46
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
47 function munin_commands.cap(conn)
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
48 conn:write("cap\n");
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
49 end
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
50
1648
648ce9087902 mod_munin: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1624
diff changeset
51 function munin_commands.list(conn)
1624
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
52 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
53 end
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
54
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
55 function munin_commands.config(conn, line)
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
56 -- TODO what exactly?
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
57 local stat = line:match("%s(%S+)");
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
58 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
59 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
60 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
61 end
1648
648ce9087902 mod_munin: Cleanup [luacheck]
Kim Alvefur <zash@zash.se>
parents: 1624
diff changeset
62 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
63 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
64 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
65 end
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 conn:write(".\n");
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
68 end
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
69
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
70 function munin_commands.fetch(conn, line)
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
71 local stat = line:match("%s(%S+)");
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
72 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
73 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
74 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
75 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
76 end
1624
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 conn:write(".\n");
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 function munin_commands.quit(conn)
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
82 conn:close();
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
83 end
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
84
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
85 module:hook("stats-updated", function (event)
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
86 local all_stats, this = event.stats_extra;
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
87 local host, sect, name, typ, key;
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
88 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
89 if not ignore_stats:contains(stat) then
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
90 this = all_stats[stat];
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
91 -- 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
92 host, sect, name, typ = stat:match("^/([^/]+)/([^/]+)/(.+):(%a+)$");
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
93 if host == nil then
3130
c47cd8dbd310 mod_munin: Allow names containing any number of “:”.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 3129
diff changeset
94 sect, name, typ = stat:match("^([^.]+)%.(.+):(%a+)$");
1624
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
95 elseif host == "*" then
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
96 host = nil;
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
97 end
1898
d85ddd3e588a mod_munin: Fix syntax error
Kim Alvefur <zash@zash.se>
parents: 1897
diff changeset
98 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
99 sect = sect:sub(13);
1898
d85ddd3e588a mod_munin: Fix syntax error
Kim Alvefur <zash@zash.se>
parents: 1897
diff changeset
100 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
101 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
102 end
1624
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
103 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
104
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
105 if not meta:get(key) then
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
106 if host then
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
107 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
108 else
3129
2ffc268ba2fa mod_munin: Don’t use host when it is nil.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 1898
diff changeset
109 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
110 end
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
111 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
112 meta:set(key, "", "graph_category", sect);
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
113
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 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
116 meta:set(key, name, "label", name);
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
117 end
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
118
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
119 data:set(key, name, value);
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 end
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
122 end);
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
123
4553
7e5c8186f121 mod_munin: Port to new OpenMetrics based statistics module
Jonas Schäfer <jonas@wielicki.name>
parents: 3130
diff changeset
124 local function openmetrics_handler(event)
7e5c8186f121 mod_munin: Port to new OpenMetrics based statistics module
Jonas Schäfer <jonas@wielicki.name>
parents: 3130
diff changeset
125 local registry = event.metric_registry
7e5c8186f121 mod_munin: Port to new OpenMetrics based statistics module
Jonas Schäfer <jonas@wielicki.name>
parents: 3130
diff changeset
126 local host, sect, name, typ, key;
7e5c8186f121 mod_munin: Port to new OpenMetrics based statistics module
Jonas Schäfer <jonas@wielicki.name>
parents: 3130
diff changeset
127 for family_name, metric_family in pairs(registry:get_metric_families()) do
7e5c8186f121 mod_munin: Port to new OpenMetrics based statistics module
Jonas Schäfer <jonas@wielicki.name>
parents: 3130
diff changeset
128 if not ignore_stats:contains(family_name) then
7e5c8186f121 mod_munin: Port to new OpenMetrics based statistics module
Jonas Schäfer <jonas@wielicki.name>
parents: 3130
diff changeset
129 -- module:log("debug", "changed_stats[%q] = %s", stat, tostring(value));
7e5c8186f121 mod_munin: Port to new OpenMetrics based statistics module
Jonas Schäfer <jonas@wielicki.name>
parents: 3130
diff changeset
130 local host_key
7e5c8186f121 mod_munin: Port to new OpenMetrics based statistics module
Jonas Schäfer <jonas@wielicki.name>
parents: 3130
diff changeset
131 if metric_family.label_keys[1] == "host" then
7e5c8186f121 mod_munin: Port to new OpenMetrics based statistics module
Jonas Schäfer <jonas@wielicki.name>
parents: 3130
diff changeset
132 host_key = 1
7e5c8186f121 mod_munin: Port to new OpenMetrics based statistics module
Jonas Schäfer <jonas@wielicki.name>
parents: 3130
diff changeset
133 end
7e5c8186f121 mod_munin: Port to new OpenMetrics based statistics module
Jonas Schäfer <jonas@wielicki.name>
parents: 3130
diff changeset
134 if family_name:sub(1, 12) == "prosody_mod_" then
7e5c8186f121 mod_munin: Port to new OpenMetrics based statistics module
Jonas Schäfer <jonas@wielicki.name>
parents: 3130
diff changeset
135 sect, name = family_name:match("^prosody_mod_([^/]+)/(.+)$")
7e5c8186f121 mod_munin: Port to new OpenMetrics based statistics module
Jonas Schäfer <jonas@wielicki.name>
parents: 3130
diff changeset
136 else
7e5c8186f121 mod_munin: Port to new OpenMetrics based statistics module
Jonas Schäfer <jonas@wielicki.name>
parents: 3130
diff changeset
137 sect, name = family_name:match("^([^_]+)_(.+)$")
7e5c8186f121 mod_munin: Port to new OpenMetrics based statistics module
Jonas Schäfer <jonas@wielicki.name>
parents: 3130
diff changeset
138 end
7e5c8186f121 mod_munin: Port to new OpenMetrics based statistics module
Jonas Schäfer <jonas@wielicki.name>
parents: 3130
diff changeset
139 name = clean_fieldname(name)
7e5c8186f121 mod_munin: Port to new OpenMetrics based statistics module
Jonas Schäfer <jonas@wielicki.name>
parents: 3130
diff changeset
140
7e5c8186f121 mod_munin: Port to new OpenMetrics based statistics module
Jonas Schäfer <jonas@wielicki.name>
parents: 3130
diff changeset
141 local metric_type = metric_family.type_
7e5c8186f121 mod_munin: Port to new OpenMetrics based statistics module
Jonas Schäfer <jonas@wielicki.name>
parents: 3130
diff changeset
142 if metric_type == "gauge" or metric_type == "unknown" then
7e5c8186f121 mod_munin: Port to new OpenMetrics based statistics module
Jonas Schäfer <jonas@wielicki.name>
parents: 3130
diff changeset
143 typ = "GAUGE"
7e5c8186f121 mod_munin: Port to new OpenMetrics based statistics module
Jonas Schäfer <jonas@wielicki.name>
parents: 3130
diff changeset
144 else
7e5c8186f121 mod_munin: Port to new OpenMetrics based statistics module
Jonas Schäfer <jonas@wielicki.name>
parents: 3130
diff changeset
145 typ = "DCOUNTER"
7e5c8186f121 mod_munin: Port to new OpenMetrics based statistics module
Jonas Schäfer <jonas@wielicki.name>
parents: 3130
diff changeset
146 end
7e5c8186f121 mod_munin: Port to new OpenMetrics based statistics module
Jonas Schäfer <jonas@wielicki.name>
parents: 3130
diff changeset
147
7e5c8186f121 mod_munin: Port to new OpenMetrics based statistics module
Jonas Schäfer <jonas@wielicki.name>
parents: 3130
diff changeset
148 for labelset, metric in metric_family:iter_metrics() do
7e5c8186f121 mod_munin: Port to new OpenMetrics based statistics module
Jonas Schäfer <jonas@wielicki.name>
parents: 3130
diff changeset
149 host = host_key and labelset[host_key] or "global"
7e5c8186f121 mod_munin: Port to new OpenMetrics based statistics module
Jonas Schäfer <jonas@wielicki.name>
parents: 3130
diff changeset
150 local name_parts = {}
7e5c8186f121 mod_munin: Port to new OpenMetrics based statistics module
Jonas Schäfer <jonas@wielicki.name>
parents: 3130
diff changeset
151 for i, label in ipairs(labelset) do
7e5c8186f121 mod_munin: Port to new OpenMetrics based statistics module
Jonas Schäfer <jonas@wielicki.name>
parents: 3130
diff changeset
152 if i ~= host_key then
7e5c8186f121 mod_munin: Port to new OpenMetrics based statistics module
Jonas Schäfer <jonas@wielicki.name>
parents: 3130
diff changeset
153 t_insert(name_parts, label)
7e5c8186f121 mod_munin: Port to new OpenMetrics based statistics module
Jonas Schäfer <jonas@wielicki.name>
parents: 3130
diff changeset
154 end
7e5c8186f121 mod_munin: Port to new OpenMetrics based statistics module
Jonas Schäfer <jonas@wielicki.name>
parents: 3130
diff changeset
155 end
7e5c8186f121 mod_munin: Port to new OpenMetrics based statistics module
Jonas Schäfer <jonas@wielicki.name>
parents: 3130
diff changeset
156 local full_name = t_concat(name_parts, "_")
7e5c8186f121 mod_munin: Port to new OpenMetrics based statistics module
Jonas Schäfer <jonas@wielicki.name>
parents: 3130
diff changeset
157 local display_name = #name_parts > 0 and full_name or name
7e5c8186f121 mod_munin: Port to new OpenMetrics based statistics module
Jonas Schäfer <jonas@wielicki.name>
parents: 3130
diff changeset
158 key = clean_fieldname(s_format("%s_%s_%s", host or "global", sect, name));
7e5c8186f121 mod_munin: Port to new OpenMetrics based statistics module
Jonas Schäfer <jonas@wielicki.name>
parents: 3130
diff changeset
159
7e5c8186f121 mod_munin: Port to new OpenMetrics based statistics module
Jonas Schäfer <jonas@wielicki.name>
parents: 3130
diff changeset
160 local unit
7e5c8186f121 mod_munin: Port to new OpenMetrics based statistics module
Jonas Schäfer <jonas@wielicki.name>
parents: 3130
diff changeset
161 local factor = 1
7e5c8186f121 mod_munin: Port to new OpenMetrics based statistics module
Jonas Schäfer <jonas@wielicki.name>
parents: 3130
diff changeset
162 unit = metric_family.unit
7e5c8186f121 mod_munin: Port to new OpenMetrics based statistics module
Jonas Schäfer <jonas@wielicki.name>
parents: 3130
diff changeset
163 if unit == "seconds" and typ == "DCOUNTER" then
7e5c8186f121 mod_munin: Port to new OpenMetrics based statistics module
Jonas Schäfer <jonas@wielicki.name>
parents: 3130
diff changeset
164 factor = 100
7e5c8186f121 mod_munin: Port to new OpenMetrics based statistics module
Jonas Schäfer <jonas@wielicki.name>
parents: 3130
diff changeset
165 unit = "%time"
7e5c8186f121 mod_munin: Port to new OpenMetrics based statistics module
Jonas Schäfer <jonas@wielicki.name>
parents: 3130
diff changeset
166 elseif typ == "DCOUNTER" then
7e5c8186f121 mod_munin: Port to new OpenMetrics based statistics module
Jonas Schäfer <jonas@wielicki.name>
parents: 3130
diff changeset
167 unit = unit .. "/s"
7e5c8186f121 mod_munin: Port to new OpenMetrics based statistics module
Jonas Schäfer <jonas@wielicki.name>
parents: 3130
diff changeset
168 end
7e5c8186f121 mod_munin: Port to new OpenMetrics based statistics module
Jonas Schäfer <jonas@wielicki.name>
parents: 3130
diff changeset
169
7e5c8186f121 mod_munin: Port to new OpenMetrics based statistics module
Jonas Schäfer <jonas@wielicki.name>
parents: 3130
diff changeset
170 if not meta:get(key, "") then
7e5c8186f121 mod_munin: Port to new OpenMetrics based statistics module
Jonas Schäfer <jonas@wielicki.name>
parents: 3130
diff changeset
171 meta:set(key, "", "graph_title", s_format(metric_family.description));
7e5c8186f121 mod_munin: Port to new OpenMetrics based statistics module
Jonas Schäfer <jonas@wielicki.name>
parents: 3130
diff changeset
172 if unit ~= "" then
7e5c8186f121 mod_munin: Port to new OpenMetrics based statistics module
Jonas Schäfer <jonas@wielicki.name>
parents: 3130
diff changeset
173 meta:set(key, "", "graph_vlabel", unit);
7e5c8186f121 mod_munin: Port to new OpenMetrics based statistics module
Jonas Schäfer <jonas@wielicki.name>
parents: 3130
diff changeset
174 end
7e5c8186f121 mod_munin: Port to new OpenMetrics based statistics module
Jonas Schäfer <jonas@wielicki.name>
parents: 3130
diff changeset
175 meta:set(key, "", "graph_category", sect);
7e5c8186f121 mod_munin: Port to new OpenMetrics based statistics module
Jonas Schäfer <jonas@wielicki.name>
parents: 3130
diff changeset
176 end
7e5c8186f121 mod_munin: Port to new OpenMetrics based statistics module
Jonas Schäfer <jonas@wielicki.name>
parents: 3130
diff changeset
177 if not meta:get(key, display_name) then
7e5c8186f121 mod_munin: Port to new OpenMetrics based statistics module
Jonas Schäfer <jonas@wielicki.name>
parents: 3130
diff changeset
178 meta:set(key, display_name, "label", display_name);
7e5c8186f121 mod_munin: Port to new OpenMetrics based statistics module
Jonas Schäfer <jonas@wielicki.name>
parents: 3130
diff changeset
179 meta:set(key, display_name, "type", typ)
7e5c8186f121 mod_munin: Port to new OpenMetrics based statistics module
Jonas Schäfer <jonas@wielicki.name>
parents: 3130
diff changeset
180 end
7e5c8186f121 mod_munin: Port to new OpenMetrics based statistics module
Jonas Schäfer <jonas@wielicki.name>
parents: 3130
diff changeset
181
7e5c8186f121 mod_munin: Port to new OpenMetrics based statistics module
Jonas Schäfer <jonas@wielicki.name>
parents: 3130
diff changeset
182 for suffix, extra_labels, value in metric:iter_samples() do
7e5c8186f121 mod_munin: Port to new OpenMetrics based statistics module
Jonas Schäfer <jonas@wielicki.name>
parents: 3130
diff changeset
183 if metric_type == "histogram" or metric_type == "summary" then
7e5c8186f121 mod_munin: Port to new OpenMetrics based statistics module
Jonas Schäfer <jonas@wielicki.name>
parents: 3130
diff changeset
184 if suffix == "_sum" then
7e5c8186f121 mod_munin: Port to new OpenMetrics based statistics module
Jonas Schäfer <jonas@wielicki.name>
parents: 3130
diff changeset
185 data:set(key, display_name, value * factor)
7e5c8186f121 mod_munin: Port to new OpenMetrics based statistics module
Jonas Schäfer <jonas@wielicki.name>
parents: 3130
diff changeset
186 end
7e5c8186f121 mod_munin: Port to new OpenMetrics based statistics module
Jonas Schäfer <jonas@wielicki.name>
parents: 3130
diff changeset
187 elseif suffix == "_total" or suffix == "" then
7e5c8186f121 mod_munin: Port to new OpenMetrics based statistics module
Jonas Schäfer <jonas@wielicki.name>
parents: 3130
diff changeset
188 data:set(key, display_name, value * factor)
7e5c8186f121 mod_munin: Port to new OpenMetrics based statistics module
Jonas Schäfer <jonas@wielicki.name>
parents: 3130
diff changeset
189 end
7e5c8186f121 mod_munin: Port to new OpenMetrics based statistics module
Jonas Schäfer <jonas@wielicki.name>
parents: 3130
diff changeset
190 end
7e5c8186f121 mod_munin: Port to new OpenMetrics based statistics module
Jonas Schäfer <jonas@wielicki.name>
parents: 3130
diff changeset
191 end
7e5c8186f121 mod_munin: Port to new OpenMetrics based statistics module
Jonas Schäfer <jonas@wielicki.name>
parents: 3130
diff changeset
192 end
7e5c8186f121 mod_munin: Port to new OpenMetrics based statistics module
Jonas Schäfer <jonas@wielicki.name>
parents: 3130
diff changeset
193 end
7e5c8186f121 mod_munin: Port to new OpenMetrics based statistics module
Jonas Schäfer <jonas@wielicki.name>
parents: 3130
diff changeset
194 end
7e5c8186f121 mod_munin: Port to new OpenMetrics based statistics module
Jonas Schäfer <jonas@wielicki.name>
parents: 3130
diff changeset
195
7e5c8186f121 mod_munin: Port to new OpenMetrics based statistics module
Jonas Schäfer <jonas@wielicki.name>
parents: 3130
diff changeset
196 module:hook("openmetrics-updated", openmetrics_handler);
7e5c8186f121 mod_munin: Port to new OpenMetrics based statistics module
Jonas Schäfer <jonas@wielicki.name>
parents: 3130
diff changeset
197
1624
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
198 module:provides("net", {
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
199 listener = munin_listener;
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
200 default_mode = "*l";
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
201 default_port = 4949;
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
202 });
0f20390f6fa5 mod_munin: Implementation of the Munin collection protocol
Kim Alvefur <zash@zash.se>
parents:
diff changeset
203