#!/usr/bin/perl
#
# nbumail.pl - 	NBU nightly reports.  Generate reports of successful, 
#		failed jobs along with sending reports to end-users.
#
# USAGE: bperror {-S|-statuscode status_code}
#            [-r|-recommendation] [-p|-platform Unx|NTx] [-v]
#        bperror  [-all|-problems|-media|{-backstat [-by_statcode]}]
#            [-L|-l|-U] [-columns ncols]
#            [-d mm/dd/yyyy HH:MM:SS|-hoursago hours] [-e mm/dd/yyyy HH:MM:SS]
#            [-client client_name] [-server server_name]
#            [-jobid job_id]
#            [-M master_server,...] [-v]
#        bperror  [-s {severity[+]}|severity ...] [-t type ...]
#            [-L|-l|-U] [-columns ncols]
#            [-d mm/dd/yyyy HH:MM:SS|-hoursago hours] [-e mm/dd/yyyy HH:MM:SS]
#            [-client client_name] [-server server_name]
#            [-jobid job_id]
#            [-M master_server,...] [-v]
# 
#        -by_statcode is used only with both -U and -backstat
# 
#        Valid values for ncols:
#            40 or more
# 
#        Valid values for severity:
#            ALL, DEBUG, INFO, WARNING, ERROR, CRITICAL
# 
#        Valid values for type:
#            ALL, ARCHIVE, BACKSTAT, BACKUP, GENERAL,
#            MEDIADEV, RETRIEVE, SECURITY
#
# FORMAT OF BPERROR OUTPUT:
#
# STATUS CLIENT        POLICY           SCHED      SERVER      TIME COMPLETED
#   1    rsysnbu       NBU_Master       Daily_Incr rsysnbu     06/12/2003 16:07:42 
#                  (the requested operation was partially successful)
#   0    sapqas        SAPQAS_INCR      Default-Ap sapnt2      06/12/2003 16:08:52 
#  54    sapqas        SAPQAS_INCR      Default-Ap sapnt2      06/12/2003 16:11:43 
#                 (timed out connecting to client)
# NBU Status
#   0 = Success
#  99 = (NDMP backup failure)
#  13 = (file read failed)
#  71 = (none of the files in the file list exist)
# 150 = (termination requested by administrator)
#   6 = (the backup failed to back up the requested files)
#   1 = (the requested operation was partially successful)
#  54 = (timed out connecting to client)

##
## EDIT USER CONFIGURATION
##

my $tmpdir = q{c:/temp};
my $successFile=$tmpdir . '/' . q{nbusuccess.mail};
my $failureFile=$tmpdir . '/' . q{nbufail.mail};
my $masterServer = q{NBU_MASTER_SERVER};
my $BPERROR=q{E:/VERITAS/NetBackup/bin/admincmd/bperror.exe};

$email=q{john.doe@somewhere.com,jane.buck@somewhereelse.com};


##
## END OF USER CONFIGURATION
##

# bperror output into array
#@lines = `cat bperror.txt`;
@lines = `"$BPERROR" -U -backstat -hoursago 25`;


# notification table: client, email_address


open(SUCCESS,	">$successFile") || die "$0: $!";
open(FAILED,	">$failureFile") || die "$0: $!";

my %success=();
my %failed=();

foreach my $line (@lines) {
	$line =~ s/^\s+//; # remove leading space
	next if $line =~ /^\(/;  # error lines begin with paren
	next if $line =~ /^STATUS/;  # header line from bperror

	my ($status,$client,$policy,$sched,$server,$date,$time) = split(/\s+/,$line);
	if (!$status) {
		# successful jobs
		push @{$success{$client . $policy}}, [$status,$client,$policy,$sched,$server,$date,$time];
	} else {
		next if /\(.*\)/;
		# failed jobs
		push @{$failed{$client . $policy}}, [$status,$client,$policy,$sched,$server,$date,$time];
	}
}

print SUCCESS "Completed NBU Jobs\n";
printf SUCCESS "%6s   %-15s %-20s %-15s %-15s %-22s\n",qw{STATUS CLIENT POLICY SCHED SERVER},'TIME COMPLETED';
printf SUCCESS "%-100s \n",'=' x 100;

foreach my $key ( sort keys %success ) {
	foreach my $ary (@{$success{$key}} ) {
		printf SUCCESS ("%6i   %-15s %-20s %-15s %-15s %-22s\n",
			$ary->[0],$ary->[1],$ary->[2],$ary->[3],
			$ary->[4],$ary->[5] . ' ' . $ary->[6]);
	}
}


print FAILED "FAILED NBU Jobs\n";
printf FAILED "%6s   %-15s %-20s %-15s %-15s %-22s\n",qw{STATUS CLIENT POLICY SCHED SERVER},'TIME COMPLETED';
printf FAILED "%-100s \n",'=' x 100;

foreach my $key ( sort keys %failed ) {
	foreach my $ary (@{$failed{$key}} ) {
		printf FAILED ("%6i   %-15s %-20s %-15s %-15s %-22s\n",
			$ary->[0],$ary->[1],$ary->[2],$ary->[3],
			$ary->[4],$ary->[5] . ' ' . $ary->[6]);
	}
}



close(SUCCESS);
close(FAILED);

# blat the mail and cleanup

$error = system(qq{blat $failureFile -s "Failed NBU Jobs on $masterServer" -t $email -q});
warn "blat failed to send $failureFile to $email - $!\n" if $error;

$error = system(qq{blat $successFile -s "Successful NBU Jobs on $masterServer" -t $email -q});
warn "blat failed to send $successFile to $email - $!\n" if $error;

#unlink $failureFile or warn "failed to delete $failureFile - $!\n";
#unlink $successFile or warn "failed to delete $successFile  - $!\n";

