]> arthur.barton.de Git - netdata.git/blob - python.d/redis.chart.py
1. changed arguments from decode() in base.py to support versions of Python 2.6
[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', 'clients', 'slaves']
23
24 CHARTS = {
25     'operations': {
26         'options': [None, 'Operations', 'operations/s', 'Statistics', 'redis.operations', 'line'],
27         'lines': [
28             ['instantaneous_ops_per_sec', 'operations', 'absolute']
29         ]},
30     'hit_rate': {
31         'options': [None, 'Hit rate', 'percent', 'Statistics', 'redis.hit_rate', 'line'],
32         'lines': [
33             ['hit_rate', 'rate', 'absolute']
34         ]},
35     'memory': {
36         'options': [None, 'Memory utilization', 'kilobytes', 'Memory', 'redis.memory', 'line'],
37         'lines': [
38             ['used_memory', 'total', 'absolute', 1, 1024],
39             ['used_memory_lua', 'lua', 'absolute', 1, 1024]
40         ]},
41     'keys': {
42         'options': [None, 'Database keys', 'keys', 'Keys', 'redis.keys', 'line'],
43         'lines': [
44             # lines are created dynamically in `check()` method
45         ]},
46     'clients': {
47         'options': [None, 'Clients', 'clients', 'Clients', 'redis.clients', 'line'],
48         'lines': [
49             ['connected_clients', 'connected', 'absolute'],
50             ['blocked_clients', 'blocked', 'absolute']
51         ]},
52     'slaves': {
53         'options': [None, 'Slaves', 'slaves', 'Replication', 'redis.slaves', 'line'],
54         'lines': [
55             ['connected_slaves', 'connected', 'absolute']
56         ]}
57 }
58
59
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"
65         self.port = 6379
66         self.unix_socket = None
67         self.order = ORDER
68         self.definitions = CHARTS
69         self._keep_alive = True
70         self.chart_name = ""
71         self.passwd =  None
72         if 'pass' in configuration:
73             self.passwd = configuration['pass']
74
75     def _get_data(self):
76         """
77         Get data from socket
78         :return: dict
79         """
80         try:
81             if self.passwd:
82                 info_request = self.request
83                 self.request = "AUTH " + self.passwd + "\r\n"
84                 raw = self._get_raw_data().strip()
85                 if raw != "+OK":
86                     self.error("invalid password")
87                     return None
88                 self.request = info_request
89             raw = self._get_raw_data().split("\n")
90         except AttributeError:
91             self.error("no data received")
92             return None
93         data = {}
94         for line in raw:
95             if line.startswith(('instantaneous', 'keyspace', 'used_memory', 'connected', 'blocked')):
96                 try:
97                     t = line.split(':')
98                     data[t[0]] = int(t[1])
99                 except (IndexError, ValueError):
100                     pass
101             elif line.startswith('db'):
102                 tmp = line.split(',')[0].replace('keys=', '')
103                 record = tmp.split(':')
104                 data[record[0]] = int(record[1])
105         try:
106             data['hit_rate'] = int((data['keyspace_hits'] / float(data['keyspace_hits'] + data['keyspace_misses'])) * 100)
107         except:
108             data['hit_rate'] = 0
109
110         if len(data) == 0:
111             self.error("received data doesn't have needed records")
112             return None
113         else:
114             return data
115
116     def _check_raw_data(self, data):
117         """
118         Check if all data has been gathered from socket.
119         Parse first line containing message length and check against received message
120         :param data: str
121         :return: boolean
122         """
123         length = len(data)
124         supposed = data.split('\n')[0][1:]
125         offset = len(supposed) + 4  # 1 dollar sing, 1 new line character + 1 ending sequence '\r\n'
126         if (not supposed.isdigit()) :
127             return True
128         supposed = int(supposed)
129         if length - offset >= supposed:
130             return True
131         else:
132             return False
133
134         return False
135
136     def check(self):
137         """
138         Parse configuration, check if redis is available, and dynamically create chart lines data
139         :return: boolean
140         """
141         self._parse_config()
142         if self.name == "":
143             self.name = "local"
144             self.chart_name += "_" + self.name
145         data = self._get_data()
146         if data is None:
147             return False
148
149         for name in data:
150             if name.startswith('db'):
151                 self.definitions['keys']['lines'].append([name, None, 'absolute'])
152
153         return True