Skip to content

Commit

Permalink
mxvmem: add a short summary display (-s)
Browse files Browse the repository at this point in the history
  • Loading branch information
thomas committed May 8, 2023
1 parent 66495a2 commit 7c439d3
Showing 1 changed file with 41 additions and 10 deletions.
51 changes: 41 additions & 10 deletions forensics/mxvmem
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,8 @@ class ProcFsHandler():

class ProcMemClassifier(Classifier):

keys = ('DZT','S','R','dzt','s','r')

def maxMem(self):
m = max(k[1].val for k in self)
return m
Expand All @@ -291,15 +293,14 @@ class ProcMemClassifier(Classifier):
# 10 for the user, 12 is default width for the number,
# 2 for fillers and such, 2 for the border
bar_width = width - 10 - 12 - 2 - 2
keys = ('DZT','S','R','dzt','s','r')
ret = list()

for k in self:
user = k[0]
mtot = k[1].val
bar = ' %-10s' % str(user)[:9] + '|'
nused = 0
for key in keys:
for key in self.keys:
if key in k[1].fld:
ks = k[1].fld[key]
n = int(ks.val/scmax * bar_width)
Expand All @@ -318,6 +319,27 @@ class ProcMemClassifier(Classifier):

return ret

def summaryStrings(self):
'''
returns a string array for condensed display:
string has 3 components: 'user memory states'
'''
ret = list()

for k in self:
states = ''
user = k[0]
mtot = k[1].val
line = ' %-10s' % str(user)[:9] + ':'
line += sep_fmt12(k[1].val)
for key in self.keys:
if key in k[1].fld:
states += key[0]
line += ' : [' + states + ']'
ret.append((user, mtot, line))

return ret

# ------------------------------------------------------------------- tools

def register_logs(logdir):
Expand Down Expand Up @@ -381,6 +403,8 @@ def handle_args():
help='location of forensics logs (/var/log)')
ap.add_argument('-p', dest='readproc',
help='read current data from proc', action='store_true')
ap.add_argument('-s', dest='summary',
help='print short summary', action='store_true')
ap.add_argument('forensicsfile', metavar='file', nargs='?',
help='forensics file (defaults to most recent log found)')
return ap.parse_args()
Expand Down Expand Up @@ -444,17 +468,24 @@ if __name__ == '__main__':
100 * pmc.getval()/whs.memtotal )
)

maxmem_used = pmc.maxMem()
cols, lines = get_term_size()
if args.summary:

for i in sorted(pmc.summaryStrings(), key=lambda L: L[1], reverse=True):
print(i[2])

else:

cols, lines = get_term_size()
maxmem_used = pmc.maxMem()

print(' USER ', '*** OLD/young processes ***'.center(cols-23) , 'Amount [kb]')
print(' USER ', '*** OLD/young processes ***'.center(cols-23) , 'Amount [kb]')

mem_bars = pmc.barStrings(maxmem_used, cols)
limit = lines-11 if not args.allentries else len(mem_bars)
for i in sorted(mem_bars, key=lambda L: L[1], reverse=True)[:limit]:
print(i[2])
mem_bars = pmc.barStrings(maxmem_used, cols)
limit = lines-11 if not args.allentries else len(mem_bars)
for i in sorted(mem_bars, key=lambda L: L[1], reverse=True)[:limit]:
print(i[2])

print('*** R = running, S = sleeping, D = deep sleep, zombie, or debugged ***'.center(cols))
print('*** R = running, S = sleeping, D = deep sleep, zombie, or debugged ***'.center(cols))

print()

Expand Down

0 comments on commit 7c439d3

Please sign in to comment.