I've changed my code to reflect your suggestion :-
#!/usr/bin/perl -w
#
use DBI ;
use strict ;
#
sub chk_cals {
#
#########################################################
# This Perl subroutine can be used to call an SQLPLUS #
# script. Should this routine run to completition a 1 #
# script by Perl. Should this routine run to completion #
# (True) is returned. A failure at any stage will exit #
# the calling script and send an appropriate Email to #
# all interested parties. #
#########################################################
#
#########################################################
# Ensure variable privacy by use of the strict pragma. #
#########################################################
#
use strict ;
use lib '/home/interface/scripts/Perl_Modules' ;
use ACC_Various ("update_report", "end_it", "mail", "mail_log") ;
use ACC_Oracle ;
#
#####################################
# Parameter Error Boolean #
#####################################
#
my $PARAMETER_ERROR = 0 ;
my $VALID_DB = 0 ;
#
#########################################################
# This routine must be supplied with 4 parameters or it #
# will fail with a parameter error. #
#########################################################
#
#####################################
# Parameter declarations #
#####################################
#
my $to = undef ;
my $from = undef ;
my $DB_name = undef ;
my $logfile = undef ;
#
#####################################
# DBI Handles #
#####################################
#
my $dbh = undef ;
my $sth = undef ;
#
#####################################
# DBI Error variables #
#####################################
#
# $DBI::err ;
# $DBI::errstr ;
# $DBI::state ;
#
#####################################
# DBI Variables (hashes) #
#####################################
#
my %attr = (
PrintError => 0,
RaiseError => 0
) ;
#
################################################
# Test that the requisite number of parameters #
# have been supplied. (There MUST be 4). #
################################################
#
if (@_ != 4) {
print "\n\tSub::run_sqlplus - MUST be supplied with minimum of 4 par
+ameters!" ;
$PARAMETER_ERROR = 1 ;
}
else {
#
###############################################
# Declare parameter names #
###############################################
#
$to = $_[0] ;
$from = $_[1] ;
$logfile = $_[2] ;
$DB_name = $_[3] ;
#
}
#
###############################################################
# Set up variables #
###############################################################
# Date and time variables for headings in Emails/logfile etc. #
###############################################################
#
my ($min, $hour, $day, $mon, $year) = (localtime) [1,2,3,4,5] ;
my $date = sprintf("%02d/%02d/%04d", $day, $mon +=1, $year += 1900);
my $time = sprintf("%02d%02d" , $hour , $min) ;
my $ptime = sprintf("%02d:%02d" , $hour , $min) ;
#
##############################
# Variables (Common scalars) #
##############################
#
my $trace_file = 'DBI_chk_cals_trace.log' ;
#
#############################
#
my $username = undef ;
my $password = undef ;
my $DB_pass = undef ;
my $ENV_info = undef ;
#
#####################################
# Array Variables #
#####################################
#
my @fields = undef ;
my @return_values = () ;
my @Valid_DB_Names = qw(isw-test isw-live isw-teach) ;
#
# Common or garden scalars
#############################
#
my $record_count = 0 ;
my $bcount = 0 ;
my $ecount = 0 ;
#
######################################
# Hash Table #
######################################
#
my %uid_HashLookup = (
'isw-live' => "/etc/oracleLIVEuid",
'isw-test' => "/etc/oracleTESTuid",
'isw-teach' => "/etc/oracleTEACHuid"
);
my %ENVHashLookup = (
'isw-live' => "ISWLIVE",
'isw-test' => "ISWTEST",
'isw-teach' => "ISWTEACH",
);
#
#####################################
# mail_log variables. #
#####################################
#
my $script = 'chk_cals' ;
my $mail_msg = 1 ;
my $null = "none" ;
my $subject = "Urgent - $script Failed!" ;
my $msg = "This should change according to failure!" ;
my $text = "\tUnable to write the following to $logfile
+ :-\n\n" ;
my $topline = "\n\tThe error message is -\n\n" ;
#
###########################################################
# Processing #
###########################################################
#
###########################################################
# Is it a valid Database name #
###########################################################
#
foreach (@Valid_DB_Names) {
if ($DB_name eq $_) {
$VALID_DB = 1 ;
}
}
#
if (! $VALID_DB) {
$msg = "Db name supplied - $DB_name - is not known to this scrip
+t!" ;
&update_report("$logfile","$msg",1,3,1) or &end_it($mail_msg,$to
+,$from,$subject,$msg,$text,$logfile) ;
print "\n\t$msg\n" ;
$PARAMETER_ERROR = 1 ;
}
else
{
$ENV_info = $ENVHashLookup{$DB_name} ;
#
###############################################################
# Check that DB_pass exists #
###############################################################
#
$DB_pass = $uid_HashLookup{$DB_name} ;
if ( !-e $DB_pass) {
$msg = "File $DB_pass does not exist!" ;
&update_report("$logfile","$msg",1,3,1) or &end_it($mail_msg,$t
+o,$from,$subject,$msg,$text,$logfile) ;
print "\n\t$msg\n\n" ;
$PARAMETER_ERROR = 1 ;
}
}
#
$msg = "Oracle/Perl DBI (chk_cals) run on $date at $ptime." ;
&update_report("$logfile","$msg",1,3,1) or &end_it($mail_msg,$to,$from
+,$subject,$msg,$text,$logfile) ;
#
###########################################################
# Set up the relevant profile #
###########################################################
#
$msg = "Failed to run ACC_Oracle_profile" ;
&ACC_Oracle_profile("$ENV_info") or &end_it($mail_msg,$to,$from,$subje
+ct,$msg,$null,$logfile) ;
#
$msg = "ACC_Oracle_profile completed for $DB_name Environment" ;
&update_report("$logfile","$msg",1,1,1) or &end_it($mail_msg,$to,$from
+,$subject,$msg,$text,$logfile) ;
#
###########################################################
# P A R A M E T E R E R R O R S #
###########################################################
#
if ($PARAMETER_ERROR) {
$msg = "\n\tScript $script fails with parameter errors!\n" ;
print "$msg" ;
print "\t*****************************************\n" ;
print "\t* Contact Analyst in Team 1 - Urgently! *\n" ;
print "\t*****************************************\n" ;
return 0 ;
}
#
#####################################################
# Read the Password file #
#####################################################
#
$msg = "Failed to OPEN $DB_pass for Input." ;
open INF, "<$DB_pass" or &end_it($mail_msg,$to,$from,$subject,$msg,
+$text,$logfile) ;
#
$msg = "File $DB_pass opened for input." ;
&update_report("$logfile","$msg",1,1,0) or &end_it($mail_msg,$to,$f
+rom,$subject,$msg,$text,$logfile) ;
#
$msg = "Processing I/P file :: $DB_pass." ;
&update_report("$logfile","$msg",1,1,0) or &end_it($mail_msg,$to,$f
+rom,$subject,$msg,$text,$logfile) ;
while (<INF>) {
$record_count ++ ;
chomp ;
@fields = split /\//, $_ ;
$username = $fields[0] ;
$password = $fields[1] ;
}
#
$msg = "Failed to CLOSE $DB_pass." ;
close INF or end_it($mail_msg,$to,$from,$subject,$msg,$text,$logfil
+e) ;
#
$msg = "Closed input file :: $DB_pass." ;
&update_report("$logfile","$msg",1,1,1) or &end_it($mail_msg,$to,$f
+rom,$subject,$msg,$text,$logfile) ;
#
if ($record_count != 1) {
$msg = "\n\tThere were too many records in the $DB_pass file!" ;
print "$msg" ;
print "\t*****************************************\n" ;
print "\t* Contact Analyst in Team 1 - Urgently! *\n" ;
print "\t*****************************************\n" ;
&end_it($mail_msg,$to,$from,$subject,$msg,$topline) ;
}
#
###########################################################
# Test for & remove any existing trace_file #
###########################################################
#
if (-e $trace_file) {
$msg = "DBI - Failed to unlink old $trace_file" ;
unlink $trace_file or
&end_it($mail_msg,$to,$from,$subject,$msg,$null,$logfile)
+;
#
###########################################################
# Update logfile #
###########################################################
#
$msg = "DBI - Old $trace_file unlinked." ;
&update_report("$logfile","$msg",1,1,0) or &end_it($mail_msg,$to,$f
+rom,$subject,$msg,$text,$logfile) ;
#
}
#
###########################################################
# Set tracing #
###########################################################
#
DBI->trace(2, $trace_file) ;
#
###########################################################
# Update logfile #
###########################################################
#
$msg = "DBI - Tracing switched on." ;
&update_report("$logfile","$msg",1,1,0) or &end_it($mail_msg,$to,$f
+rom,$subject,$msg,$text,$logfile) ;
#
###########################################################
# Connect to the DB #
###########################################################
#
$msg = "DBI - Failed to connect to $DB_name" ;
$dbh = DBI->connect( "dbi:Oracle:$DB_name", "$username", "$password
+", \%attr) or
&end_it($mail_msg,$to,$from,$subject,$msg,$null,$l
+ogfile) ;
#
###########################################################
# Update logfile #
###########################################################
#
$msg = "DBI - User $username connected to $DB_name." ;
&update_report("$logfile","$msg",1,1,0) or &end_it($mail_msg,$to,$f
+rom,$subject,$msg,$text,$logfile) ;
#
###########################################################
# Prepare SQL #
###########################################################
#
$msg = "DBI - Preparing SQL (SELECT (1)...........)." ;
&update_report("$logfile","$msg",1,1,0) or &end_it($mail_msg,$to,$f
+rom,$subject,$msg,$text,$logfile) ;
#
$msg = "\n\tDBI failed to prepare the supplied code(1)\n" ;
$sth = $dbh->prepare( "select count(*)
from O_YEARS y,
O_CALENDARS c
where c.CAL_ID = y.YRS_CAL_ID
and c.CAL_TYPE ='BUDGET'
and to_char(y.YRS_END_DATE,'YYYY') like (
+to_char(sysdate,'YYYY')+1)") or
&end_it($mail_msg,$to,,$from,$subject,$msg,$null,$logfile) ;
#
$msg = "DBI - prepared SQL (SELECT (1)..............)." ;
&update_report("$logfile","$msg",1,1,0) or &end_it($mail_msg,$to,$f
+rom,$subject,$msg,$text,$logfile) ;
#
###########################################################
# Execute the prepared SQL #
###########################################################
#
$msg = "\n\tFailed to execute SQL (SELECT (1)..............).\n" ;
$sth->execute() or
&end_it($mail_msg,$to,$from,$subject,$msg,$null,$logfile) ;
$msg = "DBI - SQL (SELECT (1)...............) executed." ;
&update_report("$logfile","$msg",1,1,0) or &end_it($mail_msg,$to,$f
+rom,$subject,$msg,$text,$logfile) ;
#
###########################################################
# Retrieve the returned rows of data #
###########################################################
#
$record_count = 0 ;
#
while ( $bcount = $sth->fetchrow_array() ) {
print "\n\tField bcount = $bcount.\n" ;
$record_count ++ ;
}
#
print "\n\tThe record count(bcount) returned was $record_count.\n"
+;
#
$msg = "DBI - Preparing SQL (SELECT (2)...........)." ;
&update_report("$logfile","$msg",1,1,0) or &end_it($mail_msg,$to,$f
+rom,$subject,$msg,$text,$logfile) ;
#
$sth = $dbh->prepare( "select count(*)
from O_YEARS y,
O_CALENDARS c
where c.CAL_ID=y.YRS_CAL_ID
and c.CAL_TYPE='EXTERNAL'
and to_char(y.YRS_END_DATE,'YYYY') like t
+o_char(sysdate,'YYYY')") or
&end_it($mail_msg,$to,,$from,$subject,$msg,$null,$logfile) ;
#
$msg = "DBI - prepared SQL (SELECT (2)..............)." ;
&update_report("$logfile","$msg",1,1,0) or &end_it($mail_msg,$to,$f
+rom,$subject,$msg,$text,$logfile) ;
#
###########################################################
# Execute the prepared SQL #
###########################################################
#
$msg = "\n\tFailed to execute SQL (SELECT (2)..............).\n" ;
$sth->execute() or
&end_it($mail_msg,$to,$from,$subject,$msg,$null,$logfile) ;
$msg = "DBI - SQL (SELECT (2)...............) executed." ;
&update_report("$logfile","$msg",1,1,0) or &end_it($mail_msg,$to,$f
+rom,$subject,$msg,$text,$logfile) ;
#
###########################################################
# Retrieve the returned rows of data #
###########################################################
#
$record_count = 0 ;
#
while ( $ecount = $sth->fetchrow_array() ) {
print "\n\tField ecount = $ecount.\n" ;
$record_count ++ ;
}
#
print "\n\tThe record count(ecount) returned was $record_count.\n"
+;
#
###########################################################
# Finish the extract tidily #
###########################################################
#
$msg = "\n\tDBI failed to FINISH tidily\n" ;
$sth->finish() or &end_it($mail_msg,$to,$from,$subject,$msg,$text,$
+logfile) ;
#
###########################################################
# Disconnect from the DB #
###########################################################
#
$msg = "\n\tDBI - Failed to disconnect from $DB_name." ;
$dbh->disconnect() or
&end_it($mail_msg,$to,$from,$subject,$msg,$null,$logfile) ;
#
###########################################################
# Update logfile #
###########################################################
#
$msg = "DBI - User $username disconnected from $DB_name." ;
&update_report("$logfile","$msg",1,1,0) or &end_it($mail_msg,$to,$f
+rom,$subject,$msg,$text,$logfile) ;
#
$msg = "<********** End Of Report **********>" ;
&update_report("$logfile","$msg",2,3,1) or &end_it($mail_msg,$to,$f
+rom,$subject,$msg,$text,$logfile) ;
#
return ($bcount,$ecount)
}
#
#######################################################
# Variables #
#######################################################
#
my $to = 'ronniec@it.aberdeen.net.uk' ;
my $from = 'Buckethead' ;
my $logfile = 'xxrc_test_DBI_special.log' ;
my $DB_name = 'isw-test' ;
my $result = 0 ;
my $count = 0 ;
my @response = () ;
#
#####################################
# mail_log variables. #
#####################################
#
my $script = 'chk_cals' ;
my $mail_msg = 0 ;
my $null = "none" ;
my $subject = "Script $script completion details" ;
my $msg = "See attachment for completions details" ;
my $text = "\tUnable to write the following to $logfile
+ :-\n\n" ;
my $row = undef ;
#
print "\n\t<************ SOR $script ***************>\n" ;
#
my ($bcount,$ecount) = &chk_cals("$to","$from","$logfile","$DB_name")
+;
#
print "\n\tScalar bcount :: $bcount\n" ;
#
print "\n\tScalar ecount :: $ecount\n" ;
#
$result = &mail_log($to,$from,$subject,$msg,$logfile) ;
#
if (! $result )
{
$msg = "\tFailed to send completions Email!\n" ;
$subject = "Urgent - $0 Failed!" ;
print "$msg" ;
print "\t*****************************************\n" ;
print "\t* Contact Analyst in Team 1 - Urgently! *\n" ;
print "\t*****************************************\n" ;
exit(99) ;
}
#
$mail_msg = 1 ;
#
print "\n\t<************ EOR $script ***************>\n" ;
but it still doesn't work. When printing out the $bcount/$ecount variables in the calling script I get the standard error -
<***** EOR ACC_Oracle_profile *****>
Field bcount = 1.
The record count(bcount) returned was 1.
Field ecount = 1.
The record count(ecount) returned was 1.
Use of uninitialized value in concatenation (.) or string at xxrcdbi.p
+l line 447
.
Scalar bcount ::
Use of uninitialized value in concatenation (.) or string at xxrcdbi.p
+l line 449
.
Scalar ecount ::
Sub::mail_log - Ends without errors.
<************ EOR chk_cals ***************>
This is the problem I'm encountering whichever method I use - any ideas?
Ronnie "Bamboozeled" Cruickshank
|