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.
smart-top/smart/files/__init__.py

65 lines
2.5 KiB
Python

"""
This file will submit an alert to either a mailbox given a set of parameters, this will perform as following :
- as-a-service
- embedded
"""
import os
import pandas as pd
import subprocess
import glob
from datetime import datetime
def post(**args):
"""
This function will submit a report to a given target provided some input
:key will perform as-a-service
:data data that will be submitted to smtp/queue server
:smtp will send the file to a mailbox
"""
pass
def parse(_stream):
"""
:stream single from the output command that has been executed
"""
_blocks = _stream.replace(' ',' ').split(' ')
if len(_blocks) > 6 :
_user = _blocks[1]
_group= _blocks[2]
_size = _blocks[3] # if units are not specified please interpet this as bytes
_date = "-".join(_blocks[4:6])
_time = _blocks[6]
_name = _blocks[-1]
if ':' not in _time :
_date = _date+' '+_time
_time = '00:00'
else:
_date = _date+'-'+str(datetime.now().year)
_name = _blocks[-1]
return {'user':_user,'date':_date,'time':_time,'size':_size,'content':None,'name':_name}
def apply(_cmd, parser=None):
handler = subprocess.Popen(_cmd,shell=True,stdout=subprocess.PIPE,encoding='utf-8')
stream = handler.communicate()[0].split('\n')
stream = [line.strip() for line in stream]
if not parser :
# print (dict(zip(['hash','names'],stream[0].split())))
stream = [ line.strip().replace(' ',' ').split(' ') for line in stream if len(line.strip().split()) == 2]
return pd.DataFrame([dict(zip(['content','name'],line)) for line in stream])
# return pd.DataFrame([ line.split() for line in stream ])
# return pd.DataFrame( dict(zip(['checksum','name'],[line.strip().split(' '))) for line in stream if line.strip() != '']) )
else:
return pd.DataFrame([ parser(line.strip()) for line in stream if line.strip() != ''])
def read (path):
"""
This function will read files in a folder and provide has expressions of the files
"""
_cmd = ["""find :path -type f -exec md5sum "{}" + """ , """find :path -type f -exec ls -lh "{}" + |grep -E " .*$" -o """]
_df = apply(_cmd[0].replace(":path",path))
_data= apply(_cmd[1].replace(":path",path),parse)
if _data.shape[0] == _df.shape[0] :
_data['content'] = _df.content
return _data
read('/home/steve/tmp/logs')