#!/usr/bin/perl

####################################################################
# Load modules
use Term::ANSIColor;
use LWP::Simple;

####################################################################
# Defign the format for output
format STDOUT_TOP =
     HOSTNAME      SERVICE      STATUS    DATE                 DETAILS  
.

format STDOUT =
@<<< @<<<<<<<<<<<< @<<<<<<<<<<< @<<<<<<<< @<<<<<<<<<<<<<<<<<<  ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$counter $hostname,      $service,      $status,    $date,                $details                
.

#
#  Grab out the pars we want to see.
#
my $page = get("http://monitor/cgi/showview?REQ_CHAFF=20030418010614-18YZFXE9202&amp;REQ_SCHEME=MACHDB&amp;REQ_STATE=view_dead&amp;CTL_FILTER=^deathstar")
  or die "Couldn't fetch page.";

####################################################################
#        \s*  ==  matches any whitespace character, including new line
#    [^\S\n]  ==  any whitespace but newlines

#
#  Split page into table rows
#
(@trs)=split (/<tr>/ , "$page" );

#
#  shift header off top of array
#
shift (@trs);  # First bit of header stuffs
shift (@trs);  # Second bit of header stuffs
shift (@trs);  # Columb headers:  HOSTNAME     SERVICE   STATUS   DATE                DETAILS


$counter="0";  
   
#
#  For each row, split into td's (table detail/data)
#
foreach $tr (@trs){
      $counter++;
      
     #debug# print "$counter\t$tr\n";  #debug# 
      
      ####################################################################
      #        \s*  ==  matches any whitespace character, including new line
      #    [^\S\n]  ==  any whitespace but newlines

      #
      # Split out the talbe data into output: 
      #       HOSTNAME     SERVICE   STATUS   DATE                DETAILS
      #
      if ("$tr" =~ m{
                     (<td\s*bgcolor=\#\w{6}><font\s*color=\#\w{6}\s*size=1><b>
                     (\w+\d{3})                                                  # HOSTNAME 
		     <\/b><\/font><\/td>                                       
		     ||                                                          # OR
		     (<td>\&nbsp\;<\/td>)                                       # Can be a blank if representing deathstar from before
		     )
		     <td><font\s*size=1\s*color=\#\w{6}><b>
		     (\w+)                                                       # SERVICE 
		     <\/b><\/font><\/td><td><font\s*size=1\s*color=\#\w{6}><b>
		     (\w+)                                                       # STATUS
		     <\/b><\/font><\/td><td><font\s*size=1\s*color=\#\w{6}><b> 
		     (\d{4}-\d{2}-\d{2}\s*\d{2}:\d{2}:\d{2})                     # DATE
		     <\/b><\/font><\/td><td><font\s*size=1\s*color=\#\w{6}><b>
		     (.*)                                                        # DEATAILS
		     <\/b><\/font><\/td><\/tr>
	}xig )
     {
        
        $hostname_test = "$2$3";
	$service  = "$4"; 
	$status   = "$5";
	$date     = "$6";
	$details  = "$7";   

	$hostname_test = "$2$3";
	unless ( $hostname_test =~ m/<td>\&nbsp\;<\/td>/ ){
	  $hostname="$hostname_test";
	}
	#
	# I don't care about long ping times.
	#	
	if ( $details =~ m/PING WARNING/ ){
	   next;
	}
	write STDOUT ;
      }
}

