python 监视Twemproxy

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 监视Twemproxy相关的知识,希望对你有一定的参考价值。

### Option: UserParameter
#       User-defined parameter to monitor. There can be several user-defined parameters.
#       Format: UserParameter=<key>,<shell command>
#       See 'zabbix_agentd' directory for examples.
#
# Mandatory: no
# Default:
# UserParameter=
UserParameter=redis_pool.discovery,/usr/bin/python /usr/local/zabbix/bin/twemproxy_pools_discovery.py
UserParameter=redis_server.discovery,/usr/bin/python /usr/local/zabbix/bin/twemproxy_pools_servers_discovery.py
UserParameter=nutcracker_info[*],/usr/bin/python /etc/zabbix/zabbix_agentd.d/twemproxy_status.py --metric $1
UserParameter=nutcracker_pool_info[*],/usr/bin/python /etc/zabbix/zabbix_agentd.d/twemproxy_status.py --metric $1 --poolname $2
UserParameter=nutcracker_server_info[*],/usr/bin/python /etc/zabbix/zabbix_agentd.d/twemproxy_status.py --metric $1 --poolserver $2
#! /usr/bin/env python
import socket
import json
import argparse
 
#{
#    "service": "nutcracker",
#    "source": "gintama-taiwan-redis1",
#    "version": "0.4.0",
#    "uptime": 136873,
#    "timestamp": 1427168759,
#    "total_connections": 489,
#    "curr_connections": 7,
#    "gintama": {
#        "client_eof": 0,
#        "client_err": 420,
#        "client_connections": 0,
#        "server_ejects": 62,
#        "forward_error": 57,
#        "fragments": 0,
#        "192.168.100.68:6379": {
#            "server_eof": 1,
#            "server_err": 10,
#            "server_timedout": 0,
#            "server_connections": 1,
#            "server_ejected_at": 1427074216548518,
#            "requests": 627,
#            "request_bytes": 329980,
#            "responses": 617,
#            "response_bytes": 280709,
#            "in_queue": 0,
#            "in_queue_bytes": 0,
#            "out_queue": 0,
#            "out_queue_bytes": 0
#        },
#
#
 
##"{#REDIS_SERVER}":"gintama@192.168.100.69:6381"
##"{#REDIS_POOL}":"gintama"
 
 
 
 
class NutcrackerServer(object):
    def __init__(self): 
        self.server = '127.0.0.1'
        self.port = '22222'
 
    def nutcracker_status(self):        
        conn = socket.create_connection((self.server, self.port))
        buf = True
        content = ''
 
        while buf:
            buf = conn.recv(4096)
            content += buf
        conn.close()
        self.data = json.loads(content)
 
    def nutcracker_info(self,base_metric):
        self.nutcracker_status()
        print self.data[base_metric]
 
    def nutcracker_pool_info(self,pool,pool_metric):
        self.nutcracker_status()
        print self.data[pool][pool_metric]
 
    def nutcracker_server_info(self,pool,redis_server,server_metric):
        self.nutcracker_status()
        print self.data[pool][redis_server][server_metric]
 
 
def parse_args():
    parser=argparse.ArgumentParser(description='Twemproxy monitoring tool with Zabbix!', argument_default=False)
    parser.add_argument('--metric',default='service',dest='metric',action='store',required=True,help='the twemproxy metric,such as uptime or version,etc')
    parser.add_argument("--poolname",default='gintama',dest='poolname',action='store',required=False,help='the twemproxy pool and server discoveried by zabbix')
    parser.add_argument("--poolserver",default='gintamaXXXX192.168.100.69:6380',dest='poolserver',action='store',required=False,help='the twemproxy pool and server discoveried by zabbix')
    args=parser.parse_args()
    return args
 
def main():
    args=parse_args()
    metric=''
    pool=''
    redis_server=''
    if args.metric:       
       metric=args.metric
    else:
       print "invalid metric" 
    if args.poolname:
       pool=args.poolname
    else:
       print "invalid pool name"
 
    if args.poolserver:
       pool=args.poolserver.split('XXXX')[0]
       redis_server=args.poolserver.split('XXXX')[1]
    else:
       print "invalid pool server"
 
    twemproxy = NutcrackerServer()
     
 
    if not metric:    
       print "invalid metric"
    elif metric in ["service","source","version","uptime","timestamp","total_connections","curr_connections"]:
#       print "nutcracker base information"
       twemproxy.nutcracker_info(metric)
    elif pool!="" and metric in ["client_eof","client_err","client_connections","server_ejects","forward_error","fragments"]:
#       print "nutcracker" + " " + pool + " " + "information"
       twemproxy.nutcracker_pool_info(pool,metric)
    elif pool and redis_server and metric in ["server_eof","server_err","server_timedout","server_connections","server_ejected_at","requests","request_bytes","responses","response_bytes","in_queue","in_queue_bytes","out_queue","out_queue_bytes"]:
#       print "nutcracker" + " " + pool + " " + redis_server + " " + "information"
       twemproxy.nutcracker_server_info(pool,redis_server,metric)
    else:
       print "invalid metric" 
 
 
 
if __name__ == '__main__':
     main()

以上是关于python 监视Twemproxy的主要内容,如果未能解决你的问题,请参考以下文章

twemproxy发送流程探索——剖析twemproxy代码正编

twemproxy架构分析

[转]Twemproxy 介绍与使用

Redis集群高可用(Keepalived+Twemproxy)

laravel项目利用twemproxy部署redis集群的完整步骤

Linux-Centos7源码编译安装Twemproxy服务