__init__.py 5.6 KB

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