def stop(self, section_id=None): itemconf = self._chp.get_section_dict(section_id) appname = itemconf.get("appname", None) stop_cmd = itemconf.get("stop_cmd", None) self._logger.dictlog(level="info", width=20, **itemconf) scolor.info(u"#### [%s] 停止应用【%s】 ####" % (sdate().datetime_human(), appname)) if str(stop_cmd).strip() == None: scolor.warn(u"应用【%s】停止命令为空" % (appname)) return c2 = cmds(stop_cmd) stdo = c2.stdo() stde = c2.stde() retcode = c2.code() #exeop = str("%s\n%s" % (stdo,stde)).strip() if retcode == 0 and str(stde).strip() == "": scolor.info(u"停止应用【%s】成功!" % (appname)) else: scolor.error(u"停止应用【%s】失败!" % (appname)) logobj = { u"命令": stop_cmd, u"标准输出": stdo, u"错误输出": stde, u"返回码": retcode, u"orders": [u"命令", u"标准输出", u"错误输出", u"返回码"] } self._logger.dictlog(level="info", width=12, **logobj)
def is_process_exist(self, process_uniq_flag): ## check process if not process_uniq_flag or str(process_uniq_flag).strip() == "": return True, None, None, None, None proc_flag_ary = re.split("[ ,;]+", str(process_uniq_flag).strip()) grep_str = "" for proc_chk_str in proc_flag_ary: if str(proc_chk_str).strip() == None: continue grep_str += "grep -E '%s' | " % (proc_chk_str) grep_str = str(grep_str).strip()[:-1].strip() self_script_name = sys.argv[0] cmdstr = "ps -ef | %s | grep -v grep | grep -v '%s' | wc -l" % ( grep_str, self_script_name) c1 = cmds(cmdstr) stdo, stde, retcode = c1.stdo(), c1.stde(), c1.code() logdict = { "命令": cmdstr, "标准输出": stdo, "标准错误输出": stde, "返回码": retcode, "orders": ["命令", "标准输出", "标准错误输出", "返回码", "检测结果"], } self._logger.dictlog(level="info", width=10, **logdict) if retcode == 0 and int(str(stdo).strip()) > 0: return (True, cmdstr, stdo, stde, retcode) return (False, cmdstr, stdo, stde, retcode)
def is_port_exist(self, process_port): if not process_port or str(process_port).strip() == "": return (True, None, None, None, None) cmdstr = "ss -anlt | grep ':%s ' | wc -l " % ( str(process_port).strip()) c1 = cmds(cmdstr) stdo, stde, retcode = c1.stdo(), c1.stde(), c1.code() logdict = { "命令": cmdstr, "标准输出": stdo, "标准错误输出": stde, "返回码": retcode, "orders": ["命令", "标准输出", "标准错误输出", "返回码", "检测结果"], } self._logger.dictlog(level="info", width=10, **logdict) if retcode == 0 and int(str(stdo).strip()) > 0: return (True, cmdstr, stdo, stde, retcode) return (False, cmdstr, stdo, stde, retcode)
def status(self, section_id=None): itemconf = self._chp.get_section_dict(section_id) appname = itemconf.get("appname", None) process_uniq_flag = itemconf.get("process_uniq_flag", None) process_port = itemconf.get("process_port", None) status_cmd = itemconf.get("status_cmd", None) self._logger.dictlog(level="info", width=20, **itemconf) scolor.info(u"#### [%s] 检测应用【%s】状态 ####" % (sdate().datetime_human(), appname)) if status_cmd and str(status_cmd).strip(): c2 = cmds(status_cmd, timeout=10) stdo, stde, retcode = c2.stdo(), c2.stde(), c2.code() logdict = { "命令": status_cmd, "标准输出": stdo, "标准错误输出": stde, "返回码": retcode, "orders": ["命令", "标准输出", "标准错误输出", "返回码"], } self._logger.dictlog(level="info", width=10, **logdict) scolor.info(u"-> 根据用户指定的状态命令检测应用状态") print " 命令:%s" % (status_cmd) print "执行结果:" print "=========" print str(str(stdo).strip() + "\n" + str(stde).strip()).strip() if process_uniq_flag and str(process_uniq_flag).strip(): (flag, cmdstr, stdo, stde, retcode) = self.is_process_exist(process_uniq_flag) scolor.info(u"-> 根据应用进程唯一标识检测") print " 命令:%s" % (cmdstr) print "执行结果:" print "=========" print str(str(stdo).strip() + "\n" + str(stde).strip()).strip() if process_port: (flag, cmdstr, stdo, stde, retcode) = self.is_port_exist(process_port) scolor.info(u"-> 根据应用进程端口检测") print " 命令:%s" % (cmdstr) print "执行结果:" print "=========" print str(str(stdo).strip() + "\n" + str(stde).strip()).strip()
def is_check_ok(self, check_cmd, check_str): if not check_cmd or not check_str or str( check_cmd).strip() == "" or str(check_str).strip() == "": return True, None, None, None, None c1 = cmds(check_cmd) stdo, stde, retcode = c1.stdo(), c1.stde(), c1.code() logdict = { "命令": check_cmd, "标准输出": stdo, "标准错误输出": stde, "返回码": retcode, "检测结果": "OK" if retcode == 0 and str(stdo).strip() == str(check_str) else "BAD", "orders": ["命令", "标准输出", "标准错误输出", "返回码", "检测结果"], } self._logger.dictlog(level="info", width=10, **logdict) if retcode == 0 and str(stdo).strip() == str(check_str): return (True, check_cmd, stdo, stde, retcode) return (False, check_cmd, stdo, stde, retcode)
def backup_one(conffile, input_section, debug=False): bc = conf_helper(conffile) itemconf = bc.get_section_dict(input_section) srcdir = itemconf.get("srcdir", None) destdir = itemconf.get("destdir", None) brange = itemconf.get("range", None) files = itemconf.get("files", None) logger = slog("/tmp/backup.log", debug=debug) if bc.has_section(input_section) == False: scolor.info("备份标识不存在: %s" % (input_section)) return logobj = { u"备份标识": input_section, u"备份目录": srcdir, u"存储目录": destdir, u"范围(包含or不包含)": brange, u"文件(夹)列表": files, u"orders": [u"section", u"备份目录", u"存储目录", u"范围(包含or不包含)"] } logger.dictlog(level="info", width=20, **logobj) scolor.info(u"尝试执行备份任务,备份信息如下: ") scolor.info(u"===============================") print sstr().dictstr(width=20, **logobj) try: ## src dir srcdir = str(srcdir).strip() while '//' in str(srcdir): srcdir = str(srcdir).replace('//', '/') while str(srcdir).endswith('/'): srcdir = srcdir[:-1] pos = str(srcdir).rfind('/') dirroot = srcdir[:pos] ## dirname = srcdir[pos + 1:] ## relative dir name ## dest dir destdir = str(destdir).strip() while '//' in str(destdir): destdir = str(destdir).replace('//', '/') while str(destdir).endswith('/') and len(destdir) > 2: destdir = destdir[:-1] if os.path.exists(srcdir) == False or os.path.exists(destdir) == False: scolor.error(u"=> 备份任务") infobj = { u"状态": u"失败", u"错误": u"备份目录或存储目录不存在", u"备份目录": srcdir, u"存储目录": destdir, u"orders": [u"状态", u"错误", u"备份目录", u"存储目录"] } scolor.error(sstr().dictstr(width=8, **infobj)) return if str(brange).lower().strip() not in ["include", "exclude"]: scolor.error(u"=> 备份任务") infobj = { u"状态": u"失败", u"错误": u"备份范围错误,只能是include或exclude", u"orders": [u"状态", u"错误"] } scolor.error(sstr().dictstr(width=4, **infobj)) return ## include or exclude files files = str(files).strip() files_ary = re.split("[ |,|;]+", files) last_files = [] if files_ary: for each_file in files_ary: if not each_file: continue while '//' in str(each_file): each_file = str(each_file).replace('//', '/') while str(each_file).endswith('/'): each_file = each_file[:-1] while str(each_file).startswith('/'): each_file = each_file[1:] each_file = each_file.replace(" ", "") last_files.append(each_file) nowdt = sdate().datetime() nd = sdate().date() tarname = "%s_%s.tar.gz " % (dirname, nowdt) tmpfile = "/tmp/%s" % (tarname) last_destdir = "%s/%s/" % (destdir, nd) subffix = "" tar_cmdstr = "" if str(brange).lower().strip() == "include": for ef in last_files: subffix = subffix + dirname + "/" + ef + " " if str(subffix).strip() == "": ## 备份整个目录 tar_cmdstr = "cd %s && tar czf %s %s" % (dirroot, tmpfile, dirname) else: ## 备份指定的目录 tar_cmdstr = "cd %s && tar czf %s %s" % (dirroot, tmpfile, subffix) if str(brange).lower().strip() == "exclude": for ef in last_files: subffix = subffix + "--exclude=" + dirname + "/" + ef + " " tar_cmdstr = "cd %s && tar czf %s %s %s" % (dirroot, tmpfile, dirname, subffix) c1 = cmds(tar_cmdstr, timeout=1800) stdo1 = c1.stdo() stde1 = c1.stde() retcode1 = c1.code() logobj = { u"命令": tar_cmdstr, u"标准输出": stdo1, u"错误输出": stde1, u"返回码": retcode1, u"orders": [u"命令", u"标准输出", u"错误输出", u"返回码"] } logger.dictlog(level="info", width=12, **logobj) ## 打包归档失败 if retcode1 != 0: scolor.error(u"=> 备份任务") infobj = { u"状态": u"失败", u"错误": u"打包归档失败,具体:%s" % (stde1), u"orders": [u"状态", u"错误"] } scolor.error(sstr().dictstr(width=4, **infobj)) return #cmdstr = "mkdir -p %s && mv %s %s && rm -frv %s" % (last_destdir, tmpfile, last_destdir, tmpfile) cmdstr = "mkdir -p %s && mv -v %s %s" % (last_destdir, tmpfile, last_destdir) c2 = cmds(cmdstr) stdo2 = c2.stdo() stde2 = c2.stde() retcode2 = c2.code() logobj = { u"命令": cmdstr, u"标准输出": stdo2, u"错误输出": stde2, u"返回码": retcode2, u"orders": [u"命令", u"标准输出", u"错误输出", u"返回码"] } logger.dictlog(level="info", width=12, **logobj) if retcode2 != 0: scolor.error(u"=> 备份任务") infobj = { u"状态": u"失败", u"错误": u"创建目录或移动文件失败,具体:%s" % (stde1), u"orders": [u"状态", u"错误"] } scolor.error(sstr().dictstr(width=4, **infobj)) return scolor.info(u"=> 备份任务") infobj = { u"状态": u"成功", u"原目录": srcdir, u"备份位置": "%s%s" % (last_destdir, tarname), u"orders": [u"状态", u"原目录"] } scolor.info(sstr().dictstr(width=8, **infobj)) except: print traceback.format_exc()
def run(self, sid): try: conf = self.get_backup_conf(sid) if not conf: return False srcfiles = conf.get("srcfiles", "") destdir = conf.get("destdir", "") excludes = conf.get("excludes", "") zip_flag = conf.get("zip", "") keep = conf.get("keep", "") timeout = int(conf.get("timeout", 0)) srcfiles = srcfiles.strip() destdir = destdir.strip() if srcfiles == "" or destdir == "": msg = u"srcfiles:{0} or destdir:{1} is empty.".format( srcfiles, destdir) print msg self._logger.warn(msg) return False file_ln = re.split("[,|;|\s]+", srcfiles) if not file_ln: return False auto_destdir = os.path.join(destdir, sdate().datetime_ex()) if not os.path.exists(destdir): os.makedirs(destdir) os.makedirs(auto_destdir) for (idx, f) in enumerate(file_ln): f = unicode(f.strip(), "utf-8") if not f: continue if not os.path.exists(f): msg = u"File Not Exist. File: {0}".format(f) print msg self._logger.warn(msg) continue cmdstr = self.oh(f, auto_destdir, excludes) print u"[FILE-{0}]".format(idx + 1) print u" FROM: {0}".format(f) print u" TO: {0}".format(auto_destdir) print u" KEEP: {0}".format(keep) print u" ZIP: {0}".format(zip_flag) #print u"CMDSTR: {0}".format(cmdstr) if sys.platform == 'win32': ## 解决中文路径引起的错误 cmdstr = cmdstr.encode("gbk") command = cmds(cmdstr, timeout=timeout) (stdo, stde, retcode) = (command.stdo(), command.stde(), command.code()) logdict = { "cmdstr": get_right_content(cmdstr), "stdo": stdo, "stde": stde, "retcode": retcode, } self._logger.dictlog(level="info", **logdict) if retcode == 0: print(u"=> backup result: SUCCESS") if zip_flag.strip() == "1": directory = os.path.join(auto_destdir, f.split(os.path.sep)[-1]) tarflag = self.tar_gz(directory) if tarflag == True: print(u"=> tar result: SUCCESS") self.delete(directory) else: print(u"=> tar result: SUCCESS") else: print(u"=> backup result: FAILURE") ## sort dir desc by ctime dirs = self.sort_file(destdir) keep = int(str(keep).strip()) if keep <= 0: return ## delete old version del_dirs = dirs[keep - 1:] for d in del_dirs: del_path = os.path.join(destdir, d) self.delete(del_path) print u"=> Delete: {0}".format(del_path) except: tb = traceback.format_exc() self._logger.error(tb)