3 // collect statistics from bind (named) v9.10+
5 // bind statistics documentation at:
6 // http://jpmens.net/2013/03/18/json-in-bind-9-s-statistics-server/
7 // https://ftp.isc.org/isc/bind/9.10.3/doc/arm/Bv9ARM.ch06.html#statistics
9 // example configuration in /etc/netdata/named.conf
10 // the module supports auto-detection if bind is running in localhost
14 "enable_autodetect": true,
19 "url": "http://127.0.0.1:8888/json/v1/server",
24 "url": "http://10.0.0.1:8888/xml/v3/server",
31 // the following is the bind named.conf configuration required
35 inet 127.0.0.1 port 8888 allow { 127.0.0.1; };
39 var url = require('url');
40 var http = require('http');
41 var XML = require('pixl-xml');
42 var netdata = require('netdata');
44 if(netdata.options.DEBUG === true) netdata.debug('loaded ' + __filename + ' plugin');
48 enable_autodetect: true,
53 chartFromMembersCreate: function(service, obj, id, title_suffix, units, family_prefix, category_prefix, type, priority, algorithm, multiplier, divisor) {
55 id: id, // the unique id of the chart
56 name: '', // the unique name of the chart
57 title: service.name + ' ' + title_suffix, // the title of the chart
58 units: units, // the units of the chart dimensions
59 family: family_prefix + '_' + service.name, // the family of the chart
60 category: category_prefix + '_' + service.name, // the category of the chart
61 type: type, // the type of the chart
62 priority: priority, // the priority relative to others in the same family and category
63 update_every: Math.round(service.update_every / 1000), // the expected update frequency of the chart
69 if(typeof(obj[x]) !== 'undefined' && obj[x] !== 0) {
71 chart.dimensions[x] = {
72 id: x, // the unique id of the dimension
73 name: x, // the name of the dimension
74 algorithm: algorithm, // the id of the netdata algorithm
75 multiplier: multiplier, // the multiplier
76 divisor: divisor, // the divisor
77 hidden: false // is hidden (boolean)
85 chart = service.chart(id, chart);
86 this.charts[id] = chart;
90 chartFromMembers: function(service, obj, id_suffix, title_suffix, units, family_prefix, category_prefix, type, priority, algorithm, multiplier, divisor) {
91 var id = 'named_' + service.name + '.' + id_suffix;
92 var chart = this.charts[id];
94 if(typeof chart === 'undefined') {
95 chart = this.chartFromMembersCreate(service, obj, id, title_suffix, units, family_prefix, category_prefix, type, priority, algorithm, multiplier, divisor);
96 if(chart === null) return false;
99 // check if we need to re-generate the chart
101 if(typeof(chart.dimensions[x]) === 'undefined') {
102 chart = this.chartFromMembersCreate(service, obj, id, title_suffix, units, family_prefix, category_prefix, type, priority, algorithm, multiplier, divisor);
103 if(chart === null) return false;
110 service.begin(chart);
112 if(typeof(chart.dimensions[x]) !== 'undefined') {
114 service.set(x, obj[x]);
119 if(found > 0) return true;
123 // an index to map values to different charts
130 // transform the XML response of bind
131 // to the JSON response of bind
132 xml2js: function(service, data_xml) {
133 var d = XML.parse(data_xml);
134 if(d === null) return null;
137 var len = d.server.counters.length;
139 var a = d.server.counters[len];
140 if(typeof a.counter === 'undefined') continue;
141 if(a.type === 'opcode') a.type = 'opcodes';
142 else if(a.type === 'qtype') a.type = 'qtypes';
143 else if(a.type === 'nsstat') a.type = 'nsstats';
144 var aa = data[a.type] = {};
146 var alen2 = a.counter.length;
147 while(alen < alen2) {
148 aa[a.counter[alen].name] = parseInt(a.counter[alen]._Data);
154 var vlen = d.views.view.length;
156 var vname = d.views.view[vlen].name;
157 data.views[vname] = { resolver: {} };
158 var len = d.views.view[vlen].counters.length;
160 var a = d.views.view[vlen].counters[len];
161 if(typeof a.counter === 'undefined') continue;
162 if(a.type === 'resstats') a.type = 'stats';
163 else if(a.type === 'resqtype') a.type = 'qtypes';
164 else if(a.type === 'adbstat') a.type = 'adb';
165 var aa = data.views[vname].resolver[a.type] = {};
167 var alen2 = a.counter.length;
168 while(alen < alen2) {
169 aa[a.counter[alen].name] = parseInt(a.counter[alen]._Data);
178 processResponse: function(service, data) {
183 // pepending on the URL given
184 if(service.request.path.match(/^\/xml/) !== null)
185 r = named.xml2js(service, data);
187 r = JSON.parse(data);
189 if(typeof r === 'undefined' || r === null) {
190 netdata.serviceError(service, "Cannot parse these data: " + data);
194 if(service.added !== true)
197 if(typeof r.nsstats !== 'undefined') {
198 // we split the nsstats object to several others
199 var global_requests = {}, global_requests_enable = false;
200 var global_failures = {}, global_failures_enable = false;
201 var global_failures_detail = {}, global_failures_detail_enable = false;
202 var global_updates = {}, global_updates_enable = false;
203 var protocol_queries = {}, protocol_queries_enable = false;
204 var global_queries = {}, global_queries_enable = false;
205 var global_queries_success = {}, global_queries_success_enable = false;
206 var default_enable = false;
207 var RecursClients = 0;
209 // RecursClients is an absolute value
210 if(typeof r.nsstats['RecursClients'] !== 'undefined') {
211 RecursClients = r.nsstats['RecursClients'];
212 delete r.nsstats['RecursClients'];
215 for( var x in r.nsstats ) {
216 // we maintain an index of the values found
217 // mapping them to objects splitted
219 var look = named.lookups.nsstats[x];
220 if(typeof look === 'undefined') {
221 // a new value, not found in the index
223 if(x === 'Requestv4') {
224 named.lookups.nsstats[x] = {
226 type: 'global_requests'
229 else if(x === 'Requestv6') {
230 named.lookups.nsstats[x] = {
232 type: 'global_requests'
235 else if(x === 'QryFailure') {
236 named.lookups.nsstats[x] = {
238 type: 'global_failures'
241 else if(x === 'QryUDP') {
242 named.lookups.nsstats[x] = {
244 type: 'protocol_queries'
247 else if(x === 'QryTCP') {
248 named.lookups.nsstats[x] = {
250 type: 'protocol_queries'
253 else if(x === 'QrySuccess') {
254 named.lookups.nsstats[x] = {
256 type: 'global_queries_success'
259 else if(x.match(/QryRej$/) !== null) {
260 named.lookups.nsstats[x] = {
262 type: 'global_failures_detail'
265 else if(x.match(/^Qry/) !== null) {
266 named.lookups.nsstats[x] = {
268 type: 'global_queries'
271 else if(x.match(/^Update/) !== null) {
272 named.lookups.nsstats[x] = {
274 type: 'global_updates'
278 // values not mapped, will remain
279 // in the default map
280 named.lookups.nsstats[x] = {
286 look = named.lookups.nsstats[x];
287 // netdata.error('lookup nsstats value: ' + x + ' >>> ' + named.lookups.nsstats[x].type);
291 case 'global_requests': global_requests[look.name] = r.nsstats[x]; delete r.nsstats[x]; global_requests_enable = true; break;
292 case 'global_queries': global_queries[look.name] = r.nsstats[x]; delete r.nsstats[x]; global_queries_enable = true; break;
293 case 'global_queries_success': global_queries_success[look.name] = r.nsstats[x]; delete r.nsstats[x]; global_queries_success_enable = true; break;
294 case 'global_updates': global_updates[look.name] = r.nsstats[x]; delete r.nsstats[x]; global_updates_enable = true; break;
295 case 'protocol_queries': protocol_queries[look.name] = r.nsstats[x]; delete r.nsstats[x]; protocol_queries_enable = true; break;
296 case 'global_failures': global_failures[look.name] = r.nsstats[x]; delete r.nsstats[x]; global_failures_enable = true; break;
297 case 'global_failures_detail': global_failures_detail[look.name] = r.nsstats[x]; delete r.nsstats[x]; global_failures_detail_enable = true; break;
298 default: default_enable = true; break;
302 if(global_requests_enable == true)
303 service.module.chartFromMembers(service, global_requests, 'received_requests', 'Bind, Global Received Requests by IP version', 'requests/s', 'named', 'named', netdata.chartTypes.stacked, 100, netdata.chartAlgorithms.incremental, 1, 1);
305 if(global_queries_success_enable == true)
306 service.module.chartFromMembers(service, global_queries_success, 'global_queries_success', 'Bind, Global Successful Queries', 'queries/s', 'named', 'named', netdata.chartTypes.line, 150, netdata.chartAlgorithms.incremental, 1, 1);
308 if(protocol_queries_enable == true)
309 service.module.chartFromMembers(service, protocol_queries, 'protocols_queries', 'Bind, Global Queries by IP Protocol', 'queries/s', 'named', 'named', netdata.chartTypes.stacked, 200, netdata.chartAlgorithms.incremental, 1, 1);
311 if(global_queries_enable == true)
312 service.module.chartFromMembers(service, global_queries, 'global_queries', 'Bind, Global Queries Analysis', 'queries/s', 'named', 'named', netdata.chartTypes.stacked, 300, netdata.chartAlgorithms.incremental, 1, 1);
314 if(global_updates_enable == true)
315 service.module.chartFromMembers(service, global_updates, 'received_updates', 'Bind, Global Received Updates', 'updates/s', 'named', 'named', netdata.chartTypes.stacked, 900, netdata.chartAlgorithms.incremental, 1, 1);
317 if(global_failures_enable == true)
318 service.module.chartFromMembers(service, global_failures, 'query_failures', 'Bind, Global Query Failures', 'failures/s', 'named', 'named', netdata.chartTypes.line, 950, netdata.chartAlgorithms.incremental, 1, 1);
320 if(global_failures_detail_enable == true)
321 service.module.chartFromMembers(service, global_failures_detail, 'query_failures_detail', 'Bind, Global Query Failures Analysis', 'failures/s', 'named', 'named', netdata.chartTypes.stacked, 960, netdata.chartAlgorithms.incremental, 1, 1);
323 if(default_enable === true)
324 service.module.chartFromMembers(service, r.nsstats, 'nsstats', 'Bind, Other Global Server Statistics', 'operations/s', 'named', 'named', netdata.chartTypes.line, 999, netdata.chartAlgorithms.incremental, 1, 1);
326 // RecursClients chart
328 var id = 'named_' + service.name + '.recursive_clients';
329 var chart = named.charts[id];
331 if(typeof chart === 'undefined') {
333 id: id, // the unique id of the chart
334 name: '', // the unique name of the chart
335 title: service.name + ' Bind, Current Recursive Clients', // the title of the chart
336 units: 'clients', // the units of the chart dimensions
337 family: 'named', // the family of the chart
338 category: 'named', // the category of the chart
339 type: netdata.chartTypes.line, // the type of the chart
340 priority: 150, // the priority relative to others in the same family and category
341 update_every: Math.round(service.update_every / 1000), // the expected update frequency of the chart
344 id: 'clients', // the unique id of the dimension
345 name: '', // the name of the dimension
346 algorithm: netdata.chartAlgorithms.absolute,// the id of the netdata algorithm
347 multiplier: 1, // the multiplier
348 divisor: 1, // the divisor
349 hidden: false // is hidden (boolean)
354 chart = service.chart(id, chart);
355 named.charts[id] = chart;
358 service.begin(chart);
359 service.set('clients', RecursClients);
364 if(typeof r.opcodes !== 'undefined')
365 service.module.chartFromMembers(service, r.opcodes, 'in_opcodes', 'Bind, Global Incoming Requests by OpCode', 'requests/s', 'named', 'named', netdata.chartTypes.stacked, 1000, netdata.chartAlgorithms.incremental, 1, 1);
367 if(typeof r.qtypes !== 'undefined')
368 service.module.chartFromMembers(service, r.qtypes, 'in_qtypes', 'Bind, Global Incoming Requests by Query Type', 'requests/s', 'named', 'named', netdata.chartTypes.stacked, 2000, netdata.chartAlgorithms.incremental, 1, 1);
370 if(typeof r.sockstats !== 'undefined')
371 service.module.chartFromMembers(service, r.sockstats, 'in_sockstats', 'Bind, Global Socket Statistics', 'operations/s', 'named', 'named', netdata.chartTypes.line, 2500, netdata.chartAlgorithms.incremental, 1, 1);
373 if(typeof r.views !== 'undefined') {
374 for( var x in r.views ) {
375 var resolver = r.views[x].resolver;
377 if(typeof resolver !== 'undefined') {
378 if(typeof resolver.stats !== 'undefined') {
380 var key = service.name + '.' + x;
381 var default_enable = false;
382 var rtt = {}, rtt_enable = false;
384 // NumFetch is an absolute value
385 if(typeof resolver.stats['NumFetch'] !== 'undefined') {
386 named.lookups.numfetch[key] = true;
387 NumFetch = resolver.stats['NumFetch'];
388 delete resolver.stats['NumFetch'];
390 if(typeof resolver.stats['BucketSize'] !== 'undefined') {
391 delete resolver.stats['BucketSize'];
394 // split the QryRTT* from the main chart
395 for( var y in resolver.stats ) {
396 // we maintain an index of the values found
397 // mapping them to objects splitted
399 var look = named.lookups.resolver_stats[y];
400 if(typeof look === 'undefined') {
401 if(y.match(/^QryRTT/) !== null) {
402 named.lookups.resolver_stats[y] = {
408 named.lookups.resolver_stats[y] = {
414 look = named.lookups.resolver_stats[y];
415 // netdata.error('lookup resolver stats value: ' + y + ' >>> ' + look.type);
419 case 'rtt': rtt[look.name] = resolver.stats[y]; delete resolver.stats[y]; rtt_enable = true; break;
420 default: default_enable = true; break;
425 service.module.chartFromMembers(service, rtt, 'view_resolver_rtt_' + x, 'Bind, ' + x + ' View, Resolver Round Trip Timings', 'queries/s', 'named', 'named', netdata.chartTypes.stacked, 5600, netdata.chartAlgorithms.incremental, 1, 1);
428 service.module.chartFromMembers(service, resolver.stats, 'view_resolver_stats_' + x, 'Bind, ' + x + ' View, Resolver Statistics', 'operations/s', 'named', 'named', netdata.chartTypes.line, 5500, netdata.chartAlgorithms.incremental, 1, 1);
431 if(typeof named.lookups.numfetch[key] !== 'undefined') {
432 var id = 'named_' + service.name + '.view_resolver_numfetch_' + x;
433 var chart = named.charts[id];
435 if(typeof chart === 'undefined') {
437 id: id, // the unique id of the chart
438 name: '', // the unique name of the chart
439 title: service.name + ' Bind, ' + x + ' View, Resolver Active Queries', // the title of the chart
440 units: 'queries', // the units of the chart dimensions
441 family: 'named', // the family of the chart
442 category: 'named', // the category of the chart
443 type: netdata.chartTypes.line, // the type of the chart
444 priority: 5000, // the priority relative to others in the same family and category
445 update_every: Math.round(service.update_every / 1000), // the expected update frequency of the chart
448 id: 'queries', // the unique id of the dimension
449 name: '', // the name of the dimension
450 algorithm: netdata.chartAlgorithms.absolute,// the id of the netdata algorithm
451 multiplier: 1, // the multiplier
452 divisor: 1, // the divisor
453 hidden: false // is hidden (boolean)
458 chart = service.chart(id, chart);
459 named.charts[id] = chart;
462 service.begin(chart);
463 service.set('queries', NumFetch);
469 if(typeof resolver.qtypes !== 'undefined')
470 service.module.chartFromMembers(service, resolver.qtypes, 'view_resolver_qtypes_' + x, 'Bind, ' + x + ' View, Requests by Query Type', 'requests/s', 'named', 'named', netdata.chartTypes.stacked, 6000, netdata.chartAlgorithms.incremental, 1, 1);
472 //if(typeof resolver.cache !== 'undefined')
473 // service.module.chartFromMembers(service, resolver.cache, 'view_resolver_cache_' + x, 'Bind, ' + x + ' View, Cache Entries', 'entries', 'named', 'named', netdata.chartTypes.stacked, 7000, netdata.chartAlgorithms.absolute, 1, 1);
475 if(typeof resolver.cachestats['CacheHits'] !== 'undefined' && resolver.cachestats['CacheHits'] > 0) {
476 var id = 'named_' + service.name + '.view_resolver_cachehits_' + x;
477 var chart = named.charts[id];
479 if(typeof chart === 'undefined') {
481 id: id, // the unique id of the chart
482 name: '', // the unique name of the chart
483 title: service.name + ' Bind, ' + x + ' View, Resolver Cache Hits', // the title of the chart
484 units: 'operations/s', // the units of the chart dimensions
485 family: 'named', // the family of the chart
486 category: 'named', // the category of the chart
487 type: netdata.chartTypes.area, // the type of the chart
488 priority: 8000, // the priority relative to others in the same family and category
489 update_every: Math.round(service.update_every / 1000), // the expected update frequency of the chart
492 id: 'CacheHits', // the unique id of the dimension
493 name: 'hits', // the name of the dimension
494 algorithm: netdata.chartAlgorithms.incremental,// the id of the netdata algorithm
495 multiplier: 1, // the multiplier
496 divisor: 1, // the divisor
497 hidden: false // is hidden (boolean)
500 id: 'CacheMisses', // the unique id of the dimension
501 name: 'misses', // the name of the dimension
502 algorithm: netdata.chartAlgorithms.incremental,// the id of the netdata algorithm
503 multiplier: -1, // the multiplier
504 divisor: 1, // the divisor
505 hidden: false // is hidden (boolean)
510 chart = service.chart(id, chart);
511 named.charts[id] = chart;
514 service.begin(chart);
515 service.set('CacheHits', resolver.cachestats['CacheHits']);
516 service.set('CacheMisses', resolver.cachestats['CacheMisses']);
520 // this is wrong, it contains many types of info:
521 // 1. CacheHits, CacheMisses - incremental (added above)
522 // 2. QueryHits, QueryMisses - incremental
523 // 3. DeleteLRU, DeleteTTL - incremental
524 // 4. CacheNodes, CacheBuckets - absolute
525 // 5. TreeMemTotal, TreeMemInUse - absolute
526 // 6. HeapMemMax, HeapMemTotal, HeapMemInUse - absolute
527 //if(typeof resolver.cachestats !== 'undefined')
528 // service.module.chartFromMembers(service, resolver.cachestats, 'view_resolver_cachestats_' + x, 'Bind, ' + x + ' View, Cache Statistics', 'requests/s', 'named', 'named', netdata.chartTypes.line, 8000, netdata.chartAlgorithms.incremental, 1, 1);
530 //if(typeof resolver.adb !== 'undefined')
531 // service.module.chartFromMembers(service, resolver.adb, 'view_resolver_adb_' + x, 'Bind, ' + x + ' View, ADB Statistics', 'entries', 'named', 'named', netdata.chartTypes.line, 8500, netdata.chartAlgorithms.absolute, 1, 1);
537 // module.serviceExecute()
538 // this function is called only from this module
539 // its purpose is to prepare the request and call
540 // netdata.serviceExecute()
541 serviceExecute: function(name, a_url, update_every) {
542 if(netdata.options.DEBUG === true) netdata.debug(this.name + ': ' + name + ': url: ' + a_url + ', update_every: ' + update_every);
543 var service = netdata.service({
545 request: netdata.requestFromURL(a_url),
546 update_every: update_every,
550 service.execute(this.processResponse);
553 configure: function(config) {
556 if(this.enable_autodetect === true) {
557 this.serviceExecute('local', 'http://localhost:8888/json/v1/server', this.update_every);
561 if(typeof(config.servers) !== 'undefined') {
562 var len = config.servers.length;
564 if(typeof config.servers[len].update_every === 'undefined')
565 config.servers[len].update_every = this.update_every;
567 config.servers[len].update_every = config.servers[len].update_every * 1000;
569 this.serviceExecute(config.servers[len].name, config.servers[len].url, config.servers[len].update_every);
578 // this is called repeatidly to collect data, by calling
579 // netdata.serviceExecute()
580 update: function(service, callback) {
581 service.execute(function(serv, data) {
582 service.module.processResponse(serv, data);
588 module.exports = named;