<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[沧海一粟]]></title> 
<link>http://www.dzhope.com/index.php</link> 
<description><![CDATA[Web系统架构与服务器运维,php开发]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[沧海一粟]]></copyright>
<item>
<link>http://www.dzhope.com/post//</link>
<title><![CDATA[linux下常用分析日志命令]]></title> 
<author>jed &lt;jed521@163.com&gt;</author>
<category><![CDATA[服务器技术]]></category>
<pubDate>Fri, 10 Feb 2012 03:52:51 +0000</pubDate> 
<guid>http://www.dzhope.com/post//</guid> 
<description>
<![CDATA[ 
	grep是一个很强大的命令。这里我们主要来讲使用grep命令来分析网站日志的方法和技巧。<br/><br/>1、合并网站日志<br/><br/>使用ls查看下待合并的日志<br/><br/>合并网站日志<br/><div class="code"><br/>cat *.log &gt;example.log #合并后缀名为log的日志文件，你也可以合并指定的日志<br/></div><br/>2.拆分我们想要的日志文件<br/><br/>比如拆分百度蜘蛛的日志文件<br/><div class="code"><br/>grep &quot;Baiduspider+&quot; example.log &gt;baiduspider.txt<br/></div><br/>比如拆分404错误日志<br/><div class="code"><br/>grep &quot;404&quot; example.log &gt;404.txt<br/></div><br/>还可以同时拆分百度和谷歌的蜘蛛。<br/><br/>这里我们使用的是egrep来实现这一功能。<br/><div class="code"><br/>egrep &quot;Baiduspider+&#124;Googlebot&quot; example.log &gt;spider.txt<br/></div><br/>3、我们还可以结合awk命令来格式话我们的日志文件<br/><br/>awk倾向于分析一行中的字段，我们需要来看一下网站的日志格式<br/><br/>我们截取百度蜘蛛访问的来源IP、时间、抓取的URL、返回码和抓取的大小。<br/><div class="code"><br/>grep &quot;Baiduspider+&quot; example.log &#124;awk &#039;&#123;print $1 &quot;&#92;t&quot; $4 &quot;&#92;t&quot; $7 &quot;&#92;t&quot; $8 &quot;&#92;t&quot; $9 &quot;&#92;t&quot; $10&#125;&#039; &gt;Baiduspider.txt<br/></div><br/>这里使用[tab]是为了导入excel文件中更加方便你的分析。<br/><br/>更新：可以不用[tab]来格式化日志，直接选择空格作为分隔符就好。<br/><br/>如何使用命令删选不重复的URL的，由于很多日志的参数设置不一样，具体到详细命令命令会有所不同<br/>首先我们还是要知道蜘蛛抓取你的URL位置在你的日志记录行的位置，拿上面的图为例子<br/><a href="http://www.dzhope.com/attachment.php?fid=65" target="_blank"><img src="http://www.dzhope.com/attachment.php?fid=65" class="insertimage" alt="点击在新窗口中浏览此图片" title="点击在新窗口中浏览此图片" border="0"/></a><br/>如图，由于每条记录的时间戳等不一样，我们不能直接使用sort命令去重，再者我们需要的只是蜘蛛抓取的URL这个参数，那么我们就直接拎出$7这个URL参数后再去重。<br/><br/>如我们要计算蜘蛛抓取的不重复URL个数<br/><div class="code"><br/>cat access.log &#124;grep Baiduspider+ &#124;awk &#039;&#123;print $7&#125;&#039;&#124;sort -u&#124;wc<br/></div><br/>要把蜘蛛抓取的不重复URL导出来，就可以去掉wc后加上>baiduspiderurl.txt等就可以了<br/><div class="code"><br/>cat access.log &#124;grep Baiduspider+ &#124;awk &#039;&#123;print $7&#125;&#039;&#124;sort -u&nbsp;&nbsp;&gt;baiduspiderurl.txt<br/></div><br/>我们还可以在导出的时候自动给每个URL加上抓取的次数<br/><div class="code"><br/>cat access.log &#124;grep Baiduspider+ &#124;awk &#039;&#123;print $7&#125;&#039;&#124;sort &#124;uniq -c&nbsp;&nbsp;&gt;baiduspiderurl.txt<br/></div><br/><br/>1. grep查找匹配文本<br/><br/> <br/><br/>【a】在文件中查找<br/><br/> $grep 'failed to initialize BeanFactory' upp-account.log<br/><br/> <br/><br/>【b】查找并显示匹配行及以下的200行<br/><br/> $grep -A 200 'failed to initialize BeanFactory' upp-account.log<br/><br/> <br/><br/>【c】查找并显示匹配行及以上的200行<br/><br/> $grep -B 200 'failed to initialize BeanFactory' upp-account.log<br/><br/> <br/><br/>【d】查找，忽略大小写，并显示匹配行及以上的200行<br/><br/> $grep -i -A 200 'failed to initialize BeanFactory' upp-account.log<br/><br/> <br/><br/> <br/><br/> <br/><br/>【Linux下日志分析的几个常用命令】<br/><br/> <br/><br/>有时候我们需要对线上的应用日志做一些分析和简单的统计工作，熟悉一下Linux下文本处理的几个命令，可能会有意想不到的收获：<br/><br/>more&nbsp;&nbsp; 查看文件内容<br/><br/>grep&nbsp;&nbsp;&nbsp;&nbsp;在文件中查找<br/><br/>awk&nbsp;&nbsp;&nbsp;&nbsp;文本处理<br/><br/>sort&nbsp;&nbsp;&nbsp;&nbsp;排序<br/><br/>sed&nbsp;&nbsp;&nbsp;&nbsp;文本处理<br/><br/>下面举个两个例子，说明一下这几个命令的简单应用<br/><br/>1、统计一下今天支付宝到淘宝、淘宝到支付宝的接口调用情况<br/><br/>第一步，找到日志文件路径，确定日志格式<br/><br/>因为接口调用走是TC，先ssh到一台tc的服务器，到日志目录下，ll 一下，看到有几个命名为alipay的日志文件，感觉是打印接口调用日志的，<br/><br/>more alipay-notify-success.log<br/><br/>输出：<br/>2009-06-29 00:00:00,421 [] INFO&nbsp;&nbsp;alipay-notify-success -<br/>*********alipay notify callback*********<br/>out_trade_no=T200P2062628786,trade_status=WAIT_BUYER_PAY,notify_action_type=createPartnerTradeAction,<br/>input:<br/><br/>从日志看出，这是支付宝回调淘宝接口的日志，格式包括：时间，订单号、状态、通知类型等等<br/><br/>more alipay.log<br/><br/>输出：<br/>2009-06-29 00:00:00,072 [] INFO&nbsp;&nbsp;core.SignedTbClientInvoker -<br/>**********Payway Request and Response*********<br/>Service Name:<br/>trade_create<br/>Request URL:<br/><a href="http://aligw.alipay.com" target="_blank">http://aligw.alipay.com</a><br/><br/>从日志上看，这是淘宝调用支付宝的日志，格式包括：时间，接口类型，请求内容等等<br/><br/>第二步，查找特征文本<br/><br/>需要分类型统计接口的调用次数，所以特征字符串就是接口的类型文本，使用grep 命令<br/><br/>grep ‘,notify_action_type’ alipay-notify-success.log<br/><br/>第二个日志比较特殊，特征文本单独一行，没有固定前缀或者后缀特征，而前一行是固定的”Service Name:“，可以查找前一行，然后grep输出的时候多输出一行：<br/><br/>grep ‘Service Name:’ -A1 alipay.log<br/><br/>grep 命令有很多可选参数，比如忽略大小写，输出前（-B before）,后（-A after）行文本等等。<br/><br/>第三步，文本分列<br/><br/>通常，日志文件的一行文本，都由几列组成，中间是分隔字符串，而我们的目标就是找到需要的列，并进行相关的计算，统计，这里就需要用到 awk 命令<br/><br/>对于 alipay-notify-success.log 日志文件，我们使用下面命令：<br/><br/>grep ‘,notify_action_type’ alipay-notify-success.log &#124; awk -F’,’ ‘&#123;a[$3]++&#125;END&#123;for (i in a) print i”,”a[i]&#125;’<br/><br/>grep命令找到符合的行，作为awk命令的输入，-F 后面是指定分隔符号，后面是表达式，首先定义一个数组a（也可以理解为是一个map）用分隔出来的第三列作为下标（key），值每次加一，END 后面是最后执行的语句，循环输出数组<br/><br/>awk命令本身很强大，可以全面地看一下它帮助<br/><br/>第四步，排序<br/><br/>第三步已经完成了计算、统计工作，最后我们根据调用次数进行一下排序，方便查看<br/><br/>这里使用sort 命令<br/><br/>grep ‘,notify_action_type’ alipay-notify-success.log &#124; awk -F’,’ ‘&#123;a[$3]++&#125;END&#123;for (i in a) print i”,”a[i]&#125;’ &#124;sort -t, -k2 -n -r<br/><br/>-t 跟ark的-F功能类似，是把一行文本分成几列，-k指定需要排序的列，-n表示按数字方式排序，-r 表示倒序<br/><br/>最后，我们看到了输出：<br/><br/>notify_action_type=createPartnerTradeAction,52641<br/>notify_action_type=payByAccountAction,44807<br/>notify_action_type=sellerSendGoodsAction,43848<br/>notify_action_type=confirmReceiveAction,40705<br/>notify_action_type=modifyTradeAction,25733<br/>notify_action_type=allowRefundAction,10407<br/>notify_action_type=autoFinishTradeAction,8351<br/>notify_action_type=closeTradeAction,8030<br/>notify_action_type=applyRefundiiiAction,2653<br/>notify_action_type=refundDisburseAction,2330<br/>notify_action_type=confirmDisburseAction,401<br/>notify_action_type=extendTimeoutLimitAction,368<br/>notify_action_type=modifyRefundiiiAction,280<br/>notify_action_type=cancelRefundiiiAction,52<br/>notify_action_type=null,20<br/>notify_action_type=unfreezeTradeAction,1<br/>notify_action_type=refundVoucherCheckPassAction,1<br/>notify_action_type=freezeTradeAction,1<br/><br/>当然这是单台机器的，根据应用的机器数量，可以大致评估一下总的情况。<br/><br/>类似的，对于 日志alipay.log<br/><br/>grep ‘Service Name:’ -A1 alipay.log &#124; sed ‘/Service Name:/’d &#124;sed ‘/–/’d &#124; awk -F’&nbsp;&nbsp;&nbsp;&nbsp;‘ ‘&#123;a[$2]++&#125;END&#123;for (i in a) print i”,”a[i]&#125;’ &#124; sort -t, -k2 -n -r<br/><br/>输出：<br/><br/>trade_create,51326<br/>send_goods_confirm_by_platform,40716<br/>confirmReceiveGoods,39351<br/>modifyTradeFee,25261<br/>cae_charge_agent,10074<br/>close_trade,3871<br/>extendTimeout,378<br/>calculate_service_fee,52<br/>union_data_prepare,15<br/>logistic_sign_in,4<br/><br/>接下来，我们再看一个例子<br/><br/>需求：统计一下，denali机器中，我的淘宝首页 这个页面的请求次数和平均响应时间<br/><br/>还是分几步：<br/><br/>1、首先找到日志和日志格式<br/><br/>Apache 的访问日志，/home/admin/cai/logs/cronlog/2009/06/2002009-06-29-taobao-access_log<br/><br/>more 2002009-06-29-taobao-access_log<br/><br/>输出：<br/><br/>58.208.1.15 148452 3251 [29/Jun/2009:00:00:04 +0800] “GET <a href="http://my.taobao.com/mytaobao/home/my_taobao.jhtml" target="_blank">http://my.taobao.com/mytaobao/home/my_taobao.jhtml</a>” 200 14147 “<a href="http://my.t" target="_blank">http://my.t</a><br/>aobao.com/mytaobao/home/my_taobao.jhtml” “Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; Trident/4.0; QQDownload 551; User-agent<br/>: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1;); SLCC1; .NET CLR 2.0.50727; MDDC; .NET CLR 3.5.30729; .NET CLR 3.0.30618)<br/><br/>2、查找特征文本<br/><br/>grep ‘GET <a href="http://my.taobao.com/mytaobao/home/my_taobao.jhtml" target="_blank">http://my.taobao.com/mytaobao/home/my_taobao.jhtml</a>’ 2009-06-29-taobao-access_log<br/><br/>3、文本分列<br/><br/>grep ‘GET <a href="http://my.taobao.com/mytaobao/home/my_taobao.jhtml" target="_blank">http://my.taobao.com/mytaobao/home/my_taobao.jhtml</a>’ 2009-06-29-taobao-access_log &#124; awk -F’ ‘ ‘&#123;i+=$2&#125;END&#123;print NR “,” i/NR/1000&#125;’<br/><br/>Tags - <a href="http://www.dzhope.com/tags/linux%25E5%2591%25BD%25E4%25BB%25A4/" rel="tag">linux命令</a> , <a href="http://www.dzhope.com/tags/linux/" rel="tag">linux</a>
]]>
</description>
</item><item>
<link>http://www.dzhope.com/post//#blogcomment</link>
<title><![CDATA[[评论] linux下常用分析日志命令]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>http://www.dzhope.com/post//#blogcomment</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>