python php报警信息,python检查php错误日志和警告日志并发送邮件

#!/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')

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值