#!/usr/bin/perl

######################################
#
# autor: Peter Hudec 
# version: 0.1.2
# description: requsts from BIND to MRTG
#
######################################


use strict;
use Getopt::Std;

my $named_stats_file="/export/named/log/named.stats";
my $diff_config_file="/export/named/log/named.mrtg";
my $mrtg_config_file="/export/named/mrtg.cfg";
my $mrtg_indexmaker="/usr/bin/indexmaker";
my $stats_home="/export/www/customers/magic.postel.sk/mrtg-bind";
my $run_rndc="/usr/sbin/rndc -c /export/named/etc/rndc.conf stats";

my %dns_data;

sub show_data {
	my ($domain, $type);
	foreach $domain (keys %dns_data) {
		foreach $type (keys %{$dns_data{$domain}}) {
			print "$domain - $type - $dns_data{$domain}{$type}\n";
		}
	}
}

sub get_new_data() {
	my $l;
	$l = `rm -f $named_stats_file`;
	$l = `$run_rndc`;
}

sub read_old_data {
	# nacitanie dat zo subora
        open(NEWF, "$diff_config_file");
        my @raw_dns_data = <NEWF>;
	my ($dns_line, $dns_number, $dns_domain, $dns_type, $value);
	foreach $dns_line (@raw_dns_data) {
		chomp $dns_line;
		($dns_domain, $dns_type, $value, $dns_number) = split (/:/, $dns_line);
		$dns_data{$dns_domain}{$dns_type}{$value} = $dns_number;	
	}
	close(NEWF);
}

sub read_new_data {
	# nacitanie dat zo subora
	open(NEWF, "$named_stats_file");
	my @raw_dns_data = <NEWF>;
	my ($dns_line, $dns_number, $dns_domain, $dns_type);

	foreach $dns_line (@raw_dns_data) {
		if ($dns_line =~ /^(success|referral|nxrrset|nxdomain|recursion|failure) ([0-9]*)(.*)$/) {
			($dns_type, $dns_number, $dns_domain) = split(/\s/, $dns_line);
			if ($dns_domain =~ /^\s*$/) { $dns_domain = "all"; };
			$dns_data{$dns_domain}{$dns_type}{old} = $dns_data{$dns_domain}{$dns_type}{new};
			$dns_data{$dns_domain}{$dns_type}{diff} = $dns_number - $dns_data{$dns_domain}{$dns_type}{new};
			$dns_data{$dns_domain}{$dns_type}{new} = $dns_number;
			# if the BIND was restarted
			$dns_data{$dns_domain}{$dns_type}{diff} = $dns_data{$dns_domain}{$dns_type}{new} if ($dns_data{$dns_domain}{$dns_type}{diff} < 0); 
		}
	}
	close(NEWF);
}

sub write_data() {
	my ($domain, $type, $value);
	open (OUTFILE, ">$diff_config_file");
	foreach $domain (keys %dns_data) {
		foreach $type (keys %{$dns_data{$domain}}) {
			foreach $value (keys %{$dns_data{$domain}{$type}}) {
				print OUTFILE "$domain:$type:$value:$dns_data{$domain}{$type}{$value}\n";
			}
		}
	}
	close(OUTFILE);
}

sub generate_config {
	my ($domain, $l);
	open (OUTFILE, ">$mrtg_config_file");
	print OUTFILE << "EOF";
WorkDir: $stats_home
Refresh: 300
Interval: 5
WriteExpires: Yes
Forks: 4

Options[_]: growright,gauge
XSize[_]: 350
YSize[_]: 80
WithPeak[_]: ymwd
LegendI[_]: Success:&nbsp;
LegendO[_]: Failure:&nbsp;
MaxBytes[_]: 999999
YLegend[_]: requests
ShortLegend[_]: requests
EOF
	foreach $domain (sort keys%dns_data) {
		print OUTFILE << "EOF";

Title[$domain]: DNS stats for $domain
Target[$domain]: `/export/named/bind2mrtg.pl -d $domain`
PageTop[$domain]: <h1><B>DNS stats for $domain</B></h1><br>
EOF
	}
	close(OUTFILE);
	$l = `$mrtg_indexmaker $mrtg_config_file > $stats_home/index.html`; 
}

sub get_domain_info {
	my ($domain) = @_;
	print "$dns_data{$domain}{success}{diff}\n";
	print "$dns_data{$domain}{failure}{diff}\n";
}

sub usage {
	print STDERR << "EOF";

usage $0 [-h] <-d domain|-g>
     -h         : this (help) message
     -d domain  : get information for the domain
     -g         : get information form BIND stat file
     -c         : generate MRTG confihuration to STDOUT
EOF
}

my %args;
getopts("hgd:c", \%args) or usage() and exit;
usage() and exit if $args{h};

if ($args{g}) {
	get_new_data();
	read_old_data();
	read_new_data();
	write_data();
}

if ($args{d}) {
	read_old_data();
	get_domain_info($args{d});
}

if ($args{c}) {
	read_old_data();
	generate_config();
}

