博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
写一个欢迎页node统计接口Py脚本(邮件,附件)-py
阅读量:4351 次
发布时间:2019-06-07

本文共 13310 字,大约阅读时间需要 44 分钟。

#!/usr/bin/env python# -*- coding:UTF-8 -*-#encoding: utf-8  from __future__ import divisionimport sys     reload(sys)sys.setdefaultencoding('utf8')import os,sys,reimport timeimport smtplibfrom email.mime.text import MIMETextfrom email.mime.multipart import MIMEMultipartimport email.MIMEBase import xlsxwriterimport globHOST="172.16.xx"logTime = time.strftime('%Y-%m-%d',time.localtime(time.time()-24*60*60))SUBJECT=u'欢迎页接口统计'+logTimeFROM="abc@139.com"TO="zhouguixiao@rich.cn"logName = ['/logs/surround/nodejs_module/172.16.182.x/app*-'+logTime,'/logs/surround/nodejs_module/172.16.182.x/app*-'+logTime]psDic = {'XXXX':'查询邮箱分区信息','XXXXX':'登录RM','NNNNNNN'}statisDic = {}statisDic1 = {}statisDic2 = {}if os.path.isfile("/tmp/login_intreface_"+logTime+".xlsx"):        os.remove("/tmp/login_intreface_"+logTime+".xlsx")workbook = xlsxwriter.Workbook("/tmp/login_intreface_"+logTime+".xlsx")worksheet = workbook.add_worksheet()bold = workbook.add_format({'bold': 1})bold.set_bg_color('#cccccc')bold.set_border(1)format = workbook.add_format()format.set_border(1)format.set_bold()headings = ['接口名称','接口说明','访问总量','TOUT','0~50毫秒(%)','51~100毫秒(%)','101~150毫秒(%)','151~200毫秒(%)','201~300毫秒(%)','300~500毫秒(%)','500~1000毫秒(%)','>1000毫秒(%)']data=[]data_status=[]worksheet.write_row('A1', headings, bold)for logglob in logName:        log=glob.glob(logglob)        for logg in log:            monitLog = open(logg,'r')            print logg            while True:                lines = monitLog.readlines(5000)                if not lines:                        break                for line in lines:                        fields = line.split('|')                        if len(fields) > 5:                                if len(fields)>5:                                        if re.search(r'HTTPERROR',line):                                            try:                                                #STATUS=fields[7].split(r'=')[1]                                                #Url=fields[6][9:fields[6].index(r'?')]                                                STATUS=fields[7].split('=')[1].replace('\n','')                                                print ("URL=%s status=%s**"%(Url,STATUS))                                                if re.search(r'\?func',fields[6]):                                                    #Url=Url.split(r'=')[1]                                                    Url=fields[6][10:fields[6].index(r'&')]                                                else:                                                    Url=fields[6][10:fields[6].index(r'?')]                                                print "---Url="+Url+" STATUS="+STATUS                                                if Url in psDic.keys():                                                    Url_value=psDic[Url]                                                else:                                                    Url_value="未知接口"                                                statisDic[Url]['total'] += 1                                                STATUS="".join(STATUS.split())                                                if STATUS == "undefined":                                                    statisDic[Url]['sother'] +=1                                                    print "-----sother="+STATUS                                                    statisDic[Url]['stotal'] +=1                                                    continue                                                if   STATUS == "200":                                                    statisDic[Url]['s200'] +=1                                                    print "-----s200r="+STATUS                                                elif STATUS == "500":                                                    print "-----s500r="+STATUS                                                    statisDic[Url]['s500'] +=1                                                elif STATUS == "502":                                                    statisDic[Url]['s502'] +=1                                                    print "-----s502r="+STATUS                                                elif STATUS == "504":                                                    statisDic[Url]['s504'] +=1                                                    print "-----s504r="+STATUS                                                statisDic[Url]['stotal'] +=1                                            except Exception,e:                                                print Url+" dxxddddfailed:"+str(e)                                                Url=Url                                        if re.search(r'NAME=HttpClient\|TIMEOUT=',line):                                                Url = fields[6]                                                if re.search(r'\?func',Url):                                                     Url=Url[8:Url.index(r'&')]                                                 else:                                                     Url=Url[8:Url.index(r'?')]                                                #print "+++Url="+Url                                                statisDic[Url]['TOUT'] +=1                                        if re.search(r'REQUESTTIME=',line):                                            RunTime = fields[8]                                            STATUS=fields[6]                                            Url = fields[7]                                            time=int(RunTime.split(r'=')[1])                                            match=re.search(r'URL=',Url)                                            if match:                                                try:                                                    if re.search(r'\?func',Url):                                                    #Url=Url.split(r'=')[1]                                                        Url=Url[4:Url.index(r'&')]                                                    else:                                                        Url=Url[4:Url.index(r'?')]                                                    #print ("***Url=%s"%Url)                                                except Exception,e:                                                    print Url+" failed:"+str(e)                                                    Url=Url                                                        #print ("RunTime=%s Url=%s match=%s time=%s"%(RunTime,Url,match.group(0),time))                                                if Url in psDic.keys():                                                    Url_value=psDic[Url]                                                else:                                                    Url_value="未知接口"                                                    #continue                                                statisDic.setdefault(Url,{'explain':Url_value,'total':0,'lt50':0,'lt100':0,'lt150':0,'lt200':0,'lt300':0,'lt500':0,'lt1000':0,'s200':0,'s500':0,'s502':0,'s504':0,'sother':0,'stotal':0,'TOUT':0})['total'] += 1                                                STATUS=STATUS.split(r'=')[1]                                                #print ("RunTime=%s Url=%s STATUS=%s "%(RunTime,Url,STATUS))                                                if time < 50 :                                                    statisDic[Url]['lt50'] += 1                                                elif time >50 and time <= 100:                                                    statisDic[Url]['lt100'] += 1                                                elif time >100 and time <= 150:                                                    statisDic[Url]['lt150'] += 1                                                elif time >150 and time <= 200:                                                    statisDic[Url]['lt200'] += 1                                                elif time >200 and time <= 300:                                                    statisDic[Url]['lt300'] += 1                                                elif time >300 and time <= 500:                                                    statisDic[Url]['lt500'] += 1                                                elif time >500 and time <= 1000:                                                    statisDic[Url]['lt1000'] += 1                                                if   STATUS == "200":                                                    statisDic[Url]['s200'] +=1                                                elif STATUS == "500":                                                    statisDic[Url]['s500'] +=1                                                elif STATUS == "502":                                                    statisDic[Url]['s502'] +=1                                                elif STATUS == "504":                                                    statisDic[Url]['s504'] +=1                                                statisDic[Url]['stotal'] +=1            monitLog.close()html_content=""for name in statisDic.keys():        explain = statisDic[name]['explain']        total = statisDic[name]['total']        lt50 = statisDic[name]['lt50']        lt100 = statisDic[name]['lt100']        lt150 = statisDic[name]['lt150']        lt200 = statisDic[name]['lt200']        lt300 = statisDic[name]['lt300']        lt500 = statisDic[name]['lt500']        lt1000 = statisDic[name]['lt1000']        TOUT = statisDic[name]['TOUT']        s200 = statisDic[name]['s200']        s500 = statisDic[name]['s500']        s502 = statisDic[name]['s502']        s504 = statisDic[name]['s504']        stotal = statisDic[name]['stotal']        sother =statisDic[name]['sother']        bfb50 = str(round(float(lt50/total)*100,2))        bfb100 = str(round(float(lt100/total)*100,2))        bfb150 = str(round(float(lt150/total)*100,2))        bfb200 = str(round(float(lt200/total)*100,2))        bfb300 = str(round(float(lt300/total)*100,2))        bfb500 = str(round(float(lt500/total)*100,2))        bfb1000 = str(round(float(lt1000/total)*100,2))        bfbGt1000 = str(round(float((total - (lt50 + lt100 + lt150 + lt200 + lt300 + lt500 + lt1000))/total)*100,2))                bfs200= str(round(float(s200/stotal)*100,2))        bfs500= str(round(float(s500/stotal)*100,2))        bfs502= str(round(float(s502/stotal)*100,2))        bfs504= str(round(float(s504/stotal)*100,2))        bfsother = str(round(float(sother/stotal)*100,2))                print ("s504:%d"% s504)        bfbGt1000 = str(round((float(total - (lt50 + lt100 + lt150 + lt200 + lt300 + lt500 + lt1000))/total)*100,2))        html_content=html_content+" %s %s %s %s %s %s %s %s %s %s %s %s  "%(name,explain,total,TOUT,bfb50,bfb100,bfb150,bfb200,bfb300,bfb500,bfb1000,bfbGt1000)        #data.append([name,explain,float(total),float(bfb50),float(bfb100),float(bfb150),float(bfb200),float(bfb300),float(bfb500),float(bfb1000),float(bfbGt1000),float(bfs200),float(bfs500),float(bfs502),float(bfs504),float(bfsother)])        data.append([name,explain,float(total),float(TOUT),float(bfb50),float(bfb100),float(bfb150),float(bfb200),float(bfb300),float(bfb500),float(bfb1000),float(bfbGt1000)])        data_status.append([name,explain,float(stotal),float(s200),float(s500),float(s502),float(s504),float(sother),float(bfs200),float(bfs500),float(bfs502),float(bfs504),float(bfsother)])chart =workbook.add_chart({'type':'column'})for i in range(len(data)):        worksheet.write_row('A'+str(i+2), data[i],format)        chart.add_series({                'categories':'=Sheet1!$D$1:$P$1',                'values':'=Sheet1!$D$'+str(i+2)+':$P$'+str(i+2),                'line':{'color':'black'},                'name':'=Sheet1!$A$'+str(i+2),        })headings_stu = ['接口名称','接口说明','stotal','s200','s500','s502','s504','sother','s200(%)','s500(%)','s502(%)','s504(%)','sother(%)']len_data=len(data)+5worksheet.write_row('A'+str(len_data), headings_stu, bold)for i in range(len(data_status)):         worksheet.write_row('A'+str(i+len_data+1), data_status[i],format)chart.set_title({'name':u'欢迎页接口报表_'+logTime})chart.set_y_axis({'name':'(%)'})chart.set_size({'width':800,'height':800})worksheet.insert_chart('A50',chart)workbook.close()html="""      
"""+html_content+"""
接口名称 接口说明 访问总量 TOUT 0~50毫秒(%) 51~100毫秒(%) 101~150毫秒(%) 151~200毫秒(%) 201~300毫秒(%) 300~500毫秒(%) 500~1000毫秒(%) >1000毫秒(%)
"""msgtop=MIMEMultipart('related')msg=MIMEText(html,"html","utf-8")msgtop.attach(msg)attach= MIMEText(open("/tmp/login_intreface_"+logTime+".xlsx","rb").read(),"base64","utf-8")attach["Content-Type"]="application/octet-stream"attach["Content-Disposition"]="attachment;filename=\"欢迎页node接口_"+logTime+".xlsx\"".decode("utf-8").encode("gb18030")msgtop.attach(attach)msgtop['Subject'] = SUBJECTmsgtop['From']=FROMmsgtop['To']=TOtry: server=smtplib.SMTP() server.connect(HOST,"25") server.starttls() #server.login("abc@139.com","passwd") server.sendmail(FROM,TO,msgtop.as_string()) server.quit()except Exception,e: print "failed:"+str(e)

  效果如下:

附件:

转载于:https://www.cnblogs.com/xiaocry/articles/5511330.html

你可能感兴趣的文章
韩版可爱小碎花创意家居收纳挂袋
查看>>
计算机基础之硬件
查看>>
python操作mysql ------- SqlAchemy正传
查看>>
如何使用 JSP JSTL 显示/制作树(tree) 菜单
查看>>
12.5号
查看>>
lintcode-medium-Binary Tree Zigzag Level Order Traversal
查看>>
logrotate日志切割
查看>>
POJ-3253 Fence Repair 贪心
查看>>
Arraylist集合遍历输出
查看>>
java中的选择结构与循环结构
查看>>
无法将类型“ASP.login_aspx”转换为“System.Web.UI.WebControls.Login”
查看>>
[cocos2dx] lua注册回调到c++
查看>>
(treap)[bzoj3224][洛谷3369][cogs1829]Tyvj 1728 普通平衡树
查看>>
Linux下常用的shell命令记录
查看>>
HTTP 常用 Header 讲解
查看>>
linux分割字符串操作
查看>>
PHP学习2
查看>>
多实例Mysql配置
查看>>
linux下安装Mongodb
查看>>
Page.RegisterStartupScript和Response.Write的区别。
查看>>