in reply to Re: improving the aesthetics of perl code
in thread improving the aesthetics of perl code
#!/usr/bin/perl -T # mship.cgi # Just add hosts ip addresses, fqdn, or just hostnames # gives a simple status of hosts up or hosts down # next step is to add logging. The aim is not # a full featured network monitor, just a quickie list # of whats up and whats down - Its probably not useful # in a large environment... # Ted Fiedler <fiedlert@gmail.com> use strict; use warnings; use Net::Ping; use CGI qw/:standard/; use POSIX qw(strftime); # Clean up our UNIX environment # for more infor read perldoc perlsec $ENV{'PATH'} = '/bin:/usr/bin'; delete @ENV{qw(IFS CDPATH ENV BASH_ENV)}; my (@hosts, @uphosts, @downhosts); if ( -e "ship.cfg" ) { open (CFGFILE, "ship.cfg") || die $!; while (my @TMP=<CFGFILE>) { next if ( /#/ ); # Lets untaint the data after making sure it only matches # word characters... For more info read perldoc perlsec. foreach $_(@TMP) { if ($_ =~ /^([\w.]+)$/) { $_ = $1; push (@hosts, $1); } else { next; } } } } else { # or enter your hosts here... # This is the most secure/preferred way to do this @hosts = qw/sapdev0 sapprd0 saptst0 pcbackup vader/; } # I haven't decided what else to do here - it just semantics... my @verbage=("are", "hosts", "are", "hosts"); # udp is default # other values can be icmp (if youre root) # or TCP, which is expensive on the wire my $p = Net::Ping->new('udp'); # Iterate through my hosts and get their status foreach my $host(@hosts) { chomp($host); # Cleans up ship.cfg if you're using it if ($p->ping($host)) { push (@uphosts, $host); # its either UP } else { push (@downhosts, $host); # or its DOWN... } } $p->close(); print header, start_html; print "<tt>\n"; print "\n<center><h3>MSHIP - my status of host IP's</h3></center>\n"; my $now=strftime "%m/%d/%Y %H:%M:%S", (localtime); print "<center>$now</center>\n"; print hr; # Just cleaning up the verbage is are host hosts etc... if (scalar(@downhosts) == 1 ) { $verbage[2]="is"; $verbage[3]="host" } if (scalar(@uphosts) == 1 ) { $verbage[0]="is"; $verbage[1]="host"; } # We don't care about things that don't exist... unless (scalar(@downhosts) == 0) { print p,"There $verbage[2] ",scalar +(@downhosts)," $verbage[3] unreachable\n",br } print "There $verbage[0] ",scalar(@uphosts)," $verbage[1] alive\n"; print p,"The following $verbage[1] $verbage[0] alive: \n",br; foreach my $uitem (sort @uphosts) { # sort is for cleanliness... print li("$uitem\n"),br; } # Again, we don't care about things that don't exist unless (scalar(@downhosts) == 0) { print p,"The following $verbage[3] +$verbage[2] unreachable: \n",br } foreach my $ditem (sort @downhosts) { print li("$ditem\n"),br; } print "</tt>\n"; print start_form, p,submit('Refresh'), end_html;
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^3: improving the aesthetics of perl code
by jdporter (Paladin) on Jan 21, 2005 at 22:44 UTC | |
Re^3: improving the aesthetics of perl code
by pingo (Hermit) on Jan 21, 2005 at 16:23 UTC | |
by Tanktalus (Canon) on Jan 21, 2005 at 16:29 UTC | |
by jdporter (Paladin) on Jan 21, 2005 at 22:25 UTC | |
by sleepingsquirrel (Chaplain) on Jan 21, 2005 at 22:43 UTC | |
Re^3: improving the aesthetics of perl code
by wfsp (Abbot) on Jan 21, 2005 at 16:44 UTC | |
Re^3: improving the aesthetics of perl code
by g0n (Priest) on Jan 21, 2005 at 16:15 UTC | |
Re^3: improving the aesthetics of perl code
by hardburn (Abbot) on Jan 21, 2005 at 22:23 UTC | |
Re^3: improving the aesthetics of perl code
by BrentDax (Hermit) on Jan 24, 2005 at 09:56 UTC |