def __init__(self, monitor_conf, conffile, name=None, logpath="/tmp/service_available_monitor.log", debug=True): self._logger = slog(logpath, debug=debug) self._monitor_conf = monitor_conf self._conffile = conffile self._name = name
def __init__(self, username, password, smtp_server, port=25, **kwargs): kwargs = dict(kwargs) logpath = kwargs.get("logpath", None) debug = kwargs.get("debug", False) self._app = kwargs.get("app", "") self._username = username self._password = password self._smtp_server = smtp_server self._port = port if port else 25 self._is_login = False self._smtp = None self._login() self._logger = None if logpath: self._logger = slog(logpath, debug=debug)
def __init__(self, username, password, smtp_server, port=25, **kwargs): kwargs = dict(kwargs) logpath = kwargs.get("logpath", None) debug=kwargs.get("debug", False) self._app = kwargs.get("app" ,"") self._username = username self._password = password self._smtp_server = smtp_server self._port = port if port else 25 self._is_login = False self._smtp = None self._login() self._logger = None if logpath: self._logger = slog(logpath, debug=debug)
def __init__(self, conffile, logfile, debug=False): self._conffile = conffile key_map = { "func": u"功能说明", "srcfiles": u"备份目录(文件)", "destdir": u"存储目录", "excludes": u"排除目录(文件)", "zip": u"是否压缩", "keep": "保留份数" } key_order = ["func", "srcfiles", "destdir", "excludes", "zip", "keep"] self._conf_instance = BUp(conffile, key_map=key_map, key_check=None, key_order=key_order, max_key_len=20) self._logger = slog(logfile, debug=debug)
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 __init__(self, chp=None, debug=False): self._chp = chp ## conf_helper instance self._debug = debug self._logger = slog("/tmp/opsctl.log", debug=debug)