#!/usr/bin/python2.7 import fsio import glob import subprocess import os import time import datetime import sys def generateLogPwd(): import base64 import hashlib backup = fsio.jsonload("/home/tool/config/backup.json") return base64.b64encode(hashlib.sha1("SecretNexoOS{0}{1}".format(getMajorVersion(), backup['log_passwd'])).digest()) def generateCfgPwd(): import base64 import hashlib pw = "NexoOS{0}".format(getMajorVersion()) return base64.b64encode(hashlib.sha1(pw).digest()) def getMajorVersion(): v = version() return v.split("-")[1] def _genLogFileName(): return _genFileNameToExport("Logs_", ".nxlgx") def _genCfgFileName(): return _genFileNameToExport("", ".nxcfg") def _genFileNameToExport(prefix, extension): ip=_getinterfaceip("tiwlan0") if not ip: ip=_getinterfaceip("eth0") ts = time.time() dateStamp = datetime.datetime.fromtimestamp(ts).strftime('%Y%m%d%H%M%S') if not ip: return prefix + version().strip() + '-' + dateStamp + extension else: return ip + "_" + prefix + version().strip() + '-' + dateStamp + extension def _getinterfaceip(ifname): oscmd='/sbin/ifconfig '+ifname+' | grep "inet\ addr" | cut -d: -f2 | cut -d" " -f1' f=os.popen(oscmd) ip = f.read() return ip.rstrip() def version(): try: with open('/etc/version', 'r') as f: version = f.readlines()[0] return version.strip() except: return '-' def file_list_logs_export(): syslogs = glob.glob("/var/log/*") syslogs_str = " ".join(syslogs) var_run_diag = glob.glob("/var/run/diag/*") var_run_diag_str = " ".join(var_run_diag) file_list = syslogs_str + " " + var_run_diag_str + " /etc/version" return file_list def _genLogFile(filelist): logfile="/tmp/"+ _genLogFileName() pwd=generateLogPwd() # print >> sys.stdout, "PW: " + pwd + "\n" #return logfile # p = subprocess.Popen(["/home/tool/bin/7za a -t7z -mx=3 " + logfile + pwd + file_list_logs_export()], shell=True) # print logfile # print pwd ZipperCmd="/home/tool/bin/7za a -t7z -mx=3 "+ logfile + " " + filelist + " -p" + pwd # print >> sys.stdout, ZipperCmd p = subprocess.Popen([ZipperCmd ],shell=True,stdout=subprocess.PIPE, stderr=subprocess.PIPE) #stdout=subprocess.PIPE, stderr=subprocess.PIPE) (stdout, stderr) = p.communicate() # p.communicate() return logfile def _genBackupFile(filelist): BackupFile="/tmp/"+_genCfgFileName() pwd=generateCfgPwd() ZipperCmd="/home/tool/bin/7za a -t7z -mx=3 "+ BackupFile + " " + filelist + " -p" + pwd p = subprocess.Popen([ZipperCmd ],shell=True,stdout=subprocess.PIPE, stderr=subprocess.PIPE) #stdout=subprocess.PIPE, stderr=subprocess.PIPE) (stdout, stderr) = p.communicate() return BackupFile #/home/tool/bin/7za #res = {'success': True, 'msg': ''} #try: #a -t7z -mx=3 $TARGET_FILE $FILE_LIST -p$PWD #cmdstring = "/home/tool/bin/7za" #cmdargs = "a -t7z -mx=3 /tmp/Test.nxlog " + file_list_logs_export() + " " + generateLogPwd() #logfile="/tmp/a.nxlgx" arg=sys.argv[1] files=sys.argv[2] #print >> sys.stdout, arg, files #files="/tmp/log/*" if arg == "logfile": print _genLogFile(files) sys.exit(0) if arg == "backup": print _genBackupFile(files) sys.exit(0) sys.exit(-1) #logfile="/tmp/"+ _genLogFileName() #if os.access(logfile, os.F_OK): # os.remove(logfile) #print (cmdstring, ) #p = subprocess.Popen(['{0}/log_exporter.sh'.format("/etc/backup"), generateLogPwd(), "Test.nxlog", file_list_logs_export(), '/tmp'], stdout=subprocess.PIPE, stderr=subprocess.PIPE) #p = subprocess.Popen(["/home/tool/bin/7za a -t7z -mx=3 " + logfile + " " + file_list_logs_export()], stdout=subprocess.PIPE, stderr=subprocess.PIPE) #(stdout, stderr) = p.communicate() #print("communicate->" + str(p.returncode)) #(stdout, stderr) = p.communicate() #if not p.returncode: # print >> sys.stderr, logfile # sys.exit(0) #else: # sys.exit(-1) #res['success'] = False # res['msg'] = str(stderr) #except Exception, e: # res['success'] = False # res['msg'] = str(e) #finally: #syslog.syslog(syslog.LOG_INFO, "DONE setting.exportFileLogs.subprocess to " + expfname + " at " + time.asctime()) #syslog.syslog(syslog.LOG_INFO, "RESULT setting.exportFileLogs.subprocess to stdout: " + str(stdout)) #syslog.syslog(syslog.LOG_INFO, "RESULT setting.exportFileLogs.subprocess to stderr: " + str(stderr)) #if p.returncode != 0: # Failure #res['success'] = False #res['msg'] = str(stderr) #print("Pwd: " + generateLogPwd() + " " + file_list_logs_export())