Repository for M.A.I.L system's analysis server.
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

mailrequest.py 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. from re import sub
  2. import sys
  3. import os
  4. import subprocess
  5. import time
  6. DEVICE_PASSWORD = b'tkadbrdhmc1!\n'
  7. SOURCE_DIR = '~/sources/'
  8. STORAGE_DIR = '/hdd/'
  9. LOG_REBOOT = '~/sources/cfg/log_reboot.txt'
  10. REC_COUNT = '/rec_count.txt'
  11. LOG_EXTRACT = '/log_extract.txt'
  12. CONTAINER_EXT_1 = ['docker', 'run', '--name', 'ext_01', '--gpus', ''"device=0"'', '--rm']
  13. CONTAINER_EXT_2 = ['docker', 'run', '--name', 'ext_02', '--gpus', ''"device=1"'', '--rm']
  14. CONTAINER_IMAGE_1 = ['ellishuntingmoon/mailsys:0.5','python3',os.path.join(os.path.abspath(os.path.expanduser(SOURCE_DIR)),'mailextract_01.py')]
  15. CONTAINER_IMAGE_2 = ['ellishuntingmoon/mailsys:0.5','python3',os.path.join(os.path.abspath(os.path.expanduser(SOURCE_DIR)),'mailextract_02.py')]
  16. def container_status():
  17. clist = []
  18. docker_process = subprocess.Popen(('docker','ps','-a'), stdout=subprocess.PIPE)
  19. try:
  20. clist = subprocess.check_output(('grep','ext_'), stdin=docker_process.stdout).decode()
  21. clist = clist.split()
  22. clist= [i for i in clist if 'ext_' in i]
  23. return clist
  24. except subprocess.CalledProcessError as e:
  25. return clist
  26. def request_extract():
  27. nowtime = time.localtime()
  28. nowdate = "%04d/%02d/%02d" % (nowtime.tm_year, nowtime.tm_mon, nowtime.tm_mday)
  29. hdd_list = os.listdir(os.path.abspath(STORAGE_DIR))
  30. reboot_list = []
  31. container_list = container_status()
  32. #print(container_list)
  33. with open (os.path.abspath(os.path.expanduser(LOG_REBOOT)), "r") as log_reboot:
  34. reboot_list = log_reboot.readlines()
  35. #NOTE: nowtime.tm_wday == 6 is sunday
  36. if (nowtime.tm_wday == 6) and (nowdate not in reboot_list[-1]) and (len(hdd_list) == 1):
  37. with open (os.path.abspath(os.path.expanduser(LOG_REBOOT)), "a+") as log_reboot:
  38. log_reboot.write("\n[REBOOT] Device reboot executed at %04d/%02d/%02d %02d:%02d:%02d" % (nowtime.tm_year, nowtime.tm_mon, nowtime.tm_mday, nowtime.tm_hour, nowtime.tm_min, nowtime.tm_sec))
  39. #FIXME: MUST ERASE COMMENT WHEN YOU COMPLETE
  40. #subprocess.call(('echo','rebooting...'))
  41. #subprocess.Popen(('sudo','-S','shutdown','-r','now'), stdin=subprocess.PIPE, stderr=subprocess.PIPE).communicate(input=PASSWORD)
  42. elif len(container_list) == 0 :
  43. hdd_list = []
  44. hdd_list = os.listdir(os.path.abspath(STORAGE_DIR))
  45. hdd_list.remove('lost+found')
  46. #roundrobin surgeons
  47. for surgeon in hdd_list:
  48. if len(container_list) != 0:
  49. break
  50. svocount = 0
  51. surgeondatalist = os.listdir(os.path.abspath(STORAGE_DIR+surgeon))
  52. #rec_count 내용 확인
  53. with open (os.path.abspath(STORAGE_DIR+surgeon+REC_COUNT), "r") as log_reboot:
  54. svocount = int(log_reboot.readline())
  55. svofulllist = [file for file in surgeondatalist if file.endswith('.svo')]
  56. #svo 개수가 rec_count -2일때 and 파일 개수가 rec_count와 동일 하면 분석 시작
  57. if (len(svofulllist) == (svocount-2)) and len(surgeondatalist) == svocount:
  58. exttime = time.localtime()
  59. with open (os.path.abspath(STORAGE_DIR+surgeon+LOG_EXTRACT), "w+") as log_extract:
  60. log_extract.write("\n[EXTRACT] EXTRACT %d svo files executed at %04d/%02d/%02d %02d:%02d:%02d" % (len(svofulllist),exttime.tm_year, exttime.tm_mon, exttime.tm_mday, exttime.tm_hour, exttime.tm_min, exttime.tm_sec))
  61. containermiddle = [\
  62. '-v', os.path.abspath(STORAGE_DIR+surgeon)+':'+ os.path.abspath(STORAGE_DIR+surgeon), \
  63. '-v', os.path.abspath(os.path.expanduser(SOURCE_DIR))+':'+os.path.abspath(os.path.expanduser(SOURCE_DIR)), \
  64. '-w', os.path.abspath(STORAGE_DIR+surgeon)
  65. ]
  66. #docker run --name ext_01 --gpus '"device=0"' --rm -v /hdd/{surgeon}:/hdd/{surgeon} -v /home/mc365/sources/:/home/mc365/sources/ -w /hdd/{surgeon} ellishuntingmoon/mailsys:0.5 python3 /home/mc365/sources/mailextract_01.py
  67. #print(CONTAINER_EXT_1 + containermiddle + CONTAINER_IMAGE_1)
  68. subprocess.Popen(CONTAINER_EXT_1 + containermiddle + CONTAINER_IMAGE_1)
  69. subprocess.Popen(CONTAINER_EXT_2 + containermiddle + CONTAINER_IMAGE_2)
  70. #svo1list = [file for file in svofulllist if file.endswith('1.svo')]
  71. #svo2list = [file for file in svofulllist if file.endswith('2.svo')]
  72. #print('ext start')
  73. time.sleep(15)
  74. container_list = container_status()
  75. else:
  76. #print('already finished')
  77. pass
  78. #print(svo1list, svo2list)
  79. else:
  80. #print('extract is in progress... wait until it finish...')
  81. pass
  82. def main():
  83. while True:
  84. request_extract()
  85. time.sleep(15)
  86. if __name__== '__main__':
  87. main()