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', 'net', 'connections', 'clients', 'slaves', 'persistence']
26 'options': [None, 'Redis Operations', 'operations/s', 'operations', 'redis.operations', 'line'],
28 ['total_commands_processed', 'commands', 'incremental'],
29 ['instantaneous_ops_per_sec', 'operations', 'absolute']
32 'options': [None, 'Redis Hit rate', 'percent', 'hits', 'redis.hit_rate', 'line'],
34 ['hit_rate', 'rate', 'absolute']
37 'options': [None, 'Redis 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, 'Redis Bandwidth', 'kilobits/s', 'network', 'redis.net', 'area'],
45 ['total_net_input_bytes', 'in', 'incremental', 8, 1024],
46 ['total_net_output_bytes', 'out', 'incremental', -8, 1024]
49 'options': [None, 'Redis Keys per Database', 'keys', 'keys', 'redis.keys', 'line'],
51 # lines are created dynamically in `check()` method
54 'options': [None, 'Redis Connections', 'connections/s', 'connections', 'redis.connections', 'line'],
56 ['total_connections_received', 'received', 'incremental', 1],
57 ['rejected_connections', 'rejected', 'incremental', -1]
60 'options': [None, 'Redis Clients', 'clients', 'connections', 'redis.clients', 'line'],
62 ['connected_clients', 'connected', 'absolute', 1],
63 ['blocked_clients', 'blocked', 'absolute', -1]
66 'options': [None, 'Redis Slaves', 'slaves', 'replication', 'redis.slaves', 'line'],
68 ['connected_slaves', 'connected', 'absolute']
71 'options': [None, 'Redis Persistence Changes Since Last Save', 'changes', 'persistence', 'redis.rdb_changes', 'line'],
73 ['rdb_changes_since_last_save', 'changes', 'absolute']
78 class Service(SocketService):
79 def __init__(self, configuration=None, name=None):
80 SocketService.__init__(self, configuration=configuration, name=name)
81 self.request = "INFO\r\n"
83 self.definitions = CHARTS
84 self._keep_alive = True
88 if 'port' in configuration:
89 self.port = configuration['port']
90 if 'pass' in configuration:
91 self.passwd = configuration['pass']
92 if 'host' in configuration:
93 self.host = configuration['host']
94 if 'socket' in configuration:
95 self.unix_socket = configuration['socket']
103 info_request = self.request
104 self.request = "AUTH " + self.passwd + "\r\n"
105 raw = self._get_raw_data().strip()
107 self.error("invalid password")
109 self.request = info_request
110 response = self._get_raw_data()
112 # error has already been logged
116 parsed = response.split("\n")
117 except AttributeError:
118 self.error("response is invalid/empty")
123 if len(line) < 5 or line[0] == '$' or line[0] == '#':
126 if line.startswith('db'):
127 tmp = line.split(',')[0].replace('keys=', '')
128 record = tmp.split(':')
129 data[record[0]] = record[1]
135 except (IndexError, ValueError):
136 self.debug("invalid line received: " + str(line))
140 self.error("received data doesn't have any records")
144 data['hit_rate'] = (int(data['keyspace_hits']) * 100) / (int(data['keyspace_hits']) + int(data['keyspace_misses']))
150 def _check_raw_data(self, data):
152 Check if all data has been gathered from socket.
153 Parse first line containing message length and check against received message
158 supposed = data.split('\n')[0][1:]
159 offset = len(supposed) + 4 # 1 dollar sing, 1 new line character + 1 ending sequence '\r\n'
160 if not supposed.isdigit():
162 supposed = int(supposed)
164 if length - offset >= supposed:
165 self.debug("received full response from redis")
168 self.debug("waiting more data from redis")
173 Parse configuration, check if redis is available, and dynamically create chart lines data
179 self.chart_name += "_" + self.name
180 data = self._get_data()
185 if name.startswith('db'):
186 self.definitions['keys']['lines'].append([name, None, 'absolute'])