]> arthur.barton.de Git - netdata.git/blob - python.d/redis.chart.py
Merge pull request #698 from paulfantom/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', 'clients', 'slaves']
23
24 CHARTS = {
25     'operations': {
26         'options': [None, 'Operations', 'operations/s', 'Statistics', 'redis.statistics', 'line'],
27         'lines': [
28             ['instantaneous_ops_per_sec', 'operations', 'absolute']
29         ]},
30     'hit_rate': {
31         'options': [None, 'Hit rate', 'percent', 'Statistics', 'redis.statistics', '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.replication', '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
70     def _get_data(self):
71         """
72         Get data from socket
73         :return: dict
74         """
75         try:
76             raw = self._get_raw_data().split("\n")
77         except AttributeError:
78             self.error("no data received")
79             return None
80         data = {}
81         for line in raw:
82             if line.startswith(('instantaneous', 'keyspace', 'used_memory', 'connected', 'blocked')):
83                 try:
84                     t = line.split(':')
85                     data[t[0]] = int(t[1])
86                 except (IndexError, ValueError):
87                     pass
88             elif line.startswith('db'):
89                 tmp = line.split(',')[0].replace('keys=', '')
90                 record = tmp.split(':')
91                 data[record[0]] = int(record[1])
92         try:
93             data['hit_rate'] = int((data['keyspace_hits'] / float(data['keyspace_hits'] + data['keyspace_misses'])) * 100)
94         except:
95             data['hit_rate'] = 0
96
97         if len(data) == 0:
98             self.error("received data doesn't have needed records")
99             return None
100         else:
101             return data
102
103     def check(self):
104         """
105         Parse configuration, check if redis is available, and dynamically create chart lines data
106         :return: boolean
107         """
108         self._parse_config()
109         if self.name == "":
110             self.name = "local"
111         self.chart_name += "_" + self.name
112         data = self._get_data()
113         if data is None:
114             return False
115
116         for name in data:
117             if name.startswith('db'):
118                 self.definitions['keys']['lines'].append([name.decode(), None, 'absolute'])
119
120         return True