#!/usr/bin/perl -wT
# ------------------------------------------------------------------------------
# File Name:            check_dns_random.pl
# Author:               Richard Mayhew - South Africa
# Date:                 2000/01/26
# Version:              1.0
# Description:          This script will check to see if dns resolves hosts
#                       randomly from a list using the check_dns plugin. 
# Email:                netsaint@splash.co.za
# ------------------------------------------------------------------------------
# Copyright 1999 (c) Richard Mayhew
# Credits go to Ethan Galstad for coding Nagios
# If any changes are made to this script, please mail me a copy of the
# changes :)
# License GPL
# ------------------------------------------------------------------------------
# Date          Author          Reason
# ----          ------          ------
# 1999/09/26    RM              Creation
# ------------------------------------------------------------------------------

# -----------------------------------------------------------------[ Require ]--
require 5.004;

# --------------------------------------------------------------------[ Uses ]--
use strict;
use lib utils.pm ;
use Plugin;
use Plugin::Parameter qw($helpparameterlist $versionparameterlist $hostnameparameter
			 $filenameparameter $commandfileparameter
			 $verboseparameter);

use vars qw($opt_H $opt_f $opt_F $PROGNAME);

use utils qw(%ERRORS &usage);

#use Socket;

$filenameparameter->default("/etc/nagios/domains.list");
$commandfileparameter->default("/usr/lib/nagios/plugins/check_dns");

my @commandparameterlist = ( $hostnameparameter, $filenameparameter, $commandfileparameter, $verboseparameter );
my $plugin = new Plugin(-revision => '$Revision: 1.2 ',
			-copyright => "2000 Richard Mayhew, 2004 Howard Wilkinson <howard\@cohtech.com>",
			-shortcomment => "Perl Check Random DNS plugin for Nagios",
			-longcomment => "Check to see if one of a randomly selected set of domain names can be resolved by a host. Uses check_dns plugin",
			-parameterlists => [ \@commandparameterlist, $helpparameterlist, $versionparameterlist ]);

# --------------------------------------------------------------[ Enviroment ]--
$ENV{PATH} = "/bin";
$ENV{BASH_ENV} = "";
$|=1;

$plugin->init();

if ( ! -f $opt_f) {
  $plugin->usage();
  usage("$PROGNAME UNKNOWN: File $opt_f does not exist\n");
}
my $wc = `/usr/bin/wc -l $opt_f`;
$wc =~ m/(\d+)/;
$wc = $1;
my $srv_file = "";

if(!open(DOMAIN,"<$opt_f")) {
  $plugin->usage();
  usage("$PROGNAME UNKNOWN: Error Opening $opt_f File!\n");
}
while (<DOMAIN>) {
  $srv_file .= $_;
}
close(DOMAIN);

my @data = split(/\n/,$srv_file);

my $x = rand $wc;
my ($z,$y) = split(/\./,$x);

$data[$z] =~ m/(.*)/;
$y = $1;

exec $opt_F, "--hostname=" . $y, "--server=".$opt_H;
