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.
97 lines
3.1 KiB
Python
97 lines
3.1 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
|
|
import numpy as np
|
|
# df = pd.DataFrame (smart.top.read(name='firefox,code'))
|
|
HOME_FOLDER = os.environ['HOME']
|
|
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 'folder' in SYS_ARGS :
|
|
for path in SYS_ARGS['folder'].split(',') :
|
|
#df = df.concat(smart.folder.read(path=path))
|
|
_df = smart.folder.read(path=path)
|
|
df = _df if df.shape[0] == 0 else pd.concat(df,_df)
|
|
cols = df.columns.tolist()
|
|
else:
|
|
df = smart.top.read()
|
|
cols = ['pid','name','user','cpu','mem','started','date','time','status']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for key in SYS_ARGS :
|
|
value = SYS_ARGS[key]
|
|
|
|
if not value or key not in df.columns:
|
|
continue
|
|
ii = df.apply(lambda row:re.match(value,str(row[key])) is not None,axis=1)
|
|
df = df[ii].copy()
|
|
|
|
df.index = np.arange(df.shape[0])
|
|
|
|
# df = pd.DataFrame(smart.top.read(name='fire'))
|
|
|
|
#log = log.append(df)
|
|
log = df if log.shape[0] ==0 else pd.concat(log,df)
|
|
if not df.empty :
|
|
print (df[cols])
|
|
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())
|