#!/usr/bin/env python# -*- coding: UTF-8 -*-import commands,time,os,syst=time.strftime('%Y%m%d',time.localtime(time.time()))t1=time.strftime('%Y-%m-%d',time.localtime(time.time()))Time=t1.split('-')[0]+ t1.split('-')[1]+t1.split('-')[2]day=t1.split('-')[2]timeStamp=time.time() - 3600timeArray = time.localtime(timeStamp)h= time.strftime("%H",timeArray)apache_dir_list = ['/wwwlocalfolder/logs/apache/','/wwwlocalfolder/logs/vhosts/hiwork_thor_v10/abr6/','/wwwlocalfolder/logs/vhosts/hiwork_thor_v10/keepmesp/','/wwwlocalfolder/logs/vhosts/hiwork_thor_v10/loveizhifu/']def check_apache(apache_log_file,day,h): try: L=len(apache_log_file.split('/')) if os.path.exists(r'%s'%apache_log_file): print """cat %s |grep -i "error" | grep "%s %s:" """ %(apache_log_file,day,h) status,result = commands.getstatusoutput("""/bin/cat %s | /bin/grep -i "error" | /bin/grep "%s %s:" """ %(apache_log_file,day,h)) print result if result.count('error') !=0: count=result.count('error') if L == 6: ProjectName = apache_log_file.split('/')[3] print ProjectName s1,r1 = commands.getstatusoutput("""/usr/bin/python /python_check/SendMail.py "有%s处错误 项目名:%s \n%s" """%(count,ProjectName,result)) else: ProjectName = apache_log_file.split('/')[5] s2,r2 = commands.getstatusoutput("""/usr/bin/python /python_check/SendMail.py "有%s处错误 项目名:%s \n%s" """%(count,ProjectName,result)) else: print "没有错误文件" except Exception, e: s3,r3 = commands.getstatusoutput("""/usr/bin/python /python_check/SendMail.py "脚本出错%s %s %s " """%(sys.argv[0],e.args[0],e.args[1]))try: for i in apache_dir_list: if h == 23: riqi=time.strftime('%Y%m%d',time.localtime(time.time()-24*60*60)) day=time.strftime('%d',time.localtime(time.time()-24*60*60)) dir = "%s%s/error_%s.log"%(i,riqi,riqi) check_apache(dir,day,h) else: dir = "%s%s/error_%s.log"%(i,Time,Time) print dir check_apache(dir,day,h)except Exception,e: s4,r4 = commands.getstatusoutput("""/usr/bin/python /python_check/SendMail.py "%s脚本出错 %s" """%(sys.argv[0],e))