add bind_rndc plugin
Ilya committed
Jan 3, 2017 at 16:40 UTC
c738ebb1336601f9be980d0773bd1cb42e962510
1 file changed
+161
python.d/bind_rndc.chart.py
new
+161
@@ -0,0 +1,161 @@
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
+
17
+
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]
28
+ try:
29
+ self.rndc = [''.join([directory, 'rndc']) for directory in DIRECTORIES
30
+ if isfile(''.join([directory, 'rndc']))][0]
31
+ except IndexError:
32
+ self.rndc = False
33
+
34
+ def check(self):
35
+
36
+ # We cant start without 'rndc' command
37
+ if not self.rndc:
38
+ self.error('Command "rndc" not found')
39
+ return False
40
+
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)
44
+ return False
45
+
46
+ size_before = getsize(self.named_stats_path)
47
+ run_rndc = Popen([self.rndc, 'stats'], shell=False)
48
+ run_rndc.wait()
49
+ size_after = getsize(self.named_stats_path)
50
+
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!
54
+ self.create_charts()
55
+
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)
59
+
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)
63
+
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))
66
+
67
+ self.info('Plugin was started successfully.')
68
+
69
+ return True
70
+ else:
71
+ self.error('Not enough permissions to run "%s stats"' % self.rndc)
72
+ return False
73
+
74
+ def _get_raw_data(self):
75
+
76
+ """
77
+ Run 'rndc stats' and read last dump from named.stats
78
+ :return: tuple(
79
+ file.read() obj,
80
+ named.stats file size
81
+ )
82
+ """
83
+
84
+ try:
85
+ current_size = getsize(self.named_stats_path)
86
+ except OSError:
87
+ return None, None
88
+
89
+ run_rndc = Popen([self.rndc, 'stats'], shell=False)
90
+ run_rndc.wait()
91
+
92
+ if run_rndc.returncode:
93
+ return None, None
94
+
95
+ try:
96
+ with open(self.named_stats_path) as bind_rndc:
97
+ bind_rndc.seek(current_size)
98
+ result = bind_rndc.read()
99
+ except OSError:
100
+ return None, None
101
+ else:
102
+ return result, current_size
103
+
104
+ def _get_data(self):
105
+
106
+ """
107
+ Parse data from _get_raw_data()
108
+ :return: dict
109
+ """
110
+
111
+ raw_data, size = self._get_raw_data()
112
+
113
+ if raw_data is None:
114
+ return None
115
+
116
+ rndc_stats = dict()
117
+
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)})
121
+
122
+ nms = dict(rndc_stats.get('Name Server Statistics', []))
123
+
124
+ to_netdata = dict()
125
+
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
138
+
139
+ return to_netdata
140
+
141
+ def create_charts(self):
142
+
143
+ self.order = ['stats_size', 'bind_stats']
144
+ self.definitions = {
145
+ 'bind_stats': {
146
+ 'options': [None, 'Name Server Statistics', 'stats', 'NS Statistics', 'bind_rndc.stats', 'line'],
147
+ 'lines': [
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"]
154
+ ]},
155
+ 'stats_size': {
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'],
158
+ 'lines': [
159
+ ["stats_size", None, "absolute", 1, 1048576]
160
+ ]}
161
+ }