#! /usr/bin/perl
# $Id: dnsmon-examine-domain,v 1.2.4.1 2008/04/13 15:49:47 ruben Exp $
############################################################################
###    (C)opyright 2003 - 2008 RIPE NCC
###    This file is part of DNSMon
###
###    DNSMon is free software: you can redistribute it and/or modify
###    it under the terms of the GNU General Public License as published by
###    the Free Software Foundation, either version 3 of the License, or
###    (at your option) any later version.
###
###    DNSMon is distributed in the hope that it will be useful,
###    but WITHOUT ANY WARRANTY; without even the implied warranty of
###    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
###    GNU General Public License for more details.
###
###    You should have received a copy of the GNU General Public License
###    along with DNSMon.  If not, see <http://www.gnu.org/licenses/>.
############################################################################

use POSIX;
use Time::Piece;
use Log::Log4perl qw(get_logger :easy);
use Socket;
use Socket6;
use Getopt::Std;

Log::Log4perl->easy_init($WARN);

# Only enable on newer versions
$Getopt::Std::STANDARD_HELP_VERSION = 1 if $Getopt::Std::VERSION >= 1.05;


{
	use RIPE::DNSMon::RRD;
	my %opts;
	my (@proto);
	my (@domain);

	$logger = get_logger();
	getopts('hvda46', \%opts);

	if ($opts{h}) {
		&usage;
	}
	if ($opts{v}) {
		Log::Log4perl->easy_init($INFO);
		$RIPE::DNSMon::RRD::opt_v = 1;
	}
	if ($opts{d}) {
		Log::Log4perl->easy_init($DEBUG);
		$RIPE::DNSMon::RRD::opt_d = 1;
	}

	if ($opts{4}) {
		push(@proto, &AF_INET);
	}
	if ($opts{6}) {
		push(@proto, &AF_INET6);
	}
	if (!$opts{4} && !$opts{6}) {
		push(@proto, &AF_INET, &AF_INET6);
	}

	if ($opts{a}) {
		@domain = (sort &RIPE::DNSMon::RRD::all_domains($af));
	} else {
		@domain = @ARGV;
	}

	if ($#ARGV < 0) {
		print "Error: no domains to process\n\n";
		&usage;
	}	

	foreach my $af (@proto) {
		foreach my $domain (@domain) {
			my $ipv = $af == &AF_INET ? "IPv4" : $af == &AF_INET6 ? "IPv6" : "Unknown";

			$lastt = RIPE::DNSMon::RRD::last_val_domain($domain,$af);
			print strftime("%Y%m%d %H:%M:%S",gmtime($lastt)), " $domain($ipv):	", (time - $lastt), " secs behind\n";
			foreach my $s (RIPE::DNSMon::RRD::domain_servers($domain,$af)) {
				printf("\t%s %s($ipv)\n",$s, strftime("%Y%m%d %H:%M:%S",gmtime(RIPE::DNSMon::RRD::last_val($s,$af))));
			}
		}
	}
}

sub HELP_MESSAGE() {
        &usage;
}

sub usage {
        print STDERR <<"EOT";
dnsmon-examine-domain [-h] | [-4|-6] [-v|-d] | [-a|domains] 
EOT
        exit 1;
}
__END__

=pod

=head1 NAME

B<dnsmon-examine-domain> -- Show time of last updates for a domain

=head1 SYNOPSIS:

dnsmon-examine-domain [-h] | [-4|-6] [-v|-d] | [-a|domains]

=head1 OPTIONS

=over

=item B<-h> Short help message

=item B<-4> Only show IPv4 domains

=item B<-6> Only show IPv6 domains

=item B<-a> Show all domains

=item B<-v> Verbose mode

=item B<-d> debug mode

=back

=head1 USAGE

dnsmon-examine-domain uses the RIPE::DNSMon::RRD API to get the time of the
last update for a particular domain. Also listed are the last update times of
the per domain nameserver set.

This can be used when the website for some reason displays old data and you
want to know whether the problem is in the RRD files used or not.


=head1 BUGS

It could show more

=head1 SEE ALSO

L<RIPE::DNSMon::RRD>, L<http://dnsmon.ripe.net/>

=cut
