同squidclient一样,varnish同样也提供了一个工具 ———— varnishstat 用于查看varnish的缓存状态 ,并且后者显的更为专一 。熟悉每个参数所表示的意义,对varnish的优化和调优是很有帮助的 。
一、varnishstat的使用参数
usage: varnishstat [-1lV] [-f field_list] [-n varnish_name] [-w delay] -1 # Print the statistics to stdout. -f field_list # Comma separated list of fields to display. # If it starts with '^' it is used as an exclusion list -l # Lists the available fields to use with the -f option -n varnish_name # The varnishd instance to get logs from -V # Display the version number and exit -w delay # Wait delay seconds between updates. Default is 1 second. Can also be be used with -1, -x or -j for repeated output. -x # Print statistics to stdout as XML. -j # Print statistics to stdout as JSON.
由上面的使用参数不难看出,varnishstat的还是十分人性化的。其带的各种参数也为各种监控工具提供了很便利的接口。例如,我们可以很方便的利用-1 、-f参数很方便的写脚本给监控工具提供结果。像-x -j参数可以很方便的通过html调用和写入数据库。
二、参数介绍
1、默认打开varnishstat
默认不加任何参数打开varnishstat时显示的内容如上图所示,默认会1秒刷新一次数据。Hitrate ratio由三个数字组成,第一个数字范围0-10,第二个数字范围0-100,第三个数字范围0-1000。分别表示过去N秒内的Hitrate avg。如果打开的时间足够长,以上三个数字就会逐渐变成10,100,1000。Hitrate avg里的内容是命中率,需要乘以100转换成百分比 。
2、varnishstat -1详细分析
[root@back ~]# /App/varnish/bin/varnishstat -1 client_conn 29407268 43.06 Client connections accepted client_drop 1 0.00 Connection dropped, no sess/wrk #client_req 代表到目前为止,浏览器向反向代理服务器发送的HTTP请求累积次数,由于可能使用长连接,所以它可能会大于上边的Client connections accepted。 client_req 29407152 43.06 Client requests received cache_hit 26187535 38.34 Cache hits cache_hitpass 614 0.00 Cache hits for pass cache_miss 3212034 4.70 Cache misses #在缓存区中查找但是没有命中缓存的次数 backend_conn 105734 0.15 Backend conn. success backend_unhealthy 0 0.00 Backend conn. not attempted backend_busy 0 0.00 Backend conn. too many backend_fail 18709 0.03 Backend conn. failures #后端请求失败数,越小越好 backend_reuse 3097249 4.53 Backend conn. reuses backend_toolate 37578 0.06 Backend conn. was closed #后端服务器已关闭的连接数 backend_recycle 3134831 4.59 Backend conn. recycles backend_retry 2582 0.00 Backend conn. retry #重新尝试连接后端服务器的次数 fetch_head 0 0.00 Fetch head fetch_length 27797 0.04 Fetch with Length fetch_chunked 3147029 4.61 Fetch chunked fetch_eof 0 0.00 Fetch EOF fetch_bad 0 0.00 Fetch had bad headers fetch_close 6895 0.01 Fetch wanted close fetch_oldhttp 0 0.00 Fetch pre HTTP/1.1 closed fetch_zero 0 0.00 Fetch zero len fetch_failed 0 0.00 Fetch failed fetch_1xx 0 0.00 Fetch no body (1xx) fetch_204 0 0.00 Fetch no body (204) fetch_304 10 0.00 Fetch no body (304) n_sess_mem 9261 . N struct sess_mem n_sess 0 . N struct sess n_object 1067 . N struct object #当前被cache的条目 n_vampireobject 0 . N unresurrected objects n_objectcore 1074 . N struct objectcore n_objecthead 1532 . N struct objecthead n_waitinglist 101664 . N struct waitinglist n_vbc 4 . N struct vbc n_wrk 10 . N worker threads #当前工作线程数 n_wrk_create 10851 0.02 N worker threads created #创建了多少线程 n_wrk_failed 0 0.00 N worker threads not created #尝试创建线程但失败,值最好为0 n_wrk_max 7529 0.01 N worker threads limited #由于线程上限限制或者线程池反应延迟导致不能成功创建的线程数 n_wrk_lqueue 0 0.00 work request queue length n_wrk_queued 89033 0.13 N queued work requests n_wrk_drop 501 0.00 N dropped work requests #塞满后扔掉 n_backend 1 . N backends n_expired 3173642 . N expired objects #代表过期的缓存内容个数,和ttl设置有关 n_lru_nuked 0 . N LRU nuked objects #由于cache空间满而不得不扔掉的cache条目,如果这个数字是0,就没必要增加cache的大小了 n_lru_moved 5483101 . N LRU moved objects #代表被淘汰的缓存内容个数 losthdr 2 0.00 HTTP header overflows n_objsendfile 0 0.00 Objects sent with sendfile n_objwrite 29345982 42.97 Objects sent with write n_objoverflow 0 0.00 Objects overflowing workspace s_sess 29407400 43.06 Total Sessions s_req 29407152 43.06 Total Requests s_pipe 0 0.00 Total pipe s_pass 7081 0.01 Total pass s_fetch 3181731 4.66 Total fetch s_hdrbytes 8439436717 12356.55 Total header bytes #代表缓存区中所有缓存内容的HTTP头信息长度 s_bodybytes 80473095940 117824.19 Total body bytes #代表缓存区中所有缓存内容的正文长度 sess_closed 29406781 43.06 Session Closed sess_pipeline 0 0.00 Session Pipeline sess_readahead 0 0.00 Session Read Ahead sess_linger 145 0.00 Session Linger sess_herd 10118 0.01 Session herd shm_records 1434412402 2100.19 SHM records shm_writes 124888270 182.85 SHM writes shm_flushes 0 0.00 SHM flushes due to overflow shm_cont 425735 0.62 SHM MTX contention shm_cycles 627 0.00 SHM cycles through buffer sms_nreq 37386 0.05 SMS allocator requests sms_nobj 0 . SMS outstanding allocations sms_nbytes 0 . SMS outstanding bytes sms_balloc 15664764 . SMS bytes allocated sms_bfree 15664764 . SMS bytes freed backend_req 3202957 4.69 Backend requests made n_vcl 5 0.00 N vcl total n_vcl_avail 5 0.00 N vcl available n_vcl_discard 0 0.00 N vcl discarded n_ban 1 . N total active bans n_ban_gone 1 . N total gone bans n_ban_add 1 0.00 N new bans added n_ban_retire 0 0.00 N old bans deleted n_ban_obj_test 0 0.00 N objects tested n_ban_re_test 0 0.00 N regexps tested against n_ban_dups 0 0.00 N duplicate bans removed hcb_nolock 29397883 43.04 HCB Lookups without lock hcb_lock 2304053 3.37 HCB Lookups with lock hcb_insert 2303884 3.37 HCB Inserts esi_errors 0 0.00 ESI parse errors (unlock) esi_warnings 0 0.00 ESI parse warnings (unlock) accept_fail 0 0.00 Accept failures client_drop_late 500 0.00 Connection dropped late uptime 682993 1.00 Client uptime dir_dns_lookups 0 0.00 DNS director lookups dir_dns_failed 0 0.00 DNS director failed lookups dir_dns_hit 0 0.00 DNS director cached lookups hit dir_dns_cache_full 0 0.00 DNS director full dnscache vmods 0 . Loaded VMODs n_gzip 0 0.00 Gzip operations n_gunzip 0 0.00 Gunzip operations LCK.sms.creat 1 0.00 Created locks LCK.sms.destroy 0 0.00 Destroyed locks LCK.sms.locks 112158 0.16 Lock Operations LCK.sms.colls 0 0.00 Collisions LCK.smp.creat 0 0.00 Created locks LCK.smp.destroy 0 0.00 Destroyed locks LCK.smp.locks 0 0.00 Lock Operations LCK.smp.colls 0 0.00 Collisions LCK.sma.creat 2 0.00 Created locks LCK.sma.destroy 0 0.00 Destroyed locks LCK.sma.locks 15953254 23.36 Lock Operations LCK.sma.colls 0 0.00 Collisions LCK.smf.creat 0 0.00 Created locks LCK.smf.destroy 0 0.00 Destroyed locks LCK.smf.locks 0 0.00 Lock Operations LCK.smf.colls 0 0.00 Collisions LCK.hsl.creat 0 0.00 Created locks LCK.hsl.destroy 0 0.00 Destroyed locks LCK.hsl.locks 0 0.00 Lock Operations LCK.hsl.colls 0 0.00 Collisions LCK.hcb.creat 1 0.00 Created locks LCK.hcb.destroy 0 0.00 Destroyed locks LCK.hcb.locks 4610661 6.75 Lock Operations LCK.hcb.colls 0 0.00 Collisions LCK.hcl.creat 0 0.00 Created locks LCK.hcl.destroy 0 0.00 Destroyed locks LCK.hcl.locks 0 0.00 Lock Operations LCK.hcl.colls 0 0.00 Collisions LCK.vcl.creat 1 0.00 Created locks LCK.vcl.destroy 0 0.00 Destroyed locks LCK.vcl.locks 367008 0.54 Lock Operations LCK.vcl.colls 0 0.00 Collisions LCK.stat.creat 1 0.00 Created locks LCK.stat.destroy 0 0.00 Destroyed locks LCK.stat.locks 29416662 43.07 Lock Operations LCK.stat.colls 0 0.00 Collisions LCK.sessmem.creat 1 0.00 Created locks LCK.sessmem.destroy 0 0.00 Destroyed locks LCK.sessmem.locks 29423687 43.08 Lock Operations LCK.sessmem.colls 0 0.00 Collisions LCK.wstat.creat 1 0.00 Created locks LCK.wstat.destroy 0 0.00 Destroyed locks LCK.wstat.locks 1392900 2.04 Lock Operations LCK.wstat.colls 0 0.00 Collisions LCK.herder.creat 1 0.00 Created locks LCK.herder.destroy 0 0.00 Destroyed locks LCK.herder.locks 49701 0.07 Lock Operations LCK.herder.colls 0 0.00 Collisions LCK.wq.creat 2 0.00 Created locks LCK.wq.destroy 0 0.00 Destroyed locks LCK.wq.locks 60646593 88.80 Lock Operations LCK.wq.colls 0 0.00 Collisions LCK.objhdr.creat 2312624 3.39 Created locks LCK.objhdr.destroy 2311197 3.38 Destroyed locks LCK.objhdr.locks 125013176 183.04 Lock Operations LCK.objhdr.colls 0 0.00 Collisions LCK.exp.creat 1 0.00 Created locks LCK.exp.destroy 0 0.00 Destroyed locks LCK.exp.locks 7030656 10.29 Lock Operations LCK.exp.colls 0 0.00 Collisions LCK.lru.creat 2 0.00 Created locks LCK.lru.destroy 0 0.00 Destroyed locks LCK.lru.locks 3174709 4.65 Lock Operations LCK.lru.colls 0 0.00 Collisions LCK.cli.creat 1 0.00 Created locks LCK.cli.destroy 0 0.00 Destroyed locks LCK.cli.locks 138 0.00 Lock Operations LCK.cli.colls 0 0.00 Collisions LCK.ban.creat 1 0.00 Created locks LCK.ban.destroy 0 0.00 Destroyed locks LCK.ban.locks 7030659 10.29 Lock Operations LCK.ban.colls 0 0.00 Collisions LCK.vbp.creat 1 0.00 Created locks LCK.vbp.destroy 0 0.00 Destroyed locks LCK.vbp.locks 0 0.00 Lock Operations LCK.vbp.colls 0 0.00 Collisions LCK.vbe.creat 1 0.00 Created locks LCK.vbe.destroy 0 0.00 Destroyed locks LCK.vbe.locks 248892 0.36 Lock Operations LCK.vbe.colls 0 0.00 Collisions LCK.backend.creat 1 0.00 Created locks LCK.backend.destroy 0 0.00 Destroyed locks LCK.backend.locks 6642998 9.73 Lock Operations LCK.backend.colls 0 0.00 Collisions SMA.s0.c_req 6386718 9.35 Allocator requests SMA.s0.c_fail 0 0.00 Allocator failures SMA.s0.c_bytes 416270568314 609479.99 Bytes allocated SMA.s0.c_freed 416266623952 609474.22 Bytes freed SMA.s0.g_alloc 2134 . Allocations outstanding SMA.s0.g_bytes 3944362 . Bytes outstanding #目前占用的内存大小 SMA.s0.g_space 6438506582 . Bytes available #可用内存大小 SMA.Transient.c_req 14033 0.02 Allocator requests SMA.Transient.c_fail 0 0.00 Allocator failures SMA.Transient.c_bytes 908921608 1330.79 Bytes allocated SMA.Transient.c_freed 908921608 1330.79 Bytes freed SMA.Transient.g_alloc 0 . Allocations outstanding SMA.Transient.g_bytes 0 . Bytes outstanding SMA.Transient.g_space 0 . Bytes available
如上所示,经常需要关注项我都在后面加了中文注解 。特别需要注意的是,命中率是通过以下公式进行计算的。
cache_hit_percent = ( cache_hit / ( cache_hit + cache_miss ) ) * 100
需要能过varnish只把关注的参数显示出来的话,可以用下面的命令:
/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 或 /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 。