__init__.py 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. """
  2. This file will submit an alert to either a mailbox given a set of parameters, this will perform as following :
  3. - as-a-service
  4. - embedded
  5. """
  6. import os
  7. import pandas as pd
  8. import subprocess
  9. import glob
  10. from datetime import datetime
  11. def post(**args):
  12. """
  13. This function will submit a report to a given target provided some input
  14. :key will perform as-a-service
  15. :data data that will be submitted to smtp/queue server
  16. :smtp will send the file to a mailbox
  17. """
  18. pass
  19. def parse(_stream):
  20. """
  21. :stream single from the output command that has been executed
  22. """
  23. _blocks = _stream.replace(' ',' ').split(' ')
  24. if len(_blocks) > 6 :
  25. _user = _blocks[1]
  26. _group= _blocks[2]
  27. _size = _blocks[3] # if units are not specified please interpet this as bytes
  28. _date = "-".join(_blocks[4:6])
  29. _time = _blocks[6]
  30. _name = _blocks[-1]
  31. if ':' not in _time :
  32. _date = _date+' '+_time
  33. _time = '00:00'
  34. else:
  35. _date = _date+'-'+str(datetime.now().year)
  36. _name = _blocks[-1]
  37. return {'user':_user,'date':_date,'time':_time,'size':_size,'content':None,'name':_name}
  38. def apply(_cmd, parser=None):
  39. handler = subprocess.Popen(_cmd,shell=True,stdout=subprocess.PIPE,encoding='utf-8')
  40. stream = handler.communicate()[0].split('\n')
  41. stream = [line.strip() for line in stream]
  42. if not parser :
  43. # print (dict(zip(['hash','names'],stream[0].split())))
  44. stream = [ line.strip().replace(' ',' ').split(' ') for line in stream if len(line.strip().split()) == 2]
  45. return pd.DataFrame([dict(zip(['content','name'],line)) for line in stream])
  46. # return pd.DataFrame([ line.split() for line in stream ])
  47. # return pd.DataFrame( dict(zip(['checksum','name'],[line.strip().split(' '))) for line in stream if line.strip() != '']) )
  48. else:
  49. return pd.DataFrame([ parser(line.strip()) for line in stream if line.strip() != ''])
  50. def read (path):
  51. """
  52. This function will read files in a folder and provide has expressions of the files
  53. """
  54. _cmd = ["""find :path -type f -exec md5sum "{}" + """ , """find :path -type f -exec ls -lh "{}" + |grep -E " .*$" -o """]
  55. _df = apply(_cmd[0].replace(":path",path))
  56. _data= apply(_cmd[1].replace(":path",path),parse)
  57. if _data.shape[0] == _df.shape[0] :
  58. _data['content'] = _df.content
  59. return _data