-
Notifications
You must be signed in to change notification settings - Fork 1
/
60_redis.py
111 lines (92 loc) · 3.58 KB
/
60_redis.py
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
#!/bin/env python
# -*- coding:utf-8 -*-
import os
import time
import json
import redis
import socket
import re
# alert
# mem_fragmentation_ratio < 1 : use swap
# rdb_last_bgsave_status = 0
# master_link_status = 0
# aof_last_bgrewrite_status = 0
# aof_last_write_status = 0
Entry = {
"Endpoint": socket.gethostname(),
"Timestamp": int(time.time()),
"Step": 60,
}
def redis_metric(result, port):
def guage(metric, value):
res = Entry.copy()
res.update({
"CounterType": "GAUGE",
"Metric": "redis.{0}".format(metric),
"TAGS": "type=redis,port={0}".format(port),
"Value": value
})
return res
def count(metric, value):
res = Entry.copy()
res.update({
"CounterType": "COUNT",
"Metric": "redis.{0}".format(metric),
"TAGS": "type=redis,port={0}".format(port),
"Value": value
})
return res
try:
r = redis.StrictRedis(host='localhost', port=port, password='X')
info = r.info()
except redis.exceptions.ResponseError:
r = redis.StrictRedis(host='localhost', port=port)
info = r.info()
# clients
connected_clients = int(info['connected_clients'])
connected_clients_pct = float(info['connected_clients']) / int(r.config_get('maxclients')['maxclients']) * 100
result.append(guage('connected_clients', connected_clients))
result.append(guage('connected_clients_pct', connected_clients_pct))
# memory
used_memory = float(info['used_memory'])
used_memory_pct = used_memory / int(info['maxmemory']) * 100
mem_fragmentation_ratio = info['mem_fragmentation_ratio']
result.append(guage('used_memory', used_memory))
result.append(guage('used_memory_pct', used_memory_pct))
result.append(guage('mem_fragmentation_ratio', mem_fragmentation_ratio))
# hit
keyspace_hits = int(info['keyspace_hits'])
keyspace_misses = int(info['keyspace_misses'])
keyspace_total = keyspace_hits + keyspace_misses
if keyspace_total == 0:
keyspace_hit_ratio = 0
else:
keyspace_hit_ratio = keyspace_hits / (keyspace_hits + keyspace_misses) * 100
result.append(guage('keyspace_hits', keyspace_hits))
result.append(guage('keyspace_hit_ratio', keyspace_hit_ratio))
# network
result.append(guage('instantaneous_input_kbps', info['instantaneous_input_kbps']))
result.append(guage('instantaneous_output_kbps', info['instantaneous_output_kbps']))
# commands
result.append(guage('instantaneous_ops_per_sec', info['instantaneous_ops_per_sec']))
result.append(count('total_commands_processed', info['total_commands_processed']))
# save
rdb_last_bgsave_status = 1 if info['rdb_last_bgsave_status'] == 'ok' else 0
aof_last_bgrewrite_status = 1 if info["aof_last_bgrewrite_status"] == 'ok' else 0
aof_last_write_status = 1 if info["aof_last_write_status"] == 'ok' else 0
result.append(guage('rdb_last_bgsave_status', rdb_last_bgsave_status))
result.append(guage('aof_last_bgrewrite_status', aof_last_bgrewrite_status))
result.append(guage('aof_last_write_status', aof_last_write_status))
# repl
role = info['role']
if role == 'slave':
master_link_status = 1 if info['master_link_status'] == 'up' else 0
result.append(guage('master_link_status', master_link_status))
if __name__ == "__main__":
result = []
for f in os.listdir('/etc/redis'):
rerz = re.findall('redis-(\d+).conf', f)
if rerz:
port = rerz[0]
redis_metric(result, port)
print(json.dumps(result))