__init__.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 meta
  19. import json
  20. import os
  21. import transport
  22. import shutil
  23. from datetime import datetime
  24. _cli = typer.Typer()
  25. # import plugins
  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(name=app)
  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([meta.__home__,meta.__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":meta.__version__},"store":{"provider":provider,"context":"write"}}
  93. _db = meta.__database__
  94. if provider in ['sqlite','sqlite3'] :
  95. _db = os.sep.join([meta.__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 = meta.__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 {meta.__home__}
  112. data store:
  113. provider {provider}
  114. database {_db}
  115. If your database has security enabled, consider updating "{meta.__home__}{os.sep}config.json" For appropriate security
  116. Visit https://github.com/lnyemba/data-transport for more.metarmation
  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([meta.__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. _store['table'] = _table
  135. # writer = transport.factory.instance(**_store)
  136. writer = transport.get.writer(**_store)
  137. writer.write(_df)
  138. if hasattr(writer,'close') :
  139. writer.close()
  140. if __name__ == '__main__' :
  141. _cli()