#!/bin/python
-- coding:UTF-8 --
import redis
import sys
"""
各个参数的解释:
keyspace_misses //表示未命中数
keyspace_hits //表示命中数
keyspace_hits_rate = keyspace_hits /(keyspace_hits + keyspace_misses)
connected_clients //客户端连接数
blocked_clients //客户端阻塞数
connected_slaves //从库数
instantaneous_ops_per_sec //客户端每秒执行命令频率
used_memory_rss //操作系统分配给redis的内存
used_memory //redis分配器分配的内存
mem_fragmentation_ratio //内存碎片比例
"""
#把参数定义为列表
keyindex = [‘used_memory‘, ‘used_memory_rss‘, ‘mem_fragmentation_ratio‘, ‘blocked_clients‘, ‘connected_clients‘,
‘connected_slaves‘,
‘instantaneous_ops_per_sec‘, ‘keyspace_hits‘, ‘keyspace_misses‘, ‘keypace_query_total_count‘,
‘keyspace_hits_rate‘, ‘status‘]
returnval = None
def zabbix_faild():
print "ZBX_NOTSUPPORTED"
sys.exit(2)
if len(sys.argv) != 2: #需要有一个参数,加上程序本身是两个参数。所以判断如果没有参数,就直接提示
print len(sys.argv)
zabbix_faild()
try:
conn=redis.Redis(host=‘172.16.17.40‘,port=‘6379‘,password=‘‘)
except Exception,e:
print e
zabbix_faild()
#下面是根据参数来判断并且取值,最终返回状态,加入到zabbix中
if sys.argv[1] in keyindex:
if sys.argv[1] == ‘status‘:#如果参数为status ,执行ping,为true返回值为1,zabbix中返回1 则表示正常。
try:
conn.ping()
returnval = 1
except Exception,e:
returnval = 0
elif sys.argv[1] == ‘keyspace_hits_rate‘:
merit = conn.info()
keyspace_hits_count = float(merit[‘keyspace_hits‘])
keyspace_misses_count = float(merit[‘keyspace_misses‘])
keyspace_hits_rate = keyspace_hits_count / (keyspace_hits_count + keyspace_misses_count) * 100
returnval = keyspace_hits_rate
elif sys.argv[1] == ‘keypace_query_total_count‘:
merit = conn.info()
keyspace_hits_count = merit[‘keyspace_hits‘]
keyspace_misses_count = merit[‘keyspace_misses‘]
keypace_query_total_count = keyspace_hits_count + keyspace_misses_count
returnval = keypace_query_total_count
else:
merit = conn.info()
try:
returnval = merit[unicode(sys.argv[1])]
except Exception,e:
pass
#判断返回值状态的函数
def ret_status ():
if returnval == None:
zabbix_faild()
else:
print returnval
ret_status()
原文地址:http://blog.51cto.com/3722525/2333671