Skip to content

Commit

Permalink
ps: Add print format in JSON
Browse files Browse the repository at this point in the history
Signed-off-by: iipeace <[email protected]>
  • Loading branch information
iipeace committed Nov 24, 2024
1 parent 421590e commit f6844cd
Showing 1 changed file with 157 additions and 104 deletions.
261 changes: 157 additions & 104 deletions guider/guider.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
__credits__ = "Peace Lee"
__license__ = "GPLv2"
__version__ = "3.9.8"
__revision__ = "241123"
__revision__ = "241124"
__maintainer__ = "Peace Lee"
__email__ = "[email protected]"
__repository__ = "https://github.com/iipeace/guider"
Expand Down Expand Up @@ -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 ]
Expand Down Expand Up @@ -50708,10 +50710,14 @@ def _printExecutedCmd():
"memprof",
"memtop",
"pkglist",
"ps",
"pslist",
"remove",
"resmon",
"restop",
"sysdump",
"sysinfo",
"syslist",
"tkill",
)
or isDrawMode
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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"):
Expand Down Expand Up @@ -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"):
Expand Down Expand Up @@ -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,
Expand All @@ -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 #
Expand Down Expand Up @@ -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:
Expand All @@ -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,
Expand All @@ -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():
Expand Down

0 comments on commit f6844cd

Please sign in to comment.