not tested redis module
paulfantom committed
Jul 12, 2016 at 16:35 UTC
ab209cd429e4b66183a7bf26e127790d7cd85cb3
1 file changed
+114
python.d/redis.chart.py
new
+114
@@ -0,0 +1,114 @@
1
+# -*- coding: utf-8 -*-
2
+# Description: redis netdata python.d module
3
+# Author: Pawel Krupa (paulfantom)
4
+
5
+from base import SocketService
6
+
7
+# default module values (can be overridden per job in `config`)
8
+#update_every = 2
9
+priority = 60000
10
+retries = 5
11
+
12
+# default job configuration (overridden by python.d.plugin)
13
+# config = {'local': {
14
+# 'update_every': update_every,
15
+# 'retries': retries,
16
+# 'priority': priority,
17
+# 'host': 'localhost',
18
+# 'port': 6379,
19
+# 'unix_socket': None
20
+# }}
21
+
22
+ORDER = ['operations', 'hit_rate', 'memory', 'keys', 'clients', 'slaves']
23
+
24
+CHARTS = {
25
+ 'operations': {
26
+ 'options': [None, 'Operations', 'operations/s', 'Statistics', 'redis.statistics', 'line'],
27
+ 'lines': [
28
+ ['instantaneous_ops_per_sec', None, 'absolute']
29
+ ]},
30
+ 'hit_rate': {
31
+ 'options': [None, 'Hit rate', 'percent', 'Statistics', 'redis.statistics', 'line'],
32
+ 'lines': [
33
+ ['hit_rate', None, 'absolute']
34
+ ]},
35
+ 'memory': {
36
+ 'options': [None, 'Memory utilization', 'bytes', 'Memory', 'redis.memory', 'line'],
37
+ 'lines': [
38
+ ['used_memory', None, 'absolute'],
39
+ ['memory_', None, 'absolute'],
40
+ ['memory_', None, 'absolute']
41
+ ]},
42
+ 'keys': {
43
+ 'options': [None, 'Database keys', 'keys', 'Keys', 'redis.keys', 'line'],
44
+ 'lines': [
45
+ # lines are created dynamically in `check()` method
46
+ ]},
47
+ 'clients': {
48
+ 'options': [None, 'Clients', 'clients', 'Clients', 'redis.clients', 'line'],
49
+ 'lines': [
50
+ ['connected_clients', None, 'absolute'],
51
+ ['blocked_clients', None, 'absolute']
52
+ ]},
53
+ 'slaves': {
54
+ 'options': [None, 'Slaves', 'slaves', 'Replication', 'redis.replication', 'line'],
55
+ 'lines': [
56
+ ['connected_slaves', None, 'absolute']
57
+ ]}
58
+}
59
+
60
+
61
+class Service(SocketService):
62
+ def __init__(self, configuration=None, name=None):
63
+ SocketService.__init__(self, configuration=configuration, name=name)
64
+ self.request = "INFO\r\n"
65
+ self.host = "localhost"
66
+ self.port = 6379
67
+ self.unix_socket = None
68
+ self.order = ORDER
69
+ self.definitions = CHARTS
70
+
71
+ def _get_data(self):
72
+ """
73
+ Get data from socket
74
+ :return: dict
75
+ """
76
+ try:
77
+ raw = self._get_raw_data().split("\n")
78
+ except AttributeError:
79
+ return None
80
+ data = {}
81
+ for line in raw:
82
+ if line.startswith(('instantaneous', 'keyspace', 'used_memory', 'connected', 'blocked')):
83
+ try:
84
+ t = line.split(':')
85
+ data[t[0]] = int(t[1])
86
+ except (IndexError, ValueError):
87
+ pass
88
+ elif line.startswith('db'):
89
+ tmp = line.split(',')[0].replace('keys=', '')
90
+ record = tmp.split(':')
91
+ data[record[0]] = int(record[1])
92
+ try:
93
+ data['hit_rate'] = int((data['keyspace_hits'] / (data['keyspace_hits'] + data['keyspace_misses'])) * 100)
94
+ except:
95
+ data['hit_rate'] = 0
96
+
97
+ return data
98
+
99
+ def check(self):
100
+ """
101
+ Parse configuration, check if redis is available, and dynamically create chart lines data
102
+ :return: boolean
103
+ """
104
+ self._parse_config()
105
+ data = self._get_data()
106
+ if data is None:
107
+ self.error("No data received")
108
+ return False
109
+
110
+ for name in data:
111
+ if name.startswith('db'):
112
+ self.definitions['keys']['lines'].append(data[name, None, 'absolute'])
113
+
114
+ return True