#!/usr/bin/lua local invoker = require 'invoker' local g_prefix = "/root/maps-info" local g_apps = { "dy_rs485", "dy_modbus_tcp", "dy_protocol_parser", } local function os_date() local tim = os.time() local od = os.date("*t", time) if type(od) ~= "table" then return tostring(tim) end return string.format("%d%02d%02d-%02d%02d%02d", od.year or 0, od.month or 0, od.day or 0, od.hour or 0, od.min or 0, od.sec or 0) end local function dump_pid_maps(pid, appn) local mapfile = string.format("/proc/%s/maps", pid) local maps = invoker.readfile(mapfile, nil, 16384) if type(maps) ~= "string" or string.len(maps) == 0 then return false end local film = string.format("%s/%s/pid%s-%s.maps", g_prefix, appn, pid, os_date()) local filp = io.open(film, "wb") if filp then filp:write(maps) filp:close(); filp = nil return true end return false end local function monitor_init() for _, appn in ipairs(g_apps) do local dirp = string.format("%s/%s", g_prefix, appn) io.stdout:write("creating directory " .. dirp .. "...\n") io.stdout:flush() invoker.invoke(invoker.NOSTDIO, "mkdir", "-p", dirp) end return true end local function collect_maps() for _, appn in ipairs(g_apps) do local eval, pids = invoker.invoke(invoker.OUTPUT, "pidof", appn) if eval ~= 0 or type(pids) ~= "string" then io.stderr:write(string.format("Error, cannot find PID for %s\n", appn)) io.stderr:flush() else for lpid in string.gmatch(pids, "%d+") do dump_pid_maps(lpid, appn) end end end end monitor_init() collect_maps() os.exit(0)