summaryrefslogtreecommitdiffstats
path: root/server/notification-providers/opsgenie.js
blob: 59a7970553704a27c5c4c5575fbb97bfd2ae1e93 (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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
const NotificationProvider = require("./notification-provider");
const axios = require("axios");
const { UP, DOWN } = require("../../src/util");

const opsgenieAlertsUrlEU = "https://api.eu.opsgenie.com/v2/alerts";
const opsgenieAlertsUrlUS = "https://api.opsgenie.com/v2/alerts";
const okMsg = "Sent Successfully.";

class Opsgenie extends NotificationProvider {
    name = "Opsgenie";

    /**
     * @inheritdoc
     */
    async send(notification, msg, monitorJSON = null, heartbeatJSON = null) {
        let opsgenieAlertsUrl;
        let priority = (!notification.opsgeniePriority) ? 3 : notification.opsgeniePriority;
        const textMsg = "Uptime Kuma Alert";

        try {
            switch (notification.opsgenieRegion) {
                case "us":
                    opsgenieAlertsUrl = opsgenieAlertsUrlUS;
                    break;
                case "eu":
                    opsgenieAlertsUrl = opsgenieAlertsUrlEU;
                    break;
                default:
                    opsgenieAlertsUrl = opsgenieAlertsUrlUS;
            }

            if (heartbeatJSON == null) {
                let notificationTestAlias = "uptime-kuma-notification-test";
                let data = {
                    "message": msg,
                    "alias": notificationTestAlias,
                    "source": "Uptime Kuma",
                    "priority": "P5"
                };

                return this.post(notification, opsgenieAlertsUrl, data);
            }

            if (heartbeatJSON.status === DOWN) {
                let data = {
                    "message": monitorJSON ? textMsg + `: ${monitorJSON.name}` : textMsg,
                    "alias": monitorJSON.name,
                    "description": msg,
                    "source": "Uptime Kuma",
                    "priority": `P${priority}`
                };

                return this.post(notification, opsgenieAlertsUrl, data);
            }

            if (heartbeatJSON.status === UP) {
                let opsgenieAlertsCloseUrl = `${opsgenieAlertsUrl}/${encodeURIComponent(monitorJSON.name)}/close?identifierType=alias`;
                let data = {
                    "source": "Uptime Kuma",
                };

                return this.post(notification, opsgenieAlertsCloseUrl, data);
            }
        } catch (error) {
            this.throwGeneralAxiosError(error);
        }
    }

    /**
     * Make POST request to Opsgenie
     * @param {BeanModel} notification Notification to send
     * @param {string} url Request url
     * @param {object} data Request body
     * @returns {Promise<string>} Success message
     */
    async post(notification, url, data) {
        let config = {
            headers: {
                "Content-Type": "application/json",
                "Authorization": `GenieKey ${notification.opsgenieApiKey}`,
            }
        };

        let res = await axios.post(url, data, config);
        if (res.status == null) {
            return "Opsgenie notification failed with invalid response!";
        }
        if (res.status < 200 || res.status >= 300) {
            return `Opsgenie notification failed with status code ${res.status}`;
        }

        return okMsg;
    }
}

module.exports = Opsgenie;