__main__.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #!/usr/bin/env python
  2. """
  3. smart-top, The Phi Technology LLC
  4. Steve L. Nyemba & Michael Meade
  5. The smart-top is a utility that enables to monitor processes (among other things) and use the data for:
  6. - detecting anomalies
  7. -
  8. """
  9. import smart.top
  10. import pandas as pd
  11. import sys
  12. import os
  13. import re
  14. import time
  15. # df = pd.DataFrame (smart.top.read(name='firefox,code'))
  16. SYS_ARGS = {}
  17. if len(sys.argv) > 1:
  18. N = len(sys.argv)
  19. for i in range(1,N):
  20. value = None
  21. if sys.argv[i].startswith('--'):
  22. key = sys.argv[i][2:] #.replace('-','')
  23. SYS_ARGS[key] = 1
  24. if i + 1 < N:
  25. value = sys.argv[i + 1] = sys.argv[i+1].strip()
  26. if key and value and not value.startswith('--'):
  27. SYS_ARGS[key] = value
  28. i += 2
  29. if __name__ == '__main__' :
  30. try:
  31. if 'help' in SYS_ARGS:
  32. print (help_me)
  33. sys.exit(0)
  34. if 'watch' in SYS_ARGS :
  35. SYS_ARGS['watch'] = int(SYS_ARGS['watch'])
  36. if SYS_ARGS['watch'] <= 1 :
  37. SYS_ARGS['watch'] = 10
  38. log = pd.DataFrame()
  39. while True:
  40. os.system('clear')
  41. print ()
  42. print ("================================= SMART TOP ================================= ")
  43. print ()
  44. df = pd.DataFrame()
  45. if 'name' in SYS_ARGS :
  46. df = df.append(pd.DataFrame(smart.top.read(name=SYS_ARGS['name'])))
  47. else:
  48. df = pd.DataFrame(smart.top.read())
  49. # df = pd.DataFrame(smart.top.read(name='fire'))
  50. log = log.append(df)
  51. print (df[['pid','name','user','cpu','mem','started','date','time','status']])
  52. if 'watch' in SYS_ARGS :
  53. time.sleep(SYS_ARGS['watch'])
  54. else:
  55. break
  56. except KeyboardInterrupt:
  57. if 'log' in SYS_ARGS :
  58. file = 'smart-top.csv' if SYS_ARGS['log'] == 1 else SYS_ARGS['log']
  59. log[['name','cmd','cpu','mem','started','date','time','status','node']].to_csv(file,index=False)
  60. print ()
  61. print ("... Exiting, Thanks for using smart-top")
  62. # pass
  63. # print (df.groupby(['user'])['cpu','mem'].sum())