#!/usr/bin/perl
#*********************************************************************
#
# Name: PrintErrorLog.
#
# Function: Prints detailed entries from the error log from the
# last run date & time, if present.
#
# Usage: PrintErrorLog [ -h | -v ]
#
# Created by: Gerry Baker (November 2004).
#
# Released:
# v1.0 - 17/11/2004.
#
#*********************************************************************
use warnings;
use strict;
use Getopt::Std;
sub verify_usage;
sub usage_error;
sub get_current_run_date;
sub get_previous_run_date;
sub print_error_log;
sub update_run_date;
#
# Declare and initialise all global variables.
#
our $version = "1.0";
our $basename = $0;
$basename =~ s<.*/><>; ### program base name ###
$= = 60; ### no. of lines per printed page ###
our $current_run_date = "";
our $previous_run_date = "";
our $errpt_date_file = "/var/adm/ras/errpt.date";
our $now = time; ### today's date and time ###
#
# Main process.
#
{
verify_usage();
get_current_run_date();
get_previous_run_date();
print_error_log();
update_run_date();
exit;
}
sub verify_usage() {
#*********************************************************************
#
# Check program flags and arguments.
#
#*********************************************************************
#
# Process program options.
#
my %option = ();
getopts ("hv", \%option) or usage_error;
usage_error if $ARGV[0];
usage_error if defined $option{h};
die ("$basename: Version $version\n") if defined $option{v};
} ### End of sub.
sub usage_error() {
#*********************************************************************
#
# Display usage error and exit.
#
#*********************************************************************
die <<EOF;
Usage:
$basename [ -h | -v ]
Switches
========
-h Displays this message and exits.
-v Displays the program version and exits.
EOF
} ### End of sub.
sub get_current_run_date() {
#*********************************************************************
#
# Retrieve the current date and time and format for use in the
# 'errpt' command.
#
#*********************************************************************
#
# Declare and initialise all local variables.
#
my @date_time = ();
my $minutes = "";
my $hours = "";
my $day = "";
my $month = "";
my $year = "";
#
# Convert today's date and time into its components.
#
@date_time = localtime ($now);
#
# Format today's date and time.
#
$minutes = sprintf ("%02d", $date_time[1]);
$hours = sprintf ("%02d", $date_time[2]);
$day = sprintf ("%02d", $date_time[3]);
$month = sprintf ("%02d", $date_time[4] + 1);
$year = sprintf ("%02d", substr ($date_time[5] + 1900, 2, 2));
#
# Concatenate date and time fields into the format required for the
# 'errpt' command.
#
$current_run_date = $month . $day . $hours . $minutes . $year;
} ### End of sub.
sub get_previous_run_date() {
#*********************************************************************
#
# Retrieve the previous run date and time for this program from the
# errpt date file, if it exists.
#
#*********************************************************************
#
# Set previous run date to undefined if the errpt date file does not
# exist.
#
if ( not -e $errpt_date_file ) {
undef $previous_run_date;
}
#
# Set previous run date to undefined if the errpt date file is
# empty.
#
elsif ( -z $errpt_date_file ) {
undef $previous_run_date;
}
else {
#
# open errpt date file for input.
#
open (ERRPT_DATE, "<", $errpt_date_file)
or die ("$basename: cannot open infile '$errpt_date_file' - $!\n");
#
# Get previous run date and time.
#
$previous_run_date = <ERRPT_DATE>;
chomp ($previous_run_date);
#
# Verify previous run date and time.
#
if ( length $previous_run_date != 10 ) {
undef $previous_run_date;
warn ("$basename: Warning - previous run date invalid!!!\n");
}
elsif ( $previous_run_date !~ /^\d+$/ ) {
undef $previous_run_date;
warn ("$basename: Warning - previous run date not numeric!!!\n");
}
#
# close errpt date file.
#
close ERRPT_DATE
or die ("$basename: error closing infile '$errpt_date_file' - $!\n");
}
} ### End of sub.
sub print_error_log() {
#*********************************************************************
#
# Print details from the error log up to and including the previous
# run date, if present.
#
#*********************************************************************
#
# Declare and initialise local variables.
#
my @month = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
my @date_time = ();
my $run_time = "";
my $run_date = "";
my $errpt_command = "/usr/bin/errpt -a";
my $lp_pipe_open = 0; ### Boolean Flag ###
my $lp_command = "lp -s -dauprt02";
#
# Declare output formats.
#
format STDOUT_TOP =
GMB_P650 ERROR LOG as at @<<<< on @<<<<<<<<<< @>>>>>>>
$run_time, $run_date, "Page $%"
.
format STDOUT =
@<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$_
.
#
# Add the previous run date to the 'errpt' command, if present.
#
if ( defined $previous_run_date ) {
$errpt_command .= " -s " . $previous_run_date;
}
#
# open input pipe to 'errpt' command.
#
open (ERRORLIST, "-|", $errpt_command)
or die ("$basename: cannot open pipe to '$errpt_command' - $!\n");
#
# Format the run date and time.
#
@date_time = localtime ($now);
$run_time = sprintf ("%02d", $date_time[2]) . ":";
$run_time .= sprintf ("%02d", $date_time[1]);
$run_date = sprintf ("%02d", $date_time[3]) . " ";
$run_date .= $month[$date_time[4]] . " ";
$run_date .= $date_time[5] + 1900;
#
# Write out each line from the error log.
#
while ( <ERRORLIST> ) {
#
# open output pipe to 'lp' command, first time only.
#
if ( not $lp_pipe_open ) {
open (STDOUT, "|-", $lp_command)
or die ("$basename: cannot open pipe to '$lp_command' - $!\n");
$lp_pipe_open = 1;
}
#
# Write a line to standard ouput.
#
write;
} ### end of while loop ###
#
# close pipe to 'errpt' command.
#
close ERRORLIST
or die ("$basename: error closing pipe to '$errpt_command' - $!\n");
#
# close pipe to 'lp' command, if open.
#
if ( $lp_pipe_open ) {
close STDOUT
or die ("$basename: error closing pipe to '$lp_command' - $!\n");
}
} ### End of sub.
sub update_run_date() {
#*********************************************************************
#
# Amend the run date on the errpt date file to today's run date.
#
#*********************************************************************
#
# open errpt date file for output.
#
open (ERRPT_DATE, ">", $errpt_date_file)
or die ("$basename: cannot open outfile '$errpt_date_file' - $!\n");
#
# Replace the error log run date.
#
print (ERRPT_DATE "$current_run_date\n");
#
# close errpt date file.
#
close ERRPT_DATE
or die ("$basename: error closing outfile '$errpt_date_file' - $!\n");
} ### End of sub.
### End of program.