天天看点

varnish的日志和统计工具 Reporting and statistics

【环境】

varnish4.x

参考:http://book.varnish-software.com/4.0/chapters/Appendix_B__Varnish_Programs.html

参考:http://www.361way.com/varnish-log/2341.html

【关于日志的记录方式】

官网原文:

One of the really nice features in Varnish is the way logging works. Instead of logging to a normal log file Varnish logs to a shared memory segment, called the VSL - the Varnish Shared Log. When the end of the segment is reached we start over, overwriting old data

翻译:

varnish的一个很好的特性特点是varnish的记录日志的方式。varnish记录日志到一个共享内存空间取代记录到一个文件中的,叫做VSL(Varnish Shared Log)。当日志记录滚到了我们开始记录的那个点,就覆盖旧的数据日志。

【自带命令】

varnishlog

varnishnsca

【区别】

varnishlog 记录的是完成的varnish工作的日志过程。

varnishnsca 是以一个行模式记录有日志格式的方式记录如apache的日志格式。

【varnishlog用法】

直接运行varnishlog 记录的是正常request和respond的日志。(vxid格式)

varnishlog常用选项:

-g 日志输出格式

<code> </code><code>[-g &lt;session|request|vxid|raw&gt;] Grouping mode (default: vxid)</code>

arnishlog gives you the raw logs, everything that is written to the logs

应用: raw 可以查看所有的日志,包括健康检查

<code>varnishlog -g raw</code>

-I 过滤

<code> </code><code>[-I &lt;[taglist:]regex&gt;]    Include by regex    过滤之匹配的</code>

应用:

<code>varnishlog -I MISS</code>

 之匹配miss的字样 (-C  忽略大小写)

-C 忽略大小写

-X 排除输出

只看健康检查日志

<code>varnishlog -g raw -i Backend_health</code>

看返回码为500以上的

<code>varnishlog -g request -q </code><code>'RespStatus &gt;= 500'</code>

排除完全不记录健康检查日志 等于: varnishlog

<code>varnishlog -g raw -X </code><code>"healthy|ping|PONG"</code>

【varnishnsca用法】

varnish日志工具

默认格式是:

%h 远端主机

%l 远端登录名(由identd而来,如果支持的话),除非IdentityCheck设为”On“,否则将得到一个”-”。

%u user 通常为 -

%t 时间

%r  请求的第一行&lt;method&gt; &lt;url&gt; &lt;version&gt;

%b 以CLF格式显示的除HTTP头以外传送的字节数,也就是当没有字节传送时显示’-‘而不是0。

%{Referer}i  

%{User-agent}i

我需要定制加入的几个:

%T 处理完请求所花时间,以秒为单位

%{Varnish:hitmiss}x   是否命中或者miss

{Varnish:handling}x    下一步的处理方式

[root@dev-vhost011 ~]# varnishncsa 

10.59.74.33 - - [29/Oct/2016:11:59:38 +0800] "HEAD http://10.59.74.33/007.jpg HTTP/1.1" 200 0 "-" "curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.47.1 OpenSSL/1.0.1e zlib/1.2.3 libidn/1.18 libssh2/1.4.2 nghttp2/1.6.0"

自定义日志格式: 这是一种不错的格式

[root@dev-vhost011 ~]# varnishncsa   -F "%t %T %D %h %l %s %H %m %{Varnish:hitmiss}x %{Varnish:handling}x" 

[29/Oct/2016:12:09:46 +0800] 0 170 10.59.74.33 - 200 HTTP/1.1 HEAD hit hit

[29/Oct/2016:12:09:52 +0800] 0 2120 10.59.74.33 - 405 HTTP/1.1 POST miss pass

生产格式

<code>varnishncsa -F </code><code>"%h %l %u %t %T "</code><code>%r</code><code>" %s %b \"%{Referer}i\" \"%{User-agent}i\" %{Varnish:hitmiss}x %{Varnish:handling}x"</code>

如:

10.59.72.13 - - [30/Oct/2016:17:47:52 +0800] 0 GET http://10.59.74.33/007.jpg HTTP/1.1 200 7569 "-" "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36" hit hit 

问题1 如何记录处理时间

%D

In client mode, time taken to serve the request, in microseconds. In backend mode, time from the request was sent to the entire body had been received.

问题2: 如何记录被backend到了那个后端的哪个机器

只能使用-b 后端模式  %h就表示后端的主机

试试:

<code>varnishncsa -</code><code>bc</code> <code>-F </code><code>"%{Varnish:side}x %h %l %u %D %t %T "</code><code>%r</code><code>" %s %b "</code><code>%{Referer}i</code><code>" "</code><code>%{User-agent}i</code><code>" %{Varnish:hitmiss}x %{Varnish:handling}x"</code>

标记是client还是backend模式:

When running varnishncsa in both backend and client mode, it is strongly advised to include the format specifier %{Varnish:side}x to distinguish between backend and client requests.

【随意补充知识】

varnish默认添加的请求响应头部:

X-Varnish:

Via:

Age:

在这里GET命令的选项无所谓。需要注意的是varnish返回的响应,varnish会增加三个相应头信息,分别是“X-Varnish”、“Via”和“Age”。这些头信息在Varnish的处理过程中非常有用。X-Varnish头信息的后面会有一个或两个数字,如果是一个数字,就表明varnish在缓存中没有发现这个请求,这个数字的含义是varnish为这个请求所做的标记ID。如果X-Varnish后是两个数字,就表明varnish在缓存中命中了这个请求,第一个数字是请求的标识ID,第二个数字是缓存的标识ID。“Via”头信息表明这个请求将经过一个代理。“Age”头信息标识出这个请求将被缓存多长时间(单位:秒)。Age在varnish缓存中存在的时间。

本文转自残剑博客51CTO博客,原文链接http://blog.51cto.com/cuidehua/1867387如需转载请自行联系原作者

cuizhiliang

继续阅读