返回顶部

收藏

服务器日志清理备份

更多
#! /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,file,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")

标签:python

收藏

0人收藏

支持

0

反对

0

»更多 您可能感兴趣的代码
  1. 2018-11-09 20:49:27Python爬虫暴漫gif下载 by 张宋付
  2. 2018-11-09 20:32:44使用requests登录V2EX by wubai
  3. 2018-11-09 20:16:01python多线程多队列 by 大猪
  4. 2018-11-09 20:01:34python通过正则获取网页上的全部链接 by 山药
  5. 2018-11-09 19:45:20python socket 轻量级服务器 by wubai
  6. 2018-11-09 19:31:00网络爬虫 by 山药
  7. 2018-11-09 19:19:49Argparse by 张宋付
  8. 2018-11-09 19:01:17python实现的Caesar加解密算法 by 风云轩
  9. 2018-11-09 18:52:21Python获取Windows的CPU数量 by 蟋蟀MM
  10. 2018-11-09 18:38:51python 判断文件、文件夹是否存在 by 山药
  11. 2018-11-09 21:11:17服务器日志清理备份 by 蟋蟀MM

发表评论