#! perl -w

=head1 NAME

check_netdiag.pl - checks the output of netdiag for errors.

=head1 VERSION

Version 1.0

=head1 AUTHOR

(c) 2003 Hannes Schulz <mail@hannes-schulz.de>

=head1 SYNOPSIS

./check_netdiag.pl [--help] [--field <field>]

 <field>: a unique key word in one of the lines listed in the code(@tests).
          default: "all" -- tests everything. Case sensitive.
Example:

  ./check_netdiag.pl --field WINS
  ./check_netdiag.pl --field "Trust relationship"

=head1 DESCRIPTION

This looks through the output of "netdiag /skip:DCList" for failures and
outputs the failed checks. A Critical is returned if checks FAIL, everything
else counts as OK.

=cut

use strict;
use Getopt::Long;

my @tests = (
	"WINS service test",
	"DNS test",
	"Netcard queries test",
	"DC discovery",
	"Trust relationship",
	"Kerberos test",
	"Bindings test",
	"LDAP test"
);

my $cmdline = "netdiag /skip:DClist";

my ($field, $help);
printHelp() 
	if(!GetOptions('f|field=s' => \$field, 'h|help' =>\$help) or $help);

$field = "all" if(!$field);

my $t1 = time;
my $netdiag = `$cmdline`;

my $test;
my @failed;
foreach $test (@tests){
	next unless($test =~ $field or $field eq "all");
  if($netdiag =~ m/$test.*Failed/ix){push @failed, $test;}
}

my $duration = time-$t1;
my $numfailed = $#failed + 1;
if($numfailed < 1){print "Failed: none. | failed=0,time=$duration\n"}
else{print "Failed: ", join(", ",@failed), " | failed=$numfailed,time=$duration\n"}

exit 2 if $numfailed;  # Nagios: Critical
exit 0 ;               # Nagios: OK

sub printHelp{
print <<EOT
$0 Version 1.0
Copyright (c) Hannes Schulz 2003

./check_netdiag.pl [--help] [--field <field>]

 <field>: a unique key word in one of the lines listed in the code(\@tests).
          default: "all" -- tests everything. Case sensitive.
Example:

  ./check_netdiag.pl --field WINS
  ./check_netdiag.pl --field "Trust relationship"

EOT
;exit 3 # Nagios: unknown
}
