用过varnish的对varnishstat肯定不陌生。直接反应varnish当前运行的状态,相比较这种当前状态的展示,我更习惯看历史曲线,所以我又将varnish与zabbix结合在一起了。

   首先,定义zabbix自定义监控

   怎么做,这就不详细说了,可以参考:http://shanks.blog.51cto.com/3899909/1310757

   我这只把自定义脚本和zabbix-agent配置文件贴出来

   自定义脚本(varnish_stat)

#!/usr/bin/python
#monitor varnish3.0 status,use zabbix.
# -*- coding: UTF-8 -*-
import commands
import sys
import json
def get_varnish_stat(type):
    varnishstat_cmd = commands.getoutput('/usr/bin/varnishstat -j')
    varnishstatus = json.loads(varnishstat_cmd)
    print varnishstatus[type]['value']
                         
def show_help():
    print '''
    %s [segment]
    ---------------------------
    [segment:]
        N_struct_object
            varnish obj number.
        Client_requests_received
            number of client request.
        Cache_hits
            in all client requests ,hit in cache number.
        Cache_misses
            in all client requests ,not hit in cache number.
        N_expired_objects
            over time obj number.
        N_LRU_nuked_objects
            no free cache storage,to be moved obj number
        Total_header_bytes
            header bytes in all storage.
        Total_body_bytes
            body bytes in all storage.
    ''' % sys.argv[0]
    sys.exit(1)
def main_():
    if len(sys.argv) == 2:
        if (sys.argv[1]) == 'N_struct_object':
            get_varnish_stat('n_object')
        elif (sys.argv[1]) == 'Client_requests_received':
            get_varnish_stat('client_req')
        elif (sys.argv[1]) == 'Cache_hits':
            get_varnish_stat('cache_hit')
        elif (sys.argv[1]) == 'Cache_misses':
            get_varnish_stat('cache_miss')
        elif (sys.argv[1]) == 'N_expired_objects':
            get_varnish_stat('n_expired')
        elif (sys.argv[1]) == 'N_LRU_nuked_objects':
            get_varnish_stat('n_lru_nuked')
        elif (sys.argv[1]) == 'Total_header_bytes':
            get_varnish_stat('s_hdrbytes')
        elif (sys.argv[1]) == 'Total_body_bytes':
            get_varnish_stat('s_bodybytes')
        else:
            show_help()
    else:
        show_help()
main_()

   zabbix-agentd配置文件(只包含varnish自定义监控部分)

# ***For Varnish***
UserParameter=varnish.stat[*],/usr/bin/python /usr/local/zabbix/script/varnish/varnish_stat $1

   效果展示

用zabbix监控varnish运行状态_第1张图片

用zabbix监控varnish运行状态_第2张图片

用zabbix监控varnish运行状态_第3张图片

用zabbix监控varnish运行状态_第4张图片

用zabbix监控varnish运行状态_第5张图片