annotate mod_pubsub_alertmanager/mod_pubsub_alertmanager.lua @ 4618:48132b6e1b16

mod_pubsub_alertmanager: Publish Alertmanager alerts into pubsub For integration with other things as pubsub subscribers.
author Kim Alvefur <zash@zash.se>
date Wed, 09 Jun 2021 01:14:46 +0200 (2021-06-08)
parents mod_pubsub_post/mod_pubsub_post.lua@c87181a98f29
children b11001bd915d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1619
43c54a27bab2 mod_pubsub_post: Module to publish to pubsub nodes from a simple HTTP POST
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
1 local st = require "util.stanza";
43c54a27bab2 mod_pubsub_post: Module to publish to pubsub nodes from a simple HTTP POST
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
2 local json = require "util.json";
43c54a27bab2 mod_pubsub_post: Module to publish to pubsub nodes from a simple HTTP POST
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
3 local uuid_generate = require "util.uuid".generate;
4618
48132b6e1b16 mod_pubsub_alertmanager: Publish Alertmanager alerts into pubsub
Kim Alvefur <zash@zash.se>
parents: 4552
diff changeset
4
48132b6e1b16 mod_pubsub_alertmanager: Publish Alertmanager alerts into pubsub
Kim Alvefur <zash@zash.se>
parents: 4552
diff changeset
5 module:depends("http");
1619
43c54a27bab2 mod_pubsub_post: Module to publish to pubsub nodes from a simple HTTP POST
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
6
43c54a27bab2 mod_pubsub_post: Module to publish to pubsub nodes from a simple HTTP POST
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
7 local pubsub_service = module:depends("pubsub").service;
43c54a27bab2 mod_pubsub_post: Module to publish to pubsub nodes from a simple HTTP POST
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
8
3014
72dbc9b66de8 mod_pubsub_post: Change to support arbitrary XML payloads
Kim Alvefur <zash@zash.se>
parents: 3013
diff changeset
9 local error_mapping = {
72dbc9b66de8 mod_pubsub_post: Change to support arbitrary XML payloads
Kim Alvefur <zash@zash.se>
parents: 3013
diff changeset
10 ["forbidden"] = 403;
72dbc9b66de8 mod_pubsub_post: Change to support arbitrary XML payloads
Kim Alvefur <zash@zash.se>
parents: 3013
diff changeset
11 ["item-not-found"] = 404;
72dbc9b66de8 mod_pubsub_post: Change to support arbitrary XML payloads
Kim Alvefur <zash@zash.se>
parents: 3013
diff changeset
12 ["internal-server-error"] = 500;
72dbc9b66de8 mod_pubsub_post: Change to support arbitrary XML payloads
Kim Alvefur <zash@zash.se>
parents: 3013
diff changeset
13 ["conflict"] = 409;
72dbc9b66de8 mod_pubsub_post: Change to support arbitrary XML payloads
Kim Alvefur <zash@zash.se>
parents: 3013
diff changeset
14 };
72dbc9b66de8 mod_pubsub_post: Change to support arbitrary XML payloads
Kim Alvefur <zash@zash.se>
parents: 3013
diff changeset
15
3017
8e48c0b233e0 mod_pubsub_post: Factor out the 'actor' into an argument
Kim Alvefur <zash@zash.se>
parents: 3016
diff changeset
16 local function publish_payload(node, actor, item_id, payload)
3014
72dbc9b66de8 mod_pubsub_post: Change to support arbitrary XML payloads
Kim Alvefur <zash@zash.se>
parents: 3013
diff changeset
17 local post_item = st.stanza("item", { xmlns = "http://jabber.org/protocol/pubsub", id = item_id, })
72dbc9b66de8 mod_pubsub_post: Change to support arbitrary XML payloads
Kim Alvefur <zash@zash.se>
parents: 3013
diff changeset
18 :add_child(payload);
3017
8e48c0b233e0 mod_pubsub_post: Factor out the 'actor' into an argument
Kim Alvefur <zash@zash.se>
parents: 3016
diff changeset
19 local ok, err = pubsub_service:publish(node, actor, item_id, post_item);
3014
72dbc9b66de8 mod_pubsub_post: Change to support arbitrary XML payloads
Kim Alvefur <zash@zash.se>
parents: 3013
diff changeset
20 module:log("debug", ":publish(%q, true, %q, %s) -> %q", node, item_id, payload:top_tag(), err or "");
72dbc9b66de8 mod_pubsub_post: Change to support arbitrary XML payloads
Kim Alvefur <zash@zash.se>
parents: 3013
diff changeset
21 if not ok then
72dbc9b66de8 mod_pubsub_post: Change to support arbitrary XML payloads
Kim Alvefur <zash@zash.se>
parents: 3013
diff changeset
22 return error_mapping[err] or 500;
72dbc9b66de8 mod_pubsub_post: Change to support arbitrary XML payloads
Kim Alvefur <zash@zash.se>
parents: 3013
diff changeset
23 end
72dbc9b66de8 mod_pubsub_post: Change to support arbitrary XML payloads
Kim Alvefur <zash@zash.se>
parents: 3013
diff changeset
24 return 202;
72dbc9b66de8 mod_pubsub_post: Change to support arbitrary XML payloads
Kim Alvefur <zash@zash.se>
parents: 3013
diff changeset
25 end
1619
43c54a27bab2 mod_pubsub_post: Module to publish to pubsub nodes from a simple HTTP POST
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
26
3014
72dbc9b66de8 mod_pubsub_post: Change to support arbitrary XML payloads
Kim Alvefur <zash@zash.se>
parents: 3013
diff changeset
27 function handle_POST(event, path)
72dbc9b66de8 mod_pubsub_post: Change to support arbitrary XML payloads
Kim Alvefur <zash@zash.se>
parents: 3013
diff changeset
28 local request = event.request;
72dbc9b66de8 mod_pubsub_post: Change to support arbitrary XML payloads
Kim Alvefur <zash@zash.se>
parents: 3013
diff changeset
29
4618
48132b6e1b16 mod_pubsub_alertmanager: Publish Alertmanager alerts into pubsub
Kim Alvefur <zash@zash.se>
parents: 4552
diff changeset
30 local payload = json.decode(event.request.body);
48132b6e1b16 mod_pubsub_alertmanager: Publish Alertmanager alerts into pubsub
Kim Alvefur <zash@zash.se>
parents: 4552
diff changeset
31 if type(payload) ~= "table" then return 400; end
48132b6e1b16 mod_pubsub_alertmanager: Publish Alertmanager alerts into pubsub
Kim Alvefur <zash@zash.se>
parents: 4552
diff changeset
32 if payload.version ~= "4" then return 501; end
3501
1df139b157fb mod_pubsub_post: Add support for WebSub authentication
Kim Alvefur <zash@zash.se>
parents: 3255
diff changeset
33
4618
48132b6e1b16 mod_pubsub_alertmanager: Publish Alertmanager alerts into pubsub
Kim Alvefur <zash@zash.se>
parents: 4552
diff changeset
34 for _, alert in ipairs(payload.alerts) do
48132b6e1b16 mod_pubsub_alertmanager: Publish Alertmanager alerts into pubsub
Kim Alvefur <zash@zash.se>
parents: 4552
diff changeset
35 local item = st.stanza("alerts", {xmlns = "urn:uuid:e3bec775-c607-4e9b-9a3f-94de1316d861:v4", status=alert.status});
48132b6e1b16 mod_pubsub_alertmanager: Publish Alertmanager alerts into pubsub
Kim Alvefur <zash@zash.se>
parents: 4552
diff changeset
36 for k, v in pairs(alert.annotations) do
48132b6e1b16 mod_pubsub_alertmanager: Publish Alertmanager alerts into pubsub
Kim Alvefur <zash@zash.se>
parents: 4552
diff changeset
37 item:text_tag("annotation", v, { name=k });
48132b6e1b16 mod_pubsub_alertmanager: Publish Alertmanager alerts into pubsub
Kim Alvefur <zash@zash.se>
parents: 4552
diff changeset
38 end
48132b6e1b16 mod_pubsub_alertmanager: Publish Alertmanager alerts into pubsub
Kim Alvefur <zash@zash.se>
parents: 4552
diff changeset
39 for k, v in pairs(alert.labels) do
48132b6e1b16 mod_pubsub_alertmanager: Publish Alertmanager alerts into pubsub
Kim Alvefur <zash@zash.se>
parents: 4552
diff changeset
40 item:text_tag("label", v, { name=k });
48132b6e1b16 mod_pubsub_alertmanager: Publish Alertmanager alerts into pubsub
Kim Alvefur <zash@zash.se>
parents: 4552
diff changeset
41 end
48132b6e1b16 mod_pubsub_alertmanager: Publish Alertmanager alerts into pubsub
Kim Alvefur <zash@zash.se>
parents: 4552
diff changeset
42 item:tag("starts", { at = alert.startsAt}):up();
48132b6e1b16 mod_pubsub_alertmanager: Publish Alertmanager alerts into pubsub
Kim Alvefur <zash@zash.se>
parents: 4552
diff changeset
43 if alert.endsAt then
48132b6e1b16 mod_pubsub_alertmanager: Publish Alertmanager alerts into pubsub
Kim Alvefur <zash@zash.se>
parents: 4552
diff changeset
44 item:tag("ends", { at = alert.endsAt }):up();
48132b6e1b16 mod_pubsub_alertmanager: Publish Alertmanager alerts into pubsub
Kim Alvefur <zash@zash.se>
parents: 4552
diff changeset
45 end
48132b6e1b16 mod_pubsub_alertmanager: Publish Alertmanager alerts into pubsub
Kim Alvefur <zash@zash.se>
parents: 4552
diff changeset
46 if alert.generatorURL then
48132b6e1b16 mod_pubsub_alertmanager: Publish Alertmanager alerts into pubsub
Kim Alvefur <zash@zash.se>
parents: 4552
diff changeset
47 item:tag("link", { href=alert.generatorURL }):up();
48132b6e1b16 mod_pubsub_alertmanager: Publish Alertmanager alerts into pubsub
Kim Alvefur <zash@zash.se>
parents: 4552
diff changeset
48 end
3255
64d1dfbd1740 mod_pubsub_post: Ensure actor is non-nil (catch inability to determine IP or simliar)
Kim Alvefur <zash@zash.se>
parents: 3254
diff changeset
49
4618
48132b6e1b16 mod_pubsub_alertmanager: Publish Alertmanager alerts into pubsub
Kim Alvefur <zash@zash.se>
parents: 4552
diff changeset
50 local ret = publish_payload(path, request.ip, uuid_generate(), item);
48132b6e1b16 mod_pubsub_alertmanager: Publish Alertmanager alerts into pubsub
Kim Alvefur <zash@zash.se>
parents: 4552
diff changeset
51 if ret ~= 202 then
48132b6e1b16 mod_pubsub_alertmanager: Publish Alertmanager alerts into pubsub
Kim Alvefur <zash@zash.se>
parents: 4552
diff changeset
52 return ret
48132b6e1b16 mod_pubsub_alertmanager: Publish Alertmanager alerts into pubsub
Kim Alvefur <zash@zash.se>
parents: 4552
diff changeset
53 end
3014
72dbc9b66de8 mod_pubsub_post: Change to support arbitrary XML payloads
Kim Alvefur <zash@zash.se>
parents: 3013
diff changeset
54 end
4618
48132b6e1b16 mod_pubsub_alertmanager: Publish Alertmanager alerts into pubsub
Kim Alvefur <zash@zash.se>
parents: 4552
diff changeset
55 return 202;
1619
43c54a27bab2 mod_pubsub_post: Module to publish to pubsub nodes from a simple HTTP POST
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
56 end
43c54a27bab2 mod_pubsub_post: Module to publish to pubsub nodes from a simple HTTP POST
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
57
43c54a27bab2 mod_pubsub_post: Module to publish to pubsub nodes from a simple HTTP POST
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
58 module:provides("http", {
43c54a27bab2 mod_pubsub_post: Module to publish to pubsub nodes from a simple HTTP POST
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
59 route = {
43c54a27bab2 mod_pubsub_post: Module to publish to pubsub nodes from a simple HTTP POST
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
60 ["POST /*"] = handle_POST;
43c54a27bab2 mod_pubsub_post: Module to publish to pubsub nodes from a simple HTTP POST
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
61 };
43c54a27bab2 mod_pubsub_post: Module to publish to pubsub nodes from a simple HTTP POST
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
62 });