#! /usr/bin/perl
# $Id: dnsmon-check-monitor-updates,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 strict;
use warnings;
use POSIX;
use Time::Piece;
use Log::Log4perl qw(get_logger :easy);
use Socket;
use Socket6;
use Statistics::Descriptive;
use RIPE::DNSMon::RRD;
use Getopt::Std;
use Time::HiRes qw(gettimeofday tv_interval);

Log::Log4perl::init("$ENV{'HOME'}/.log4perl.conf");


my $logger = get_logger();
my (%options, $ret,@message);
my $svcchk_template = "[%d] PROCESS_SERVICE_CHECK_RESULT;%s;DNSMON_UPDATES;%d;%s";

getopts("hp:m:w:", \%options);

$options{p} ||= 50; # Set to 50th percentile aka median
$options{c} ||= 4 * 3600; # Fatal error on + 4hrs delay for the given percentile
$options{w} ||= 2 * 3600; # Warning on +2 hrs for the given percentile

$ret = 3;

if (exists($options{h})) {
print <<"EOT";
usage: $0 -m maxdelay -w warndelay -p percentile
	Fatal error on + 4hrs delay for the given percentile
	Warning on +2 hrs for the given percentile
	default percentile = 50th, aka median
EOT
}

my $t0 = [gettimeofday];

foreach my $af (&AF_INET, &AF_INET6) {
	my $delta_stat = Statistics::Descriptive::Full->new();

	my ($lastt, $ipv);
	foreach my $domain (sort &RIPE::DNSMon::RRD::all_domains($af)) {
		$lastt = RIPE::DNSMon::RRD::last_val_domain($domain,$af);
		$delta_stat->add_data(time - $lastt);
	}
	$ipv = $af == &AF_INET ? "IPv4" : $af == &AF_INET6 ? "IPv6" : "Unknown";
	push(@message, 
		sprintf("domains(%s): %s", $ipv, ($delta_stat->percentile($options{p}) >= $options{c} ? 
			sprintf("ERR %d secs behind threshold (%d)", scalar $delta_stat->percentile($options{p}) - $options{c}, $options{c}) :	
			($delta_stat->percentile($options{p}) >= $options{w} ?
			sprintf("WARN %d secs behind threshold (%d)", scalar $delta_stat->percentile($options{p}) - $options{w}, $options{w}) : "OK"))));

	if ($delta_stat->percentile($options{p}) >= $options{c}) {
		$ret = 2;
	} elsif ($delta_stat->percentile($options{p}) >= $options{w}) {
		$ret = 1;
	} else {
		$ret = 0;
	}
}
printf("$svcchk_template\n", time(), $ENV{HOSTNAME}, $ret, join(", ", @message) . sprintf(" | delta=%fs", tv_interval($t0)));
