#!/usr/local/bin/perl

#
# Script to read in the 'stats.backend' file from a Tornado Front End server
# The output is identical to the HTML output of 'okaybye.pl'
# Written by Jamie Wilson - wilson88@yahoo.com
#

$input = $ARGV[0];
$output = $ARGV[1];

if (!$input || !$output) { print "\nUsage: be_stats.pl <input file - stats.backend> <output HTML file>\n"; exit; }

open (INPUT, "<$input");
open (OUTPUT, ">$output");

print OUTPUT <<EOP;
<HTML>
<HEAD>
<TITLE>news statistics summary</TITLE>
</HEAD>
<BODY>
<CENTER>
<H1>news statistics summary</H1>
<FONT SIZE=1>Generated at 
EOP
print OUTPUT scalar localtime;
print OUTPUT <<EOP;
</FONT><BR>

<HR WIDTH="80%">
<H3>Tornado Front End stats.backend Information</H3>
<TABLE BORDER=2>
<TR BGCOLOR="#396B9C">
<TH><B><FONT SIZE="-1" COLOR="#FFFFFF">Timestamp</FONT></B></TH>
<TH><B><FONT SIZE="-1" COLOR="#FFFFFF">Backend Number</FONT></B></TH>
<TH><B><FONT SIZE="-1" COLOR="#FFFFFF">Fetch Priority</FONT></B></TH>
<TH><B><FONT SIZE="-1" COLOR="#FFFFFF">Articles Found</FONT></B></TH>
<TH><B><FONT SIZE="-1" COLOR="#FFFFFF">Articles Failed</FONT></B></TH>
<TH><B><FONT SIZE="-1" COLOR="#FFFFFF">Headers Found</FONT></B></TH>
<TH><B><FONT SIZE="-1" COLOR="#FFFFFF">Headers Failed</FONT></B></TH>
<TH><B><FONT SIZE="-1" COLOR="#FFFFFF">TCP Received</FONT></B></TH>
<TH><B><FONT SIZE="-1" COLOR="#FFFFFF">Shared Received</FONT></B></TH>
<TH><B><FONT SIZE="-1" COLOR="#FFFFFF">Header Bytes</FONT></B></TH>
<TH><B><FONT SIZE="-1" COLOR="#FFFFFF">Connection Available</FONT></B></TH>
<TH><B><FONT SIZE="-1" COLOR="#FFFFFF">Highest Connection Count</FONT></B></TH>
<TH><B><FONT SIZE="-1" COLOR="#FFFFFF">Connection Pool</FONT></B></TH>
<TH><B><FONT SIZE="-1" COLOR="#FFFFFF">Connection Not Available</FONT></B></TH>
<TH><B><FONT SIZE="-1" COLOR="#FFFFFF">Backlog</FONT></B></TH>
<TH><B><FONT SIZE="-1" COLOR="#FFFFFF">Name</FONT></B></TH>
<TH><B><FONT SIZE="-1" COLOR="#FFFFFF">Connections</FONT></B></TH>
</TR>

EOP

while (<INPUT>) {
	chomp;
	@line = split(/\s+/, $_);
	$line[0]= scalar localtime $line[0];
	print OUTPUT "<TR>";
	foreach (@line) {
		if ( $line[13] > 0 || $line[14] > 10000) {
		print OUTPUT <<EOP;
<TD ALIGN=RIGHT BGCOLOR="RED"><B><TT><FONT SIZE="-1">$_</FONT></TT></B></TD>
EOP
		$alert = 1;
		}
		else {
		print OUTPUT <<EOP;
<TD ALIGN=RIGHT><B><TT><FONT SIZE="-1">$_</FONT></TT></B></TD>              
EOP
		}
	}
	print OUTPUT "</TR>"
}

print OUTPUT <<EOP;
</TABLE>
</CENTER>
</BODY>
</HTML>
EOP

close (INPUT);
close (OUTPUT);

# This part is optional, "alert" will be defined above if certain events occur.
# In this case, if the 13th column in stats.detailed (number of times the FE could not
# connect to the BE) is > 0, or the 14th column (article backlog from the BE) is > 10000 then that is bad, 
# and you could do something here, like e-mail someone who can look into it.
# if ($alert) {
# Do Something
# }

