__init__.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. """
  2. This framework allows data to be logged to a given data store i.e :
  3. - disk, cloud (google, dropbox, box, sugarsync or s3) or a queue server
  4. The intent of the framework is to work as a standalone or embedded in code as a logging framework
  5. usage:
  6. dependencies :
  7. data-transport pip install git+https://dev.the-phi.com/git/steve/data-transport.git
  8. """
  9. import smart
  10. import smart.top
  11. import smart.folder
  12. import smart.top
  13. import smart.logger
  14. import smart.files
  15. import uuid
  16. import typer
  17. import smart.info
  18. import json
  19. import os
  20. import transport
  21. import shutil
  22. from datetime import datetime
  23. _cli = typer.Typer()
  24. @_cli.command(name='log-intruder')
  25. def intrusion(path:str='/var/log/auth.log', year:int=datetime.now().year):
  26. """
  27. This function
  28. """
  29. _r = smart.logger.read(path=path,year=year)
  30. if _r :
  31. for _id in _r :
  32. if hasattr(smart.logger,_id):
  33. try:
  34. _pointer = getattr(smart.logger,_id)
  35. _df = _pointer(_r[_id])
  36. post(_df,_id)
  37. except Exception as e:
  38. print (e)
  39. pass
  40. else:
  41. print ()
  42. print ("Nothing out of the ordinary was found in")
  43. print (f"{path}")
  44. @_cli.command(name='top')
  45. def apply_apps (app:str=None,user:str=None):
  46. """
  47. This function looks at applications/commands running on the system
  48. """
  49. _df = smart.top.read()
  50. _id = 'apps' if not app else app
  51. if app :
  52. _index = _df.name == app
  53. if _index.sum() :
  54. _df = _df[_index]
  55. post(_df,_id)
  56. @_cli.command(name='archive')
  57. def _archive():
  58. """
  59. This function will archive the database, by renaming it into
  60. """
  61. _suffix = datetime.now()
  62. _suffix = "-".join([str(_value) for _value in [_suffix.year,_suffix.month,_suffix.day,_suffix.hour,_suffix.minute]])
  63. _path = os.sep.join([smart.info.__home__,smart.info.__database__])
  64. _src = _path + '.db3'
  65. if os.path.exists(_src):
  66. _target = _path +'-archived-on-'+ _suffix+'.db3'
  67. shutil.move(_src,_target)
  68. _msg = f"""Archive created successfully at:
  69. {_target}"""
  70. else:
  71. _msg = """
  72. Archive function is not available at this time, please try after logs have been stored
  73. """
  74. print(_msg)
  75. @_cli.command(name='folder')
  76. def apply_folder(path:str):
  77. """
  78. This function will read the content of a folder and generate a
  79. """
  80. _df = smart.folder.read(path=path)
  81. # print (_df)
  82. post(_df,'folders')
  83. pass
  84. @_cli.command (name='files')
  85. def apply_files(folder:str) :
  86. _df = smart.files.read(folder)
  87. post(_df,'files')
  88. @_cli.command(name='register')
  89. def apply_signup (email:str,key:str=None,provider:str='sqlite') :
  90. _config = {"system":{"email":email,"uid":str(uuid.uuid4()),"version":smart.info.__version__},"store":{"provider":provider,"context":"write"}}
  91. _db = smart.info.__database__
  92. if provider in ['sqlite','sqlite3'] :
  93. _db = os.sep.join([smart.info.__home__,_db+'.db3'])
  94. _config['store']['database'] = _db
  95. else:
  96. _config['store']['database'] = _db
  97. #
  98. # Let us store this in a folder
  99. _PATH = smart.info.__home__
  100. _verb = "written"
  101. if not os.path.exists(_PATH) :
  102. os.mkdir(_PATH)
  103. else:
  104. _verb = "updated"
  105. f = open(os.sep.join([_PATH,'config.json']),'w')
  106. f.write(json.dumps(_config))
  107. f.close()
  108. _msg = f"""
  109. The configuration file was {_verb} successfully at {smart.info.__home__}
  110. data store:
  111. provider {provider}
  112. database {_db}
  113. If your database has security enabled, consider updating "{smart.info.__home__}{os.sep}config.json" For appropriate security
  114. Visit https://github.com/lnyemba/data-transport for more information
  115. """
  116. print ()
  117. print (_msg)
  118. pass
  119. def post(_df,_table):
  120. """
  121. Store data in a given location
  122. """
  123. _path = os.sep.join([smart.info.__home__,'config.json'])
  124. f = open (_path)
  125. _config = json.loads(f.read())
  126. f.close()
  127. _store = _config['store']
  128. if _store['provider'] in ['mongodb','mongo','couch','couchdb'] :
  129. _store['collection'] = _table
  130. else:
  131. _store['table'] = _table
  132. writer = transport.factory.instance(**_store)
  133. writer.write(_df)
  134. if hasattr(writer,'close') :
  135. writer.close()
  136. if __name__ == '__main__' :
  137. _cli()
  138. # from transport import factory
  139. # class logger :
  140. # """
  141. # This class is a basic logger, it will log data regardless of the types of data, We will have subclasses that will implement various data extraction schemas:
  142. # - processes (top),
  143. # """
  144. # def __init__(self,**args):
  145. # """
  146. # :store data store (disk,mongo,couch,google,dropbox)
  147. # :args arguments to pass for the data-store (read transport documentation)
  148. # :notify function that returns true/false for notification
  149. # """
  150. # self.store = factory.instance(type=store,args=args['args'])
  151. # if 'notify' in args :
  152. # self.notify = args
  153. # pass
  154. # def log(self,row):
  155. # """
  156. # This function will log data to a data store
  157. # :row row to be stored
  158. # """
  159. # self.store.write(row=row)
  160. # if(hasattr(self,'notify')):
  161. # if (self.notify(row)) :
  162. # #
  163. # # Let us notify the backend by generating a report and submitting it
  164. # #
  165. # stream = self.get.report()
  166. # pass
  167. # else:
  168. # pass
  169. # def report(self) :