. 1 # ! / Usr / local / python / shims / python 2 from rediscluster Import StrictRedisCluster . 3 '' ' . 4 need to rediscluster the host in python . 5 PIP the install rediscluster . 6 ' '' . 7 . 8 Import Redis . 9 Import SYS 10 Import MyLog . 11 = redis_nodes [{ ' Host ' : ' IP ' , ' Port ' : Port}, 12 is { ' 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 is . 19 # ## 20 is entered redis trunked mode, if anomaly, logged, and terminates the script 21 is # ## 22 is 23 is the try : 24 redisconn StrictRedisCluster = (startup_nodes = redis_nodes, password = ' pwd ' ) 25 26 is the except Exception, E: 27 mylog.logging.error ( ' % S ' % E) 28 the sys.exit (0) 29 30 # ## 31 is defined parameter 32 # ## 33 is 34 is 35 Data = {} 36 NODEDATA = {} 37 [ HIT = 0 38 is misshit = 0 39 HitRate = 0.00 40 41 is # ## 42 is defined function, gripping monitored item 43 is # ## 44 is 45 46 is 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 ((a float (HIT) / a float (HIT + misshit)),. 3 ) 131 is Item = HitRate 132 return item 133 the else : 134 return 9999 135 136 # ## 137 script parameter passing, zabbix acquisition monitored item 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 write a redis monitoring template to monitor some basic indicators