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', 'connections', 'clients', 'slaves', 'persistence']
26 'options': [None, 'Operations', 'operations/s', 'operations', 'redis.operations', 'line'],
28 ['total_commands_processed', 'commands', 'incremental'],
29 ['instantaneous_ops_per_sec', 'operations', 'absolute']
32 'options': [None, 'Hit rate', 'percent', 'hits', 'redis.hit_rate', 'line'],
34 ['hit_rate', 'rate', 'absolute']
37 'options': [None, 'Memory utilization', 'kilobytes', 'memory', 'redis.memory', 'line'],
39 ['used_memory', 'total', 'absolute', 1, 1024],
40 ['used_memory_lua', 'lua', 'absolute', 1, 1024]
43 'options': [None, 'Database keys', 'keys', 'keys', 'redis.keys', 'line'],
45 # lines are created dynamically in `check()` method
48 'options': [None, 'Connections', 'connections/s', 'connections', 'redis.connections', 'line'],
50 ['total_connections_received', 'received', 'incremental'],
51 ['rejected_connections', 'rejected', 'incremental']
54 'options': [None, 'Clients', 'clients', 'connections', 'redis.clients', 'line'],
56 ['connected_clients', 'connected', 'absolute'],
57 ['blocked_clients', 'blocked', 'absolute']
60 'options': [None, 'Slaves', 'slaves', 'replication', 'redis.slaves', 'line'],
62 ['connected_slaves', 'connected', 'absolute']
65 'options': [None, 'Changes since last save', 'changes', 'persistence', 'redis.rdb_changes', 'line'],
67 ['rdb_changes_since_last_save', 'changes', 'absolute']
72 class Service(SocketService):
73 def __init__(self, configuration=None, name=None):
74 SocketService.__init__(self, configuration=configuration, name=name)
75 self.request = "INFO\r\n"
76 self.host = "localhost"
78 self.unix_socket = None
80 self.definitions = CHARTS
81 self._keep_alive = True
89 response = self._get_raw_data()
91 # error has already been logged
95 parsed = response.split("\n")
96 except AttributeError:
97 self.error("response is invalid/empty")
102 if len(line) < 5 or line[0] == '$' or line[0] == '#':
105 if line.startswith('db'):
106 tmp = line.split(',')[0].replace('keys=', '')
107 record = tmp.split(':')
108 data[record[0]] = record[1]
114 except (IndexError, ValueError):
115 self.debug("invalid line received: " + str(line))
119 self.error("received data doesn't have any records")
123 data['hit_rate'] = (int(data['keyspace_hits']) * 100) / (int(data['keyspace_hits']) + int(data['keyspace_misses']))
129 def _check_raw_data(self, data):
131 Check if all data has been gathered from socket.
132 Parse first line containing message length and check against received message
137 supposed = data.split('\n')[0][1:]
138 offset = len(supposed) + 4 # 1 dollar sing, 1 new line character + 1 ending sequence '\r\n'
139 supposed = int(supposed)
141 if length - offset >= supposed:
142 self.debug("received full response from redis")
145 self.debug("waiting more data from redis")
150 Parse configuration, check if redis is available, and dynamically create chart lines data
156 self.chart_name += "_" + self.name
157 data = self._get_data()
162 if name.startswith('db'):
163 self.definitions['keys']['lines'].append([name, None, 'absolute'])