马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
如果您觉得本篇CentOSLinux教程讲得好,请记得点击右边漂浮的分享程序,把好文章分享给你的小伙伴们!剧本功效:
剧本采取python2.7编写,用来剖析Apahce的拜访日记
剧本用法:
更新:
1.第二版:增长显示指定的行数
2.增长Nginx日记剖析剧本
剧本履行后果以下:
剧本内容以下:
nginx拜访日记剖析剧本
[code]<olclass="dp-pylist-paddingleft-2"><liclass="alt">#!/usr/bin/envpython
#coding=utf-8
<liclass="alt">
#------------------------------------------------------
<liclass="alt">#Name:nginx日记剖析剧本
#Purpose:此剧本只用来剖析nginx的拜访日记
<liclass="alt">#Version:1.0
#Author:LEO
<liclass="alt">#BLOG:http://linux5588.blog.51cto.com
#EMAIL:chanyipiaomiao@163.com
<liclass="alt">#Created:2013-05-07
#Modified:2013-05-07
<liclass="alt">#Copyright:(c)LEO2013
#------------------------------------------------------
<liclass="alt">
importsys
<liclass="alt">importtime
<liclass="alt">#该类是用来打印格局
classdisplayFormat(object):
<liclass="alt">
defformat_size(self,size):
<liclass="alt">格局化流量单元
KB=1024#KB->BB是字节
<liclass="alt">MB=1048576#MB->B
GB=1073741824#GB->B
<liclass="alt">TB=1099511627776#TB->B
ifsize>=TB:
<liclass="alt">size=str(size/TB)+T
elifsize<KB:
<liclass="alt">size=str(size)+B
elifsize>=GBandsize<TB:
<liclass="alt">size=str(size/GB)+G
elifsize>=MBandsize<GB:
<liclass="alt">size=str(size/MB)+M
else:
<liclass="alt">size=str(size/KB)+K
returnsize
<liclass="alt">
#界说字符串格局化
<liclass="alt">formatstring=%-15s%-10s%-12s%8s%10s%10s%10s%10s%10s%10s%10s
<liclass="alt">deftransverse_line(self):
输入横线
<liclass="alt">printself.formatstring%(-*15,-*10,-*12,-*12,-*10,-*10,-*10,-*10,-*10,-*10,-*10)
<liclass="alt">defhead(self):
输入头部信息
<liclass="alt">printself.formatstring%(IP,Traffic,Times,Times%,200,404,500,403,302,304,503)
<liclass="alt">deferror_print(self):
输入毛病信息
<liclass="alt">print
printUsage:+sys.argv[0]+NginxLogFilePath[Number]
<liclass="alt">print
sys.exit(1)
<liclass="alt">
defexecut_time(self):
<liclass="alt">输入剧本履行的时光
print
<liclass="alt">print"ScriptExecutionTime:%.3fsecond"%time.clock()
print
<liclass="alt">
#该类是用来生成主机信息的字典
<liclass="alt">classhostInfo(object):
host_info=[200,404,500,302,304,503,403,times,size]
<liclass="alt">
def__init__(self,host):
<liclass="alt">self.host=host={}.fromkeys(self.host_info,0)
<liclass="alt">defincrement(self,status_times_size,is_size):
该办法是用来给host_info中的各个值加1
<liclass="alt">ifstatus_times_size==times:
self.host[times]+=1
<liclass="alt">elifis_size:
self.host[size]=self.host[size]+status_times_size
<liclass="alt">else:
self.host[status_times_size]+=1
<liclass="alt">
defget_value(self,value):
<liclass="alt">该办法是取到各个主机信息中对应的值
returnself.host[value]
<liclass="alt">
#该类是用来剖析文件
<liclass="alt">classfileAnalysis(object):
def__init__(self):
<liclass="alt">初始化一个空字典
self.report_dict={}
<liclass="alt">self.total_request_times,self.total_traffic,self.total_200,
self.total_404,self.total_500,self.total_403,self.total_302,
<liclass="alt">self.total_304,self.total_503=0,0,0,0,0,0,0,0,0
<liclass="alt">defsplit_eachline_todict(self,line):
朋分文件中的每行,并前往一个字典
<liclass="alt">split_line=line.split()
split_dict={remote_host:split_line[0],status:split_line[8],
<liclass="alt">bytes_sent:split_line[9],}
returnsplit_dict
<liclass="alt">
defgenerate_log_report(self,logfile):
<liclass="alt">读取文件,剖析split_eachline_todict办法生成的字典
forlineinlogfile:
<liclass="alt">try:
line_dict=self.split_eachline_todict(line)
<liclass="alt">host=line_dict[remote_host]
status=line_dict[status]
<liclass="alt">exceptValueError:
continue
<liclass="alt">exceptIndexError:
continue
<liclass="alt">
ifhostnotinself.report_dict:
<liclass="alt">host_info_obj=hostInfo(host)
self.report_dict[host]=host_info_obj
<liclass="alt">else:
host_info_obj=self.report_dict[host]
<liclass="alt">
host_info_obj.increment(times,False)
<liclass="alt">ifstatusinhost_info_obj.host_info:
host_info_obj.increment(status,False)
<liclass="alt">try:
bytes_sent=int(line_dict[bytes_sent])
<liclass="alt">exceptValueError:
<p><spanstyle="padding:0px;margin:0px;border:none;color:#505050;background-color:inherit;">bytes_sent= |