Comparison

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
parent 4552:mod_pubsub_post/mod_pubsub_post.lua@c87181a98f29
child 4619:b11001bd915d
comparison
equal deleted inserted replaced
4617:260a33eabbd3 4618:48132b6e1b16
1 local st = require "util.stanza";
2 local json = require "util.json";
3 local uuid_generate = require "util.uuid".generate;
4
5 module:depends("http");
6
7 local pubsub_service = module:depends("pubsub").service;
8
9 local error_mapping = {
10 ["forbidden"] = 403;
11 ["item-not-found"] = 404;
12 ["internal-server-error"] = 500;
13 ["conflict"] = 409;
14 };
15
16 local function publish_payload(node, actor, item_id, payload)
17 local post_item = st.stanza("item", { xmlns = "http://jabber.org/protocol/pubsub", id = item_id, })
18 :add_child(payload);
19 local ok, err = pubsub_service:publish(node, actor, item_id, post_item);
20 module:log("debug", ":publish(%q, true, %q, %s) -> %q", node, item_id, payload:top_tag(), err or "");
21 if not ok then
22 return error_mapping[err] or 500;
23 end
24 return 202;
25 end
26
27 function handle_POST(event, path)
28 local request = event.request;
29
30 local payload = json.decode(event.request.body);
31 if type(payload) ~= "table" then return 400; end
32 if payload.version ~= "4" then return 501; end
33
34 for _, alert in ipairs(payload.alerts) do
35 local item = st.stanza("alerts", {xmlns = "urn:uuid:e3bec775-c607-4e9b-9a3f-94de1316d861:v4", status=alert.status});
36 for k, v in pairs(alert.annotations) do
37 item:text_tag("annotation", v, { name=k });
38 end
39 for k, v in pairs(alert.labels) do
40 item:text_tag("label", v, { name=k });
41 end
42 item:tag("starts", { at = alert.startsAt}):up();
43 if alert.endsAt then
44 item:tag("ends", { at = alert.endsAt }):up();
45 end
46 if alert.generatorURL then
47 item:tag("link", { href=alert.generatorURL }):up();
48 end
49
50 local ret = publish_payload(path, request.ip, uuid_generate(), item);
51 if ret ~= 202 then
52 return ret
53 end
54 end
55 return 202;
56 end
57
58 module:provides("http", {
59 route = {
60 ["POST /*"] = handle_POST;
61 };
62 });