summaryrefslogtreecommitdiffstats
path: root/server/notification-providers/alerta.js
blob: f9a273b49f5e977cfd21dada1adb38655642e27d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
const NotificationProvider = require("./notification-provider");
const { DOWN, UP } = require("../../src/util");
const axios = require("axios");

class Alerta extends NotificationProvider {
    name = "alerta";

    /**
     * @inheritdoc
     */
    async send(notification, msg, monitorJSON = null, heartbeatJSON = null) {
        const okMsg = "Sent Successfully.";

        try {
            let config = {
                headers: {
                    "Content-Type": "application/json;charset=UTF-8",
                    "Authorization": "Key " + notification.alertaApiKey,
                }
            };
            let data = {
                environment: notification.alertaEnvironment,
                severity: "critical",
                correlate: [],
                service: [ "UptimeKuma" ],
                value: "Timeout",
                tags: [ "uptimekuma" ],
                attributes: {},
                origin: "uptimekuma",
                type: "exceptionAlert",
            };

            if (heartbeatJSON == null) {
                let postData = Object.assign({
                    event: "msg",
                    text: msg,
                    group: "uptimekuma-msg",
                    resource: "Message",
                }, data);

                await axios.post(notification.alertaApiEndpoint, postData, config);
            } else {
                let datadup = Object.assign( {
                    correlate: [ "service_up", "service_down" ],
                    event: monitorJSON["type"],
                    group: "uptimekuma-" + monitorJSON["type"],
                    resource: monitorJSON["name"],
                }, data );

                if (heartbeatJSON["status"] === DOWN) {
                    datadup.severity = notification.alertaAlertState; // critical
                    datadup.text = "Service " + monitorJSON["type"] + " is down.";
                    await axios.post(notification.alertaApiEndpoint, datadup, config);
                } else if (heartbeatJSON["status"] === UP) {
                    datadup.severity = notification.alertaRecoverState; // cleaned
                    datadup.text = "Service " + monitorJSON["type"] + " is up.";
                    await axios.post(notification.alertaApiEndpoint, datadup, config);
                }
            }
            return okMsg;
        } catch (error) {
            this.throwGeneralAxiosError(error);
        }

    }
}

module.exports = Alerta;