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
相关推荐
猪脚踏浪2 小时前
docker 删除镜像
linux
用户805533698032 小时前
嵌入式Linux驱动开发——Pinctrl 子系统架构深度解析
linux·嵌入式
人生苦短1282 小时前
CentOS 7.9 部署 PostgreSQL 15.17 + PostGIS 3.4.8 操作文档
linux·postgresql·centos
一个心烑2 小时前
【layui页面编辑下拉框处理的三种方式】
linux·python·layui
z200509302 小时前
【linux学习】linux工具篇(下)
linux·学习
vortex52 小时前
virsh 使用指南:KVM 虚拟化管理的命令行艺术
linux·运维·服务器
行走的大喇叭2 小时前
Linux kernel目录、配置文件介绍
linux·单片机·嵌入式硬件
学困昇2 小时前
Linux 动静态库制作与原理:从 .a、.so 到 ELF 加载一次讲透
linux·运维·服务器·c语言·开发语言·c++·人工智能
ALINX技术博客2 小时前
【黑金云课堂】FPGA技术教程Linux开发:电压温度检测/USB/eMMC
linux·fpga开发