You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
71 lines
2.3 KiB
Python
71 lines
2.3 KiB
Python
#!/usr/bin/env python
|
|
"""
|
|
smart-top, The Phi Technology LLC
|
|
Steve L. Nyemba & Michael Meade
|
|
|
|
The smart-top is a utility that enables to monitor processes (among other things) and use the data for:
|
|
- detecting anomalies
|
|
-
|
|
"""
|
|
import smart.top
|
|
import pandas as pd
|
|
import sys
|
|
import os
|
|
import re
|
|
import time
|
|
# df = pd.DataFrame (smart.top.read(name='firefox,code'))
|
|
SYS_ARGS = {}
|
|
if len(sys.argv) > 1:
|
|
|
|
N = len(sys.argv)
|
|
for i in range(1,N):
|
|
value = None
|
|
if sys.argv[i].startswith('--'):
|
|
key = sys.argv[i][2:] #.replace('-','')
|
|
SYS_ARGS[key] = 1
|
|
if i + 1 < N:
|
|
value = sys.argv[i + 1] = sys.argv[i+1].strip()
|
|
if key and value and not value.startswith('--'):
|
|
SYS_ARGS[key] = value
|
|
|
|
|
|
i += 2
|
|
if __name__ == '__main__' :
|
|
try:
|
|
if 'help' in SYS_ARGS:
|
|
print (help_me)
|
|
sys.exit(0)
|
|
if 'watch' in SYS_ARGS :
|
|
SYS_ARGS['watch'] = int(SYS_ARGS['watch'])
|
|
if SYS_ARGS['watch'] <= 1 :
|
|
SYS_ARGS['watch'] = 10
|
|
log = pd.DataFrame()
|
|
while True:
|
|
os.system('clear')
|
|
print ()
|
|
print ("================================= SMART TOP ================================= ")
|
|
print ()
|
|
df = pd.DataFrame()
|
|
if 'name' in SYS_ARGS :
|
|
df = df.append(pd.DataFrame(smart.top.read(name=SYS_ARGS['name'])))
|
|
else:
|
|
df = pd.DataFrame(smart.top.read())
|
|
# df = pd.DataFrame(smart.top.read(name='fire'))
|
|
|
|
log = log.append(df)
|
|
|
|
print (df[['pid','name','user','cpu','mem','started','date','time','status']])
|
|
if 'watch' in SYS_ARGS :
|
|
time.sleep(SYS_ARGS['watch'])
|
|
|
|
else:
|
|
break
|
|
except KeyboardInterrupt:
|
|
if 'log' in SYS_ARGS :
|
|
file = 'smart-top.csv' if SYS_ARGS['log'] == 1 else SYS_ARGS['log']
|
|
log[['name','cmd','cpu','mem','started','date','time','status','node']].to_csv(file,index=False)
|
|
print ()
|
|
print ("... Exiting, Thanks for using smart-top")
|
|
|
|
# pass
|
|
# print (df.groupby(['user'])['cpu','mem'].sum()) |