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
相关推荐
Yana.nice6 小时前
Linux 只保留 30 天内日志(find命令删除日志文件)
linux·运维·chrome
DFT计算杂谈9 小时前
无 Root 权限在 Tesla K80 零门槛部署 DeepSeek 大模型
linux·服务器·网络·数据库·机器学习
Zhang~Ling10 小时前
从 fopen 到 struct file:从零开始拆解 Linux 文件 I/O
linux·运维·服务器
DeeplyMind10 小时前
Linux 深入 per-VMA lock:Linux 缺页路径如何摆脱 mmap_lock
linux·per-vma lock
爱写代码的森11 小时前
蒙三方库 | harmony-utils之FileUtil文件重命名与属性查询详解
linux·运维·服务器·华为·harmonyos·鸿蒙·huawei
XMAIPC_Robot12 小时前
软硬协同实时控制|RK3588业务调度+FPGA硬件时序,ethercat实现半导体设备微秒级响应(125us)
linux·arm开发·人工智能·fpga开发
重生的黑客12 小时前
Linux 进程优先级、切换与调度:从孤儿进程到 O(1) 调度模型
linux·运维·服务器·进程优先级·nice
骑上单车去旅行13 小时前
MD5校验对比脚本
linux·服务器·windows
平生幻14 小时前
Linux 常用命令
linux
ShirleyWang01215 小时前
让headlamp控制台能访问
linux·服务器·python·k8s·k3s