#!/opt/lnxall_app/bin/lua local invoker = require "invoker" local module = {} local MIN_HEALTH_PERCENT = 10 local TOP_FLAGS = invoker.OUTPUT + invoker.NOSTDIO + invoker.BUFSIZ + 0x1F0000 function module.checkplat() return true end function module.category() return "系统资源" end function module.description() return "检查系统资源" end function module.detailed() return "读取内存信息和 CPU 空闲率, 用于判断系统是否存在内存不足或 CPU 负载过高的问题." end function module.suggestion() return table.concat({ "1. 使用 free -h 查看内存总量和可用内存.", "2. 使用 top 查看 CPU 空闲率和占用较高的进程.", "3. 使用 ps aux --sort=-%mem | head 查看内存占用较高的进程.", }, "\n") end function module.step() local minfo = invoker.readfile("/proc/meminfo", nil, 1024) if not minfo then return { error = "failed to read /proc/meminfo" }, true end local mtotal = string.match(minfo, "MemTotal:%s+(%d+)") if mtotal then mtotal = tonumber(mtotal) end local mavail = string.match(minfo, "MemAvailable:%s+(%d+)") if mavail then mavail = tonumber(mavail) end if type(mtotal) ~= "number" or type(mavail) ~= "number" then io.stderr:write("Error, failed to parse /proc/meminfo!\n") io.stderr:flush() return { error = "failed to parse /proc/meminfo" }, true end local avail = math.floor(mavail * 100 / mtotal) local _, output = invoker.invoke(TOP_FLAGS, "busybox", "top", "-d2", "-n2") if type(output) ~= "string" then output = "" end local cpu_idle for cpui in string.gmatch(output, "([%d%.]+)%%%s+idle") do local idle = tonumber(cpui) if idle and idle >= 0 then cpu_idle = idle end end local reply = { ["内存总量 "] = mtotal, ["可用内存 "] = mavail, ["可用内存百分比"] = avail, ["CPU 空闲率 "] = cpu_idle, } local has_error = avail <= MIN_HEALTH_PERCENT if cpu_idle and cpu_idle <= MIN_HEALTH_PERCENT then has_error = true end return reply, has_error end return module