From f6844cdafc021f8c43fb5d8ec164a51bd7cb347c Mon Sep 17 00:00:00 2001 From: iipeace Date: Sun, 24 Nov 2024 22:17:09 +0900 Subject: [PATCH] ps: Add print format in JSON Signed-off-by: iipeace --- guider/guider.py | 261 ++++++++++++++++++++++++++++------------------- 1 file changed, 157 insertions(+), 104 deletions(-) diff --git a/guider/guider.py b/guider/guider.py index 437c632c..e4cf189a 100755 --- a/guider/guider.py +++ b/guider/guider.py @@ -7,7 +7,7 @@ __credits__ = "Peace Lee" __license__ = "GPLv2" __version__ = "3.9.8" -__revision__ = "241123" +__revision__ = "241124" __maintainer__ = "Peace Lee" __email__ = "iipeace5@gmail.com" __repository__ = "https://github.com/iipeace/guider" @@ -28484,8 +28484,10 @@ class SysMgr(object): - LOGCAT/LOGMON{|Option} - MEMPROF{|Option} - MEMTOP/MEMMON|Option + - PS/PSLIST - REMOVE|RemotePath - RESTOP/RESMON{|Option} + - SYSINFO/SYSLIST - WATCH [ COMMON ] @@ -50708,10 +50710,14 @@ def _printExecutedCmd(): "memprof", "memtop", "pkglist", + "ps", + "pslist", "remove", "resmon", "restop", "sysdump", + "sysinfo", + "syslist", "tkill", ) or isDrawMode @@ -50875,6 +50881,22 @@ def _getPacket(fd): SysMgr.addEnvironVar("GETPKGLIST") SysMgr.doHeapProfRec() + elif mainCmd in ("sysinfo", "syslist"): + # start sending stats in JSON # + SysMgr.jsonEnable = mainCmd == "syslist" + SysMgr.printFd = connObj + + # print system info # + SysMgr.doPrintInfo() + + elif mainCmd in ("ps", "pslist"): + # start sending stats in JSON # + SysMgr.jsonEnable = mainCmd == "pslist" + SysMgr.printFd = connObj + + # print process list # + SysMgr.doPs() + elif mainCmd in ("memtop", "memmon"): if not cmdOpt: _printErr("no target info") @@ -57019,47 +57041,7 @@ def checkCmdMode(): # KILL MODE # elif SysMgr.isKillMode(): - # make list of arguments # - if len(sys.argv) > 2: - argList = sys.argv[2:] - else: - argList = [" "] - - # print signal list # - if SysMgr.findOption("l"): - SysMgr.printList(ConfigMgr.SIG_LIST, ["0"]) - sys.exit(0) - - # remove additional options # - if len(argList) > 1: - idx = 0 - for idx, item in enumerate(argList[1:]): - if item.strip().startswith("-"): - break - - if argList[-1].strip().startswith("-"): - argList = argList[: idx + 1] - else: - argList = argList[: idx + 2] - - # convert task ID # - argList = SysMgr.convertTaskIdInput(argList) - - while 1: - # set target type # - if SysMgr.checkMode("tkill"): - isThread = True - else: - isThread = False - - # send signal # - SysMgr.sendSignalArgs(list(argList), isThread=isThread) - - # check interval # - if SysMgr.intervalEnable: - time.sleep(SysMgr.intervalEnable) - else: - break + SysMgr.doKill() # TOPDIFF MODE # elif SysMgr.checkMode("topdiff"): @@ -57130,56 +57112,7 @@ def checkCmdMode(): # READELF MODE # elif SysMgr.checkMode("readelf"): - # get path # - if SysMgr.hasMainArg(): - path = SysMgr.getMainArg() - elif SysMgr.inputParam: - path = SysMgr.inputParam - else: - SysMgr.printErr("no input value for path") - sys.exit(-1) - - SysMgr.printLogo(big=True, onlyFile=True) - - # set debug flag # - debug = not SysMgr.jsonEnable - onlyHdr = "ONLYHEADER" in SysMgr.environList - - def _printJson(path, obj): - if not SysMgr.jsonEnable: - return - jsonStr = UtilMgr.convDict2Str( - obj.attr, pretty=not SysMgr.streamEnable - ) - SysMgr.printPipe(('"%s": ' % path) + jsonStr) - - # run ELF analyzer # - try: - # print vdso # - if path == "vdso": - obj = SysMgr.getVDSO(debug=debug) - _printJson(path, obj) - else: - # get path list # - pathList = path.split(",") - pathList = UtilMgr.getFileList(pathList) - if not pathList: - SysMgr.printErr("invalid path '%s'" % path) - sys.exit(-1) - - # convert to hard-linked path set # - pathList = set(map(os.path.realpath, pathList)) - - # print items # - for p in pathList: - obj = ElfAnalyzer( - p, debug, onlyHdr=onlyHdr, incArg=True - ) - _printJson(p, obj) - except SystemExit: - sys.exit(0) - except: - SysMgr.printErr("failed to analyze '%s'" % path, True) + SysMgr.doReadelf() # LEAKTRACE MODE # elif SysMgr.checkMode("leaktrace"): @@ -77426,10 +77359,10 @@ def doPs(): ppidType = "PID" if not SysMgr.jsonEnable: - SysMgr.printPipe( + SysMgr.addPrint( ( "{10:1}\n{0:>{cl}} {1:>{pd}} {2:>{pd}} {3:1} {4:>4} {5:>4} " - "{6:>4} {7:>8} {8:>9} {9:1}\n{10:1}" + "{6:>4} {7:>8} {8:>9} {9:1}\n{10:1}\n" ).format( mode, pidType, @@ -77444,12 +77377,15 @@ def doPs(): twoLine, cl=cl, pd=pd, - ) + ), + newline=3, + force=True, ) filterGroup = SysMgr.filterGroup cmdFilter = SysMgr.environList.get("CMDLINEFILTER") + jsonData = {} nrPrint = 0 for idx, value in sortedProcData: # save status info # @@ -77487,11 +77423,11 @@ def doPs(): obj.saveUserData(idx) user = value["user"] - # lifetime # + # life # value["runtime"] = long( SysMgr.uptime - (float(value["stat"][obj.starttimeIdx]) / 100) ) - lifeTime = UtilMgr.convTime(value["runtime"]) + life= UtilMgr.convTime(value["runtime"]) # parent ID # if SysMgr.processEnable: @@ -77503,10 +77439,23 @@ def doPs(): schedPolicy, schedValue = obj.getSchedInfo(value["stat"]) sched = schedPolicy + schedValue.strip() - SysMgr.printPipe( + if SysMgr.jsonEnable: + jsonData[idx] = { + "comm": comm, + "status": stat, + "nrTask": nrTask, + "sched": sched, + "rss": rss, + "user": user, + "life": life, + "cmdline": cmdline, + } + continue + + SysMgr.addPrint( ( "{0:>{cl}} {1:>{pd}} {2:>{pd}} {3:1} {4:>4} {5:>4} " - "{6:>4} {7:>8} {8:>9} {9:1}" + "{6:>4} {7:>8} {8:>9} {9:1}\n" ).format( comm, idx, @@ -77516,20 +77465,124 @@ def doPs(): sched, rss, user[:8], - lifeTime[:9], + life[:9], cmdline, cl=cl, pd=pd, - ) + ), + force=True, ) nrPrint += 1 if SysMgr.jsonEnable: - pass + SysMgr.printPipe( + UtilMgr.convDict2Str(jsonData, pretty=not SysMgr.streamEnable) + ) else: if nrPrint == 0: - SysMgr.printPipe("\tNone") - SysMgr.printPipe(oneLine) + SysMgr.addPrint("\tNone\n", force=True) + SysMgr.addPrint(oneLine + "\n", force=True) + + # print buffer # + SysMgr.printTopStats() + + @staticmethod + def doReadelf(): + # get path # + if SysMgr.hasMainArg(): + path = SysMgr.getMainArg() + elif SysMgr.inputParam: + path = SysMgr.inputParam + else: + SysMgr.printErr("no input value for path") + sys.exit(-1) + + SysMgr.printLogo(big=True, onlyFile=True) + + # set debug flag # + debug = not SysMgr.jsonEnable + onlyHdr = "ONLYHEADER" in SysMgr.environList + + def _printJson(path, obj): + if not SysMgr.jsonEnable: + return + jsonStr = UtilMgr.convDict2Str( + obj.attr, pretty=not SysMgr.streamEnable + ) + SysMgr.printPipe(('"%s": ' % path) + jsonStr) + + # run ELF analyzer # + try: + # print vdso # + if path == "vdso": + obj = SysMgr.getVDSO(debug=debug) + _printJson(path, obj) + else: + # get path list # + pathList = path.split(",") + pathList = UtilMgr.getFileList(pathList) + if not pathList: + SysMgr.printErr("invalid path '%s'" % path) + sys.exit(-1) + + # convert to hard-linked path set # + pathList = set(map(os.path.realpath, pathList)) + + # print items # + for p in pathList: + obj = ElfAnalyzer( + p, debug, onlyHdr=onlyHdr, incArg=True + ) + _printJson(p, obj) + except SystemExit: + sys.exit(0) + except: + SysMgr.printErr("failed to analyze '%s'" % path, True) + + @staticmethod + def doKill(): + # make list of arguments # + if len(sys.argv) > 2: + argList = sys.argv[2:] + else: + argList = [" "] + + # print signal list # + if SysMgr.findOption("l"): + SysMgr.printList(ConfigMgr.SIG_LIST, ["0"]) + sys.exit(0) + + # remove additional options # + if len(argList) > 1: + idx = 0 + for idx, item in enumerate(argList[1:]): + if item.strip().startswith("-"): + break + + if argList[-1].strip().startswith("-"): + addIdx = 1 + else: + addIdx = 2 + argList = argList[: idx + addIdx] + + # convert task ID # + argList = SysMgr.convertTaskIdInput(argList) + + while 1: + # set target type # + if SysMgr.checkMode("tkill"): + isThread = True + else: + isThread = False + + # send signal # + SysMgr.sendSignalArgs(list(argList), isThread=isThread) + + # check interval # + if SysMgr.intervalEnable: + time.sleep(SysMgr.intervalEnable) + else: + break @staticmethod def doLimit():