#!/usr/bin/perl
my $to='aglt2-hardware@umich.edu';
my $gatekeeper="gate01.aglt2org";
my $from='DNChecker@'.gatekeeper;
my $subject='New DN found accessing '.gatekeeper;
my $dst = "/var/www/html/DNChecker";

open(IN,"<$dst/DNs.dat");
while (<IN>) {#This loop reads all the old DNs
    if ($_ =~ m/;/) { 
    ($DN,$cnt,$first,$last)=split(/;/,$_);
    $DNs{$DN}=0;
#Timestamps
    $first_ts{$DN}=$first;
    $last_ts{$DN}=$last;
    $found{$DN}=1;
    }
}
close(IN);
$cnt=0;

@lines=`grep " gridinfo" /var/log/messages | grep " for "`;

foreach $line (@lines) {
    if ($line =~ /^(\S+)\s(\S+) gridinfo\[(\d+)\]: JMA (\S+)\s(\S+) GATEKEEPER_JM_ID(\S+) for (.+) on ([\d\.]+)/) {
       $timestamp=$1;
       $server=$2;
       $pid=$3;
       $ymd=$4;
       $hms=$5;
       $jma=$6;
       $dn=$7;	
       if ($dn eq "") {last}
       $ip=$8;
       $IPs{$ip}++;
# Save Info
       if (!exists($first_ts{$dn})) {
           $first_ts{$dn}=$timestamp;
       }
       $last_ts{$dn}=$timestamp;
       $DNs{$dn}++;
       $cnt++;
  }
}

$debug && print " Processed $cnt DN lines and found:\n";
foreach $key (keys %DNs) {
    $debug && print "  Found DN=$key $DNs{$key} times from $first_ts{$key} to $last_ts{$key}\n";
    if (! exists($found{$key})) {#if found is 0, will go into this if
	$debug && print " New DN found: $key\n";
# email setup
 
	my $out = sprintf(localtime(time())." -- New DN \"$key\" found accessing $gatekeeper at $last_ts{$key}\n");
#	print " out=|$out|\n";
# send email using UNIX/Linux sendmail
        open(MAIL, "|/usr/sbin/sendmail -t");
 
## Mail Header
	print MAIL "To: $to\n";
	print MAIL "From: $from\n";
	print MAIL "Subject: $subject\n";
## Mail Body
	print MAIL $out;
        print MAIL " Refer to http://$gatekeeper/DNChecker/default.html for details\n";
        close(MAIL);
    }
}


# Update the HTML file
$html_file="$dst/default.html";
open(OUT,">$html_file") or die "Unable to open $html_file: $!";
print OUT "<html>\n<head>\n<title>DNs Accessing $gatekeeper </title>\n";
print OUT '<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">';
print OUT '<script src="sorttable.js"></script>'."\n";
print OUT "</head>\n\n<body>\n";
print OUT '<p align="center"><font size="+3">'." DNs Accessing $gatekeeper </font></p>";
$tdate = `date`;
print OUT '<p align="center"><font size="+1">'."Page generated at $tdate </font></p>";
print OUT '<p align="center">'."\n";
print OUT '<table class="sortable" width="1000" height="117" border="1">';
$width="0";
print OUT '  <tr bgcolor="#0099FF">';
print OUT "    <th width=\"820\">";
print OUT '<div align="center"><font size="3" face="Times New Roman, Times, serif"><strong>';
print OUT "DN</strong></font></div></th>\n";
print OUT "    <th width=\"40\">";
print OUT '<div align="center"><font size="3" face="Times New Roman, Times, serif"><strong>';
print OUT "Cnt</strong></font></div></th>\n";
print OUT "    <th width=\"230\">";
print OUT '<div align="center"><font size="3" face="Times New Roman, Times, serif"><strong>';
print OUT "FirstSeen</strong></font></div></th>\n";
print OUT "    <th width=\"230\">";
print OUT '<div align="center"><font size="3" face="Times New Roman, Times, serif"><strong>';
print OUT "LastSeen</strong></font></div></th>\n";
print OUT " </tr> \n ";

foreach $key (keys %DNs) {
   # print OUT "$key;$DNs{$key};$first_ts{$key};$last_ts{$key}\n";
            print OUT "<tr>\n";
	    print OUT " <td bgcolor=\"\#FFFFFF\">$key</td>\n";
	    print OUT " <td bgcolor=\"\#FFFFFF\">$DNs{$key}</td>\n";
	    $d=$first_ts{$key};
	$d =~ s/T/ /;#removes a T from timestamp
# Needed for older coreutils (RHEL5 versions work not RHEL4 )
	$d =~ s/-04:00/ EDT/;#if it -04:00, replace with EDT
	$d =~ s/-05:00/ EST/;#ditto with EST
	$fts = `date -d "$d" +"%b %d %Y %T %Z"`;#converts into better form
	$fts_secs = `date -d "$d" +%s`;
	$secs = `date +%s`;

	if( ($secs-$fts_secs) < 10800)#red if seen in the last 3 hours
	{
		print OUT " <td bgcolor = \"\#FF0000\">$fts</td> \n";
	}
	else#green
	{
		print OUT " <td bgcolor = \"\#00FF00\">$fts</td> \n";
	} 
	$d=$last_ts{$key};#switch to last time stamp
	$d =~ s/T/ /;
# Needed for older coreutils (RHEL5 versions work not RHEL4 )
	$d =~ s/-04:00/ EDT/;
	$d =~ s/-05:00/ EST/;
	$lts = `date -d "$d" +"%b %d %Y %T %Z"`;
	$lts_secs = `date -d "$d" +%s`;
	$secs = `date +%s`;
        print TSTOUTPUT ($secs - $lts_secs);
	if( ($secs-$lts_secs) > 86400) #24  hours
	{
		print OUT " <td bgcolor = \"\#FF0000\">$lts</td> \n";
	}
	else
	{
		print OUT " <td bgcolor = \"\#00FF00\">$lts</td> \n";
	}     
# }
}
print OUT "  </tr></table></p>\n";
print OUT "<p>The 'Cnt' field shows the number of entries found in the current logfile (/var/log/messages) while the First/LastSeen fields are preserved across runs.</p>";  
print OUT "</body>\n</html>\n";
close OUT;

# Now update DNs.dat file
open(OUT,">$dst/DNs.dat");
foreach $key (keys %DNs) {
    # print " Key=|$key|\n";
    if ($key ne "" ) {
     print OUT "$key;$DNs{$key};$first_ts{$key};$last_ts{$key}\n";
    }
}
close(OUT);
$debug && print " Finished!\n";
exit;
