]> arthur.barton.de Git - netdata.git/blob - python.d/redis.chart.py
Merge pull request #695 from ktsaou/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             return None
79         data = {}
80         for line in raw:
81             if line.startswith(('instantaneous', 'keyspace', 'used_memory', 'connected', 'blocked')):
82                 try:
83                     t = line.split(':')
84                     data[t[0]] = int(t[1])
85                 except (IndexError, ValueError):
86                     pass
87             elif line.startswith('db'):
88                 tmp = line.split(',')[0].replace('keys=', '')
89                 record = tmp.split(':')
90                 data[record[0]] = int(record[1])
91         try:
92             data['hit_rate'] = int((data['keyspace_hits'] / float(data['keyspace_hits'] + data['keyspace_misses'])) * 100)
93         except:
94             data['hit_rate'] = 0
95
96         return data
97
98     def check(self):
99         """
100         Parse configuration, check if redis is available, and dynamically create chart lines data
101         :return: boolean
102         """
103         self._parse_config()
104         if self.name == "":
105             self.name = "local"
106         self.chart_name += "_" + self.name
107         data = self._get_data()
108         if data is None:
109             self.error("No data received")
110             return False
111
112         for name in data:
113             if name.startswith('db'):
114                 self.definitions['keys']['lines'].append([name.decode(), None, 'absolute'])
115
116         return True