def batchServiceRestart(rev): ''' @服务操作函数 ''' #判断是否选择了脚本操作 citem = osaBatchLib.getConfigItem(rev) if 'service_scriptfile' in citem: script = citem['service_scriptfile'] else: script = 'service '+str(citem['service_name'])+' '+citem['service_type'] sr = osaBatchLib.scriptOrCmd(script) if sr: try: result = osaBatchLib.runCmdOrScript(script) return "{'status':'OK','result':'OK!'}" except Exception as e: save_log('ERROR','Cmd or Script is run Faild!'+str(e)) result = 'Cmd or Script is run Faild!'+str(e) return "{'status':'ERROR','result':'services error!'}" else: return "{'status':'ERROR','result':'Unknow error!'}"
def batchCmdOrShell(rev): ''' @命令或者脚本执行函数 ''' #判断是否选择了脚本操作 citem = osaBatchLib.getConfigItem(rev) if 'command_scriptfile' in citem: scmd = citem['command_scriptfile'] else: return "{'status':'ERROR','result':'Unknow error!'}" sr = osaBatchLib.scriptOrCmd(scmd) if sr: try: result = osaBatchLib.runCmdOrScript(scmd) return "{'status':'OK','result':'"+result+"'}" except Exception as e: save_log('ERROR',str(e)) result = 'Cmd or Script is run Faild!' return "{'status':'ERROR','result':'"+result+"'}" else: return "{'status':'ERROR','result':'Unknow error!'}"
def batchDiskCheck(rev): ''' @磁盘空间检查 ''' #判断是否选择了脚本操作 citem = osaBatchLib.getConfigItem(rev) devlist = osaDiskLib.disk_partitions() rlist = [] if citem['unit'] == 'MB': for devpath in devlist: info=osaDiskLib.disk_usage(devpath[1]) if int(info[1]/1024/1024) >= int(citem['diskspace_threshold']) : rlist.append(devpath[1]) elif citem['unit'] == '%' : for devpath in devlist: info=osaDiskLib.disk_usage(devpath[1]) if int(info[3]) >= int(citem['diskspace_threshold']) : rlist.append(devpath[1]) if rlist: return "{'status':'ERROR','result':'disk stat more than diskspace_threshold!detail list:"+str(rlist)+"'}" else: return "{'status':'OK','result':'disk stat is normal!'}"
def batchTopCheck(rev): ''' @负载检查 ''' #判断是否选择了脚本操作 citem = osaBatchLib.getConfigItem(rev) if citem['topstate_scriptfile'] == 'default': citem['topstate_scriptfile'] = 'uptime' #执行脚本或者指令 scmd = citem['topstate_scriptfile'] sr = osaBatchLib.scriptOrCmd(scmd) if sr: try: result = osaBatchLib.runCmdOrScript(scmd) return "{'status':'OK','result':'"+result+"'}" except Exception as e: save_log('ERROR',str(e)) result = 'Cmd or Script is Faild!' return "{'status':'ERROR','result':'"+result+"'}" return "{'status':'OK','result':'OK'}" else: return "{'status':'ERROR','result':'Unknow error!'}"
def batchDiskCheck(rev): ''' @磁盘空间检查 ''' #判断是否选择了脚本操作 citem = osaBatchLib.getConfigItem(rev) devlist = osaDiskLib.disk_partitions() rlist = [] if citem['unit'] == 'MB': for devpath in devlist: info = osaDiskLib.disk_usage(devpath[1]) if int(info[1] / 1024 / 1024) >= int(citem['diskspace_threshold']): rlist.append(devpath[1]) elif citem['unit'] == '%': for devpath in devlist: info = osaDiskLib.disk_usage(devpath[1]) if int(info[3]) >= int(citem['diskspace_threshold']): rlist.append(devpath[1]) if rlist: return "{'status':'ERROR','result':'disk stat more than diskspace_threshold!detail list:" + str( rlist) + "'}" else: return "{'status':'OK','result':'disk stat is normal!'}"
def batchAppInstalltion(rev): ''' 安装执行函数,实为调用一个shell脚本 ''' value = osaBatchLib.getConfigItem(rev) script=value['install_scriptfile'] if script: try: result = osaBatchLib.runCmdOrScript(script) return "{'status':'OK','result':'"+result+"'}" except Exception ,e: save_log('ERROR',str(e)) result = 'Install Script run failed!' return "{'status':'ERROR','result':'"+result+"'}"
def batchConfigBackup(rev): ''' @配置文件备份 ''' #源文件 citem = osaBatchLib.getConfigItem(rev) sfilelist = citem['config_backup_sourcefile'].split('|') for sfile in sfilelist: sname = os.path.basename(sfile) #文件保存目标位置 dfile = citem['config_backup_dir'] if citem['config_backup_rule'] == '1' or citem[ 'config_backup_rule'] == 1: if dfile.endswith(os.sep): dpath = dfile + sname + '.bak' else: dpath = dfile + os.sep + sname + '.bak' else: if dfile.endswith(os.sep): dpath = dfile + sname + '.bak.' + str( time.strftime('%Y-%m-%d_%H:%M:%S', time.localtime())) else: dpath = dfile + os.sep + sname + '.bak.' + str( time.strftime('%Y-%m-%d_%H:%M:%S', time.localtime())) if not os.path.exists(dfile): try: os.makedirs(dfile) except Exception as e: save_log('ERROR', 'mkdir error:' + str(e)) return "{'status':'ERROR','result':'x0052,mkdir error.'}" try: shutil.copyfile(sfile, dpath) except Exception as e: save_log('ERROR', 'copy file error:' + str(e)) return "{'status':'ERROR','result':'x0051,copy file error.'}" return "{'status':'OK','result':'config backup succeed!'}"
def batchConfigBackup(rev): ''' @配置文件备份 ''' #源文件 citem = osaBatchLib.getConfigItem(rev) sfilelist = citem['config_backup_sourcefile'].split('|') for sfile in sfilelist: sname = os.path.basename(sfile) #文件保存目标位置 dfile = citem['config_backup_dir'] if citem['config_backup_rule'] == '1' or citem['config_backup_rule'] == 1 : if dfile.endswith(os.sep): dpath = dfile + sname + '.bak' else: dpath = dfile + os.sep + sname + '.bak' else: if dfile.endswith(os.sep): dpath = dfile + sname + '.bak.' + str(time.strftime('%Y-%m-%d_%H:%M:%S', time.localtime())) else: dpath = dfile + os.sep + sname + '.bak.' + str(time.strftime('%Y-%m-%d_%H:%M:%S', time.localtime())) if not os.path.exists(dfile): try: os.makedirs(dfile) except Exception as e: save_log('ERROR','mkdir error:'+str(e)) return "{'status':'ERROR','result':'x0052,mkdir error.'}" try: shutil.copyfile(sfile,dpath) except Exception as e: save_log('ERROR','copy file error:'+str(e)) return "{'status':'ERROR','result':'x0051,copy file error.'}" return "{'status':'OK','result':'config backup succeed!'}"
def batchFileClean(rev): ''' @文件清理函数 ''' #需要清理的目录目录 citem = osaBatchLib.getConfigItem(rev) dfile = citem['cleaner_sourcefile'] if not os.path.isdir(dfile): return "{'status':'ERROR','result':'x0032'}" #文件被移动的位置 mpath = citem['cleaner_targetpath'] if not os.path.isdir(mpath): mpath = '/dev/null' #操作高级选项 advance = citem['cleaner_advance'] #执行清理 if advance == 'rm_dir': re = osaSysFunc.removeall(dfile) if re == False : result = "{'status':'ERROR','result':'x0031'}" else: result = "{'status':'OK','result':'clear over!'}" return result hlist = advance.split(',') for ftype in hlist: re = osaSysFunc.mvfile(dfile,mpath,(ftype.replace('.','\\.'))) if re == False: return "{'status':'ERROR','result':'x0033'}" return "{'status':'OK','result':'clear over!'}"
def batchFileClean(rev): ''' @文件清理函数 ''' #需要清理的目录目录 citem = osaBatchLib.getConfigItem(rev) dfile = citem['cleaner_sourcefile'] if not os.path.isdir(dfile): return "{'status':'ERROR','result':'x0032'}" #文件被移动的位置 mpath = citem['cleaner_targetpath'] if not os.path.isdir(mpath): mpath = '/dev/null' #操作高级选项 advance = citem['cleaner_advance'] #执行清理 if advance == 'rm_dir': re = osaSysFunc.removeall(dfile) if re == False: result = "{'status':'ERROR','result':'x0031'}" else: result = "{'status':'OK','result':'clear over!'}" return result hlist = advance.split(',') for ftype in hlist: re = osaSysFunc.mvfile(dfile, mpath, (ftype.replace('.', '\\.'))) if re == False: return "{'status':'ERROR','result':'x0033'}" return "{'status':'OK','result':'clear over!'}"
def batchFileRecv(rev): ''' @文件接收函数 ''' #文件保存目标位置 citem = osaBatchLib.getConfigItem(rev) dfile = citem['targetpath'] #文件名 dfilename = os.path.basename(dfile) #文件目录 fdir = os.path.dirname(dfile) #目标目录不存在则创建 if not os.path.exists(fdir): try: os.makedirs(fdir) except Exception as e: save_log('ERROR', 'mkdir is error:' + str(e)) return "{'status':'ERROR','result':'x0027'}" #端口号 fileport = osaBatchLib.getConfigPort(rev) #临时文件 tempfile = osaBatchLib.tempFilePath(rev) #开始接收文件 try: fsize = file_recv_main(host='0.0.0.0', port=int(fileport), filename=tempfile) except Exception as e: save_log('ERROR', 'file recv error:' + str(e)) return "{'status':'ERROR','result':'x0026'}" if fsize: #对文件进行处理 advance = citem['advance'].split('|') backfile = dfile + '.bak.' + str( time.strftime('%Y-%m-%d_%H:%M:%S', time.localtime())) if advance[0] == 'cut': if os.path.exists(dfile): try: os.rename(dfile, backfile) os.rename(tempfile, dfile) except Exception as e: save_log('ERROR', str(e)) return "{'status':'ERROR','result':'x0022'}" else: try: os.rename(tempfile, dfile) except: shutil.copy(tempfile, dfile) elif advance[0] == 'copy': if os.path.exists(dfile): try: os.rename(dfile, backfile) os.rename(tempfile, dfile) os.remove(backfile) except Exception as e: save_log('ERROR', str(e)) return "{'status':'ERROR','result':'x0023'}" else: try: os.rename(tempfile, dfile) except: shutil.copy(tempfile, dfile) else: return "{'status':'ERROR','result':'x0021'}" if advance[1] == 'document_integrity': dict = osaBatchLib.revToDict(rev) if dict['md5'] != osaBatchLib.md5sum(dfile): return "{'status':'ERROR','result':'x0024'}" #执行脚本或者指令 scmd = citem['distribution_script'] sr = osaBatchLib.scriptOrCmd(scmd) if sr: try: result = osaBatchLib.runCmdOrScript(scmd) return "{'status':'OK','result':'" + result + "'}" except Exception as e: save_log('ERROR', str(e)) result = 'Cmd or Script is Faild!' return "{'status':'ERROR','result':'" + result + "'}" return "{'status':'OK','result':'OK'}" return "{'status':'ERROR','result':'x0025'}"
def batchFileRecv(rev): ''' @文件接收函数 ''' #文件保存目标位置 citem = osaBatchLib.getConfigItem(rev) dfile = citem['targetpath'] #文件名 dfilename = os.path.basename(dfile) #文件目录 fdir = os.path.dirname(dfile) #目标目录不存在则创建 if not os.path.exists(fdir): try: os.makedirs(fdir) except Exception as e: save_log('ERROR','mkdir is error:'+str(e)) return "{'status':'ERROR','result':'x0027'}" #端口号 fileport = osaBatchLib.getConfigPort(rev) #临时文件 tempfile = osaBatchLib.tempFilePath(rev) #开始接收文件 try: fsize = file_recv_main(host='0.0.0.0',port=int(fileport),filename = tempfile) except Exception as e: save_log('ERROR','file recv error:'+str(e)) return "{'status':'ERROR','result':'x0026'}" if fsize: #对文件进行处理 advance = citem['advance'].split('|') backfile = dfile+'.bak.'+str(time.strftime('%Y-%m-%d_%H:%M:%S', time.localtime())) if advance[0] == 'cut': if os.path.exists(dfile): try: os.rename(dfile,backfile) os.rename(tempfile,dfile) except Exception as e: save_log('ERROR',str(e)) return "{'status':'ERROR','result':'x0022'}" else: try: os.rename(tempfile,dfile) except: shutil.copy(tempfile,dfile) elif advance[0] == 'copy': if os.path.exists(dfile): try: os.rename(dfile,backfile) os.rename(tempfile,dfile) os.remove(backfile) except Exception as e: save_log('ERROR',str(e)) return "{'status':'ERROR','result':'x0023'}" else: try: os.rename(tempfile,dfile) except: shutil.copy(tempfile,dfile) else: return "{'status':'ERROR','result':'x0021'}" if advance[1] == 'document_integrity': dict = osaBatchLib.revToDict(rev) if dict['md5'] != osaBatchLib.md5sum(dfile): return "{'status':'ERROR','result':'x0024'}" #执行脚本或者指令 scmd = citem['distribution_script'] sr = osaBatchLib.scriptOrCmd(scmd) if sr: try: result = osaBatchLib.runCmdOrScript(scmd) return "{'status':'OK','result':'"+result+"'}" except Exception as e: save_log('ERROR',str(e)) result = 'Cmd or Script is Faild!' return "{'status':'ERROR','result':'"+result+"'}" return "{'status':'OK','result':'OK'}" return "{'status':'ERROR','result':'x0025'}"