apacheにエラーがあることを確認してメールを送信します

2492 ワード

#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import commands,time,os,sys
t=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() - 3600
timeArray = 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 
%s" """%(count,ProjectName,result)) else: ProjectName = apache_log_file.split('/')[5] s2,r2 = commands.getstatusoutput("""/usr/bin/python /python_check/SendMail.py " %s   :%s 
%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))