Previous Article Next Article 用 MRTG 畫 BIND DNS 的查詢量統計
Posted in Linux

用 MRTG 畫 BIND DNS 的查詢量統計

用 MRTG 畫 BIND DNS 的查詢量統計 Posted on 2013 年 03 月 28 日Leave a comment

BIND DNS MRTG

 

從 BIND dump 出來的統計資訊如下 (不過最新版本的 BIND 它又長的完全不一樣了)

+++ Statistics Dump +++ (1364372549)
success 272949870
referral 26846823
nxrrset 48759965
nxdomain 32763784
recursion 540826
failure 6113
--- Statistics Dump --- (1364372549)

這個值是累加的

所以排程是五分鐘取一次,然後扣掉前五鐘前的值,也就等於是這五分鐘內所產生的 query 量 ,然後再除以 5 等於是平均每分鐘的量

Queryies = (success + referral + nxrrset + nxdomain + recursion + failure ) / 5

Replied = (Queries – failure ) / 5

MRTG.cfg (參考來源 : http://www.megalinux.net/dns-monitoring-using-mrtg/)

(我稍作修改)

WorkDir: /path/dns/query
Target[ssorc]: `/path/check_dns_stats.pl`
Options[ssorc]: gauge,growright,nopercent,integer,unknaszero
Title[ssorc]: DNS Server
RouterUptime[ssorc]: public@localhost
MaxBytes[ssorc]: 32000
AbsMax[ssorc]: 64000
WithPeak[ssorc]: wmy
Colours[ssorc]: YELLOW #F9C000,RED #F90000,LIGHT YELLOW #FFFFBB,LIGTH RED #FF8080
ShortLegend[ssorc]:queries/m
YLegend[ssorc]: Qs per Minute
Legend1[ssorc]: Queries received over 1 minute
Legend2[ssorc]: Replied Queries received over 1 minute
Legend3[ssorc]: Maximal Queries over 5 minutes
Legend4[ssorc]: Maximal Replied Queries over 5 minutes
LegendI[ssorc]: Queries: 
LegendO[ssorc]: Replied: 
PageTop[ssorc]: <H1>DNS Info</H1><TABLE><TR><TD>System:</TD> <TD>ssorc</TD></TR><TD>Description:</TD><TD>DNS Monitor</TD></TR><TR><TD>Details:</TD> <TD>Query Monitor</TD></TABLE>

 

check_dns_stats.pl (參考來源 : http://www.megalinux.net/dnsstats.pl)

(我稍作修改)

#!/usr/bin/perl
use strict;
# Set files to use, change these to suit your needs
my $oldstatfile = "/var/tmp/dns-old.txt";
my $newstatfile = "/var/named/data/named_stats.txt";
# Obtain old DNS stats from stored file
open(OLDF, "$oldstatfile");
my @raw_data = <OLDF>;
close(OLDF);
my $oldquery = @raw_data[0];
my $oldreply = @raw_data[1];
chomp($oldquery);
chomp($oldreply);
# Check if there is data, if not, initialize
if (! $oldquery) {
 $oldquery = 0;
}
if (! $oldreply) {
 $oldreply = 0;
}
# Get new data from file, first clean the stats file and dump new
system("cat /dev/null > $newstatfile");
system("/usr/sbin/rndc stats");
open(NEWF, "$newstatfile");
my @dns_data = <NEWF>;
close(NEWF);
my $newquery = 0;
my $newreply = 0; 
my $newfailure = 0;
foreach my $line (@dns_data) {
 if ($line =~ /^\w+ ([0-9]*)/) {
 $newquery = $newquery + $1;
 }
 if ($line =~ /^failure ([0-9]*)/) {
 $newfailure = $1;
 }
}
$newreply = $newquery - $newfailure;
# Calculate difference between old and new. Divide by 5 to get per minute
my $diffquery = ($newquery - $oldquery) / 5;
my $diffreply = ($newreply - $oldreply) / 5;
# Store new data
open(OLDF, ">$oldstatfile");
print(OLDF "$newquery\n$newreply");
close(OLDF);
# Print difference
print("$diffquery \n$diffreply \n");

發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *