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.

89 lines
2.6 KiB
Python

"""
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
dependencies:
- top (on the os)
@TODO:
Test this thing on windows to see if it works
"""
import pandas as pd
import numpy as np
import subprocess
import os
import datetime
# from transport import factory
import sys
import hashlib
import re
from io import StringIO
import importlib as IL
import importlib.util
class plugin :
"""
This is a context specific decorator to be used for generic commands.
This decorator should be placed on parsing functions
"""
def __init__(self,**_args):
"""
:name name of the plugin
:mode restrict to reader/writer
:about tell what the function is about
"""
self._cmd = _args['cmd']
self._alias = _args['alias'] if 'alias' in _args else None
def __call__(self,pointer,**kwargs):
def wrapper(_args,**kwargs):
return pointer(_args,**kwargs)
#
# @TODO:
# add attributes to the wrapper object
#
# self._name = pointer.__name__ if not self._name else self._name
# setattr(wrapper,'context',True)
setattr(wrapper,'cmd',self._cmd)
setattr(wrapper,'alias',self._alias)
return wrapper
def read(**args) :
"""
This function will perform the actual reads of process informations.
@return {user,pid,start,status, name, args, mem,cpu}
"""
# cmd = args['cmd'] #"ps -eo pid,user,pmem,pcpu,stat,etime,args|awk 'OFS=\";\" {$1=$1; if($5 > 9) print }'"
xchar = ";"
try:
_parser = args['parser'] if 'parser' in args else None
stream = None
cmd = None
if _parser and hasattr(_parser,'cmd'):
cmd = getattr(_parser,'cmd')
elif 'cmd' in args :
cmd = args['cmd']
if cmd :
handler = subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE)
stream = str(handler.communicate()[0]).replace("b'",'')
return _parser (stream) if _parser else stream
# return stream
#
# At this point we need to load the parser or return the output as is
# if 'parser' in args :
# _parser = args['parser']
# return _parser(stream)
# else:
# return stream
except Exception as e:
print (e)
pass
# if __name__ == '__main__' :
# #
# # Being directly called (external use of the )
# print(read())