#!/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)
NowYear = time.localtime()[0]
php_log_file = "/wwwlocalfolder/logs/php/php_errors.log"

def check_php(php_log_file,type):
	try:
		if os.path.exists(r'%s'%php_log_file):
			if type == "error":
				status,result = commands.getstatusoutput("""cat %s |grep -ni "error"  | grep "%s %s:" """ %(php_log_file,NowYear,h))
				if result.count('error') !=0:
					count=result.count('error')
					print count
					s,r = commands.getstatusoutput("""/usr/bin/python /python_check/SendMail.py "错误 有%s处错误\n%s" """ %(count,result))
			elif type == "warning":
				status,result = commands.getstatusoutput("""cat %s |grep -ni "Warning" | grep "%s %s:" """ %(php_log_file,NowYear,h))
				if result.count('Warning') != 0:
					count=result.count('Warning')
					s1,r1 = commands.getstatusoutput("""/usr/bin/python /python_check/SendMail.py "警告 有%s处错误\n%s" """ %(count,result))
		else:	
			print "没有错误文件"
	except Exception,e:
		s2,r2 = commands.getstatusoutput("""/usr/bin/python /python_check/SendMail.py "%s脚本出错 %s" """ %(sys.argv[0],e))

check_php(php_log_file,'error')
check_php(php_log_file,'warning')