#!perl
# Many Thanks for
# home777@mail.ru  Ovseenko Andrey and
#
########################################################################################
# Was originally prspoa.pl 1.0.0 25 OCT 2002 by
# Koltogyan Sergey Rubenovich
# skoltogyan@mail.ru
# NPP AMI, Donetsk, ukraine
########################################################################################
# Rewritten-expanded by Rob Anderson (rob.anderson@americansouthwest.com) 24 MAR 2005
# Will run on NW5.0sp6A Perl + ActiveState PERL 5.8.4 for Windows at least.
########################################################################################
# Date     Version    Description
########################################################################################
# 03/24/05 1.00.01    Add FID to output & user/resource count
# 03/30/05 1.00.02    Add Processed Date/Version, cleaned up FID and user name handling
# 04/20/05 1.00.03    Add handling of GW6.5.4 at least
# 05/19/05 1.01.00    Bug - not handling over threshold/limits users properly
#                     Add red background for users over the size limit
#                     Add orange background for users over the threshold size
#                     Correct bug with processing time for job for 6.5
# 06/16/05 1.01.01    Bug fixes - Long mailbox ID's/Fid & MailboxID's with Spaces & add
#                       version to footer
########################################################################################



########################################################################################
########################################################################################
########################################################################################
########################################################################################
########################################################################################
########################################################################################
#
#Installation:
#
#Put the program somewhere accessable to your perl system.
#
#ex for Netware I've installed it in the sys:\perl\scripts directory
#  invoke from the NW5 command line:
#      load perl --noscreen sys:\perl\scripts\gwstats.pl
#      unload perl
#
########################################################################################
########################################################################################
########################################################################################
########################################################################################
########################################################################################
########################################################################################


sub doreport; #predeclaring the subroutine

### Below is code to get the current date and time ###
   ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
@nummonths = ("01","02","03","04","05","06","07","08","09","10","11","12");
@namemonths = ("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
@numyears = ("2002","2003","2004","2005");
if ($hour < 10) { $hour = "0$hour"; }
if ($mday < 10) { $mday = "0$mday"; }
if ($min < 10) { $min = "0$min"; }
$year=$year+1900;
$year_search = $year;
$fmtdate = "@nummonths[$mon]/$mday/$year";
$sdate = "$year@nummonths[$mon]$mday";
$stime = "$hour\:$min";
########################################################################################


########################################################################################
#Change the below to your Company name if desired
########################################################################################
$coname = "Your Company Name Here";
########################################################################################


########################################################################################
# Enter your GW Domain and GW Po Information Below
########################################################################################

$gwdomain = "domain"; #enter the domain name for path purposes, and appearing on the report
$gwpo     = "po";     #enter the PO name for path purposes, and to appear on the report

########################################################################################
#Input and Ouptut Filenames here
#########################################################################################
## Note, unless you're using linux, you're going to want to use \ in your paths.  Don't!#
## PERL doesn't like that very much.  use /, eg f:/$gwpo/wpcsout/ofs/contents.log       #
#########################################################################################

my $TOSAVE     = "sys:/novonyx/suitespot/docs/gwstats/$sdate-$gwdomain.html"; #puts into my novonyx webserver's directory
my $FROMREAD   = "mail:/$gwpo/wpcsout/ofs/contents.log";                      #name of contents.log is set in during the GWCHECK run
#my $INCLPUDB   = -1;



doreport;      #actually executes the subroutine

exit (0);      #leave the program


########################################################################################
#No Changes below here unless you REALLY know what you're doing!!!
########################################################################################
sub doreport {

  @arr = ();

  open (FL, "<$FROMREAD") || die("Source File Error $!");
  open (FL1, ">$TOSAVE")  || die("Target File Error $!");

  print FL1 "<HTML>\n";
  print FL1 "<html><head><title>Groupwise Stats</title></head>\n";


  print FL1 "<H1>$coname</H1>\n";
  print FL1 "<H2>Groupwise System Statistics for $fmtdate</H2>\n";
  print FL1 "<H2>For: ($gwdomain.$gwpo)<H2>\n";


  $progver='GWSTATS Version 01.01.01';
  $BASESTR=' ';
  $user = '';
  $size = '';
  $inbox = '';
  $outbox = '';
  $trash = '';
  $pousers = '';
  $fid = '';
  $processed = '';
  $proctime = '';
  $ver = '';

  while (<FL>) {
        if ($_ =~ /PROCESSING COMPLETED- total processing time:\s+(.{1,})/) {
            $proctime = $1;
        }
        if ($_ =~ /Accumulated task processing times spent on the job:\s+(.{1,})/) { #6.5  job time
            $proctime = $1;
        }
        if ($_ =~ /Processed on\s+(.{1,})/) {
            $processed = $1;
        }

        if ($_ =~ /Version\s+(.{1,})/) {
            $version = $1;
        }
        if ($_ =~ /Version\s+(\d+)/) {
            $ver = $1;
        }

        if ($_ =~ /Number of Users...............\s+(\w+)/) {
            $pousers = $1;
        }
        if ($_ =~ /Checking user =\s(.{1,14})\s*\(/) {
            $user = $1;
        }
        if ($_ =~ /Checking user =\s$user\s*\((\w+)\)/) {
            $fid = $1
        }
        if ($_ =~ /Checking user =\s$user\s*\($fid\)\s+\d+\s+\w+\S\s+\S+\s\S+\s+\((.{1,}\s+)\[/) {
            $fullname = $1;
        }
 	if ($_ =~ /(\d+) (\w+) in use by user's mail/) {
            $size = $1;
            $uom = $2;
 	}
        if ($_ =~ /User stats:  (\w+) InBox, (\w+) OutBox, (\w+) /) {
            $inbox = $1;
            $outbox = $2;
            $trash = $3;
        }

        if ($_ =~ /Disk space management values:\s+Size Limit - (\d+)(\w+),\s+Threshold -\s+(\S+)/) {
            $sizelim = $1;
            $uomthreshold = $2;
            $pctofthreshold = $3;
        }

        $exceedthreshold = -1;

        if ($_ =~ /Problem 90- Threshold limit exceeded\. Current size:\s+(\d+)(\w+)/) {
            $exceedthreshold = 1;
            $size = $1;
            $uom = $2;
        }
        $exceedlimit = -1;

        if ($_ =~ /Problem 89- Size Limit Exceeded\.\s+Current size:\s+(\d+)(\w+)/) {
            $exceedlimit = 1;
            $size = $1;
            $uom = $2;
        }

#        if ($user && $size && $fid) { ##This means there must be both a user name and the size > zeros and a FID
        if ($user && $size) { ##This means there must be both a user name and the size > zeros
          my %hash;
          $hash{'user'} = $user;
          $hash{'size'} = $size;
          $hash{'inbox'} = $inbox;
          $hash{'outbox'} = $outbox;
          $hash{'trash'} = $trash;
          $hash{'fid'} = $fid;
          $hash{'fullname'} = $fullname;
          $hash{'sizelim'} = $sizelim;
          $hash{'pctofthreshold'} = $pctofthreshold;
          $hash{'exceedthreshold'} = $exceedthreshold;
          $hash{'exceedlimit'} = $exceedlimit;
          push @arr, \%hash;

          $user = '';
          $size = '';
          $inbox = '';
          $outbox = '';
          $trash = '';
          $fid = '';
          $fullname = '';
          $sizelim = '';
          $pctofthreshold = '';
          $exceedthreshold = -1;
          $exceedlimit = -1;
        }
  };

  @arr = sort {$b->{'size'} <=> $a->{'size'}} @arr;

  print FL1 "<pre>";
  print FL1 "<hr size=1 width=100% align=left>";
  print FL1 "<table border>";
  print FL1 "<H4>Summary Information</H4>\n";
  print FL1 "<tr><td align=left><font size=2>GroupWise Version:</td><td align=right>$version <br></td>";
  print FL1 "<tr><td align=left><font size=2>Users/Resources in Post Office $gwpo:</td><td align=right>$pousers<br></td>";
  print FL1 "<tr><td align=left><font size=2>Date GWCHECK Processed: </td><td align=right>$processed<br></td>";
  print FL1 "<tr><td align=left><font size=2>GWCHECK Processing Time: </td><td align=right>$proctime<br></td>";
  print FL1 "</table>\n";


  print FL1 "<pre>";
  print FL1 "<hr size=1 width=100% align=left>";
  print FL1 "<table border><TD>";
  print FL1 "<tr><B><th>Mailbox ID</th>";
  if ($ver >= 6) {
    print FL1 "<th>User Name</th>";
  }
  print FL1 "<th>Mailbox Size (in $uom)</th><th>Inbox Items</th><th>Outbox Items</th><th>Trash Items</th><th>FID</th>";
  if ($ver >= 6) {
    print FL1 "<th>Size Limit (in $uomthreshold)</th><th>% of Threshold</th>";
  }

  print FL1 "</tr>\n";



  foreach (@arr) {
    print FL1 '<tr bgcolor=#FFFFFF>'; #white
    if ($_->{'exceedthreshold'} > 0) {
      print FL1 '<tr bgcolor=#FF8040>'; #orange
    }
    if ($_->{'exceedlimit'} > 0) {
      print FL1 '<tr bgcolor=#FF0000>'; #red
    }
    print FL1 '<td>';
    print FL1 $_->{'user'};
    if ($ver >= 6) {
      print FL1 '<br></td><td align=right>';
      print FL1 $_->{'fullname'};
    }
    print FL1 '<br></td><td align=right>';
    print FL1 $_->{'size'};
    print FL1 '<br></td><td align=right>';
    print FL1 $_->{'inbox'};
    print FL1 '<br></td><td align=right>';
    print FL1 $_->{'outbox'};
    print FL1 '<br></td><td align=right>';
    print FL1 $_->{'trash'};
    print FL1 '<br></td><td align=right>';
    print FL1 $_->{'fid'};
    if ($ver >= 6) {
      print FL1 '<br></td><td align=right>';
      print FL1 $_->{'sizelim'};
      print FL1 '<br></td><td align=right>';
      print FL1 $_->{'pctofthreshold'};
    }
    print FL1 "<br></td></tr>\n";
    }

  print FL1 "</table>\n";


  if ($ver >= 6) {
    print FL1 "<pre>";
    print FL1 "<hr size=1 width=100% align=left>";
    print FL1 "<table border>";
    print FL1 "<H2>Color Legend</H2>\n";
    print FL1 "<th>Color</th><th>Description</th>\n";
    print FL1 "<tr bgcolor=#FF0000><td></td><td>Users Over Limit <br></td>";
    print FL1 "<tr bgcolor=#FF8040><td></td><td>Users Over Threshold <br></td>";
    print FL1 "<tr bgcolor=#FFFFFF><td></td><td>Users Under Quota <br></td>";
    print FL1 "</table>\n";
  }

  print FL1 "<table width=100%><tr><td><hr noshade></td></tr><p><tr><td><center><font size=1><br>This page created at $fmtdate $stime<br>$progver<br>";
  print FL1 "\&copy; $year $coname<br></font></center></td></tr></table>";

  print FL1 "</BODY></HTML>";
  close(FL);
  close(FL1);
}
