服务器日志清理备份
发布时间:2020-12-17 17:21:18 所属栏目:Python 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 #! /usr/bin/env python#coding=utf-8import osimport timeimport subprocessimport httplib2import jsonfrom mailtool import sendMailfrom datetime
以下代码由PHP站长网 52php.cn收集自互联网 现在PHP站长网小编把它分享给大家,仅供参考 #! /usr/bin/env python #coding=utf-8 import os import time import subprocess import httplib2 import json from mailtool import sendMail from datetime import datetime import tarfile def command(command): p = subprocess.Popen(command,shell=True,stdout=subprocess.PIPE,stderr=subprocess.STDOUT) result = str() for line in p.stdout.readlines(): result = result + str(line,encoding="utf-8") return result def getip(): h = httplib2.Http(".cache") resp,content = h.request("http://ip.taobao.com/service/getIpInfo2.php?ip=myip","GET") result = str(content,encoding="utf-8") result = json.loads(result) return (result["data"]["ip"]) def listfile(path): if os.path.isdir(path) and os.path.exists(path): for file in os.listdir(path): yield path,file else: sendMail("目录存在问题",getip() + " " + command("hostname") + " " + path + " 不是目录或者目录不存在","***@***.cn") print("not value path " + path) def createTarfile(path,file,logbakpath): baklogfile = logbakpath + "/" + file+".tar.gz" if os.path.exists(baklogfile) : print(str(datetime.now()) + " " + baklogfile + " alread exists") return; tar = tarfile.open(baklogfile,"w:gz") tar.add(os.path.join(path,file)) tar.close() def checklinkpath(path): if os.path.islink(path): return os.readlink(path) return path def checkpath(path): while path.endswith("/"): path = path[:-1] return path def mkBackpath(logbakpath): if not os.path.exists(logbakpath): os.makedirs(logbakpath) print("make bak dir success") def processlog(path,file): path = checklinkpath(path) path = checkpath(path) logbakpath = os.path.dirname(path) + "/logsBack" mkBackpath(logbakpath) #文件的日期为10天前的文件,压缩,备份,删除, createdatetime = datetime.fromtimestamp(os.path.getctime(path+"/"+file)) intervaldays = (datetime.now() - createdatetime).days if not intervaldays and (file.endswith(".log") or file.endswith(".out")): #当天的文件不处理 print(str(datetime.now()) + " today file not process " + path +"/" + file) return if file.startswith("."): #隐藏的文件不处理 print(str(datetime.now()) + " hidden file not process " + path +"/" + file) return if os.path.isdir(os.path.join(path,file)): print(str(datetime.now()) + " dir not process " + path +"/" + file) return print(str(datetime.now()) + " start process " + path +"/" + file) #文件不为当前的,在logsBack下面产生对应文件的备份文件,如果文件超过7天删除该文件 createTarfile(path,logbakpath) if intervaldays > 7: os.remove(os.path.join(path,file)) print(str(datetime.now()) + " del log file " + path +"/" + file) if __name__ == "__main__": ''' 处理日志文件的工具 ''' targetList = [ "/work/java_project/***/src/main/webapp/logs//","/home/***/links/***logs" ] try: for path in targetList: for filepath,filename in listfile(path): processlog(filepath,filename) print("process path " + path + " success!") except Exception as e: sendMail("处理文件异常 ",getip() + " " + command("hostname") + " 处理文件存在异常" + e,"***@***.cn") #check disk disk = os.statvfs("/") hd = {} hd['available'] = disk.f_bsize * disk.f_bavail/(1024**3) hd['capacity'] = disk.f_bsize * disk.f_blocks/(1024**3) hd['used'] = disk.f_bsize * disk.f_bfree/(1024**3) if hd['used']/hd['capacity'] > 0.80: message = "磁盘已使用情况:t" + str(hd['used']/hd['capacity']) +"%rn" message = message + "磁盘剩余情况:t" + str(hd['available']/hd['capacity']) +"%rn" message = message + str(hd); sendMail("磁盘空间监控 ","服务iq:t"+getip() + "rnhostname:t" + command("hostname") + "rn磁盘空间不足(单位为G),明细:rn" + message,"***@***.cn") 以上内容由PHP站长网【52php.cn】收集整理供大家参考研究 如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |