#!/usr/bin/perl -w
# -*- perl -*-

=head1 NAME

ntp_ - Plugin to monitor NTP statistics

=head1 CONFIGURATION

The following environment variables are used by this plugin:

 [ntp_*]
  env.lowercase       - lowercase hostnames after lookup
  env.nodelay 1       - Set to 1 to remove delay

=head1 NOTES

This plugin is now "manual" because it's not very useful, it is
imposible to determine which peer is used as "master". There is a
ntp_offset plugin that reports a more useful value.

=head1 AUTHOR

Author unknown

=head1 LICENSE

License unknown

=head1 MAGIC MARKERS

#%# family=manual
#%# capabilities=autoconf suggest

=cut

use strict;
use Net::hostent;
use Socket;

my $nodelay = $ENV{'nodelay'} || 0;

if ($ARGV[0] and $ARGV[0] eq "autoconf") {
	`ntpq -c help >/dev/null 2>/dev/null`;
	if ($? eq "0") {
		if (`ntpq -c "hostnames no" -c peers | wc -l` > 0) {
			print "yes\n";
			exit 0;
		} else {
			print "no (unable to list peers)\n";
			exit 0;
		}
	} else {
		print "no (ntpq not found)\n";
		exit 0;
	}
}

if ($ARGV[0] and $ARGV[0] eq "suggest") {
	my @lines = `ntpq -c "hostnames no" -c peers`;
	foreach (@lines) {
		next unless /^.(\d+\.\d+\.\d+\.\d+)/;
		next if /^.224\.0\.1\.1/;
		my $addr = $1;
		my $name;
		if( my $lcid= /^.127\.127\.1\.(\d+)/) {
			$lcid = $lcid - 1;
			$name = "LOCAL($lcid)";
		} else {
			$name = gethostbyaddr(inet_aton($addr));
			$name = defined $name ? $name->name : $addr;
		}
		$name = lc $name if exists $ENV{"lowercase"};
		$name =~ s/[\.\-()]/_/g;
		print $name, "\n";
	}
	exit 0;
}

$0 =~ /ntp_(.+)*$/; 
my $name = $1;
die "No hostname provided" unless defined $name;

if ($ARGV[0] and $ARGV[0] eq "config") {
	my @lines = `ntpq -c "hostnames no" -c peers`;
	my $host;
	foreach (@lines) {
		next unless /^.(\d+\.\d+\.\d+\.\d+)/;
		next if /^.224\.0\.1\.1/;
		my $addr = $1;
		my $host;
		if( my $lcid= /^.127\.127\.1\.(\d+)/) {
			$lcid = $lcid - 1;
			$host = "LOCAL($lcid)"
		} else {
			$host = gethostbyaddr(inet_aton($addr));
			$host = defined $host ? $host->name : $addr;
		}
		$host = lc $host if exists $ENV{"lowercase"};
		my $host_ = $host;
		$host_ =~ s/[\.\-()]/_/g;
		next unless $host_ eq $name;
		print "graph_title NTP statistics for peer $host\n";
	}
	print "graph_args --base 1000 --vertical-label seconds --lower-limit 0\n";
	print "graph_category time\n";
	print "delay.label Delay\n";
	print "delay.draw LINE\n";
	print "delay.graph no\n" if $nodelay;
	print "delay.cdef delay,1000,/\n";
        print "offset.label Offset\n";
        print "offset.draw LINE\n";
	print "offset.cdef offset,1000,/\n";
        print "jitter.label Jitter\n";
        print "jitter.draw LINE\n";
	print "jitter.cdef jitter,1000,/\n";
        exit 0;
}

my @lines = `ntpq -c "hostnames no" -c peers`;
foreach (@lines) {
	next unless /^.(\d+\.\d+\.\d+\.\d+)/;
	next if /^.224\.0\.1\.1/;
	my $addr = $1;
	my $host;
	if( my $lcid= /^.127\.127\.1\.(\d+)/) {
		$lcid = $lcid - 1;
		$host = "LOCAL($lcid)"
	} else {
		$host = gethostbyaddr(inet_aton($addr));
		$host = defined $host ? $host->name : $addr;
	}
	$host = lc $host if exists $ENV{"lowercase"};
	$host =~ s/[\.\-()]/_/g;
	next unless $host eq $name;
	my @F = split;
	print <<"EOT";
delay.value $F[7]
offset.value $F[8]
jitter.value $F[9]
EOT
}

exit 0;

# vim:syntax=perl
