34fw 发表于 2015-1-14 08:29:24

检查apache错误日志

#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import commands,time,os
t=time.strftime('%Y%m%d',time.localtime(time.time()))
t1=time.strftime('%Y-%m-%d',time.localtime(time.time()))
Time=t1.split('-')+ t1.split('-')+t1.split('-')
day=t1.split('-')
h=time.localtime()
apache_log_file = "/log/logs/apache/%s/error_%s.log"%(Time,Time)
apache_vhost_abr6_file = "/log/logs/vhosts/hiwork_thor_v10/abr6/%s/error_%s.log"%(Time,Time)
apache_vhost_keepmesp_file = "/log/logs/vhosts/hiwork_thor_v10/keepmesp/%s/error_%s.log"%(Time,Time)
apache_vhost_loveizhifu_file = "/log/logs/vhosts/hiwork_thor_v10/loveizhifu/%s/error_%s.log"%(Time,Time)

def check_apache(apache_log_file):
    try:
      L=len(apache_log_file.split('/'))
      if os.path.exists(r'%s'%apache_log_file):
            status,result = commands.getstatusoutput("""cat %s |grep -ni "error" | grep "%s %s:" """ %(apache_log_file,day,h))
            if result.count('error') !=0:
                count=result.count('error')
                if L == 6:
                  ProjectName = apache_log_file.split('/')
                  print ProjectName
                  s1,r1 = commands.getstatusoutput("""/usr/bin/python SendMail.py "有%s处错误 项目名:%s \n%s" """%(count,ProjectName,result))
                else:
                  ProjectName = apache_log_file.split('/')
                  s2,r2 = commands.getstatusoutput("""/usr/bin/python SendMail.py "有%s处错误 项目名:%s \n%s" """%(count,ProjectName,result))
      else:
            print "没有错误文件"
    except Exception, e:
      s2,r2 = commands.getstatusoutput("""/usr/bin/python SendMail.py "脚本出错%s %s %s " """%(sys.argv,e.args,e.args))
try:
    check_apache(apache_log_file)
    check_apache(apache_vhost_abr6_file)
    check_apache(apache_vhost_keepmesp_file)
    check_apache(apache_vhost_loveizhifu_file)
except Exception,e:
    s3,r3 = commands.getstatusoutput("""/usr/bin/python SendMail.py "%s脚本出错%s" """%(sys.argv,e))



页: [1]
查看完整版本: 检查apache错误日志