|
| 1 | +local http = require("http") |
| 2 | +local inspect = require("inspect") |
| 3 | +local json = require("json") |
| 4 | +local time = require("time") |
| 5 | +local cmd = require("cmd") |
| 6 | +local strings = require("strings") |
| 7 | +local filepath = require("filepath") |
| 8 | + |
| 9 | +package.path = filepath.dir(debug.getinfo(1).source) .. './?.lua;' .. package.path |
| 10 | +functions = require("functions") |
| 11 | + |
| 12 | +log_level = os.getenv("GLUA_SCRIPT_LOG_LEVEL") or 'info' |
| 13 | +local server, err = http.server("127.0.0.1:3001") |
| 14 | +if err then error(err) end |
| 15 | + |
| 16 | +-- main block program |
| 17 | +while true do |
| 18 | + local req, resp = server:accept() -- lock and wait request |
| 19 | + |
| 20 | + local raw_body, err = req.body() |
| 21 | + if err then error(err) end |
| 22 | + resp:code(200) |
| 23 | + resp:done() |
| 24 | + |
| 25 | + if req.method == 'POST' then |
| 26 | + |
| 27 | + body, err = json.decode(urldecode(raw_body:gsub("payload=", ""))) |
| 28 | + if err then error(err) end |
| 29 | + |
| 30 | + actions = body['actions'] |
| 31 | + response_url = body['response_url'] |
| 32 | + user = body['user']['name'] |
| 33 | + |
| 34 | + original_message = body['original_message'] |
| 35 | + original_attach = original_message['attachments'] |
| 36 | + original_fields = original_attach[1]['fields'] |
| 37 | + |
| 38 | + if log_level == 'debug' then |
| 39 | + print("[DEBUG] RAW BODY: \n"..inspect(body)) |
| 40 | + print("[DEBUG] ACTIONS: \n"..inspect(actions)) |
| 41 | + end |
| 42 | + |
| 43 | + for _, v in pairs(actions) do --parce actions |
| 44 | + |
| 45 | + if v['value'] ~= "" then |
| 46 | + j, err = json.decode(v['value']) |
| 47 | + duration = j['duration'] |
| 48 | + alertmanager_url = j['url'] |
| 49 | + if err then error(err) end |
| 50 | + |
| 51 | + if j['labels'] then |
| 52 | + matchers = {} |
| 53 | + for n, v in pairs(j['labels']) do --making data for alertmanager |
| 54 | + for k, v in pairs(v) do |
| 55 | + matcher = {isRegex = false, name = k, value = v} |
| 56 | + table.insert(matchers, n, matcher) |
| 57 | + end |
| 58 | + end |
| 59 | + |
| 60 | + else |
| 61 | + print("[INFO] No labels in value container") |
| 62 | + end |
| 63 | + |
| 64 | + end_time = format_time_with_offset(duration) |
| 65 | + start_time = format_time_with_offset(0) |
| 66 | + silence = {comment = "Silenced by Slack!", createdBy = user, startsAt = start_time, endsAt = end_time, matchers = matchers} |
| 67 | + data, err = json.encode(silence) |
| 68 | + if err then error(err) end |
| 69 | + |
| 70 | + client = http.client() |
| 71 | + url = alertmanager_url.."/api/v2/silences" -- Bot uses api v2 |
| 72 | + request, err = http.request("POST", url, data) |
| 73 | + request:header_set("Content-Type", "application/json") |
| 74 | + |
| 75 | + if err then error(err) end |
| 76 | + |
| 77 | + local result, err = client:do_request(request) |
| 78 | + if err then |
| 79 | + error(err) |
| 80 | + elseif result.code ~= 200 then |
| 81 | + error(result.body) |
| 82 | + else |
| 83 | + -- We need to create new message for slack with changed field |
| 84 | + |
| 85 | + silenced_text = ":thumbsup: successfully silenced by "..user.." for "..duration.." hours (ends "..end_time..")" |
| 86 | + for _, v in pairs(original_fields) do |
| 87 | + if v['title'] == 'Status' then |
| 88 | + v['value'] = silenced_text |
| 89 | + end |
| 90 | + end |
| 91 | + |
| 92 | + new_actions = {} |
| 93 | + |
| 94 | + for i, v in pairs(original_attach[1]['actions']) do |
| 95 | + if v['name'] and v['name'] == 'silence' then |
| 96 | + print('[INFO] Skiping action SILENCE because we have a new one') |
| 97 | + else |
| 98 | + table.insert(new_actions, v) |
| 99 | + end |
| 100 | + end |
| 101 | + new_attach = original_attach |
| 102 | + new_attach[1]['actions'] = new_actions |
| 103 | + |
| 104 | + new_message = {} |
| 105 | + new_message['text'] = "" |
| 106 | + new_message['attachments'] = new_attach |
| 107 | + new_message['replace_original'] = true |
| 108 | + new_message['response_type'] = "in_channel" |
| 109 | + |
| 110 | + if log_level == 'debug' then |
| 111 | + print("[DEBUG] NEW CREATED MESSAGE:\n"..inspect(new_message)) |
| 112 | + end |
| 113 | + |
| 114 | + message, err = json.encode(new_message) |
| 115 | + if err then error(err) end |
| 116 | + |
| 117 | + request, err = http.request("POST", response_url, message) |
| 118 | + local result, err = client:do_request(request) |
| 119 | + |
| 120 | + if err then error(err) end |
| 121 | + print("[DEBUG] Slack responce: \n"..result.body) |
| 122 | + end |
| 123 | + else |
| 124 | + print("[INFO] Skipping action because we do not value in action") |
| 125 | + end |
| 126 | + end |
| 127 | + end |
| 128 | +end |
0 commit comments