#!/usr/bin/perl use warnings; # Author: David Morris # Version: $Id: vitalsigns.plx,v 1.1 2004/11/05 20:51:25 davey Exp $ # Purpose: Prints red against hosts that don't ping # License: Released under the GNU Public Licence (GPL) # The terms of the GPL are available at: # http://www.gnu.org/licenses/gpl.html use Getopt::Std; getopt('c'); getopt('s'); # Packages eval 'require Net::Ping'; if ($@) { print "Net::Ping not found\n" if ($@); } else { print "Net::Ping ok\n"; use Net::Ping; } eval 'require IO::Socket'; if ($@) { print "IO::Socket not found\n" if ($@); } else { print "IO::Socket ok\n"; use IO::Socket; } eval 'require Term::ANSIColor'; if ($@) { print "Term::ANSIColor not found\n" if ($@); } else { print "Term::ANSIColor ok\n"; use Term::ANSIColor; } eval 'require DBI'; if ($@) { print "DBI not found\n" if ($@); } else { print "DBI ok\n"; use DBI; } # Variables $timeout = 10; # Number of seconds to allow for response from pingecho $sleepduration = $opt_s; # How long to sleep for before pinging all the boxes again. $column = 1; $columns = $opt_c; # How many columns of hostnames to print $inffile = "hosts.inf"; # File with list of host names sub DATABASE { $database = "somedb"; $data_source = "DBI:mysql:$database"; $username = "someuser"; $password = "somepass"; $query = "SELECT name from servers where role = \"live\" or support = \"livesupp\" order by location"; # Build the hosts hash from a database type source $dbh = DBI->connect($data_source, $username, $password) or die "Can't connect to $data_source: $dbh->errstr\n"; $sth = $dbh->prepare($query); $sth->execute(); $sth->bind_columns(\$host); while($sth->fetch()){ chomp($host); $hosts{$host} = $STATUS; } $dbh->disconnect; } sub FILE { # Build the hosts hash from a file open (HOSTS, $inffile) || die ("Could not open the file: $infile\n"); while () { $host = $_; chomp($host); $hosts{$host} = $STATUS; } } while () { # Process the $hosts hash and print results #&DATABASE ; # # Build the $hosts hash from a file &FILE; # # Build the $hosts hash from a file foreach $host (sort keys %hosts) { # What do when a host responds to ping: if (pingecho($host, $timeout)) { # Start a new line by printing a carriage return: \n if ( $column == $columns) { print color("green"), "$host\n", color("reset"); $column = 1; } # Continue the line by printing a tab: \t else { print color("green"), "$host\t", color("reset"); $column++; } } # What to do when a host doesn't respond to ping: else { # Start a new line by printing \n if ($column == $columns) { print colored("$host\t", 'blink red on_white'); #print color("red"), "$host \n", color("reset"); $column = 1; } else { print colored("$host\t", 'blink red on_white'); $column++; } } } $column = 1; # Make sure we start printing at column 1 when entering # the loop again. sleep $sleepduration; print "\n\n"; }