]> arthur.barton.de Git - netdata.git/blob - python.d/redis.chart.py
Merge remote-tracking branch 'upstream/master'
[netdata.git] / python.d / redis.chart.py
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 = 60
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', 'net', 'connections', 'clients', 'slaves', 'persistence']
23
24 CHARTS = {
25     'operations': {
26         'options': [None, 'Redis Operations', 'operations/s', 'operations', 'redis.operations', 'line'],
27         'lines': [
28             ['total_commands_processed', 'commands', 'incremental'],
29             ['instantaneous_ops_per_sec', 'operations', 'absolute']
30         ]},
31     'hit_rate': {
32         'options': [None, 'Redis Hit rate', 'percent', 'hits', 'redis.hit_rate', 'line'],
33         'lines': [
34             ['hit_rate', 'rate', 'absolute']
35         ]},
36     'memory': {
37         'options': [None, 'Redis Memory utilization', 'kilobytes', 'memory', 'redis.memory', 'line'],
38         'lines': [
39             ['used_memory', 'total', 'absolute', 1, 1024],
40             ['used_memory_lua', 'lua', 'absolute', 1, 1024]
41         ]},
42     'net': {
43         'options': [None, 'Redis Bandwidth', 'kilobits/s', 'network', 'redis.net', 'area'],
44         'lines': [
45             ['total_net_input_bytes', 'in', 'incremental', 8, 1024],
46             ['total_net_output_bytes', 'out', 'incremental', -8, 1024]
47         ]},
48     'keys': {
49         'options': [None, 'Redis Keys per Database', 'keys', 'keys', 'redis.keys', 'line'],
50         'lines': [
51             # lines are created dynamically in `check()` method
52         ]},
53     'connections': {
54         'options': [None, 'Redis Connections', 'connections/s', 'connections', 'redis.connections', 'line'],
55         'lines': [
56             ['total_connections_received', 'received', 'incremental', 1],
57             ['rejected_connections', 'rejected', 'incremental', -1]
58         ]},
59     'clients': {
60         'options': [None, 'Redis Clients', 'clients', 'connections', 'redis.clients', 'line'],
61         'lines': [
62             ['connected_clients', 'connected', 'absolute', 1],
63             ['blocked_clients', 'blocked', 'absolute', -1]
64         ]},
65     'slaves': {
66         'options': [None, 'Redis Slaves', 'slaves', 'replication', 'redis.slaves', 'line'],
67         'lines': [
68             ['connected_slaves', 'connected', 'absolute']
69         ]},
70     'persistence': {
71         'options': [None, 'Redis Persistence Changes Since Last Save', 'changes', 'persistence', 'redis.rdb_changes', 'line'],
72         'lines': [
73             ['rdb_changes_since_last_save', 'changes', 'absolute']
74         ]}
75 }
76
77
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"
82         self.order = ORDER
83         self.definitions = CHARTS
84         self._keep_alive = True
85         self.chart_name = ""
86         self.passwd = None
87         self.port = 6379
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']
96
97     def _get_data(self):
98         """
99         Get data from socket
100         :return: dict
101         """
102 <<<<<<< HEAD
103         if self.passwd:
104             info_request = self.request
105             self.request = "AUTH " + self.passwd + "\r\n"
106             raw = self._get_raw_data().strip()
107             if raw != "+OK":
108                 self.error("invalid password")
109                 return None
110             self.request = info_request
111
112 =======
113 >>>>>>> upstream/master
114         response = self._get_raw_data()
115         if response is None:
116             # error has already been logged
117             return None
118
119         try:
120             parsed = response.split("\n")
121         except AttributeError:
122             self.error("response is invalid/empty")
123             return None
124
125         data = {}
126         for line in parsed:
127             if len(line) < 5 or line[0] == '$' or line[0] == '#':
128                 continue
129
130             if line.startswith('db'):
131                 tmp = line.split(',')[0].replace('keys=', '')
132                 record = tmp.split(':')
133                 data[record[0]] = record[1]
134                 continue
135
136             try:
137                 t = line.split(':')
138                 data[t[0]] = t[1]
139             except (IndexError, ValueError):
140                 self.debug("invalid line received: " + str(line))
141                 pass
142
143         if len(data) == 0:
144             self.error("received data doesn't have any records")
145             return None
146
147         try:
148             data['hit_rate'] = (int(data['keyspace_hits']) * 100) / (int(data['keyspace_hits']) + int(data['keyspace_misses']))
149         except:
150             data['hit_rate'] = 0
151
152         return data
153
154     def _check_raw_data(self, data):
155         """
156         Check if all data has been gathered from socket.
157         Parse first line containing message length and check against received message
158         :param data: str
159         :return: boolean
160         """
161         length = len(data)
162         supposed = data.split('\n')[0][1:]
163         offset = len(supposed) + 4  # 1 dollar sing, 1 new line character + 1 ending sequence '\r\n'
164         if not supposed.isdigit():
165             return True
166         supposed = int(supposed)
167
168         if length - offset >= supposed:
169             self.debug("received full response from redis")
170             return True
171
172         self.debug("waiting more data from redis")
173         return False
174
175     def check(self):
176         """
177         Parse configuration, check if redis is available, and dynamically create chart lines data
178         :return: boolean
179         """
180         self._parse_config()
181         if self.name == "":
182             self.name = "local"
183             self.chart_name += "_" + self.name
184         data = self._get_data()
185         if data is None:
186             return False
187
188         for name in data:
189             if name.startswith('db'):
190                 self.definitions['keys']['lines'].append([name, None, 'absolute'])
191
192         return True