]> arthur.barton.de Git - netdata.git/blob - python.d/bind_rndc.chart.py
Merge pull request #1520 from ktsaou/master
[netdata.git] / python.d / bind_rndc.chart.py
1 # -*- coding: utf-8 -*-
2 # Description: bind rndc netdata python.d module
3 # Author: l2isbad
4
5 from base import SimpleService
6 from re import compile, findall
7 from os.path import getsize, isfile, split
8 from os import access as is_accessible, R_OK
9 from subprocess import Popen
10
11 priority = 60000
12 retries = 60
13 update_every = 30
14
15 DIRECTORIES = ['/bin/', '/usr/bin/', '/sbin/', '/usr/sbin/']
16 NMS = ['requests', 'responses', 'success', 'auth_answer', 'nonauth_answer', 'nxrrset', 'failure',
17        'nxdomain', 'recursion', 'duplicate', 'rejections']
18 QUERIES = ['RESERVED0', 'A', 'NS', 'CNAME', 'SOA', 'PTR', 'MX', 'TXT', 'X25', 'AAAA', 'SRV', 'NAPTR',
19            'A6', 'DS', 'RRSIG', 'DNSKEY', 'SPF', 'ANY', 'DLV']
20
21
22 class Service(SimpleService):
23     def __init__(self, configuration=None, name=None):
24         SimpleService.__init__(self, configuration=configuration, name=name)
25         self.named_stats_path = self.configuration.get('named_stats_path', '/var/log/bind/named.stats')
26         self.regex_values = compile(r'([0-9]+) ([^\n]+)')
27         # self.options = ['Incoming Requests', 'Incoming Queries', 'Outgoing Queries',
28         # 'Name Server Statistics', 'Zone Maintenance Statistics', 'Resolver Statistics',
29         # 'Cache DB RRsets', 'Socket I/O Statistics']
30         self.options = ['Name Server Statistics', 'Incoming Queries', 'Outgoing Queries']
31         self.regex_options = [r'(%s(?= \+\+)) \+\+([^\+]+)' % option for option in self.options]
32         try:
33             self.rndc = [''.join([directory, 'rndc']) for directory in DIRECTORIES
34                          if isfile(''.join([directory, 'rndc']))][0]
35         except IndexError:
36             self.rndc = False
37
38     def check(self):
39         # We cant start without 'rndc' command
40         if not self.rndc:
41             self.error('Command "rndc" not found')
42             return False
43
44         # We cant start if stats file is not exist or not readable by netdata user
45         if not is_accessible(self.named_stats_path, R_OK):
46             self.error('Cannot access file %s' % self.named_stats_path)
47             return False
48
49         size_before = getsize(self.named_stats_path)
50         run_rndc = Popen([self.rndc, 'stats'], shell=False)
51         run_rndc.wait()
52         size_after = getsize(self.named_stats_path)
53
54         # We cant start if netdata user has no permissions to run 'rndc stats'
55         if not run_rndc.returncode:
56             # 'rndc' was found, stats file is exist and readable and we can run 'rndc stats'. Lets go!
57             self.create_charts()
58             
59             # BIND APPEND dump on every run 'rndc stats'
60             # that is why stats file size can be VERY large if update_interval too small
61             dump_size_24hr = round(86400 / self.update_every * (int(size_after) - int(size_before)) / 1048576, 3)
62             
63             # If update_every too small we should WARN user
64             if self.update_every < 30:
65                 self.info('Update_every %s is NOT recommended for use. Increase the value to > 30' % self.update_every)
66             
67             self.info('With current update_interval it will be + %s MB every 24hr. '
68                       'Don\'t forget to create logrotate conf file for %s' % (dump_size_24hr, self.named_stats_path))
69
70             self.info('Plugin was started successfully.')
71
72             return True
73         else:
74             self.error('Not enough permissions to run "%s stats"' % self.rndc)
75             return False
76
77     def _get_raw_data(self):
78         """
79         Run 'rndc stats' and read last dump from named.stats
80         :return: tuple(
81                        file.read() obj,
82                        named.stats file size
83                       )
84         """
85
86         try:
87             current_size = getsize(self.named_stats_path)
88         except OSError:
89             return None, None
90         
91         run_rndc = Popen([self.rndc, 'stats'], shell=False)
92         run_rndc.wait()
93
94         if run_rndc.returncode:     
95             return None, None
96
97         try:
98             with open(self.named_stats_path) as bind_rndc:
99                 bind_rndc.seek(current_size)
100                 result = bind_rndc.read()
101         except OSError:
102             return None, None
103         else:
104             return result, current_size
105
106     def _get_data(self):
107         """
108         Parse data from _get_raw_data()
109         :return: dict
110         """
111
112         raw_data, size = self._get_raw_data()
113
114         if raw_data is None:
115             return None
116
117         rndc_stats = dict()
118         
119         # Result: dict.
120         # topic = Cache DB RRsets; body = A 178303 NS 86790 ... ; desc = A; value = 178303
121         # {'Cache DB RRsets': [('A', 178303), ('NS', 286790), ...],
122         # {Incoming Queries': [('RESERVED0', 8), ('A', 4557317680), ...],
123         # ......
124         for regex in self.regex_options:
125             rndc_stats.update({topic: [(desc, int(value)) for value, desc in self.regex_values.findall(body)]
126                                for topic, body in findall(regex, raw_data)})
127         
128         nms = dict(rndc_stats.get('Name Server Statistics', []))
129
130         inc_queries = {'i' + k: 0 for k in QUERIES}
131         inc_queries.update({'i' + k: v for k, v in rndc_stats.get('Incoming Queries', [])})
132         out_queries = {'o' + k: 0 for k in QUERIES}
133         out_queries.update({'o' + k: v for k, v in rndc_stats.get('Outgoing Queries', [])})
134         
135         to_netdata = dict()
136         to_netdata['requests'] = sum([v for k, v in nms.items() if 'request' in k and 'received' in k])
137         to_netdata['responses'] = sum([v for k, v in nms.items() if 'responses' in k and 'sent' in k])
138         to_netdata['success'] = nms.get('queries resulted in successful answer', 0)
139         to_netdata['auth_answer'] = nms.get('queries resulted in authoritative answer', 0)
140         to_netdata['nonauth_answer'] = nms.get('queries resulted in non authoritative answer', 0)
141         to_netdata['nxrrset'] = nms.get('queries resulted in nxrrset', 0)
142         to_netdata['failure'] = sum([nms.get('queries resulted in SERVFAIL', 0), nms.get('other query failures', 0)])
143         to_netdata['nxdomain'] = nms.get('queries resulted in NXDOMAIN', 0)
144         to_netdata['recursion'] = nms.get('queries caused recursion', 0)
145         to_netdata['duplicate'] = nms.get('duplicate queries received', 0)
146         to_netdata['rejections'] = nms.get('recursive queries rejected', 0)
147         to_netdata['stats_size'] = size
148         
149         to_netdata.update(inc_queries)
150         to_netdata.update(out_queries)
151         return to_netdata
152
153     def create_charts(self):
154         self.order = ['stats_size', 'bind_stats', 'incoming_q', 'outgoing_q']
155         self.definitions = {
156             'bind_stats': {
157                 'options': [None, 'Name Server Statistics', 'stats', 'Name Server Statistics', 'bind_rndc.stats', 'line'],
158                 'lines': [
159                          ]},
160             'incoming_q': {
161                 'options': [None, 'Incoming queries', 'queries','Incoming queries', 'bind_rndc.incq', 'line'],
162                 'lines': [
163                         ]},
164             'outgoing_q': {
165                 'options': [None, 'Outgoing queries', 'queries','Outgoing queries', 'bind_rndc.outq', 'line'],
166                 'lines': [
167                         ]},
168             'stats_size': {
169                 'options': [None, '%s file size' % split(self.named_stats_path)[1].capitalize(), 'megabytes',
170                             '%s size' % split(self.named_stats_path)[1].capitalize(), 'bind_rndc.size', 'line'],
171                 'lines': [
172                          ["stats_size", None, "absolute", 1, 1048576]
173                         ]}
174                      }
175         for elem in QUERIES:
176             self.definitions['incoming_q']['lines'].append(['i' + elem, elem, 'incremental'])
177             self.definitions['outgoing_q']['lines'].append(['o' + elem, elem, 'incremental'])
178
179         for elem in NMS:
180             self.definitions['bind_stats']['lines'].append([elem, None, 'incremental'])