1 # -*- coding: utf-8 -*-
2 # Description: bind rndc netdata python.d module
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
15 DIRECTORIES = ['/bin/', '/usr/bin/', '/sbin/', '/usr/sbin/']
18 class Service(SimpleService):
19 def __init__(self, configuration=None, name=None):
20 SimpleService.__init__(self, configuration=configuration, name=name)
21 self.named_stats_path = self.configuration.get('named_stats_path', '/var/log/bind/named.stats')
22 self.regex_values = compile(r'([0-9]+) ([^\n]+)')
23 # self.options = ['Incoming Requests', 'Incoming Queries', 'Outgoing Queries',
24 # 'Name Server Statistics', 'Zone Maintenance Statistics', 'Resolver Statistics',
25 # 'Cache DB RRsets', 'Socket I/O Statistics']
26 self.options = ['Name Server Statistics']
27 self.regex_options = [r'(%s(?= \+\+)) \+\+([^\+]+)' % option for option in self.options]
29 self.rndc = [''.join([directory, 'rndc']) for directory in DIRECTORIES
30 if isfile(''.join([directory, 'rndc']))][0]
36 # We cant start without 'rndc' command
38 self.error('Command "rndc" not found')
41 # We cant if stats file is not exist or not readable by netdata user
42 if not is_accessible(self.named_stats_path, R_OK):
43 self.error('Cannot access file %s' % self.named_stats_path)
46 size_before = getsize(self.named_stats_path)
47 run_rndc = Popen([self.rndc, 'stats'], shell=False)
49 size_after = getsize(self.named_stats_path)
51 # We cant start if netdata user has no permissions to run 'rndc stats'
52 if not run_rndc.returncode:
53 # 'rndc' was found, stats file is exist and readable and we can run 'rndc stats'. Lets go!
56 # BIND APPEND dump on every run 'rndc stats'
57 # that is why stats file size can be VERY large if update_interval too small
58 dump_size_24hr = round(86400 / self.update_every * (int(size_after) - int(size_before)) / 1048576, 3)
60 # If update_every too small we should WARN user
61 if self.update_every < 30:
62 self.info('Update_every %s is NOT recommended for use. Increase the value to > 30' % self.update_every)
64 self.info('With current update_interval it will be + %s MB every 24hr. '
65 'Don\'t forget to create logrotate conf file for %s' % (dump_size_24hr, self.named_stats_path))
67 self.info('Plugin was started successfully.')
71 self.error('Not enough permissions to run "%s stats"' % self.rndc)
74 def _get_raw_data(self):
77 Run 'rndc stats' and read last dump from named.stats
85 current_size = getsize(self.named_stats_path)
89 run_rndc = Popen([self.rndc, 'stats'], shell=False)
92 if run_rndc.returncode:
96 with open(self.named_stats_path) as bind_rndc:
97 bind_rndc.seek(current_size)
98 result = bind_rndc.read()
102 return result, current_size
107 Parse data from _get_raw_data()
111 raw_data, size = self._get_raw_data()
118 for regex in self.regex_options:
119 rndc_stats.update({k: [(y, int(x)) for x, y in self.regex_values.findall(v)]
120 for k, v in findall(regex, raw_data)})
122 nms = dict(rndc_stats.get('Name Server Statistics', []))
126 to_netdata['requests'] = sum([v for k, v in nms.items() if 'request' in k and 'received' in k])
127 to_netdata['responses'] = sum([v for k, v in nms.items() if 'responses' in k and 'sent' in k])
128 to_netdata['success'] = nms.get('queries resulted in successful answer', 0)
129 to_netdata['auth_answer'] = nms.get('queries resulted in authoritative answer', 0)
130 to_netdata['nonauth_answer'] = nms.get('queries resulted in non authoritative answer', 0)
131 to_netdata['nxrrset'] = nms.get('queries resulted in nxrrset', 0)
132 to_netdata['failure'] = sum([nms.get('queries resulted in SERVFAIL', 0), nms.get('other query failures', 0)])
133 to_netdata['nxdomain'] = nms.get('queries resulted in NXDOMAIN', 0)
134 to_netdata['recursion'] = nms.get('queries caused recursion', 0)
135 to_netdata['duplicate'] = nms.get('duplicate queries received', 0)
136 to_netdata['rejections'] = nms.get('recursive queries rejected', 0)
137 to_netdata['stats_size'] = size
141 def create_charts(self):
143 self.order = ['stats_size', 'bind_stats']
146 'options': [None, 'Name Server Statistics', 'stats', 'NS Statistics', 'bind_rndc.stats', 'line'],
148 ["requests", None, "incremental"], ["responses", None, "incremental"],
149 ["success", None, "incremental"], ["auth_answer", None, "incremental"],
150 ["nonauth_answer", None, "incremental"], ["nxrrset", None, "incremental"],
151 ["failure", None, "incremental"], ["nxdomain", None, "incremental"],
152 ["recursion", None, "incremental"], ["duplicate", None, "incremental"],
153 ["rejections", None, "incremental"]
156 'options': [None, '%s file size' % split(self.named_stats_path)[1].capitalize(), 'megabyte',
157 '%s size' % split(self.named_stats_path)[1].capitalize(), 'bind_rndc.size', 'line'],
159 ["stats_size", None, "absolute", 1, 1048576]