飞书设置服务器异常消息

Lua 复制代码
-- 配置区域
local FEISHU_WEBHOOK = "https://open.feishu.cn/open-apis/bot/v2/hook/你的TOKEN"
local CPU_THRESHOLD = 90    -- CPU 报警阈值 80%
local MEM_THRESHOLD = 90    -- 内存报警阈值 85%
local DISK_PATH = "/"       -- 监控的磁盘分区
-- 获取 CPU 使用率 (读取 /proc/stat)
local function get_cpu_usage()
    local f = io.open("/proc/stat", "r")
    local line = f:read("*l")
    f:close()
    -- 解析:user nice system idle ...
    local user, nice, system, idle = line:match("cpu%s+(%d+)%s+(%d+)%s+(%d+)%s+(%d+)")
    local total = user + nice + system + idle
    return total, idle
end
local last_total, last_idle = 0, 0
-- 获取内存和硬盘使用率
local function get_sys_info()
    -- 内存 (单位 MB)
    local f_mem = io.popen("free -m | grep Mem")
    local mem_str = f_mem:read("*a")
    f_mem:close()
    local total_m, used_m = mem_str:match("Mem:%s+(%d+)%s+(%d+)")
    local mem_per = (used_m / total_m) * 100
    -- 硬盘 (单位 %)
    local f_disk = io.popen("df -h " .. DISK_PATH .. " | tail -1")
    local disk_str = f_disk:read("*a")
    f_disk:close()
    local disk_per = disk_str:match("(%d+)%%")
    return math.floor(mem_per), tonumber(disk_per)
end
-- 发送飞书消息
local function send_to_feishu(msg)
    local cfg_setting = cfg_query.data_setting
    local server_name = cfg_setting.config.server_name
    local body = {
        msg_type = "text",
        content = {
            text = "【服务器状态报警】\n" .. string.format("⚠️ %s \n", server_name) .. msg
        }
    }
    -- Skynet httpc 调用
	body = json.encode(body)
	local host, path = util.get_url_host(FEISHU_WEBHOOK)
	local ok, status, res = pcall(httpc.post_json, host, path, body)
    if not ok then
        skynet.error("飞书发送失败:", status)
    end
end

local function monitor()
    skynet.error("系统监控服务已启动...")
    skynet.fork(function()
        while true do
            -- 1. 计算 CPU 使用率 (需要两次采样计算差值)
            local t1, i1 = get_cpu_usage()
            skynet.sleep(100) -- 间隔 1 秒
            local t2, i2 = get_cpu_usage()
            local cpu_per = math.floor(100 * (1 - (i2 - i1) / (t2 - t1)))
            -- 2. 获取内存和硬盘
            local mem_per, disk_per = get_sys_info()
            -- 3. 判断是否报警
            local alert_msg = ""
            if cpu_per > CPU_THRESHOLD then
                alert_msg = alert_msg .. string.format("⚠️ CPU 占用过高: %d%%\n", cpu_per)
            end
            if mem_per > MEM_THRESHOLD then
                alert_msg = alert_msg .. string.format("⚠️ 内存占用过高: %d%%\n", mem_per)
            end
            if disk_per > MEM_THRESHOLD then
                alert_msg = alert_msg .. string.format("⚠️ 硬盘使用过高: %d%%\n", disk_per)
            end
            if alert_msg ~= "" then
                send_to_feishu(alert_msg)
                skynet.sleep(6000*5) -- 报警后冷却 5 分钟,避免刷屏
            end
            skynet.sleep(500) -- 每 5 秒检测一次
        end
    end)
end

飞书机器人准备

在飞书群聊中:设置 -> 群机器人 -> 添加机器人 -> 自定义机器人。

复制产生的 Webhook URL。

安全设置建议勾选"自定义关键词",例如设置关键词为 报警。

相关推荐
NiceCloud喜云3 天前
Claude Code 跑 HyperFrames 实测:本地生成 AI 视频素材全流程
java·运维·人工智能·自动化·json·音视频·飞书
逆向命运3 天前
PC企微搜索手机号窗口绕过
c语言·汇编·c++·飞书·企业微信
NiceCloud喜云3 天前
Claude Code Routines 实战:三种触发器跑通云端自动化编码
android·运维·数据库·人工智能·自动化·json·飞书
不正经绣才3 天前
【扣子Coze教程】1分钟提取500+小红书笔记评论,自动同步飞书(附源码)
飞书·教程·工作流·coze·扣子
在繁华处4 天前
Hermes Agent 完全使用指南:从安装到多平台部署的全流程教程
python·开源·飞书
XLYcmy5 天前
Agent身份与权限系统设计方案
windows·网络安全·ai·llm·飞书·api·agent
项目工具测评实验室5 天前
复杂项目管理工具选型:飞书项目、PingCode、ONES 深度对比与真实场景分析
数据库·飞书·pingcode
2601_955781986 天前
飞书远程控机:OpenClaw配置全攻略
人工智能·开源·github·飞书·open claw安装·open claw部署
weipt6 天前
OpenClaw 配置飞书机器人完整指南:从零到每天定时推送天气
机器人·飞书
乐观的Terry8 天前
用 cc-connect 将 Claude Code 接入飞书机器人
机器人·飞书