OK, OK, here's the entire thing. Please, be gentle, I'm a sys admin, not a programmer...
#!/usr/bin/perl
######################################################################
#
# Script name: assemble_erorrs.pl
#
# Author: Colin Coe
#
# Purpose: Assembles store error messages before emailling them
# to the operators and systems group.
#
# History: <snipped>
#
# Conventions: Variable names are in uppercase (ie $HOME)
# Function names are in proper case (ie &File_Length)
#
######################################################################
######################################################################
## 'use' and 'require' section
require "ctime.pl";
######################################################################
######################################################################
## Define static variables
# Misc vaiables
$HOME = $ENV{"HOME"};
$DATE = &ctime(time);
$MAIL_LIST = "me\@employer";
# File and directory name variables
$STORE_LIST = "/store_list.full_listing";
$STORE_LIST_TMP = "/tmp/stores.$$";
$STORE_LIST_NEW = "/tmp/stores_new.$$";
$STORE_LIST_TMP_NEW = "/tmp/stores_tmp_new.$$";
$DELIVERY_DIR = $HOME . "/delivery";
$NO_CORRUPT_DIR = $HOME . "/no_corrupt_stores";
$OUTPUT_FILE = $HOME . "/store_errors";
$OUTPUT_FILE_CORRUPT = $HOME . "/store_corrupt";
$PING_OUTPUT = "/tmp/ping_check.out";
$ERR_LIST = "/tmp/err_list.$$";
$COR_LIST = "/tmp/cor_list.$$";
$NO_ERR_LIST = "/tmp/no_err_list.$$";
$NO_COR_LIST = "/tmp/no_cor_list.$$";
$TMP_LIST = "/tmp/tmp_file.$$";
$REPORTED_ERROR = "/tmp/err_report.$$";
$REPORTED_CORRUPT = "/tmp/cor_report.$$";
# File masks
$ERROR_MASK = $DELIVERY_DIR . "error*";
$NO_ERROR_MASK = $DELIVERY_DIR . "no_error*";
$CORRUPT_MASK = $NO_CORRUPT_DIR . "corrupt*";
$NO_CORRUPT_MASK = $NO_CORRUPT_DIR . "no_corrupt*";
# Counter variables
$NUM_NO_ERRORS = &Num_Files($NO_ERROR_MASK);
$NUM_ERRORS = &Num_Files($ERROR_MASK);
$NUM_NO_CORRUPT = &Num_Files($NO_CORRUPT_MASK);
$NUM_CORRUPT = &Num_Files($CORRUPT_MASK);
$NUM_STORES = &File_Length($STORE_LIST);
$NUM_ERR_REPORTED = ($NUM_ERRORS + $NUM_NO_ERRORS) - $NUM_STORES;
$NUM_COR_REPORTED = ($NUM_CORRUPT + $NUM_NO_CORRUPT) - $NUM_STORES;
######################################################################
## Main program begins here
# Do HA check and stop execution if not on bbsmce
warn "About to do HA_Check\n";
&HA_Check || exit;
#if (&HA_Check) || die "Exiting because running on wrong node\n";
open(OUTFILE, "> $OUTPUT_FILE") || die "Could not open $OUTPUT_FILE fo
+r writing: $!\n";
&Outfile_Header;
&Errors;
&Corrupt;
close(OUTFILE);
# Email result
&Clean_Up;
exit;
# End of main program
######################################################################
## Functions and sub routines
# Function to return the number of lines in a file
# Returns the number of lines in the file
sub File_Length {
local($FILE) = chomp($_);
$COUNT = 0;
open(INFILE, "< $FILE") || die "Can't open $FILE for reading:
+$!\n";
while (<INFILE>) {
$COUNT += 1;
}
close(INFILE);
$COUNT;
}
# Sub routine to print header to output file
sub Outfile_Header {
print OUTFILE "<snip> listing for " . $DATE;
}
# Sub routine to print error section header to output file
sub Error_Section_Header {
print OUTFILE "\nError Section\n";
}
# Sub routine to print corruption section header to output file
sub Corruption_Section_Header {
print OUTFILE "\nCorruption Section\n";
}
# Function to return the number of files matching the file mask, also
# populates $ERR_LIST
# Returns: -1 on error
# 0 no error files found
# >0 the number of cfiles found
sub Num_Files {
local($MASK, $LIST) = chomp(local($_));
local($NUM_FILES);
open(OUTFILE, "ls -la $MASK > $TMP_FILE");
if ($? = 0) {
&File_Length($TMP_FILE);
while (<OUTFILE>) {
$STRING = local($_);
($JUNK, $STORE) = split('.');
open(OUTFILE, ">> $LIST");
print OUTFILE $STORE . "\n";
close(OUTFILE);
}
close(OUTFILE);
} else {
$NUM_FILES=0;
}
$NUM_FILES;
}
# Function to test which node the script is running on
# Returns: 0 if P01
# 1 if P02, P03 or P04
sub HA_Check {
if ("`netstat -i | grep -i P01`" eq "P01") {
return(0);
} else {
return(1);
}
close(INFILE);
}
sub Errors {
&Error_Section_Header;
&Num_Files($ERROR_MASK, $ERR_LIST);
&Num_Files($NO_ERROR_MASK, $NO_ERR_LIST);
print OUTFILE "There are a total of " . $NUM_STORES . " stores
+\n";
print OUTFILE "\tThere were " . $NUM_ERR_REPORTED . " stores t
+hat reported in.\n";
print OUTFILE "\tThere were " . $NUM_STORES - $NUM_ERR_REPORTE
+D . " that did not report in.\n";
open(ERRFILE, "`cat $ERR_LIST $NO_ERR_LIST $STORE_LIST | sort
+| uniq -u` > $REPORTED_ERROR");
close(ERRFILE);
open(INFILE, $REPORTED_ERROR);
while (<INFILE>) {
print OUTFILE "\t\t" . chomp($_) . "\n";
}
close(INFILE);
print OUTFILE "\tThere were " . $NUM_ERR . " that reported err
+ors.\n";
open(ERRFILE, "> $ERR_LIST");
while (<ERRFILE>) {
print OUTFILE "\t\t" . chomp($_) . "\n";
}
close(ERRFILE);
}
sub Corrupt {
&Corrupt_Section_Header;
&Num_Files($CORRUPT_MASK, $COR_LIST);
&Num_Files($NO_CORRUPT_MASK, $NO_COR_LIST);
print OUTFILE "There are a total of " . $NUM_STORES . " stores
+\n";
print OUTFILE "\tThere were " . $NUM_COR_REPORTED . " stores t
+hat reported in.\n";
print OUTFILE "\tThere were " . $NUM_STORES - $NUM_COR_REPORTE
+D . " that did not report in.\n";
open(CORFILE, "`cat $COR_LIST $NO_COR_LIST $STORE_LIST | sort
+| uniq -u` > $REPORTED_CORRUPT");
close(CORFILE);
open(INFILE, $REPORTED_CORRUPT);
while (<INFILE>) {
print OUTFILE "\t\t" . chomp($_) . "\n";
}
close(INFILE);
print OUTFILE "\tThere were " . $NUM_COR . " that reported err
+ors.\n";
open(CORFILE, "> $COR_LIST");
while (<CORFILE>) {
print OUTFILE "\t\t" . chomp($_) . "\n";
}
close(CORFILE);
}
# Sub routine to remove temporary files
sub Clean_Up {
unlink($STORE_LIST_TMP);
unlink($STORE_LIST_NEW);
unlink($STORE_LIST_TMP_NEW);
unlink($OUTPUT_FILE);
unlink($OUTPUT_FILE_CORRUPT);
unlink($ERR_LIST);
unlink($COR_LIST);
unlink($NO_ERR_LIST);
unlink($NO_COR_LIST);
unlink($TMP_LIST);
unlink($REPORTED_ERROR);
unlink($REPORTED_CORRUPT);
}