#!/opt/ivrutools/bin/perl

use DBI;
use CGI;

$ENV{TZ}="PST8PDT";
$MAXCOLS=16;
print "Content-type: text/html\n\n";

eval {
		
		require "rslmon_sub.pl";
		require "rslloc6.pl";
		require "timelocal.pl";
		$cgi=new CGI;
		
		$savemode=1 if($cgi->param('savemode'));
		$sortorder='dctr' unless($sortorder=$cgi->param('sortorder'));
		$testmode=1 if($cgi->param('testmode'));
		require "timelocal.pl";
		$cgi=new CGI;
		
		$savemode=1 if($cgi->param('savemode'));
		$sortorder='dctr' unless($sortorder=$cgi->param('sortorder'));
		$testmode=1 if($cgi->param('testmode'));
		
		unless($savemode)
		{
				# One-time message for clients
				$MESSAGEDIR="/var/rslmon/mon4_intro";
				$REMOTE_HOST=$ENV{REMOTE_ADDR}
				unless($REMOTE_HOST=$ENV{REMOTE_HOST});
				if(-d $MESSAGEDIR && $REMOTE_HOST && ! -f "$MESSAGEDIR/$REMOTE_HOST")
				{
						print "<script>\n";
						print "window.open('http://civworld.telebank.wellsfargo.com/rslscript/NewVersion.htm','NewRSLMon','width=500,height=400,directories=no,location=no,menubar=no,scrollbars=yes,status=no,alwaysRaised=Yes,toolbar=no,resizable=no')\n";
						print "</script>\n";
						system("touch $MESSAGEDIR/$REMOTE_HOST 2>> /tmp/touch.err");
				}
		}
		
# Defaults
		$etime1=time()-30;
		$def_etime=$etime=($etime1-$etime1%60);
		(@etimes)=localtime($etime);
		if($endtime=$cgi->param("endtime"))
		{
				($etimes[2],$etimes[1])=split(/:/,$endtime);
		}
		$etimes[3]=$iday								if($iday=$cgi->param("day"));
		$etimes[4]=$imonth-1				if($imonth=$cgi->param("month"));
		$etimes[5]=$iyear-1900		if($iyear=$cgi->param("year"));
		if(($etimet=timelocal(@etimes))!=$etime)
		{
				$no_refresh=1;
				$etime=$etimet;
		}
		
# Time period for error/usage in minutes
		$period_e=60;
# Time period for performance in minutes
		$period_p=5		if(!($period_p=$cgi->param("threshrange")));
# Period over which to calculate performance alerts
		$period_q=9		if($period_p<9);
		
		$def_stime_e=$stime_e=$etime-$period_e*60;
		$stime_p=$etime-$period_p*60;
		$stime_q=$etime-$period_q*60;
		(@stime_es)=localtime($stime_e);
		
		if($starttime=$cgi->param("starttime"))
		{
				($stime_es[2],$stime_es[1])=split(/:/,$starttime);
				$stime_e=timelocal(@stime_es);
		}
		
		%sitenames=('ACH','ACH','CON','Concord','DOSC','Deposit Ops','ELM','El Monte','ORG','Orange','OCSP','OCSPortal','PCS','PCS','BOI','Boise','LUB','Lubbock','PHX','Phoenix','RNO','Reno','STPS','Shoreview','SAC','Sacto NBBC','BUT','Sacto PB','SHN','Shoreview NBBC','SHP','Shoreview PB','SIO','SiouxFalls','UTA','SLC','WLK','Westlake','UNK','(Unknown)','TOTAL','TOTAL');
		
		%dctrlist=('CON','CH','ACH','CH','DOSC','CH','ELM','CH','ORG','CH','PCS','CH','SAC','CH','BUT','CH','OCSP','CH','BOI','MN','LUB','MN','PHX','MN','RNO','MN','SHN','MN','SHP','MN','SIO','MN','UTA','MN','WLK','MN');
		%loblist=('SAC','NBBC','SHN','NBBC','CON','OCS','UTA','OCS','OCSP','OCS','DOSC','Other','PCS','Other','ACH','Other','ELM','PB','ORG','PB','BUT','PB','BOI','PB','LUB','PB','PHX','PB','RNO','PB','SHP','PB','STPS','PB','SIO','PB','WLK','PB');

		if($sortorder eq 'dctr')
		{
				foreach $node ( sort keys %dctrlist )
				{
						push(@linelist,$node) if(($dctrlist{$node} eq "CH") && ($node ne "BUT"));
				}
				foreach $node ( sort keys %dctrlist ) #Butano, a.k.a. Sacto PB, sorts last:
				{
						push(@linelist,$node) if(($dctrlist{$node} eq "CH") && ($node eq "BUT"));				
				}
				foreach $node ( sort keys %dctrlist )
				{
						push(@linelist,$node) if($dctrlist{$node} eq "MN");
				}
		}
		elsif($sortorder eq 'lob')
		{
				print STDERR "in by_lob\n";
				foreach $node ( sort keys %loblist )
				{
						push(@linelist,$node) if($loblist{$node} eq "NBBC");
				}
				foreach $node ( sort keys %loblist )
				{
						push(@linelist,$node) if($loblist{$node} eq "OCS");
				}
				foreach $node ( sort keys %loblist )
				{
						push(@linelist,$node) if($loblist{$node} eq "PB");
				}
				foreach $node ( sort keys %loblist )
				{
						push(@linelist,$node) if($loblist{$node} eq "Other");
				}
		} # end	if($sortorder eq 'lob')

		if ($stime_e >= $etime){
				# starttime is later than endtime
				print "Start time is greater than end time\n";
				exit(1);
		}
   #Name "main::etime_t" used only once: possible typo at ./rslmon6.tmp.cgi line 85.:
#		elsif ($etime_t > time()) { 
		  elsif ($etimet > time()) {
				# endtime is later than currtime
				print "End time is greater than current time\n";
		}
		
##########################################################
# if invalid times input, default to last hour range
# old: n for node; s for site
# new: d for data center; l for line o' business
##########################################################
		$cachefile= "out"; #.(($sortorder eq "lob")?"l":"d");
#		if($testmode){$cachefile="/tmp/main6_default_".(($sortorder eq "lob")?"l":"d"); }
		open(LOG,">> /tmp/cachelog") if(-f "/tmp/cachelog");
		$x=localtime;
		
		if(!$savemode && $etime==$def_etime && $stime_e==$def_stime_e)
		{
				print LOG "$x: report hit $ENV{REMOTE_HOST}\n";
				close(LOG);

				&print_header;
				open(CACHE,"$cachefile") || die("unable to open $cachefile\n");
				@lines=<CACHE>;

#    &sort_by_dctr(\@lines) if($sortorder eq 'dctr');
#    &sort_by_lob (\@lines) if($sortorder eq 'lob');
				&print_lines(\@lines);
				
				&print_footer;
				exit(0);
  } #end if(!$savemode && $etime==$def_etime && $stime_e==$def_stime_e)						
	
# dsoule 24-Feb-2006:
# We only query the db in savemode now; otherwise we sort & spew the cached data.
# Consequently, savemode must be invoked every 30 seconds 24/7, like this:
# 				grep rslsave6 /etc/inittab
# 				r6:234:respawn:/opt/ivrumgt/http/rslmon/rslsave6>> /tmp/rslsave6.out 2>&1
#
#  The nice thing is that savemode saves the averages and totals so all the raw 
# data needs is sorting and HTML tags. 

						my $dbh=DBI->connect('DBI:mysql:rsl:rsldata','rslremote2','readonly');
						die if(!$dbh || $dbh->errstr);
						
##########################################################
# load error and performance data into array
##########################################################
						@perftable = &load_perftable;
						
##########################################################
# parse through performance table now to obtain threshold
# and pct allowed data for acct and ssn lookups
##########################################################
						foreach $prowlist (@perftable) {
								($prowtype,$prowdesc,$prowinstr,$prowthresh,$prowpct) = split(/\t/,"@$prowlist");
								if ($prowtype eq "12") {
										$ssntablethresh = $prowthresh;
										$pctallowed{SSN} = $prowpct;
								}
								if ($prowtype eq "13") {
										$accttablethresh = $prowthresh;
										$pctallowed{ACCT} = $prowpct;
								}
						}
						
##########################################################
# set up 3 ranges for ssn and acct lookups
##########################################################
						$threshperf{R1R2,SSN} = 6;
						$threshperf{R2R3,SSN} = 10;
						$threshperf{R3,SSN} = 20;
						$threshperf{R1R2,ACCT} = 6;
						$threshperf{R2R3,ACCT} = 10;
						$threshperf{R3,ACCT} = 20;
						$thresherrors{R1R2,SSN}="60,40,30";
						$thresherrors{R2R3,SSN}="60,40,30";
						$thresherrors{R3,SSN}="60,40,30";
						$thresherrors{R1R2,ACCT}="50,35,25";
						$thresherrors{R2R3,ACCT}="50,35,25";
						$thresherrors{R3,ACCT}="50,35,25";
# Default error threshlevels
						$ERRORTHRESH="15,10,5";
# Minimum number of transactions in each x minute window before performance alerting occurs
						$MINPERFCNT=5;
# How many minutes in each window
						$WINSIZE=3;
						
						@alertcolor=("","yellow","orange","red");
						
						
##########################################################
# begin formatting HTML
##########################################################
						if(!$savemode)
						{		
# TITLE AND PAGE HEADER
								$no_refresh=0 if($cgi->param('savemode'));
								print "<HTML><HEAD><TITLE>RSLMon - by Data Center</TITLE>\n";
								print "<META http-equiv=\"Refresh\" content=\"180\">\n" unless($no_refresh);
								print "</HEAD>\n";
								print "<BODY BGCOLOR=white>\n<A NAME=TOP></A>\n";
								print "\n<H3><FONT COLOR=red>RSLMon by Data Center</FONT>";
								if($etimes[3] != $stime_es[3] || $etimes[4] != $stime_es[4] || $etimes[5] != $stime_es[5])
								{
										printf " from %d/%d/%d to %d/%d/%d",$stime_es[4]+1,$stime_es[3],$stime_es[5]+1900,$etimes[4]+1,$etimes[3],$etimes[5]+1900;
								}
								else
								{
										printf " for %d/%d/%d",$etimes[4]+1,$etimes[3],$etimes[5]+1900;
								}
								print "</H3><SMALL>";
						}		
##########################################################
# begin reading through RSL entries
##########################################################
						
						for($time=$stime_e;$time<=$etime;$time+=24*60*60)
						{
								my(@times)=localtime($time);
								my($date)=sprintf("%04d%02d%02d",$times[5]+1900,$times[4]+1,$times[3]);
								$orderfield="center";
								
								my($stmt)="select $orderfield, type, duration, dursec, stamp from lookup_$date where stamp >= $stime_q and stamp < $etime";
								my($in)=$dbh->prepare($stmt);
								$in->execute();
								die("Error retrieving lookups $err ($stmt)\n") if($err=$in->errstr);
								while(($key,$type,$duration,$dursec,$stamp)=$in->fetchrow_array())
								{
										$minrange=int((($etime-$stamp)/60)/$WINSIZE);
										$perfcntmin{TOT,$type,$key,$minrange}++ if($minrange<3);
										if($stamp>=$stime_p)
										{
												$perfcnt{TOT,$type,$key}++;
												$perfdur{TOT,$type,$key}+=$duration;
										}
										if($dursec>$threshperf{R3,$type})
										{
												$perfcntmin{R3,$type,$key,$minrange}++ if($minrange<3);
												$perfcnt{R3,$type,$key}++			if($stamp>=$stime_p);
										}
										elsif($dursec>$threshperf{R2R3,$type})
										{
												$perfcntmin{R2R3,$type,$key,$minrange}++		if($minrange<3);
												$perfcnt{R2R3,$type,$key}++		if($stamp>=$stime_p);
										}
										elsif($dursec>=$threshperf{R1R2,$type})
										{
												$perfcntmin{R1R2,$type,$key,$minrange}++		if($minrange<3);
												$perfcnt{R1R2,$type,$key}++		if($stamp>=$stime_p);
										}
								} #end while(($key,$type,$duration,$dursec,$stamp)=$in->fetchrow_array())
								# 'Quick' way to get usage - assumes all users do lookups and all registered CTI users get pops
								# Agents logged in
								my($stmt)="select $orderfield, agentid from lookup_$date where stamp >= $stime_e and stamp < $etime group by $orderfield, agentid";
								my($in)=$dbh->prepare($stmt);
								$in->execute();
								while(($key,$agentid)=$in->fetchrow_array())
								{
										$usercnt{$key}++;
								}
#    print STDERR "got user count for $key at ".localtime()."\n";
								
								# Calculate number of lookups
								my($stmt)="select $orderfield, type, count(*) from lookup_$date where stamp >= $stime_e and stamp < $etime group by $orderfield, type";
								my($in)=$dbh->prepare($stmt);
								$in->execute();
								while(($key,$type,$count)=$in->fetchrow_array())
								{
										$lookup{$type,$key}=$count;
								}
								if($orderfield ne "node")
								{
										# Agents registered for CTI
										my($stmt)="select $orderfield, agentid from ctipop_$date where stamp >= $stime_e and stamp < $etime group by $orderfield, agentid";
										my($in)=$dbh->prepare($stmt);
										$in->execute();
										while(($key,$agentid)=$in->fetchrow_array())
										{
												$ctireg{$key}++;
										}
										
										# Number of CTI pops
										my($stmt)="select $orderfield, count(*) from ctipop_$date where stamp >= $stime_e and stamp < $etime group by $orderfield";
										my($in)=$dbh->prepare($stmt);
										$in->execute();
										while(($key,$count)=$in->fetchrow_array())
										{
												$ctipop{$key}=$count;
										}
										
								} #end if($orderfield ne "node")
						} #end for($time=$stime_e;$time<=$etime;$time+=24*60*60)
						
						if(!$savemode)
						{ #if(!$savemode)
								print "<BR><SMALL>\n<TABLE BORDER>\n<TR BGCOLOR=#C8C8C8>\n";
								print "<TH ALIGN=CENTER ROWSPAN=3><SMALL>Call<BR> Center </TH>\n";
								print "<TH ALIGN=CENTER ROWSPAN=3><SMALL>Data <BR> Center </TH>\n";
								print "<TH COLSPAN=10><SMALL>Performance Summary for last $period_p minutes\n";
								printf "<TH COLSPAN=7><SMALL>Usage Summary from %02d:%02d to %02d:%02d\n",$stime_es[2],$stime_es[1],$etimes[2],$etimes[1];
								
								print "<TR BGCOLOR=#C8C8C8>\n";
								print "<TH COLSPAN=5><SMALL>Account Lookups (sec)\n";
								print "<TH COLSPAN=5><SMALL>SSN Lookups (sec)\n";
								print "<TH><SMALL>CIV<TH COLSPAN=3><SMALL>Lookups<TH COLSPAN=2><SMALL>CTI\n";		
								print "<TR BGCOLOR=#C8C8C8>";
								foreach $type ( "ACCT","SSN" )
								{
										print "<TH><SMALL>TOTAL";
										print "<TH><SMALL>Avg.";
										print "<TH><SMALL>".$threshperf{R1R2,$type}."-".$threshperf{R2R3,$type};
										print "<TH><SMALL>".$threshperf{R2R3,$type}."-".$threshperf{R3,$type};
										print "<TH><SMALL>Over<BR>".$threshperf{R3,$type}."\n";
								}
								print "<TH><SMALL>Users\n";
								print "<TH><SMALL>Acct<TH><SMALL>SSN<TH><SMALL>Total\n";
								print "<TH><SMALL>Reg.<TH><SMALL>Pops\n";
								
##########################################################
# Print rows of table
##########################################################
								$rownr=0;
								foreach $linecode ( @linelist, "TOTAL" )
								{
#				next if($linecode eq "ACH"); #investigate totals mismatch with server sort
										next if($linecode eq "SFO");
										next if($linecode eq "STPS"); #change this if needed to handle antique data
										print "\n";
										print "<TR>" if($sortorder eq "lob" && $prevc && $dctrlist{$linecode} ne $prevc);
										print "<TR";
										print " BGCOLOR=#C8C8C8"		if(($rownr++)%2);
										print "><TD ALIGN=CENTER VALIGN=TOP>";
										&print_site_link($linecode);
										$prevc=$dctrlist{$linecode};
										print "</TH>";
										
										### Account and SSN lookups ###
										foreach $type ( "ACCT", "SSN" )
										{
												print "<TD ALIGN=CENTER><SMALL>$perfcnt{TOT,$type,$linecode}";
												print "<TD ALIGN=CENTER><SMALL>";
												if($perfcnt{TOT,$type,$linecode})
												{
														printf "%0.2f", $perfdur{TOT,$type,$linecode}/($perfcnt{TOT,$type,$linecode}*1000);
												}
												else
												{
														print "&nbsp;";
												}
												$perfdur{TOT,$type,"TOTAL"}+=$perfdur{TOT,$type,$linecode};
												$perfcnt{TOT,$type,"TOTAL"}+=$perfcnt{TOT,$type,$linecode};
												
												foreach $thresh ( "R1R2","R2R3","R3")
												{
														my($alert);
														print "<TD ALIGN=CENTER";
														print " BGCOLOR=$alertcolor[$alert]"	if($alert=&perfalert($thresh,$type,$linecode));
														print "><SMALL>";
														print $perfcnt{$thresh,$type,$linecode}?$perfcnt{$thresh,$type,$linecode}:"&nbsp;";
														print "</TD>";
														$perfcnt{$thresh,$type,"TOTAL"}+=$perfcnt{$thresh,$type,$linecode};
												}
										}
										
										# Usage
										print "<TD ALIGN=CENTER><SMALL>";
										print $usercnt{$linecode}+0;
										print "</TD>";
										
										print "<TD ALIGN=CENTER><SMALL>&nbsp\;$lookup{ACCT,$linecode}</TD>";
										print "<TD ALIGN=CENTER><SMALL>&nbsp\;$lookup{SSN,$linecode}</TD>";
										print "<TD ALIGN=CENTER><SMALL>&nbsp\;";
										print $lookup{"ACCT",$linecode}+$lookup{"SSN",$linecode};
										print "</TD>";
										print "<TD ALIGN=CENTER><SMALL>&nbsp\;$ctireg{$linecode}</TD>";
										print "<TD ALIGN=CENTER><SMALL>&nbsp\;$ctipop{$linecode}</TD>";
										$usercnt{"TOTAL"}+=$usercnt{$linecode};
										$lookup{ACCT,"TOTAL"}+=$lookup{ACCT,$linecode};
										$lookup{SSN,"TOTAL"}+=$lookup{SSN,$linecode};
										
										$ctireg{"TOTAL"}+=$ctireg{$linecode};
										$ctipop{"TOTAL"}+=$ctipop{$linecode};
								} #end foreach $linecode ( @linelist, "TOTAL" )
								
								print "</TABLE>\n\n";
								
								print "<TABLE BGCOLOR=WHITE><TR><TH ALIGN=CENTER><SMALL>\n";
# LINK TO REQUEST PAGE
								print "<A HREF=rslmon6_req.cgi>New Request</A>";
# LINK TO THRESHOLD TABLE
								print "<TH ALIGN=CENTER><SMALL><A HREF=./rslmon_table.cgi#PERFTABLE TARGET=\"table_frame\">";
								print "User Action/Threshold Table";
								print "</A>";
# LINK TO CONTACT TABLE
								print "<TH ALIGN=CENTER><SMALL><A HREF=./rslmon_table.cgi#CONTABLE TARGET=\"table_frame\">";
								print "Contact Numbers";
								print "</A>";
								print "<TH ALIGN=CENTER><SMALL><A HREF=\"http://civworld.telebank.wellsfargo.com/rslscript/RSLAlgorithm.htm\">Alarming Algorithm</A></TH>";
								print "<TH ALIGN=CENTER><SMALL><A HREF=\"rslbylob6.cgi";
								print "\">Sort by ";
								print $sortorder eq "dctr"?"Line of Business":"Data Center";
								print "</A>";
								
								print "</TH></TR></TABLE>\n";
								print "<!-- testmode -->\n" if($testmode);
								print "<!-- $0 ".localtime()." -->\n";
								print "</BODY></HTML>\n";
						} #end if(!$savemode)
						
				RAW:
						if($savemode) #write the raw data to the cache file
						{
								open(CACHE,"> $cachefile.tmp") || die "unable to open $cachefile.tmp\n";
#print STDERR "open(CACHE,\">$cachefile.tmp\")\n";
								foreach $linecode ( @linelist, "TOTAL" )
								{
										print CACHE "$linecode|";
										print CACHE "$dctrlist{$linecode}|"; #datacenter column
										print CACHE "$loblist{$linecode}|";  #lob column
										
										foreach $type ( "ACCT", "SSN" )
										{
												print CACHE $perfcnt{TOT,$type,$linecode}."|";
												
												if($perfcnt{TOT,$type,$linecode})
												{
														printf CACHE "%0.2f|", $perfdur{TOT,$type,$linecode}/
																		($perfcnt{TOT,$type,$linecode}*1000);
												} else { print CACHE "|"; }
												
												$perfdur{TOT,$type,"TOTAL"}+=$perfdur{TOT,$type,$linecode};
												$perfcnt{TOT,$type,"TOTAL"}+=$perfcnt{TOT,$type,$linecode};
												
												foreach $thresh ( "R1R2","R2R3","R3")
												{
														print CACHE $perfcnt{$thresh,$type,$linecode}?$perfcnt{$thresh,$type,$linecode}:"";
														print CACHE "|";
														$perfcnt{$thresh,$type,"TOTAL"}+=$perfcnt{$thresh,$type,$linecode};
												}
										} #end foreach $type ( "ACCT", "SSN" )
										
										# Usage
										print CACHE $usercnt{$linecode}+0 ."|";
										print CACHE $lookup{ACCT,$linecode}."|";
										print CACHE $lookup{SSN,$linecode}."|";
										print CACHE $lookup{"ACCT",$linecode}+$lookup{"SSN",$linecode}."|";
										
										print CACHE $ctireg{$linecode}."|";
										print CACHE $ctipop{$linecode}."|\n";
										
										$usercnt{"TOTAL"}+=$usercnt{$linecode};
										$lookup{ACCT,"TOTAL"}+=$lookup{ACCT,$linecode};
										$lookup{SSN,"TOTAL"}+=$lookup{SSN,$linecode};
										
										$ctireg{"TOTAL"}+=$ctireg{$linecode};
										$ctipop{"TOTAL"}+=$ctipop{$linecode};
								} #end foreach $linecode ( @linelist, "TOTAL" )
								
								close(CACHE);
								rename "$cachefile.tmp","$cachefile";
								
						} #end if($savemode)
						
						sub print_site_link
						{
								my ($site)=@_;
								print "<SMALL>";
								if ($site ne "TOTAL") {
#why not reuse this script for single center?:
#				print "<A HREF=\"rslmon6.cgi?";
#					print "<A HREF=\"rslmon_site.cgi?";
#				print "starttime$starttime";
#				print "&endtime=$endtime";
#				print "&month=$imonth";
#				print "&day$iday";
#				print "&year=$iyear";
#				print "&threshrange=$threshrange";
#				print "&selectsite=$site";
#				print "\">";
										print "$sitenames{$site}";
										print "<TD ALIGN=CENTER><SMALL>$dctrlist{$site}"; #datacenter column
#				print "</A>";
								}
								else { #if $site eq TOTAL
										print "$site";
										print "<TD ALIGN=CENTER><SMALL>"; #datacenter column
								}
						}

# Calculate if performance alert reached
# 0 - No, 1 - Yellow, 2 - Orange, 3 - Red
sub perfalert
{
		my($thresh,$type,$linecode)=@_;
		my(@p);
		for($m=0;$m<=2;$m++)
		{
				if($perfcntmin{TOT,$type,$linecode,$m}>$MINPERFCNT)
				{
						push(@p,int($perfcntmin{$thresh,$type,$linecode,$m}*100/$perfcntmin{TOT,$type,$linecode,$m}));
				}
				else
				{
						push(@p,0);
				}
		}
		$stmp="$linecode $type $thresh $perfcntrange{$type,$linecode}";
		for($m=0;$m<3;$m++)
		{
				$stmp.="\t$m: $perfcntmin{TOT,$type,$linecode,$m}|$perfcntmin{$thresh,$type,$linecode,$m}|$p[$m]";
		}
		#&alertlog($stmp);
		my($l1,$l2,$l3)=split(/,/,$thresherrors{$thresh,$type});
		if($p[0]>=$l1 || ($p[0]>=$l2 && $p[1]>=$l2) || ($p[0]>=$l3		&& $p[1]>=$l3 && $p[2]>=$l3))
		{
				&alertlog("PERF 3: $stmp");
				return(3);
		}
		if($p[0]>=$l2 || ($p[0]>=$l3 && $p[1]>=$l3))
		{
				&alertlog("PERF 2: $stmp");
				return(2);
		}
		if($p[0]>=$l3)
		{
				&alertlog("PERF 1: $stmp");
				return(1);
		}
		return(0);
}

# Build array with alert status for each error, errortype
sub gen_erroralerts
{
		foreach $key ( keys %errkeylist )
		{
				my($linecode,$errortype,$errorcode)=split($;,$key);
				my(@p);
				for($m=0;$m<=2;$m++)
				{
						push(@p,$errcnt{$linecode,$errortype,$errorcode,$m});
				}
				$stmp="$linecode $errortype $errorcode";
				for($m=0;$m<3;$m++)
				{
						$stmp.="\t$m: $p[$m]";
				}
				#&alertlog($stmp);
				my($l1,$l2,$l3)=split(/,/,&get_errorthresh($errortype,$errorcode));
				if($p[0]>=$l1 || ($p[0]>=$l2 && $p[1]>=$l2) || ($p[0]>=$l3		&& $p[1]>=$l3 && $p[2]>=$l3))
				{
						&alertlog("ERROR 3: $stmp");
						$erroralert{$linecode,$errortype,$errorcode}=3;
				}
				elsif($p[0]>=$l2 || ($p[0]>=$l3 && $p[1]>=$l3))
				{
						&alertlog("ERROR 2: $stmp");
						$erroralert{$linecode,$errortype,$errorcode}=2;
				}
				elsif($p[0]>=$l3)
				{
						&alertlog("ERROR 1: $stmp");
						$erroralert{$linecode,$errortype,$errorcode}=1;
				}
				$erroralert{$linecode,$errortype}=$erroralert{$linecode,$errortype,$errorcode}
				if($erroralert{$linecode,$errortype}<$erroralert{$linecode,$errortype,$errorcode});
		}
}

# Get error thresholds
sub get_errorthresh
{
		my($errortype,$errorcode)=@_;
		return($errorthresh{$errortype,$errorcode})		if($errorthresh{$errortype,$errorcode});
		return($errorthresh{$errortype})		if($errorthresh{$errortype});
		return($ERRORTHRESH);
}
sub alertlog
{
		my($msg)=@_;
		if(!$o)
		{
				open(LOG,">> /tmp/alertlog");
				$o=1;
		}
		my($x);
		$x=localtime($etime);
		print LOG "$x $msg\n";
}

sub print_lines
{
# print "print_lines got: @lines\n";
# print STDERR "print_one_line got: \$line: $line\n";

# For each sort key, find a matching row and print it.
# For each row, see if it contains a match for sort key, and if it does, print it.  
@table=sort_by_dctr(@lines) if($sortorder eq 'dctr');
@table=sort_by_lob (@lines) if($sortorder eq 'lob');

				foreach $row (@table){ 
# print STDERR "print_lines got \$row: $row\n";
						my($center, $dc, $lob, @vals)=split(/\|/, $row);
# print STDERR "got \$center: $center \$dc: $dc \$lob: $lob from \$row: $row, \@vals: @vals\n";
						# next if($center eq 'ACH');
						if(!($linenum++ %2)) {	print "<TR BGCOLOR=#C8C8C8>\n" } else { print "\n<TR>" };
						print "<TD ALIGN=CENTER VALIGN=TOP>".$sitenames{$center}."\t</TD>\n";
						print "<TD ALIGN=CENTER><SMALL>".    $dctrlist{$center}."\t</TD>\n" if($sortorder eq 'dctr');
						print "<TD ALIGN=CENTER><SMALL>".    $loblist{$center}."\t</TD>\n"   if($sortorder eq 'lob');
						
						$valnum=0; #column counter
						foreach $val(@vals) {
								$valnum++;
#print STDERR "\$val $valnum: $val\n";
								next if($valnum < 1 || $valnum > $MAXCOLS);              #skip center abbrev. 
								print "<TD ALIGN=CENTER><SMALL>";
								print $val eq ""?"&nbsp;":"$val";
								print "</TD>";
								print " \t  <!-- [$linenum,$valnum,$center] -->";
								print "\n";
								$prevc=$dctrlist{$center} if($sortorder eq 'dctr');
								$prevc= $loblist{$center} if($sortorder eq 'lob'); #really lob here, not data center
						} #end foreach $val(@vals)
			} # end foreach $row (@table)
 } #end print_lines
		
sub print_footer
{
  print "</TABLE>\n\n";
  print "<TABLE BGCOLOR=WHITE><TR><TH ALIGN=CENTER><SMALL>";
# LINK TO REQUEST PAGE
#  print "<A HREF=rslmon7_req.cgi>New Request</A>";
# LINK TO THRESHOLD TABLE
  print "<TH ALIGN=CENTER><SMALL><A HREF=./rslmon_table.cgi#PERFTABLE TARGET=\"table_frame\">";
  print "User Action/Threshold Table";
  print "</A>";
# LINK TO CONTACT TABLE
  print "<TH ALIGN=CENTER><SMALL><A HREF=./rslmon_table.cgi#CONTABLE TARGET=\"table_frame\">";
  print "Contact Numbers";
  print "</A>";
  print "<TH ALIGN=CENTER><SMALL><A HREF=\"http://civworld.telebank.wellsfargo.com/rslscript/RSLAlgorithm.htm\">Alarming Algorithm</A></TH>";
  print "<TH ALIGN=CENTER><SMALL><A HREF=\"rslmon6.tmp.cgi?sortorder=";
  print $sortorder eq "dctr"?"lob":"dctr";
  print "\">Sort by ";
  print $sortorder eq "dctr"?"Line of Business":"Data Center";
  print "</A>";

  print "<TH ALIGN=CENTER><SMALL><A HREF=out>data</A>";

  print "</TH></TR></TABLE>\n";
  print "<!-- testmode -->\n" if($testmode);
  print "<!-- ".`basename $0`.localtime()." -->\n";
  print "</BODY></HTML>\n";
} #end print_footer


sub print_header
{
  print "<HTML><HEAD><TITLE>RSLMon - by Data Center</TITLE>\n" if($sortorder eq 'dctr');
  print "<HTML><HEAD><TITLE>RSLMon - by Line of Business</TITLE>" if($sortorder eq 'lob');
  print "<META http-equiv=\"Refresh\" content=\"180\">" unless($no_refresh);
  print "</HEAD>\n";
  print "<BODY BGCOLOR=white>\n<A NAME=TOP></A>\n";
  print "<H3><FONT COLOR=red>RSLMon by Data Center</FONT>\n" if($sortorder eq 'dctr');
  print "<H3><FONT COLOR=red>RSLMon by Line of Business</FONT>\n" if($sortorder eq 'lob');
  
  
  if($etimes[3] != $stime_es[3] || $etimes[4] != $stime_es[4] || $etimes[5] != $stime_es[5])
  {
    printf " from %d/%d/%d to %d/%d/%d",$stime_es[4]+1,$stime_es[3],$stime_es[5]+1900,$etimes[4]+1,$etimes[3],$etimes[5]+1900;
  }
  else
  {
    printf " for %d/%d/%d",$etimes[4]+1,$etimes[3],$etimes[5]+1900;
  }
  print "</H3><SMALL><BR><SMALL>";
  
  print "<TABLE BORDER>\n";
  
  print "<TH BGCOLOR=#C8C8C8 ALIGN=CENTER ROWSPAN=3><SMALL>Call <BR> Center </TH>\n";
  print "<TH BGCOLOR=#C8C8C8 ALIGN=CENTER ROWSPAN=3><SMALL>Data <BR> Center </TH>\n" if($sortorder eq "dctr");
  print "<TH BGCOLOR=#C8C8C8 ALIGN=CENTER ROWSPAN=3><SMALL>Line of <BR> Business </TH>\n" if($sortorder eq "lob");
  
  print "<TH BGCOLOR=#C8C8C8 COLSPAN=10><SMALL>Performance Summary for last $period_p minutes\n";
  printf "<TH BGCOLOR=#C8C8C8 COLSPAN=6><SMALL>Usage Summary from %02d:%02d to %02d:%02d\n",$stime_es[2],$stime_es[1],$etimes[2],$etimes[1];
  print "<TR BGCOLOR=#C8C8C8>\n";
  print "<TH COLSPAN=5><SMALL>Account Lookups (sec)\n";
  print "<TH COLSPAN=5><SMALL>SSN Lookups (sec)\n";
  print "<TH><SMALL>CIV<TH COLSPAN=3><SMALL>Lookups<TH COLSPAN=2><SMALL>CTI\n";
  print "<TR BGCOLOR=#C8C8C8>";
  foreach $type ( "ACCT","SSN" )
  {
    print "<TH><SMALL>TOTAL";
    print "<TH><SMALL>Avg.";
    print "<TH><SMALL>".$threshperf{R1R2,$type}."-".$threshperf{R2R3,$type};
    print "<TH><SMALL>".$threshperf{R2R3,$type}."-".$threshperf{R3,$type};
    print "<TH><SMALL>Over<BR>".$threshperf{R3,$type}."\n";
  }
  print "<TH><SMALL>Users\n";
  print "<TH><SMALL>Acct<TH><SMALL>SSN<TH><SMALL>Total\n";
  print "<TH><SMALL>Reg.<TH><SMALL>Pops\n";
} #end print_header

sub sort_by_dctr
{
 %dctrlist=('CON','CH','ACH','CH','DOSC','CH','ELM','CH','ORG','CH','PCS','CH','SAC','CH','BUT','CH','OCSP','CH','BOI','MN','LUB','MN','PHX','MN','RNO','MN','SHN','MN','SHP','MN','SIO','MN','UTA','MN','WLK','MN');
ROW:
	foreach $row (@lines)
	{
		($ctr,$dc,$lob,@vals)=split(/\|/, $row, 4);
		foreach $key (%dctrlist)
		{
			if(($dctrlist{$ctr} eq 'CH')&&($ctr ne 'BUT')){ push(@table, $row); next ROW;}
		}
		foreach $key (%dctrlist)
		{
			if(($dctrlist{$ctr} eq 'CH')&&($ctr eq 'BUT')){ push(@table, $row); next ROW; }
		}
		foreach $key (%dctrlist)
		{
			if( $dctrlist{$ctr} eq 'MN'){ push(@table, $row); next ROW; }
		}
		foreach $key (%dctrlist)
		{
			if( $ctr eq 'TOTAL'){ push(@table, $row); next ROW; }
		}
	}
 return @table;
}

sub sort_by_lob
{

 %loblist= ('SAC','NBBC','SHN','NBBC','CON','OCS','UTA','OCS','OCSP','OCS','DOSC','Other','PCS','Other','ACH','Other','ELM','PB','ORG','PB','BUT','PB','BOI','PB','LUB','PB','PHX','PB','RNO','PB','SHP','PB','STPS','PB','SIO','PB','WLK','PB');

	ROW:
	foreach $row (@lines)
	{
			($ctr,$dc,$lob,@vals)=split(/\|/, $row, 4);
			foreach $key (%loblist)
			{
					if($loblist{$ctr} eq 'NBBC') { push(@table, $row); next ROW; }
			}
			foreach $key (%loblist)
			{
					if($loblist{$ctr} eq 'OCS')  { push(@table, $row); next ROW; }
			}
			foreach $key (%loblist)
			{
					if($loblist{$ctr} eq 'PB' )  { push(@table, $row); next ROW; }
			}
			foreach $key (%loblist)
			{
					if($loblist{$ctr} eq 'Other'){ push(@table, $row); next ROW; }
			}
			foreach $key (%loblist)
			{
					if( $ctr eq 'TOTAL'){ push(@table, $row); next ROW; }
			}
	}
 return @table;
}

};

print "<FONT COLOR=red SIZE=+2>Error \"$@\" running script\n"		if(($@) && (!$savemode));
__DATA__

print "\n";

# Local Variables:
# tab-width:1
# perl-indent-level:2
# End:
