Listing 6
#!/opt/bin/perl5
###################################################################
#                        PrnQtReport.pl                           #
# This is the print usage report script running as a              #
# cron job on the print quota server.                             #
#                                                                 #
# System administrators need to modify the $Rdate to reset        #
# the print quota at the beginning of a semester.                 #
#                                                                 #
# --------------------------------------------------------------  #
# This program is free software; you can redistribute it and/or   #
# modify it under the terms of the GNU General Public License     #
# as published by the Free Software Foundation; either version 2  #
# of the License, or (at your option) any later version.          #
#                                                                 #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of  #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the   #
# GNU General Public License for more details.                    #
#                                                                 #
# You should have received a copy of the GNU General Public       #
# License along with this program; if not, write to the Free      #
# Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, #
# MA  02111-1307, USA.                                            #
###################################################################
#
$LogDir="/PrnQT/log";
$Receiver = "uns_prin";
$Log1 = "$LogDir/report";
$Log3 = "$LogDir/overquota";
$DateStamp = "$LogDir/DATE";
#
$PrnQTDir = "/PrnQT/prnquota";
$UsrQTDir = "$PrnQTDir/usrinfo";
$CurPrnList = "$PrnQTDir/prninfo/printerlist";
$NewPrnList = "$PrnQTDir/prninfo/printerlist.new";
$QuotaLeft = "$LogDir/usrinfo";
### RDate needs to be update by the end of every semester ###
$RDate = "970824";
###
$PrnDbFile="$PrnQTDir/prninfo/prndbase";
$WeekTotal=0;
#
#################################################
#    Create Report Header for Public printer    #
#################################################
#
# Print Time Stamp
#
$BeginDate=`cat $DateStamp`;
$EndDate=`date`;
$EDate=`date '+%y%m%d'`;
`date > $DateStamp`;
print BUF2 "$BeginDate";
print BUF2 "$EndDate\n";
#
# Backup and compress old report file
#
if ( -f $Log1 ) {
  `mv $Log1 $Log1.$EDate`;
  `/opt/bin/gzip $Log1.$EDate`;
}
#
# Open Log files
#
open( BUF2, ">$Log1");
open( BUF4, ">$Log3");
#
# Check if the old printer list file exists
#
if ( ! -f $CurPrnList ) {
  &CreatePrnList(old);
}
#
# Create the new printer list file
#
&CreatePrnList(new);
#
# Read pritner names and number of printers
#
$ii=0;
open( CURPRN, "<$CurPrnList");
while ( <CURPRN> ) {
   chop;
   $CurPRN[$ii] = $_;
   $ii++;
}
close ( CURPRN);
#
$jj=0;
open( NEWPRN, "<$NewPrnList");
while ( <NEWPRN> ) {
   chop;
   $NewPRN[$jj] = $_;
   $jj++;
}
close ( NEWPRN);
#
# Print abbreviated printer names
#
$Name[0]="UserName";
$Name[1]="Total";
for ( $Index=0; $Index <= $#CurPRN; $Index++ ) {
   $CurPRNPg[$Index]=0;
   $RR=$Index % 4;
   if ( $RR == 0 ) { print BUF2 "\n"; } 
   $Name[$Index+2] ="pr" . $Index;
   print BUF2 "$Name[$Index+2]=$CurPRN[$Index]\t";
}
print BUF2 "\n\n";
#
# Pirnt Title Bar
#
printf BUF2 "%8s",$Name[0];
for ( $Index=1; $Index <= $#Name; $Index++ ) {
   printf BUF2 "%5s",$Name[$Index];
}
print BUF2 "\n";
$TLength=8+5*$#Name;
for ( $Index=0; $Index <=$TLength; $Index++ ) {
   print BUF2 "*";
} 
print BUF2 "\n";
#
# Print title for overquota report
#
printf BUF4 "%8s%6s%12s%11s\n","UserName","Quota","PagePrinted","PageExcess";
#
#############################
#  Print User Usage Report  #
#############################
open(USERQT,"ls -1 $UsrQTDir|");
while ( <USERQT> ) {
   chop;
   $FileName=$_;
   open( QTINFO,"< $UsrQTDir/$FileName");
   while( <QTINFO> ) {
      @QT=split(/:/,$_);
   }
   close (QTINFO);
   #
   # Over quota report 
   #
   if ( $QT[1] > $QT[0] ) {
       printf BUF4 "%8s%6d%12d%11d\n",$FileName, $QT[0], $QT[1], $QT[1]-$QT[0];
   }
   #
   printf BUF2 "%8s",$FileName;
   printf BUF2 "%5d",$QT[2];
   $WeekTotal=$QT[2]+$WeekTotal; 
   for ( $II=0; $II <= $#CurPRN; $II++ ) {
      $CurPRN[$II]=$CurPRN[$II]+$QT[$II+3];
      printf BUF2 "%5d",$QT[$II+3];
   }
   print BUF2 "\n";
   #
   # Initalize the User Quota File accroding to printer list file
   #
   open( USERQTFILE, ">$UsrQTDir/$FileName");
   printf USERQTFILE "$QT[0]:$QT[1]:0";
   for ( $KK=0; $KK <= $#NewPRN; $KK++ ) {
          $NewQT[$KK]=0; 
          printf USERQTFILE ":%d",$NewQT[$KK];
     } 
   print USERQTFILE "\n";
   if ( $EDate == $RDate )
   { open( QUOTALEFT, ">$QuotaLeft/$FileName");
     if ( $QT[0]-$QT[1] > 0 && $QT[0] > 500 )
     {  if ( $QT[1] > 500 )
        { $QT[0]=$QT[0]-$QT[1];
        }
        else
        { $QT[0]=$QT[0]-500;
        }
        printf QUOTALEFT "$QT[0]\n";   
        `chown lp $QuotaLeft/$FileName`;
        `chmod 444 $QuotaLeft/$FileName`;
     }
     else
     {   `rm $QuotaLeft/$FileName`; }
     close(QUOTALEFT);
     `cp $UsrQTDir/$FileName /prn/log/usrback`;
     `rm $UsrQTDir/$FileName`;
   }
   close(USEQTFILE);
}
for ( $Index=0; $Index <=$TLength; $Index++ ) {
   print BUF2 "*";
} 
print BUF2 "\n";
printf BUF2 "%-8s", "Total";
printf BUF2 "%5d", $WeekTotal;
for ( $JJ=0; $JJ <= $#CurPRN; $JJ++ ) {
   printf BUF2 "%5d",$CurPRN[$JJ];
}
print BUF2 "\n";
close ( BUF4 );
close ( BUF2 );
#
# Update printer list file
#
`mv $NewPrnList $CurPrnList`;
`chmod 644 $CurPrnList`;
#
# Mail the report
#
system("/usr/bin/mailx -s\"Public Printer Report From $BeginDate to $EndDate\" $Receiver < $Log1");
system("/usr/bin/mailx -s\"Over-Printing_quota list\" $Receiver< $Log3");
# 
#######################################################################
#                                Functions                            #
#######################################################################
#
sub CreatePrnList {
    local($Status)=@_;
    local($III)=0;
    if ( $Status eq "old" ) {
       $ListFileName=$CurPrnList;
    }
    else {
       $ListFileName=$NewPrnList;
    }
    open( TTMP, ">$ListFileName");
    open(PRNDB,"<$PrnDbFile");
    while ( <PRNDB> ) {
        chop;
        local(@Info)=split(/:/,$_);
        if ( $Info[1] eq "public" ) {
             print TTMP "$Info[0]\n";
           }
        $III++;
    }
    close(PRNDB);
    close(TTMP); 
}
