同squidclient一样,varnish同样也提供了一个工具 ———— varnishstat 用于查看varnish的缓存状态 ,并且后者显的更为专一 。熟悉每个参数所表示的意义,对varnish的优化和调优是很有帮助的 。

一、varnishstat的使用参数

 1usage: varnishstat [-1lV] [-f field_list] [-n varnish_name] [-w delay]
 2    -1                           # Print the statistics to stdout.
 3    -f field_list                # Comma separated list of fields to display.
 4                                 # If it starts with '^' it is used as an exclusion list
 5    -l                           # Lists the available fields to use with the -f option
 6    -n varnish_name              # The varnishd instance to get logs from
 7    -V                           # Display the version number and exit
 8    -w delay                     # Wait delay seconds between updates.  Default is 1 second. Can also be be used with -1, -x or -j for repeated output.
 9    -x                           # Print statistics to stdout as XML.
10    -j                           # Print statistics to stdout as JSON.

由上面的使用参数不难看出,varnishstat的还是十分人性化的。其带的各种参数也为各种监控工具提供了很便利的接口。例如,我们可以很方便的利用-1 、-f参数很方便的写脚本给监控工具提供结果。像-x -j参数可以很方便的通过html调用和写入数据库。

二、参数介绍

1、默认打开varnishstat

varnishstat

默认不加任何参数打开varnishstat时显示的内容如上图所示,默认会1秒刷新一次数据。Hitrate ratio由三个数字组成,第一个数字范围0-10,第二个数字范围0-100,第三个数字范围0-1000。分别表示过去N秒内的Hitrate avg。如果打开的时间足够长,以上三个数字就会逐渐变成10,100,1000。Hitrate avg里的内容是命中率,需要乘以100转换成百分比 。

2、varnishstat -1详细分析

  1[root@back ~]# /App/varnish/bin/varnishstat -1
  2client_conn           29407268        43.06 Client connections accepted
  3client_drop                  1         0.00 Connection dropped, no sess/wrk
  4#client_req 代表到目前为止,浏览器向反向代理服务器发送的HTTP请求累积次数,由于可能使用长连接,所以它可能会大于上边的Client connections accepted。
  5client_req            29407152        43.06 Client requests received
  6cache_hit             26187535        38.34 Cache hits
  7cache_hitpass              614         0.00 Cache hits for pass
  8cache_miss             3212034         4.70 Cache misses #在缓存区中查找但是没有命中缓存的次数
  9backend_conn            105734         0.15 Backend conn. success
 10backend_unhealthy            0         0.00 Backend conn. not attempted
 11backend_busy                 0         0.00 Backend conn. too many
 12backend_fail             18709         0.03 Backend conn. failures #后端请求失败数,越小越好
 13backend_reuse          3097249         4.53 Backend conn. reuses
 14backend_toolate          37578         0.06 Backend conn. was closed #后端服务器已关闭的连接数
 15backend_recycle        3134831         4.59 Backend conn. recycles
 16backend_retry             2582         0.00 Backend conn. retry #重新尝试连接后端服务器的次数
 17fetch_head                   0         0.00 Fetch head
 18fetch_length             27797         0.04 Fetch with Length
 19fetch_chunked          3147029         4.61 Fetch chunked
 20fetch_eof                    0         0.00 Fetch EOF
 21fetch_bad                    0         0.00 Fetch had bad headers
 22fetch_close               6895         0.01 Fetch wanted close
 23fetch_oldhttp                0         0.00 Fetch pre HTTP/1.1 closed
 24fetch_zero                   0         0.00 Fetch zero len
 25fetch_failed                 0         0.00 Fetch failed
 26fetch_1xx                    0         0.00 Fetch no body (1xx)
 27fetch_204                    0         0.00 Fetch no body (204)
 28fetch_304                   10         0.00 Fetch no body (304)
 29n_sess_mem                9261          .   N struct sess_mem
 30n_sess                       0          .   N struct sess
 31n_object                  1067          .   N struct object    #当前被cache的条目
 32n_vampireobject              0          .   N unresurrected objects
 33n_objectcore              1074          .   N struct objectcore
 34n_objecthead              1532          .   N struct objecthead
 35n_waitinglist           101664          .   N struct waitinglist
 36n_vbc                        4          .   N struct vbc
 37n_wrk                       10          .   N worker threads #当前工作线程数
 38n_wrk_create             10851         0.02 N worker threads created #创建了多少线程
 39n_wrk_failed                 0         0.00 N worker threads not created #尝试创建线程但失败,值最好为0
 40n_wrk_max                 7529         0.01 N worker threads limited  #由于线程上限限制或者线程池反应延迟导致不能成功创建的线程数
 41n_wrk_lqueue                 0         0.00 work request queue length
 42n_wrk_queued             89033         0.13 N queued work requests
 43n_wrk_drop                 501         0.00 N dropped work requests #塞满后扔掉
 44n_backend                    1          .   N backends
 45n_expired              3173642          .   N expired objects #代表过期的缓存内容个数,和ttl设置有关
 46n_lru_nuked                  0          .   N LRU nuked objects #由于cache空间满而不得不扔掉的cache条目,如果这个数字是0,就没必要增加cache的大小了
 47n_lru_moved            5483101          .   N LRU moved objects #代表被淘汰的缓存内容个数
 48losthdr                      2         0.00 HTTP header overflows
 49n_objsendfile                0         0.00 Objects sent with sendfile
 50n_objwrite            29345982        42.97 Objects sent with write
 51n_objoverflow                0         0.00 Objects overflowing workspace
 52s_sess                29407400        43.06 Total Sessions
 53s_req                 29407152        43.06 Total Requests
 54s_pipe                       0         0.00 Total pipe
 55s_pass                    7081         0.01 Total pass
 56s_fetch                3181731         4.66 Total fetch
 57s_hdrbytes          8439436717     12356.55 Total header bytes #代表缓存区中所有缓存内容的HTTP头信息长度
 58s_bodybytes        80473095940    117824.19 Total body bytes #代表缓存区中所有缓存内容的正文长度
 59sess_closed           29406781        43.06 Session Closed
 60sess_pipeline                0         0.00 Session Pipeline
 61sess_readahead               0         0.00 Session Read Ahead
 62sess_linger                145         0.00 Session Linger
 63sess_herd                10118         0.01 Session herd
 64shm_records         1434412402      2100.19 SHM records
 65shm_writes           124888270       182.85 SHM writes
 66shm_flushes                  0         0.00 SHM flushes due to overflow
 67shm_cont                425735         0.62 SHM MTX contention
 68shm_cycles                 627         0.00 SHM cycles through buffer
 69sms_nreq                 37386         0.05 SMS allocator requests
 70sms_nobj                     0          .   SMS outstanding allocations
 71sms_nbytes                   0          .   SMS outstanding bytes
 72sms_balloc            15664764          .   SMS bytes allocated
 73sms_bfree             15664764          .   SMS bytes freed
 74backend_req            3202957         4.69 Backend requests made
 75n_vcl                        5         0.00 N vcl total
 76n_vcl_avail                  5         0.00 N vcl available
 77n_vcl_discard                0         0.00 N vcl discarded
 78n_ban                        1          .   N total active bans
 79n_ban_gone                   1          .   N total gone bans
 80n_ban_add                    1         0.00 N new bans added
 81n_ban_retire                 0         0.00 N old bans deleted
 82n_ban_obj_test               0         0.00 N objects tested
 83n_ban_re_test                0         0.00 N regexps tested against
 84n_ban_dups                   0         0.00 N duplicate bans removed
 85hcb_nolock            29397883        43.04 HCB Lookups without lock
 86hcb_lock               2304053         3.37 HCB Lookups with lock
 87hcb_insert             2303884         3.37 HCB Inserts
 88esi_errors                   0         0.00 ESI parse errors (unlock)
 89esi_warnings                 0         0.00 ESI parse warnings (unlock)
 90accept_fail                  0         0.00 Accept failures
 91client_drop_late           500         0.00 Connection dropped late
 92uptime                  682993         1.00 Client uptime
 93dir_dns_lookups              0         0.00 DNS director lookups
 94dir_dns_failed               0         0.00 DNS director failed lookups
 95dir_dns_hit                  0         0.00 DNS director cached lookups hit
 96dir_dns_cache_full           0         0.00 DNS director full dnscache
 97vmods                        0          .   Loaded VMODs
 98n_gzip                       0         0.00 Gzip operations
 99n_gunzip                     0         0.00 Gunzip operations
100LCK.sms.creat                1         0.00 Created locks
101LCK.sms.destroy              0         0.00 Destroyed locks
102LCK.sms.locks           112158         0.16 Lock Operations
103LCK.sms.colls                0         0.00 Collisions
104LCK.smp.creat                0         0.00 Created locks
105LCK.smp.destroy              0         0.00 Destroyed locks
106LCK.smp.locks                0         0.00 Lock Operations
107LCK.smp.colls                0         0.00 Collisions
108LCK.sma.creat                2         0.00 Created locks
109LCK.sma.destroy              0         0.00 Destroyed locks
110LCK.sma.locks         15953254        23.36 Lock Operations
111LCK.sma.colls                0         0.00 Collisions
112LCK.smf.creat                0         0.00 Created locks
113LCK.smf.destroy              0         0.00 Destroyed locks
114LCK.smf.locks                0         0.00 Lock Operations
115LCK.smf.colls                0         0.00 Collisions
116LCK.hsl.creat                0         0.00 Created locks
117LCK.hsl.destroy              0         0.00 Destroyed locks
118LCK.hsl.locks                0         0.00 Lock Operations
119LCK.hsl.colls                0         0.00 Collisions
120LCK.hcb.creat                1         0.00 Created locks
121LCK.hcb.destroy              0         0.00 Destroyed locks
122LCK.hcb.locks          4610661         6.75 Lock Operations
123LCK.hcb.colls                0         0.00 Collisions
124LCK.hcl.creat                0         0.00 Created locks
125LCK.hcl.destroy              0         0.00 Destroyed locks
126LCK.hcl.locks                0         0.00 Lock Operations
127LCK.hcl.colls                0         0.00 Collisions
128LCK.vcl.creat                1         0.00 Created locks
129LCK.vcl.destroy              0         0.00 Destroyed locks
130LCK.vcl.locks           367008         0.54 Lock Operations
131LCK.vcl.colls                0         0.00 Collisions
132LCK.stat.creat               1         0.00 Created locks
133LCK.stat.destroy             0         0.00 Destroyed locks
134LCK.stat.locks        29416662        43.07 Lock Operations
135LCK.stat.colls               0         0.00 Collisions
136LCK.sessmem.creat            1         0.00 Created locks
137LCK.sessmem.destroy            0         0.00 Destroyed locks
138LCK.sessmem.locks       29423687        43.08 Lock Operations
139LCK.sessmem.colls              0         0.00 Collisions
140LCK.wstat.creat                1         0.00 Created locks
141LCK.wstat.destroy              0         0.00 Destroyed locks
142LCK.wstat.locks          1392900         2.04 Lock Operations
143LCK.wstat.colls                0         0.00 Collisions
144LCK.herder.creat               1         0.00 Created locks
145LCK.herder.destroy             0         0.00 Destroyed locks
146LCK.herder.locks           49701         0.07 Lock Operations
147LCK.herder.colls               0         0.00 Collisions
148LCK.wq.creat                   2         0.00 Created locks
149LCK.wq.destroy                 0         0.00 Destroyed locks
150LCK.wq.locks            60646593        88.80 Lock Operations
151LCK.wq.colls                   0         0.00 Collisions
152LCK.objhdr.creat         2312624         3.39 Created locks
153LCK.objhdr.destroy       2311197         3.38 Destroyed locks
154LCK.objhdr.locks       125013176       183.04 Lock Operations
155LCK.objhdr.colls               0         0.00 Collisions
156LCK.exp.creat                  1         0.00 Created locks
157LCK.exp.destroy                0         0.00 Destroyed locks
158LCK.exp.locks            7030656        10.29 Lock Operations
159LCK.exp.colls                  0         0.00 Collisions
160LCK.lru.creat                  2         0.00 Created locks
161LCK.lru.destroy                0         0.00 Destroyed locks
162LCK.lru.locks            3174709         4.65 Lock Operations
163LCK.lru.colls                  0         0.00 Collisions
164LCK.cli.creat                  1         0.00 Created locks
165LCK.cli.destroy                0         0.00 Destroyed locks
166LCK.cli.locks                138         0.00 Lock Operations
167LCK.cli.colls                  0         0.00 Collisions
168LCK.ban.creat                  1         0.00 Created locks
169LCK.ban.destroy                0         0.00 Destroyed locks
170LCK.ban.locks            7030659        10.29 Lock Operations
171LCK.ban.colls                  0         0.00 Collisions
172LCK.vbp.creat                  1         0.00 Created locks
173LCK.vbp.destroy                0         0.00 Destroyed locks
174LCK.vbp.locks                  0         0.00 Lock Operations
175LCK.vbp.colls                  0         0.00 Collisions
176LCK.vbe.creat                  1         0.00 Created locks
177LCK.vbe.destroy                0         0.00 Destroyed locks
178LCK.vbe.locks             248892         0.36 Lock Operations
179LCK.vbe.colls                  0         0.00 Collisions
180LCK.backend.creat              1         0.00 Created locks
181LCK.backend.destroy            0         0.00 Destroyed locks
182LCK.backend.locks        6642998         9.73 Lock Operations
183LCK.backend.colls              0         0.00 Collisions
184SMA.s0.c_req             6386718         9.35 Allocator requests
185SMA.s0.c_fail                  0         0.00 Allocator failures
186SMA.s0.c_bytes      416270568314    609479.99 Bytes allocated
187SMA.s0.c_freed      416266623952    609474.22 Bytes freed
188SMA.s0.g_alloc              2134          .   Allocations outstanding
189SMA.s0.g_bytes           3944362          .   Bytes outstanding #目前占用的内存大小
190SMA.s0.g_space        6438506582          .   Bytes available #可用内存大小
191SMA.Transient.c_req        14033         0.02 Allocator requests
192SMA.Transient.c_fail           0         0.00 Allocator failures
193SMA.Transient.c_bytes    908921608      1330.79 Bytes allocated
194SMA.Transient.c_freed    908921608      1330.79 Bytes freed
195SMA.Transient.g_alloc            0          .   Allocations outstanding
196SMA.Transient.g_bytes            0          .   Bytes outstanding
197SMA.Transient.g_space            0          .   Bytes available

如上所示,经常需要关注项我都在后面加了中文注解 。特别需要注意的是,命中率是通过以下公式进行计算的。

1cache_hit_percent = ( cache_hit / ( cache_hit + cache_miss ) ) * 100

需要能过varnish只把关注的参数显示出来的话,可以用下面的命令:

1/App/varnish/bin/varnishstat -f SMA.s0.c_bytes -f SMA.s0.c_freed -f SMA.s0.g_alloc -f SMA.s0.g_bytes -f SMA.s0.g_space -f SMA.s0.c_req
23/App/varnish/bin/varnishstat -f SMA.s0.c_bytes,SMA.s0.c_freed,SMA.s0.g_alloc,SMA.s0.g_bytes,SMA.s0.g_space,SMA.s0.c_req

具体监控里,根据个人应用和需要的不同,灵活的设置以上参数。我在nagios上监控的参数主要有四个:cache_hit_percent、n_object、SMA.s0.g_bytes、SMA.s0.g_space 。