uptime kuma 飞书告警

原生的飞书告警只支持 text 类型,不支持 interactive 卡片,因此无法使用飞书告警。

要使用告警需要修改feishu.js的源码。步骤如下(我这里使用的是源码安装,docker安装的自行研究一下)。

一、下载源码

复制代码
git clone https://github.com/louislam/uptime-kuma.git

二、进入目录

复制代码
cd ./uptime-kuma/server/notification-providers

三、修改feishu.js

javascript 复制代码
const NotificationProvider = require("./notification-provider");
const axios = require("axios");
const { DOWN, UP } = require("../../src/util");

class Feishu extends NotificationProvider {
    name = "Feishu";

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

        try {
            let config = this.getAxiosConfigWithProxy({});
            if (heartbeatJSON == null) {
                let testdata = {
                    msg_type: "text",
                    content: {
                        text: msg,
                    },
                };
                await axios.post(notification.feishuWebHookUrl, testdata, config);
                return okMsg;
            }

            if (heartbeatJSON["status"] === DOWN) {
                let downdata = {
                    msg_type: "interactive",
                    card: {
                        config: {
                            update_multi: false,
                            wide_screen_mode: true,
                        },
                        header: {
                            title: {
                                tag: "plain_text",
                                content: "UptimeKuma Alert: [Down] " + monitorJSON["name"],
                            },
                            template: "red",
                        },
                        elements: [
                            {
                                tag: "div",
                                text: {
                                    tag: "lark_md",
                                    content: getContent(heartbeatJSON),
                                },
                            },
                        ],
                    },
                };
                await axios.post(notification.feishuWebHookUrl, downdata, config);
                return okMsg;
            }

            if (heartbeatJSON["status"] === UP) {
                let updata = {
                    msg_type: "interactive",
                    card: {
                        config: {
                            update_multi: false,
                            wide_screen_mode: true,
                        },
                        header: {
                            title: {
                                tag: "plain_text",
                                content: "UptimeKuma Alert: [UP] " + monitorJSON["name"],
                            },
                            template: "green",
                        },
                        elements: [
                            {
                                tag: "div",
                                text: {
                                    tag: "lark_md",
                                    content: getContent(heartbeatJSON),
                                },
                            },
                        ],
                    },
                };
                await axios.post(notification.feishuWebHookUrl, updata, config);
                return okMsg;
            }
        } catch (error) {
            this.throwGeneralAxiosError(error);
        }
    }
}

/**
 * Get content
 * @param {?object} heartbeatJSON Heartbeat details (For Up/Down only)
 * @returns {string} Return Successful Message
 */
function getContent(heartbeatJSON) {
    return [
        "**Message**: " + heartbeatJSON["msg"],
        "**Ping**: " + (heartbeatJSON["ping"] == null ? "N/A" : heartbeatJSON["ping"] + " ms"),
        `**Time (${heartbeatJSON["timezone"]})**: ${heartbeatJSON["localDateTime"]}`,
    ].join("\n");
}

module.exports = Feishu;

修改内容或者直接替换feishu.js

javascript 复制代码
const NotificationProvider = require("./notification-provider");
const axios = require("axios");
const { DOWN, UP } = require("../../src/util");

class Feishu extends NotificationProvider {
    name = "Feishu";

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

        try {
            let config = this.getAxiosConfigWithProxy({});

            // 测试消息
            if (heartbeatJSON == null) {
                let testdata = {
                    msg_type: "text",
                    content: {
                        text: msg,
                    },
                };
                await axios.post(notification.feishuWebHookUrl, testdata, config);
                return okMsg;
            }

            // 告警内容(统一用 text 格式)
            let statusText = heartbeatJSON.status === DOWN ? "【异常】" : "【恢复】";
            let title = `UptimeKuma 告警 ${statusText}:${monitorJSON.name}`;
            let msgContent = [
                title,
                "消息:" + heartbeatJSON.msg,
                "Ping:" + (heartbeatJSON.ping ?? "N/A") + "ms",
                "时间:" + heartbeatJSON.localDateTime + " (" + heartbeatJSON.timezone + ")"
            ].join("\n");

            let data = {
                msg_type: "text",
                content: {
                    text: msgContent
                }
            };

            await axios.post(notification.feishuWebHookUrl, data, config);
            return okMsg;

        } catch (error) {
            this.throwGeneralAxiosError(error);
        }
    }
}

module.exports = Feishu;

四、开始构建和安装

1.安装pm2

javascript 复制代码
npm install pm2

2.启动项目(要在你下载的目录底下)

javascript 复制代码
cd uptime-kuma
npm run setup
pm2 start server/server.js --name uptime-kuma

3.访问http:IP:3001(需要配置数据库和默认管理员用户)

4.之后进行测试一下飞书告警正常提示如下

###写在最后:

飞书的告警最好加密,这里建议使用IP白名单,因为用签名uptime kuma没有选项填入。

另外3001是默认端口如果需要修改为其他的端口可以如下命令

javascript 复制代码
pm2 stop uptime-kuma

PORT=3005 pm2 start server/server.js --name uptime-kuma

pm2 save

如果是第一次执行

javascript 复制代码
PORT=3005 pm2 start server/server.js --name uptime-kuma

pm2 save

另外可以查看uptime kuma状态

javascript 复制代码
pm2 status
相关推荐
orion571 天前
Missing Semester Class1:course overview and introduction of shell
linux
用户120487221612 天前
Linux驱动编译与加载
linux·嵌入式
用户805533698032 天前
Input 子系统架构:Core、Handler、Driver 三层是怎么协作的
linux·嵌入式
用户805533698032 天前
RK-Forge外设系列开篇 - 把板子从「能启动」变成「能用」:Ethernet/SPI/MMC 三个纯接线外设
linux·github·嵌入式
七歌杜金房2 天前
我终于又有了自己的 Linux 电脑
linux·debian·mac
tntxia3 天前
linux curl命令详解_curl详解
linux
扛枪的书生4 天前
Linux 网络管理器用法速查
linux
顺风尿一寸4 天前
Java Socket 内核之旅:从 SocketChannel.read() 到 tcp_recvmsg 与 epoll 的完整调用链路
linux
XIAOHEZIcode4 天前
Ubuntu 终端美化全栈指南:Bash 到 Kitty 踩坑实录
linux·ubuntu·命令行
唐青枫4 天前
别再只会用 cron:Linux systemd Timer 定时任务实战详解
linux