__init__.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. """
  2. This file contains class and functions that extract data from running processes like top and stores them into a data store of the calling codes choice
  3. dependencies:
  4. - top (on the os)
  5. @TODO:
  6. Test this thing on windows to see if it works
  7. """
  8. import pandas as pd
  9. import numpy as np
  10. import subprocess
  11. import os
  12. import datetime
  13. # from transport import factory
  14. import sys
  15. import hashlib
  16. import re
  17. from io import StringIO
  18. import importlib as IL
  19. import importlib.util
  20. class plugin :
  21. """
  22. This is a context specific decorator to be used for generic commands.
  23. This decorator should be placed on parsing functions
  24. """
  25. def __init__(self,**_args):
  26. """
  27. :name name of the plugin
  28. :mode restrict to reader/writer
  29. :about tell what the function is about
  30. """
  31. self._cmd = _args['cmd']
  32. self._alias = _args['alias'] if 'alias' in _args else None
  33. def __call__(self,pointer,**kwargs):
  34. def wrapper(_args,**kwargs):
  35. return pointer(_args,**kwargs)
  36. #
  37. # @TODO:
  38. # add attributes to the wrapper object
  39. #
  40. # self._name = pointer.__name__ if not self._name else self._name
  41. # setattr(wrapper,'context',True)
  42. setattr(wrapper,'cmd',self._cmd)
  43. setattr(wrapper,'alias',self._alias)
  44. return wrapper
  45. def read(**args) :
  46. """
  47. This function will perform the actual reads of process informations.
  48. @return {user,pid,start,status, name, args, mem,cpu}
  49. """
  50. # cmd = args['cmd'] #"ps -eo pid,user,pmem,pcpu,stat,etime,args|awk 'OFS=\";\" {$1=$1; if($5 > 9) print }'"
  51. xchar = ";"
  52. try:
  53. _parser = args['parser'] if 'parser' in args else None
  54. stream = None
  55. cmd = None
  56. if _parser and hasattr(_parser,'cmd'):
  57. cmd = getattr(_parser,'cmd')
  58. elif 'cmd' in args :
  59. cmd = args['cmd']
  60. if cmd :
  61. handler = subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE)
  62. stream = str(handler.communicate()[0]).replace("b'",'')
  63. return _parser (stream) if _parser else stream
  64. # return stream
  65. #
  66. # At this point we need to load the parser or return the output as is
  67. # if 'parser' in args :
  68. # _parser = args['parser']
  69. # return _parser(stream)
  70. # else:
  71. # return stream
  72. except Exception as e:
  73. print (e)
  74. pass
  75. # if __name__ == '__main__' :
  76. # #
  77. # # Being directly called (external use of the )
  78. # print(read())