下面是编程之家 jb51.cc 通过网络收集整理的代码片段。
编程之家小编现在分享给大家,也给大家做个参考。
#! /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']) +"%\r\n"
message = message + "磁盘剩余情况:\t" + str(hd['available']/hd['capacity']) +"%\r\n"
message = message + str(hd);
sendMail("磁盘空间监控 ","服务iq:\t"+getip() + "\r\nhostname:\t" + command("hostname") + "\r\n磁盘空间不足(单位为G),明细:\r\n" + message,"***@***.cn")
以上是编程之家(jb51.cc)为你收集整理的全部代码内容,希望文章能够帮你解决所遇到的程序开发问题。
如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。
总结
以上是编程之家为你收集整理的服务器日志清理备份全部内容,希望文章能够帮你解决服务器日志清理备份所遇到的程序开发问题。
如果觉得编程之家网站内容还不错,欢迎将编程之家网站推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。