__init__.py 5.6 KB

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