lua get cpu and mem

本文介绍了一个使用Lua编写的脚本,该脚本能够通过执行特定命令获取进程的CPU和内存使用情况,并将相关信息记录到指定文件中。脚本通过解析命令输出来筛选包含配置文件路径的行,进而提取所需数据。
function split_str(str,delim)
    t = {}
    for v in string.gmatch(str, delim.."(%w+)") do
        table.insert(t,v)
    end
    return t
end

-- local CMD =  "ps -u|grep battle/config.battle "
-- local CMDEND = "servers/battle/config.battle"
-- local output_file_path = "./servers/client/cpumem_battle.txt"
function cpumeminfo(CMD,CMDEND,output_file_path)
    local input_file_path = "./servers/battle/cpumem_battle.txt"
    local file = io.open(input_file_path,"w")
    assert(file)
    file:write("")
    file:close()

    file = io.open(input_file_path,"r")
    assert(file)
    os.execute(CMD.." >> "..input_file_path)
    for cpumem_row in file:lines() do
-- local cpumem_row = "zhuk      225101450 124.8  1.2 826470240 1040080 pts/4 Sl+  12:35  70:51 ./servers/battle/config.battle"
        if string.find(cpumem_row,CMDEND) then
            local cpumem_row_table = split_str(" "..cpumem_row," ")
            local output_file = io.open(output_file_path,"a")
            assert(output_file)
            local now_time = 3600*os.date("%H")+60*os.date("%M")+os.date("%S")
            output_file:write(now_time.." "..cpumem_row_table[3].." "..cpumem_row_table[6].."\n")
            output_file:close()
        end
    end
    file:close()
end
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值