I woke up this morning and logged onto my suse laptop to check email etc... I noticed an old perl code folder that I hadn`t look into in a while and found this script along with a few others.
I thought I would post it because it has some pretty good ideas as well as get thoughts from other perl peeps on how to improve the way I write code. Mind you I have improved in some areas while slacking in others since I wrote this. :-)
#!/usr/perl/bin
#######################################################################
#Created By Wendell L. Hatcher
# 03/16/2009
# This process logs onto the mer2-PGPCLP1 server and checks the 6 a.m. Files logs
# as well as the sftp server to confirm that the file was or was not received.
#####################################################################
#main variable declaration#######################################################
use strict;
my $DIR = "D:\\FileRepository\\Distributor\\";
my $mdayminus;
my ($seconds, $minutes, $hours, $day, $month, $year, $wday, $yday,$isdst);
my ($seconds2, $minutes2, $hours2, $day2, $month2, $year2, $wday2,$yday2, $isdst2);
my $logpath = 'D:\FileRepository\Distributor\Perl\Logs'."\\"; #Global var for logpath
my $logprocessname = '6AMFileCheckerProcess';
my $deflogname = $logpath."generic_perl.log";
my $msg;
my $rc;
my $emailreceiver = 'wendell.hatcher@XXXXXXXX.com';
my $subjectemail;
my $erroremail;
my $status;
##################Main program################################
#Create and Open a log file.
my $logname = logName($logprocessname);
$msg = "Successfully created and name $logname\n";
$rc =0;
openLog($logname);
success($logname,$msg,$rc);
#
#AGMT Outbound filechecker subroutine call.
# Get the current date and minus one for the day before
#
AGMTOutbound($DIR);
#DHPP filechecker subroutine call.
#DHPP($DIR);
#GCI-Outbound filechecker subroutine call.
#GCIOutbound($DIR);
#Inbound filechecker subroutine call.
#Inbound($DIR);
#IRT-Outbound subroutine call.
#IRTOutbound($DIR);
#OOB subroutine call.
#OOB($DIR);
#Outbound subroutine call.
#Outbound($DIR);
#SUMMIT subroutine call.
#SUMMIT($DIR);
##############################################################
#AGMT Outbound filechecker subroutin.
sub AGMTOutbound {
my($a); # declare single variable for inbound file constant directory.
($a) = @_; # assign constant directory.
my $newdir = $a."AGMT-Outbound";
my $backupdir = $a."AGMT-Outbound\\Backup";
my $filename = "AGMT";
my $backupfile = "AGMT";
my @dir = ();
my @directorynames = ();
my $singlebackupfile;
my $count = 0;
my $mtime;
my $atime;
my $dirwildcard = 'AGMT/*AGMT*';
########First Check for Files in FTP Server###################################
my $agmtpsftppath = "D:\\Batch_Apps\\Partners\\Distributor\\AGMT-Outbound";
my $agmtpsftbat = 'psftpagmt.bat';
checkFTP($agmtpsftppath,$agmtpsftbat );
#Change Directory path and verify a file exists.#######################################
chdir($newdir);
opendir (DIRH, $newdir) or die "couldn`t open $newdir: $!";
@dir = grep {-f && /$filename/i} readdir DIRH;
closedir DIRH;
foreach $filename(@dir) {
#Check the main directory to confirm files do not exists on that level
next if $filename eq '.' or $filename eq '..';
if (-f $filename){
$subjectemail = " $filename, :File exists on main directory process failed.\n";
$status = 16;
fail($logname,$subjectemail,$status);
Email($emailreceiver,$subjectemail,$status);
closeLog($logname);
return exit(16);
}
}
#Check the backup directory and confirm 6 backup files from yesterday exist within the directory
# Once all 6 files are confirmed exit out with a return code of 0.
chdir($backupdir);
opendir (DIR, $backupdir) or die "$!";
@directorynames = grep {-f && /$backupfile/i} readdir DIR;
close DIR;
#Check the backup directory and confirm all of the files are there and current for todays date. #####################################
foreach $singlebackupfile(@directorynames) {
if ($singlebackupfile =~ /.(AGMT).*/i )
{
($mtime) = (stat("$singlebackupfile"))[9];
($seconds, $minutes, $hours, $day, $month, $year, $wday, $yday,$isdst) = localtime();
($seconds2, $minutes2, $hours2, $day2, $month2, $year2, $wday2,$yday2, $isdst2) = localtime($mtime);
if ($day == $day2 && $month == $month2) {
$count++;
$subjectemail = "The file name: $singlebackupfile and first day: $day first month: $month and secondday: $day2 and second month: $month2\n";
$status = 0;
success($logname,$subjectemail,$status);
if($count <= 6) {
$subjectemail = " GCI 6 am jobs Data processessed successfully";
$status = 0;
success($logname,$subjectemail,$status);
}
}
}
} if ($count == 0) {
$subjectemail = "Process Failed!!\n";
$status = 16;
fail($logname,$subjectemail,$status);
Email($emailreceiver,$subjectemail,$status);
closeLog($logname);
return exit(16);
}
}
############################################################
#DHPP filechecker process
sub DHPP {
my($a); # declare single variable for inbound file constant directory.
($a) = @_; # assign constant directory.
my $newdir = $a."DHPP";
my $backupdir = $a."DHPP\\Backup";
my $filename = "DHPP";
my $backupfile = "DHPP";
my @dir = ();
my @directorynames = ();
my $singlebackupfile;
my $count = 0;
my $mtime;
my $atime;
my $dirwildcard = 'DHPP/*DHPP*';
#Change Directory path and verify a file exists.#######################################
chdir($newdir);
opendir (DIRH, $newdir) or die "couldn`t open $newdir: $!";
@dir = grep {-f && /$filename/i} readdir DIRH;
closedir DIRH;
foreach $filename(@dir) {
#Check the main directory to confirm files do not exists on that level
next if $filename eq '.' or $filename eq '..';
if (-f $filename){
$subjectemail = " $filename, :File exists on main directory process failed.\n";
$status = 16;
fail($logname,$subjectemail,$status);
Email($emailreceiver,$subjectemail,$status);
closeLog($logname);
return exit(16);
}
}
#Check the backup directory and confirm 6 backup files from yesterday exist within the directory
# Once all 6 files are confirmed exit out with a return code of 0.
chdir($backupdir);
opendir (DIR, $backupdir) or die "$!";
@directorynames = grep {-f && /$backupfile/i} readdir DIR;
close DIR;
#Check the backup directory and confirm all of the files are there and current for todays date. #####################################
foreach $singlebackupfile(@directorynames) {
if ($singlebackupfile =~ /.(DHPP).*/i )
{
($mtime) = (stat("$singlebackupfile"))[9];
($seconds, $minutes, $hours, $day, $month, $year, $wday, $yday,$isdst) = localtime();
($seconds2, $minutes2, $hours2, $day2, $month2, $year2, $wday2,$yday2, $isdst2) = localtime($mtime);
if ($day == $day2 && $month == $month2) {
$count++;
$subjectemail = "The file name: $singlebackupfile and first day: $day first month: $month and secondday: $day2 and second month: $month2\n";
$status = 0;
success($logname,$subjectemail,$status);
if($count <= 6) {
$subjectemail = " DHPP 6 am jobs Data processessed successfully";
$status = 0;
success($logname,$subjectemail,$status);
}
}
}
}
}
###########################################################
#GCI-Outbound
sub GCIOutbound {
my($a); # declare single variable for inbound file constant directory.
($a) = @_; # assign constant directory.
my $newdir = $a."GCI-Outbound";
my $backupdir = $a."GCI-Outbound\\Backup";
my $filename = "GCI";
my $backupfile = "GCI";
my @dir = ();
my @directorynames = ();
my $singlebackupfile;
my $count = 0;
my $mtime;
my $atime;
my $dirwildcard = 'GCI/*GCI*';
########First Check for Files in FTP Server###################################
my $gcipsftppath = "D:\\Batch_Apps\\Partners\\Distributor\\GCI-Outbound";
my $gcipsftbat = 'psftpgci.bat';
checkFTP($gcipsftppath,$gcipsftbat );
#Change Directory path and verify a file exists.#######################################
chdir($newdir);
opendir (DIRH, $newdir) or die "couldn`t open $newdir: $!";
@dir = grep {-f && /$filename/i} readdir DIRH;
closedir DIRH;
foreach $filename(@dir) {
#Check the main directory to confirm files do not exists on that level
next if $filename eq '.' or $filename eq '..';
if (-f $filename){
$subjectemail = " $filename, :File exists on main directory process failed.\n";
$status = 16;
fail($logname,$subjectemail,$status);
Email($emailreceiver,$subjectemail,$status);
closeLog($logname);
return exit(16);
}
}
#Check the backup directory and confirm 6 backup files from yesterday exist within the directory
# Once all 6 files are confirmed exit out with a return code of 0.
chdir($backupdir);
opendir (DIR, $backupdir) or die "$!";
@directorynames = grep {-f && /$backupfile/i} readdir DIR;
close DIR;
#Check the backup directory and confirm all of the files are there and current for todays date. #####################################
foreach $singlebackupfile(@directorynames) {
if ($singlebackupfile =~ /.(AGMT).*/i )
{
($mtime) = (stat("$singlebackupfile"))[9];
($seconds, $minutes, $hours, $day, $month, $year, $wday, $yday,$isdst) = localtime();
($seconds2, $minutes2, $hours2, $day2, $month2, $year2, $wday2,$yday2, $isdst2) = localtime($mtime);
if ($day == $day2 && $month == $month2) {
$count++;
$subjectemail = "The file name: $singlebackupfile and first day: $day first month: $month and secondday: $day2 and second month: $month2\n";
$status = 0;
success($logname,$subjectemail,$status);
if($count <= 6) {
$subjectemail = " GCI 6 am jobs Data processessed successfully";
$status = 0;
success($logname,$subjectemail,$status);
}
}
}
} if ($count == 0) {
$subjectemail = "GCI Process Failed!!\n";
$status = 16;
fail($logname,$subjectemail,$status);
Email($emailreceiver,$subjectemail,$status);
closeLog($logname);
return exit(16);
}
}
###########################################################
#Inbound
sub Inbound {
my($a); # declare single variable for inbound file constant directory.
($a) = @_; # assign constant directory.
my $newdir = $a."Ibound";
my $backupdir = $a."Inbound\\Backup";
my $filename = "SHIPMENT";
my $backupfile = "SHIPMENT";
my @dir = ();
my @directorynames = ();
my $singlebackupfile;
my $count = 0;
my $mtime;
my $atime;
my $dirwildcard = 'Inbound/*SHIPMENT*';
#Change Directory path and verify a file exists.#######################################
chdir($newdir);
opendir (DIRH, $newdir) or die "couldn`t open $newdir: $!";
@dir = grep {-f && /$filename/i} readdir DIRH;
closedir DIRH;
foreach $filename(@dir) {
#Check the main directory to confirm files do not exists on that level
next if $filename eq '.' or $filename eq '..';
if (-f $filename){
$subjectemail = " $filename, :File exists on main directory process failed.\n";
$status = 16;
fail($logname,$subjectemail,$status);
Email($emailreceiver,$subjectemail,$status);
closeLog($logname);
return exit(16);
}
}
#Check the backup directory and confirm 6 backup files from yesterday exist within the directory
# Once all 6 files are confirmed exit out with a return code of 0.
chdir($backupdir);
opendir (DIR, $backupdir) or die "$!";
@directorynames = grep {-f && /$backupfile/i} readdir DIR;
close DIR;
#Check the backup directory and confirm all of the files are there and current for todays date. #####################################
foreach $singlebackupfile(@directorynames) {
if ($singlebackupfile =~ /.(SHIPMENT).*/i )
{
($mtime) = (stat("$singlebackupfile"))[9];
($seconds, $minutes, $hours, $day, $month, $year, $wday, $yday,$isdst) = localtime();
($seconds2, $minutes2, $hours2, $day2, $month2, $year2, $wday2,$yday2, $isdst2) = localtime($mtime);
if ($day == $day2 && $month == $month2) {
$count++;
$subjectemail = "The file name: $singlebackupfile and first day: $day first month: $month and secondday: $day2 and second month: $month2\n";
$status = 0;
success($logname,$subjectemail,$status);
if($count <= 6) {
$subjectemail = " Inbound 6 am jobs Data processessed successfully";
$status = 0;
success($logname,$subjectemail,$status);
}
}
}
}
}
##########################################################
#IRT-Outbound
sub IRTOutbound {
my($a); # declare single variable for inbound file constant directory.
($a) = @_; # assign constant directory.
my $newdir = $a."IRT-Outbound";
my $backupdir = $a."IRT-Outbound\\Backup";
my $filename = "IRT";
my $backupfile = "IRT";
my @dir = ();
my @directorynames = ();
my $singlebackupfile;
my $count = 0;
my $mtime;
my $atime;
my $dirwildcard = 'IRT/*IRT*';
#Change Directory path and verify a file exists.#######################################
chdir($newdir);
opendir (DIRH, $newdir) or die "couldn`t open $newdir: $!";
@dir = grep {-f && /$filename/i} readdir DIRH;
closedir DIRH;
foreach $filename(@dir) {
#Check the main directory to confirm files do not exists on that level
next if $filename eq '.' or $filename eq '..';
if (-f $filename){
$subjectemail = " $filename, :File exists on main directory process failed.\n";
$status = 16;
fail($logname,$subjectemail,$status);
Email($emailreceiver,$subjectemail,$status);
closeLog($logname);
return exit(16);
}
}
#Check the backup directory and confirm 6 backup files from yesterday exist within the directory
# Once all 6 files are confirmed exit out with a return code of 0.
chdir($backupdir);
opendir (DIR, $backupdir) or die "$!";
@directorynames = grep {-f && /$backupfile/i} readdir DIR;
close DIR;
#Check the backup directory and confirm all of the files are there and current for todays date. #####################################
foreach $singlebackupfile(@directorynames) {
if ($singlebackupfile =~ /.(IRT).*/i )
{
($mtime) = (stat("$singlebackupfile"))[9];
($seconds, $minutes, $hours, $day, $month, $year, $wday, $yday,$isdst) = localtime();
($seconds2, $minutes2, $hours2, $day2, $month2, $year2, $wday2,$yday2, $isdst2) = localtime($mtime);
if ($day == $day2 && $month == $month2) {
$count++;
$subjectemail = "The file name: $singlebackupfile and first day: $day first month: $month and secondday: $day2 and second month: $month2\n";
$status = 0;
success($logname,$subjectemail,$status);
if($count <= 6) {
$subjectemail = " IRT 6 am jobs Data processessed successfully";
$status = 0;
success($logname,$subjectemail,$status);
}
}
}
}
}
#########################################################
#OOB
sub OOB {
my($a); # declare single variable for inbound file constant directory.
($a) = @_; # assign constant directory.
my $newdir = $a."OOB";
my $backupdir = $a."OOB\\Backup";
my $filename = "OOB";
my $backupfile = "OOB";
my @dir = ();
my @directorynames = ();
my $singlebackupfile;
my $count = 0;
my $mtime;
my $atime;
my $dirwildcard = 'OOB/*OOB*';
#Change Directory path and verify a file exists.#######################################
chdir($newdir);
opendir (DIRH, $newdir) or die "couldn`t open $newdir: $!";
@dir = grep {-f && /$filename/i} readdir DIRH;
closedir DIRH;
foreach $filename(@dir) {
#Check the main directory to confirm files do not exists on that level
next if $filename eq '.' or $filename eq '..';
if (-f $filename){
$subjectemail = " $filename, :File exists on main directory process failed.\n";
$status = 16;
fail($logname,$subjectemail,$status);
Email($emailreceiver,$subjectemail,$status);
closeLog($logname);
return exit(16);
}
}
#Check the backup directory and confirm 6 backup files from yesterday exist within the directory
# Once all 6 files are confirmed exit out with a return code of 0.
chdir($backupdir);
opendir (DIR, $backupdir) or die "$!";
@directorynames = grep {-f && /$backupfile/i} readdir DIR;
close DIR;
#Check the backup directory and confirm all of the files are there and current for todays date. #####################################
foreach $singlebackupfile(@directorynames) {
if ($singlebackupfile =~ /.(OOB).*/i )
{
($mtime) = (stat("$singlebackupfile"))[9];
($seconds, $minutes, $hours, $day, $month, $year, $wday, $yday,$isdst) = localtime();
($seconds2, $minutes2, $hours2, $day2, $month2, $year2, $wday2,$yday2, $isdst2) = localtime($mtime);
if ($day == $day2 && $month == $month2) {
$count++;
$subjectemail = "The file name: $singlebackupfile and first day: $day first month: $month and secondday: $day2 and second month: $month2\n";
$status = 0;
success($logname,$subjectemail,$status);
if($count <= 6) {
$subjectemail = " OOB 6 am jobs Data processessed successfully";
$status = 0;
success($logname,$subjectemail,$status);
}
}
}
}
}
#########################################################
#Outbound
sub Outbound {
my($a); # declare single variable for inbound file constant directory.
($a) = @_; # assign constant directory.
my $newdir = $a."Outbound";
my $backupdir = $a."Outbound\\Backup";
my $filename = "SHIPMENT";
my $backupfile = "SHIPMENT";
my @dir = ();
my @directorynames = ();
my $singlebackupfile;
my $count = 0;
my $mtime;
my $atime;
my $dirwildcard = 'SHIPMENT/*SHIPMENT*';
########First Check for Files in FTP Server###################################
my $gcipsftppath = "D:\\Batch_Apps\\Partners\\Distributor\\Outbound";
my $gcipsftbat = 'psftout.bat';
checkFTP($outsftppath,$outsftbat );
#Change Directory path and verify a file exists.#######################################
chdir($newdir);
opendir (DIRH, $newdir) or die "couldn`t open $newdir: $!";
@dir = grep {-f && /$filename/i} readdir DIRH;
closedir DIRH;
foreach $filename(@dir) {
#Check the main directory to confirm files do not exists on that level
next if $filename eq '.' or $filename eq '..';
if (-f $filename){
$subjectemail = " $filename, :File exists on main directory process failed.\n";
$status = 16;
fail($logname,$subjectemail,$status);
Email($emailreceiver,$subjectemail,$status);
closeLog($logname);
return exit(16);
}
}
#Check the backup directory and confirm 6 backup files from yesterday exist within the directory
# Once all 6 files are confirmed exit out with a return code of 0.
chdir($backupdir);
opendir (DIR, $backupdir) or die "$!";
@directorynames = grep {-f && /$backupfile/i} readdir DIR;
close DIR;
#Check the backup directory and confirm all of the files are there and current for todays date. #####################################
foreach $singlebackupfile(@directorynames) {
if ($singlebackupfile =~ /.(SHIPMENT).*/i )
{
($mtime) = (stat("$singlebackupfile"))[9];
($seconds, $minutes, $hours, $day, $month, $year, $wday, $yday,$isdst) = localtime();
($seconds2, $minutes2, $hours2, $day2, $month2, $year2, $wday2,$yday2, $isdst2) = localtime($mtime);
if ($day == $day2 && $month == $month2) {
$count++;
$subjectemail = "The file name: $singlebackupfile and first day: $day first month: $month and secondday: $day2 and second month: $month2\n";
$status = 0;
success($logname,$subjectemail,$status);
if($count <= 6) {
$subjectemail = " OUTBOUND 6 am jobs Data processessed successfully";
$status = 0;
success($logname,$subjectemail,$status);
}
}
}
} if ($count == 0) {
$subjectemail = "OUTBOUND Process Failed!!\n";
$status = 16;
fail($logname,$subjectemail,$status);
Email($emailreceiver,$subjectemail,$status);
closeLog($logname);
return exit(16);
}
}
}
#########################################################
#SUMMIT
sub SUMMIT {
my($a); # declare single variable for inbound file constant directory.
($a) = @_; # assign constant directory.
my $newdir = $a."SUMMIT";
my $backupdir = $a."SUMMIT\\Backup";
my $filename = "SUMMIT";
my $backupfile = "SUMMIT";
my @dir = ();
my @directorynames = ();
my $singlebackupfile;
my $count = 0;
my $mtime;
my $atime;
my $dirwildcard = 'SUMMIT/*SUMMIT*';
#Change Directory path and verify a file exists.#######################################
chdir($newdir);
opendir (DIRH, $newdir) or die "couldn`t open $newdir: $!";
@dir = grep {-f && /$filename/i} readdir DIRH;
closedir DIRH;
foreach $filename(@dir) {
#Check the main directory to confirm files do not exists on that level
next if $filename eq '.' or $filename eq '..';
if (-f $filename){
$subjectemail = " $filename, :File exists on main directory process failed.\n";
$status = 16;
fail($logname,$subjectemail,$status);
Email($emailreceiver,$subjectemail,$status);
closeLog($logname);
return exit(16);
}
}
#Check the backup directory and confirm 6 backup files from yesterday exist within the directory
# Once all 6 files are confirmed exit out with a return code of 0.
chdir($backupdir);
opendir (DIR, $backupdir) or die "$!";
@directorynames = grep {-f && /$backupfile/i} readdir DIR;
close DIR;
#Check the backup directory and confirm all of the files are there and current for todays date. #####################################
foreach $singlebackupfile(@directorynames) {
if ($singlebackupfile =~ /.(SUMMIT).*/i )
{
($mtime) = (stat("$singlebackupfile"))[9];
($seconds, $minutes, $hours, $day, $month, $year, $wday, $yday,$isdst) = localtime();
($seconds2, $minutes2, $hours2, $day2, $month2, $year2, $wday2,$yday2, $isdst2) = localtime($mtime);
if ($day == $day2 && $month == $month2) {
$count++;
$subjectemail = "The file name: $singlebackupfile and first day: $day first month: $month and secondday: $day2 and second month: $month2\n";
$status = 0;
success($logname,$subjectemail,$status);
if($count <= 6) {
$subjectemail = " SUMMIT 6 am jobs Data processessed successfully";
$status = 0;
success($logname,$subjectemail,$status);
}
}
}
}
}
##########################################################
#Check FTP files
sub checkFTP {
#set path to putty to use putty sftp.
my $puttystatus;
my $psftppath $_[0];
my $dir;
my $ftpbatfile$_[1];
my $status;
($psftppath, $ftpbatfile) = @_;
chdir($psftppath);
my $newputty = $ftpbatfile;
$puttystatus= system($newputty);
die "$newputty exited with error: $?" unless $status ==0;
success($logname,$puttystatus,$status);
#Send email notification with information to the client through email and log inside of output file.##################################
if ($puttystatus != 0) {
$subjectemail = "FTP process failed for some reason check 6am logs.\n";
fail($logname,$subjectemail,$puttystatus);
Email($emailreceiver,$subjectemail,$puttystatus);
}
}
##############################################################
#Email Success or Failure
sub Email {
use Net::SMTP;
my $emailto;
my $emailsubject;
my $emailerror;
($emailto, $emailsubject,$emailerror) = @_;
my $smtp_temp = Net::SMTP->new('rf-mail1.XXXXXXXX', Debug => 1);
#my $smtp_temp = Net::SMTP->new('localhost', Debug => 0);
$smtp_temp->mail('IT.ApplicationsMgmt.CustomApps@XXXXXXXX');
$smtp_temp->to($emailto);
$smtp_temp->data();
$smtp_temp->datasend($emailsubject);
$smtp_temp->datasend($emailerror);
$smtp_temp->quit;
}
#### Logging For the application############################################################################
#This part of the application can be taken apart and used within other apps on its own with a little tweaking.
#Calls writeLog to write a line indicating success to a log
#INPUT: Message and return code to write to log.
#RETURNS: Message and return code for the method
sub success {
my $self = $_[0];
my $msg = $_[1]; #Message to log
my $rc = $_[2]; #Return code to log
my $flag = "SUCCESS";
($msg,$rc) = writeLog($self,$msg,$rc,$flag);
return $msg,$rc;
}
#Calls writeLog to write a line indicating warning to a log
#INPUT: Message and return code to write to log.
#RETURNS: Message and return code for the method
sub warn {
my $self = $_[0];
my $msg = $_[1]; #Message to log
my $rc = $_[2]; #Return code to log
my $flag = "WARN";
($msg,$rc) = writeLog($self,$msg,$rc,$flag);
return $msg,$rc;
}
#Calls writeLog to write a line indicating failure to a log
#INPUT: Message and return code to write to log.
#RETURNS: Message and return code for the method
sub fail {
my $self = $_[0];
my $msg = $_[1]; #Message to log
my $rc = $_[2]; #Return code to log
my $flag = "ERROR";
($msg,$rc) = writeLog($self,$msg,$rc,$flag);
return $msg,$rc;
}
#Writes a line indicating failure to a log
#INPUT: Log reference, message, return code, flag to write to log.
# Also, $self->{NAME} (name of log) is required
#RETURNS: Message and return code for the method
sub writeLog {
my $self = $_[0];
my $msg = $_[1]; #Message to log
my $rc = $_[2]; #Return code to log
my $flag = $_[3];
my $logname = $self; #Name of log to write to
$msg =~ s/\n*//g; #Remove newlines
if ($logname eq "") { #Requires a log reference to be passed
$msg = "Filename $logname does not exist";
$rc = 1001;
return $msg,$rc;
}
my $date = getDT(); #Get the timestamp
($submsg,$subrc) = fileChecks($logname); #Check to see if the file is readable, writable
if ($subrc != 0) {
open(LOG,">>$deflogname"); #Writes to default log
print LOG "$date $subrc ERROR: $submsg\n";
close LOG;
$msg = "$submsg".", wrote to $deflogname instead";
return $msg,$rc;
} else {
$rc = "0 " if $rc == 0;
open(LOG,">>$logname"); #Opens log
print LOG "$date $rc $flag: $msg\n"; #Writes log record
close LOG; #Closes log
return $msg,$rc;
}
}
#Opens a log, and prints the header information
#INPUT: $self->{NAME} (name of log) is required
#RETURNS: Message and return code for the method
sub openLog {
my ($msg,$rc);
my $date = getDT(); #Get the timestamp
my $logname = $_[0]; #Name of log to write to
if ($logname eq "") { #Requires a log reference to be passed
$msg = "Filename $logname does not exist";
$rc = 1001;
return $msg,$rc;
}
($msg,$rc) = fileChecks($logname); #Check to see if the file is readable, writable
if ($rc != 0) { #Writes to default log
open(LOG,">>$deflogname");
print LOG "###################### BEGIN ######################\n";
print LOG "$date $rc ERROR: $msg\n";
print LOG "####################### END #######################\n";
close LOG;
$msg = "$msg".", wrote to $deflogname instead";
return $msg,$rc;
} else {
open(LOG,">>$logname"); #Opens log
$msg = "Log $logname opened";
$rc = "0 ";
print LOG "###################### BEGIN ######################\n";
print LOG "$date $rc SUCCESS: $msg\n"; #Writes log message
close LOG; #Closes log
return $msg,$rc;
}
}
#Opens a log, and prints the footer information
#INPUT: $self->{NAME} (name of log) is required
#RETURNS: Message and return code for the method
sub closeLog {
my ($msg,$rc);
my $date = getDT(); #Get the timestamp
my $logname = $_[0]; #Name of log to write to
if ($logname eq "") { #Requires a log reference to be passed
$msg = "Filename $logname does not exist";
$rc = 1001;
return $msg,$rc;
}
($msg,$rc) = fileChecks($logname); #Check to see if the file is readable, writable
if ($rc != 0) { #Writes to default log
open(LOG,">>$deflogname");
print LOG "###################### BEGIN ######################\n";
print LOG "$date $rc ERROR: $msg\n";
print LOG "####################### END #######################\n";
close LOG;
$msg = "$msg".", wrote to $deflogname instead";
return $msg,$rc;
} else {
open(LOG,">>$logname"); #Opens log
$msg = "Log $logname closed";
$rc = "0 ";
print LOG "$date $rc SUCCESS: $msg\n"; #Writes log message
print LOG "####################### END #######################\n";
close LOG; #Closes log
return $msg,$rc;
}
}
#Creates the name of the log to write to
#INPUT: Original process name
#RETURNS: Entire logname
sub logName {
my $process = $_[0];
my $shortdate = getLogExt();
my $logname = $logpath.$process.".$shortdate.log";
return $logname;
}
#Checks to see if file is readable, writable, exists
#If file does not exist, it will be created
#INPUT: Logname to check
#RETURNS: Message and return code
sub fileChecks {
my $logname = $_[0];
my ($msg,$rc);
$rc = 0;
if (!-e $logname) {
unless (open(LOG,">>$logname") && (print LOG "") && close LOG) {
$msg = "Filename $logname not writable";
$rc = 1002;
}
} elsif (!-r $logname) {
$msg = "Filename $logname not readable";
$rc = 1003;
} elsif (!-w $logname) {
$msg = "Filename $logname not writable";
$rc = 1002;
} else {
$msg = "Filename $logname is ready for writing";
$rc = 0;
}
return $msg,$rc;
}
#Generates short timestamp for logname
#INPUT: n/a
#RETURNS: Timestamp
sub getLogExt {
my @time = localtime;
my ($second,$minute,$hour,$day,$month,$year) = @time;
$month = $month +1;
$month = "0".$month if $month < 10;
$day = "0".$day if $day < 10;
$year = $year + 1900;
my $stamp = $day.$month.$year;
return $stamp;
}
#Generates timestamp for log
#INPUT: n/a
#RETURNS: Timestamp
sub getDT {
my @time = localtime;
my ($second,$minute,$hour,$day,$month,$year) = @time;
$second = "0".$second if $second < 10;
$minute = "0".$minute if $minute < 10;
$hour = "0".$hour if $hour < 10;
$day = "0".$day if $day < 10;
$month = $month +1;
$month = "0".$month if $month < 10;
$year = $year + 1900;
my $stamp = "$month"."/"."$day"."/"."$year $hour:$minute:$second";
return $stamp;
}
Recent Comments