龙空技术网

PHP慢日志分析生成报告「python脚本」

运维蚂蚁 116

前言:

此时朋友们对“php生成txt”大体比较着重,我们都需要了解一些“php生成txt”的相关内容。那么小编也在网摘上收集了一些关于“php生成txt””的相关资讯,希望同学们能喜欢,看官们一起来了解一下吧!

#!/usr/bin/python#coding=utf-8import hashlibimport os,sys,re,time,socket,commandsimport smtplibfrom email.MIMEText import MIMETextfrom email.Header import Headerlog_file="/data/logs/backup/;host=str(socket.gethostname().strip())fuhao=os.linesep#分析一周的php慢日志def LogPool():    dd=time.strftime('%Y%m%d')    www_log_slow_pool=open('/tmp/www_log_slow00.txt','w')    for num in range(1,8):        zz=int(dd)-int(num)        print '%s-%d' %(log_file,zz)        log_files=open('%s-%d' %(log_file,zz))        for a in log_files:            www_log_slow_pool.write(a)        log_files.close()    www_log_slow_pool.close()def LogClear():    os.system("cat /tmp/www_log_slow00.txt |grep -v runtime|grep -v 'pool www'|grep -v unknown|grep -v dump |awk  '{print $3}' >/tmp/www_log_slow01.txt")    os.system("sed -i '1'd /tmp/www_log_slow01.txt")    f=open('/tmp/www_log_slow01.txt')    file=open('/tmp/www_log_slow02.txt','w')    for line in f:        aa=line.strip()        len_char=len(aa)        if len_char != 0:            file.write(aa)        else:            file.write(fuhao)            continue    f.close()    file.close()    commands.getstatusoutput("cat /tmp/www_log_slow02.txt |sort |uniq -c|sort -rn|head -n 20 >/tmp/www_log_slow03.txt")    file01=open('/tmp/www_log_slow03.txt')    phpslow_name='/tmp/php_slow.txt'    file02=open(phpslow_name,'w')    for xx in file01:        url=re.split(r"/data/",xx)        row=re.findall(r"/data/",xx)        sum=int(len(row))        for i in xrange(sum):            mm=str(row[i])+url[i]            if i == 0:                mm="以下慢日志重复次数:"+url[i]            file02.write(mm)            file02.write(fuhao)    file01.close()    file02.close()def LogRemove():    for logsum in range(4):        rmfile='/tmp/www_log_slow0%d.txt'%logsum        os.remove(rmfile)   def SendMail():    mail_host='smtp.ftxjoy.com'    mail_user='mayi@163.com'    mail_pass='*******'    sender='mayi@163.com'    rece=['ceshi@163.com','yanfa@163.com']         file=open('/tmp/php_slow.txt')    msg=''    for line in file:         msg = msg+line    file.close()    message=MIMEText(msg)    message['subject']=Header("php_slow(182.5)",'utf-8')    try:        smtpObj = smtplib.SMTP()         smtpObj.connect(mail_host, 25)        smtpObj.login(mail_user,mail_pass)          smtpObj.sendmail(sender, rece, message.as_string())        print "邮件发送成功"    except smtplib.SMTPException:        print "Error: 无法发送邮件"LogPool()LogClear()LogRemove()SendMail()

标签: #php生成txt