Here is some code that I have written. I know that there is a better way of
writing this. As it stands the
code between the "#Evaluation code" does not work as desired. I cannot seem
to get these if ((case1) and (case2))
type statements to work. How would I and what am I doing wrong? Also, is
there a better way of doing this?

Any pointers for a more perlish way of writing this?? Thanks in advance for
your help.


Regards,
Lorne


---- SNIP ----

use strict;
use Net::Ping;
use Net::SMTP;

my $host_number = 0;
my @host;
my @failed_pings;

#Constant type values
our $smtp_server = "mail.server.com";
our $timeout = 200;
our $max_failed = 4;

#Read in values from configuration file
open(CONFFILE, "$ARGV[0]") or die "$0: error during open(): $!\n";
while (<CONFFILE>)
{

    my ($field_1, $field_2) = split(/:/, $_);
    push (@host, $field_1);
    print "Host: $field_1\n";
    push (@failed_pings, $field_2);
    print "Failed Pings: $field_2\n";


}
close(CONFFILE);

#Iterate through hosts and update failed pings accordingly
foreach my $hostname (@host)
{
my $host_state = ping_host($hostname, $timeout);

print "Host Number: $host_number\n";
print "$hostname\t$host_state\n";

#Evaluation code

#Host was down and is now back up
if (($host_state eq 1) and ($failed_pings[$host_number] ge $max_failed))
{
    print "Host: $hostname is up.\n";
    #$failed_pings[$host_number] = 0;
}

#No errors at all
if (($host_state eq 1) and ($failed_pings[$host_number] lt $max_failed))
{
    print "Host: $hostname has no errors.\n";
    #$failed_pings[$host_number] = 0;
}

#Failure for one ping
if (($host_state eq 0) and ($failed_pings[$host_number] lt $max_failed))
{
    print "Host: $hostname failed for one ping.\n";
    #$failed_pings[$host_number]++;
}

#Hostname is completely down
if (($host_state eq 0) and ($failed_pings[$host_number] ge $max_failed))
{
    print "Host: $hostname is down.\n";
    #$failed_pings[$host_number]++;
}

#Evaluation code

#Now update indices so as not to trip my poorly written if statements
if ($host_state eq 0) {$failed_pings[$host_number]++}
else {$failed_pings[$host_number] = 0}


#Index for failed pings
    $host_number++;
}


#Reset index and open conf file for input.
$host_number = 0;
open (RESULTSFILE, ">$ARGV[0]");

#Add new values to conf file
foreach my $hostname (@host)
{

#Be sure of no trailing \n
    if ($host_number lt $#host)
    {
    print RESULTSFILE "$hostname:$failed_pings[$host_number]\n";
    print "Inserting Data\n";
}
    else
    {
 print RESULTSFILE "$hostname:$failed_pings[$host_number]";
 print "Inserting Data\n";
 }

    $host_number ++;
}
#End iterate through hosts

close(CONFFILE);

#---- Functions ----

#---- Ping Host ----
sub ping_host
{
    my ($hostname, $timeout) = @_;

    my $ping_pointer = Net::Ping->new("icmp");
    if (!$ping_pointer->ping($hostname, $timeout))
    {
 return(0)
 }

    return (1);
}


#---- Send SMTP mail based on input ----
sub send_mail
{

my ($smtp_to, $smtp_from, $smtp_subject, $message_body) = @_;

#---- Testing Output ----
print "Sending mail..";
print "\nTo: $smtp_to\n";
print "From: $smtp_from\n";
print "Subject: $smtp_subject\n";
print "Data: $message_body";


# Create SMTP Object
my $smtp = Net::SMTP->new($smtp_server);

print "Your Domain is: ", $smtp->domain;
print "\n",$smtp->banner,"\n";

# Send Mail
$smtp->mail();
$smtp->to($smtp_to);
$smtp->data();
$smtp->datasend("To: $smtp_to\n");
$smtp->datasend("From: $smtp_from\n");
$smtp->datasend("Subject: $smtp_subject\n");
$smtp->datasend("\n");
$smtp->datasend($message_body);
$smtp->dataend();

$smtp->quit();

}

---- SNIP ----



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to