1 # -*- coding: utf-8 -*-
2 # Description: redis netdata python.d module
3 # Author: Pawel Krupa (paulfantom)
5 from base import SocketService
7 # default module values (can be overridden per job in `config`)
12 # default job configuration (overridden by python.d.plugin)
13 # config = {'local': {
14 # 'update_every': update_every,
16 # 'priority': priority,
17 # 'host': 'localhost',
22 ORDER = ['operations', 'hit_rate', 'memory', 'keys', 'clients', 'slaves']
26 'options': [None, 'Operations', 'operations/s', 'Statistics', 'redis.statistics', 'line'],
28 ['instantaneous_ops_per_sec', 'operations', 'absolute']
31 'options': [None, 'Hit rate', 'percent', 'Statistics', 'redis.statistics', 'line'],
33 ['hit_rate', 'rate', 'absolute']
36 'options': [None, 'Memory utilization', 'kilobytes', 'Memory', 'redis.memory', 'line'],
38 ['used_memory', 'total', 'absolute', 1, 1024],
39 ['used_memory_lua', 'lua', 'absolute', 1, 1024]
42 'options': [None, 'Database keys', 'keys', 'Keys', 'redis.keys', 'line'],
44 # lines are created dynamically in `check()` method
47 'options': [None, 'Clients', 'clients', 'Clients', 'redis.clients', 'line'],
49 ['connected_clients', 'connected', 'absolute'],
50 ['blocked_clients', 'blocked', 'absolute']
53 'options': [None, 'Slaves', 'slaves', 'Replication', 'redis.replication', 'line'],
55 ['connected_slaves', 'connected', 'absolute']
60 class Service(SocketService):
61 def __init__(self, configuration=None, name=None):
62 SocketService.__init__(self, configuration=configuration, name=name)
63 self.request = "INFO\r\n"
64 self.host = "localhost"
66 self.unix_socket = None
68 self.definitions = CHARTS
69 self._keep_alive = True
78 raw = self._get_raw_data().split("\n")
79 except AttributeError:
80 self.error("no data received")
84 if line.startswith(('instantaneous', 'keyspace', 'used_memory', 'connected', 'blocked')):
87 data[t[0]] = int(t[1])
88 except (IndexError, ValueError):
90 elif line.startswith('db'):
91 tmp = line.split(',')[0].replace('keys=', '')
92 record = tmp.split(':')
93 data[record[0]] = int(record[1])
95 data['hit_rate'] = int((data['keyspace_hits'] / float(data['keyspace_hits'] + data['keyspace_misses'])) * 100)
100 self.error("received data doesn't have needed records")
105 def _check_raw_data(self, data):
107 Check if all data has been gathered from socket.
108 Parse first line containing message length and check against received message
113 supposed = data.split('\n')[0][1:]
114 offset = len(supposed) + 4 # 1 dollar sing, 1 new line character + 1 ending sequence '\r\n'
115 supposed = int(supposed)
116 if length - offset >= supposed:
125 Parse configuration, check if redis is available, and dynamically create chart lines data
131 self.chart_name += "_" + self.name
132 data = self._get_data()
137 if name.startswith('db'):
138 self.definitions['keys']['lines'].append([name, None, 'absolute'])