Lorne Easton wrote:
> 
> 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.

The first suggestion I would make is to use a hash instead of two
arrays to store your data and you should use numerical comparison
operators to compare numbers (< instead of lt.)


> use strict;
> use Net::Ping;
> use Net::SMTP;
> 
> my $host_number = 0;

my %hosts;


> #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>)
> {

    chomp;
    my ( $host, $count ) = split /:/;
    $hosts{ $host } = $count;
    print "Host: $host\nFailed Pings: $count\n";


> }
> close(CONFFILE);
> 
> #Iterate through hosts and update failed pings accordingly

for my $hostname ( keys %hosts ) {


> my $host_state = ping_host($hostname, $timeout);
> 
> print "Host Number: $host_number\n";
> print "$hostname\t$host_state\n";
> 
> #Evaluation code

if ( $host_state ) {
    print "Host: $hostname is up.\n"         if $hosts{$hostname} >= $max_failed;
    print "Host: $hostname has no errors.\n" if $hosts{$hostname} < $max_failed;
    $hosts{$hostname} = 0;
    }
else {
    print "Host: $hostname is down.\n"             if $hosts{$hostname} >= $max_failed;
    print "Host: $hostname failed for one ping.\n" if $hosts{$hostname} < $max_failed;
    $hosts{$hostname}++;
    }


> #Index for failed pings
>     $host_number++;
> }
> 
> #Reset index and open conf file for input.
> $host_number = 0;

open RESULTSFILE, ">$ARGV[0]" or die "Cannot open '$ARGV[0]' $!";


> #Add new values to conf file

while ( my ( $host, $count ) = each %hosts ) {
    print RESULTSFILE "$host:$count\n";
    print "Inserting Data\n";
    }


>     $host_number ++;
> }
> #End iterate through hosts

close RESULTSFILE;


> #---- 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();
> 
> }



John
-- 
use Perl;
program
fulfillment

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

Reply via email to