首页 > 编程语言 > 详细

python-Redis cluster基础指标监控

时间:2019-11-26 18:54:19      阅读:77      评论:0      收藏:0      [点我收藏+]
  1 #!/usr/local/python/shims/python
  2 from rediscluster import StrictRedisCluster
  3 ‘‘‘
  4 需要在宿主机python中安装rediscluster
  5 pip install rediscluster
  6 ‘‘‘
  7 
  8 import redis
  9 import sys
 10 import mylog
 11 redis_nodes =  [{host:ip,port:port},
 12                 {host:ip,port:port},
 13                 {host:ip,port:port},
 14                 {host:ip,port:port},
 15                 {host:ip,port:port},
 16                 {host:ip,port:port},
 17                    ]
 18 
 19 ###
 20 进入redis集群模式,如果异常,记录到日志中,并终止脚本
 21 ###
 22 
 23 try:
 24     redisconn = StrictRedisCluster(startup_nodes=redis_nodes,password=pwd)
 25     
 26 except Exception,e:
 27     mylog.logging.error(%s %e )
 28     sys.exit(0)
 29 
 30 ###
 31 定义参数
 32 ###
 33 
 34 
 35 data = {}
 36 NodeData = {}
 37 hit = 0
 38 misshit = 0
 39 hitrate = 0.00
 40 
 41 ###
 42 定义函数,抓取监控项
 43 ###
 44 
 45 
 46 def ClusterState(item):
 47     cluster_state = redisconn.execute_command(cluster,info)
 48     cluster_state = cluster_state.split(\r\n)
 49     try:
 50         for i in cluster_state:
 51             data[i.split(:)[0]] = i.split(:)[1]
 52     except:
 53         pass
 54     if  item == clusterstatus:
 55         state = data[cluster_state]
 56         if state == ok :
 57             item = 1
 58         else:
 59             item = 0
 60         return item
 61 
 62     elif item == clusterslotsfail:
 63         item = data[cluster_slots_fail]
 64         return item  
 65 
 66     elif item == clusterknownnodes:
 67         item = data[cluster_known_nodes]
 68         return item
 69     else:
 70         return 9999
 71 def NodeInfoServer(item):
 72     node_info = redisconn.info(Server)
 73     NodeData = node_info[ip:port]
 74     if item == uptime_in_days:
 75         item = NodeData[uptime_in_days]
 76         return item
 77     else:
 78         return 9999   
 79     
 80 def NodeInfoClients(item):
 81     node_info = redisconn.info(Clients)
 82     NodeData = node_info[ip:port]
 83     if item == connected_clients:
 84         item = NodeData[connected_clients]
 85         return item
 86     else:
 87         return 9999
 88 
 89 def NodeInfoMemory(item):
 90     node_info = redisconn.info(Memory)
 91     NodeData = node_info[ip:port]
 92     if item == used_memory_human:
 93         item = NodeData[used_memory_human]
 94         return item
 95     elif item == total_system_memory_human:
 96         item = NodeData[total_system_memory_human]
 97         return item
 98     else:
 99         return 9999
100 
101 def NodeInfoPersistence(item):
102     node_info = redisconn.info(Persistence)
103     NodeData = node_info[ip:port]
104     
105     if item == rdb_last_bgsave_status:
106         item = NodeData[rdb_last_bgsave_status]    
107         if item == ok :
108             item = 1
109         else:
110             item = 0
111         return item
112     else:
113         return 9999
114 
115 def NodeInfoStats(item):
116     node_info = redisconn.info(Stats)
117     NodeData = node_info[ip:port]
118     if item == instantaneous_ops_per_sec:
119         item = NodeData[instantaneous_ops_per_sec]
120         return item
121     elif item == instantaneous_input_kbps:
122         item = NodeData[instantaneous_input_kbps]
123         return item
124     elif item == instantaneous_output_kbps:
125         item = NodeData[instantaneous_output_kbps]
126         return item
127     elif item == hit:
128         hit =  NodeData[keyspace_hits] 
129         misshit = NodeData[keyspace_misses]
130         hitrate = round((float(hit) / float(hit + misshit)) ,3)
131         item = hitrate
132         return item        
133     else:
134         return 9999
135 
136 ###
137 脚本传参,zabbix获取监控项
138 ###
139 
140 if sys.argv[1] == status:
141      print ClusterState(clusterstatus)
142 elif sys.argv[1] == slotsfail: 
143      print ClusterState(clusterslotsfail)
144 elif sys.argv[1] == nodes:
145      print ClusterState(clusterknownnodes)
146 elif sys.argv[1] == day:
147      print NodeInfoServer(uptime_in_days)
148 elif sys.argv[1] == clients:
149      print NodeInfoClients(connected_clients)
150 elif sys.argv[1] == usememory:
151      print NodeInfoMemory(used_memory_human)
152 elif sys.argv[1] == sysmemory:
153      print NodeInfoMemory(total_system_memory_human)
154 elif sys.argv[1] == rdb:
155      print NodeInfoPersistence(rdb_last_bgsave_status)
156 elif sys.argv[1] == ops:
157      print NodeInfoStats(instantaneous_ops_per_sec)
158 elif sys.argv[1] == input_kbps:
159      print NodeInfoStats(instantaneous_input_kbps)
160 elif sys.argv[1] == output_kbps:
161      print NodeInfoStats(instantaneous_output_kbps)
162 elif sys.argv[1] == hit:
163      print NodeInfoStats(hit)

 

 

#python写一个redis监控模版,监控一些基础指标

python-Redis cluster基础指标监控

原文:https://www.cnblogs.com/Jame-mei/p/11937433.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!