一个简单的监控redis性能的python脚本

释放双眼,带上耳机,听听看~!

一个简单的监控redis性能的python脚本

上一篇已经讲了如何监控memcached了,现在也顺带讲如何监控redis。

首先介绍下监控redis那些信息:

  • Redis ping:检验ping
  • Redis alive:查看检查端口是否alive
  • Redis connections:查看连接数
  • Redis blockedClients:正在等待阻塞客户端数量
  • Redis connectionsUsage:redis的连接使用率
  • Redis memoryUsage:redis内存使用量
  • Redis memoryUsageRate:redis内存使用率
  • Redis evictedKeys:运行以来删除过的key的数量
  • Redis rejectedConnections:拒绝连接数
  • Redis ops:redis的OPS
  • Redis hitRate:redis命中率

安装需要的环境


1
2
1pip install redis
2

脚本内容


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3__author__ = 'chenmingle'
4
5import sys
6import subprocess
7import json
8
9try:
10    import redis
11except Exception, e:
12    print 'pip install redis'
13    sys.exit(1)
14
15class Redis(object):  
16    def __init__(self, host, port, password=None):
17        self.host = host
18        self.port = port
19        self.password = password
20        if self.password:
21            self.rds = redis.StrictRedis(host=host, port=port, password=self.password)
22        else:
23            self.rds = redis.StrictRedis(host=host, port=port)
24        try:
25            self.info = self.rds.info()
26        except Exception, e:
27            self.info = None
28
29    def redis_connections(self):
30        try:
31            return self.info['connected_clients']
32        except Exception, e:
33            return 0
34
35    def redis_connections_usage(self):
36        try:
37            curr_connections = self.redis_connections()
38            max_clients = self.parse_config('maxclients')
39            rate = float(curr_connections) / float(max_clients)
40            return "%.2f" % (rate * 100)
41        except Exception, e:
42            return 0
43
44    def redis_used_memory(self):
45        try:
46            return self.info['used_memory']
47        except Exception, e:
48            return 0
49
50    def redis_memory_usage(self):
51        try:
52            used_memory = self.info['used_memory']
53            max_memory = self.info['maxmemory']
54            system_memory = self.info['total_system_memory']
55            if max_memory:
56                rate = float(used_memory) / float(max_memory)
57            else:
58                rate = float(used_memory) / float(system_memory)
59            return "%.2f" % (rate * 100)
60        except Exception, e:
61            return 0
62
63    def redis_ping(self):
64        try:
65            return self.rds.ping()
66        except Exception, e:
67            return False
68
69    def rejected_connections(self):
70        try:
71            return self.info['rejected_connections']
72        except Exception, e:
73            return 999
74
75    def evicted_keys(self):
76        try:
77            return self.info['evicted_keys']
78        except Exception, e:
79            return 999
80
81    def blocked_clients(self):
82        try:
83            return self.info['blocked_clients']
84        except Exception, e:
85            return 0
86
87    def ops(self):
88        try:
89            return self.info['instantaneous_ops_per_sec']
90        except Exception, e:
91            return 0
92
93    def hitRate(self):
94        try:
95            misses = self.info['keyspace_misses']
96            hits = self.info['keyspace_hits']
97            rate = float(hits) / float(int(hits) + int(misses))
98            return "%.2f" % (rate * 100)
99        except Exception, e:
100            return 0
101
102    def parse_config(self, type):
103        try:
104            return self.rds.config_get(type)[type]
105        except Exception, e:
106            return None
107
108    def test(self):
109        print 'Redis ping: %s' % self.redis_ping()
110        print 'Redis alive: %s ' % check_alive(self.host, self.port)
111        print 'Redis connections: %s' % self.redis_connections()
112        print 'Redis blockedClients %s' % self.blocked_clients()
113        print 'Redis connectionsUsage: %s%%' % self.redis_connections_usage()
114        print 'Redis memoryUsage: %s' % self.redis_used_memory()
115        print 'Redis memoryUsageRate: %s%%' % self.redis_memory_usage()
116        print 'Redis evictedKeys: %s' % self.evicted_keys()
117        print 'Redis rejectedConnections: %s' % self.rejected_connections()
118        print 'Redis ops: %s' % self.ops()
119        print 'Redis hitRate: %s%%' % self.hitRate()
120
121def check_alive(host, port):
122    cmd = 'nc -z %s %s > /dev/null 2>&1' % (host, port)
123    return subprocess.call(cmd, shell=True)
124
125def parse(type, host, port, password):
126    rds = Redis(host, port, password)
127    if type == 'connections':
128        print rds.redis_connections()
129    elif type == 'connectionsUsage':
130        print rds.redis_connections_usage()
131    elif type == 'blockedClients':
132        print rds.blocked_clients()
133    elif type == 'ping':
134        print rds.redis_ping()
135    elif type == 'alive':
136        print check_alive(host, port)
137    elif type == 'memoryUsage':
138        print rds.redis_used_memory()
139    elif type == 'memoryUsageRate':
140        print rds.redis_memory_usage()
141    elif type == 'rejectedConnections':
142        print rds.rejected_connections()
143    elif type == 'evictedKeys':
144        print rds.evicted_keys()
145    elif type == 'hitRate':
146        print rds.hitRate()
147    elif type == 'ops':
148        print rds.ops()
149    else:
150        rds.test()
151
152if __name__ == '__main__':
153    try:
154        type = sys.argv[1]
155        host = sys.argv[2]
156        port = sys.argv[3]
157        if sys.argv.__len__() >=5:
158            password = sys.argv[4]
159        else:
160            password = None
161    except Exception, e:
162        print "Usage: python %s type 127.0.0.1 6379" % sys.argv[0]
163        sys.exit(1)
164    parse(type, host, port, password)
165

测试脚本,查看监控redis信息(假如redis没设置密码,可不填密码执行):


1
2
3
4
5
6
7
8
9
10
11
12
13
1/bin/python /home/python/check_redis.py test 192.168.4.18 6379 password
2Redis ping: True
3Redis alive: 0
4Redis connections: 447
5Redis blockedClients 0
6Redis connectionsUsage: 4.47%
7Redis memoryUsage: 2885122048
8Redis memoryUsageRate: 17.32%
9Redis evictedKeys: 0
10Redis rejectedConnections: 0
11Redis ops: 1050
12Redis hitRate: 71.87%
13

最后加入到zabbix自定义key上


1
2
3
4
1cat /etc/zabbix/zabbix_agentd.d/redis.conf
2# Redis
3UserParameter=redis.stats[*],/bin/python /home/python/check_redis.py $1 192.168.4.18 6379 password
4

接下来加入自定义模版的操作可以参考上一篇博客

给TA打赏
共{{data.count}}人
人已打赏
安全技术

c++ list, vector, map, set 区别与用法比较

2022-1-11 12:36:11

安全技术

umi搭建react+antd项目(一)环境配置

2021-12-21 16:36:11

个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索