package dbautil;
$dbautil::VERSION = (split " ",'#$Id ')[3];
use strict;
use Time::localtime;
use IPC::Open3;
use Mail::Mailer;
use Digest::MD5;
# My Global
my $content;
my $ora_sid;
my $ora_home;
my $run_date;
my $this_day;
my %conf;
my $home;
$dbauitl::p;
$dbautil::ora_sid;
$dbautil::ora_home;
$dbautil::run_date;
$dbautil::debug;
$dbautil::this_day;
# Param - Oracle_SID
sub main::ReadConfigFile {
# Open and read file
$home = $ENV{'HOME'};
open (INPUT, $home."/dba.conf") || die "\n Invalid config file: ".$home."/dba.conf" ;
while () {
unless ($_=~ /^[-,#]/) {
$content = $content.$_;
}
}
close (INPUT);
# Get ORACLE_SID
$ora_sid = $_[0];
$run_date = sprintf("%02s%02s%02s", localtime->mday(), (localtime->mon()+1), localtime->year());
$this_day = substr(ctime(time),0,3);
# Eval config
eval "\%conf = ($content);";
$ora_home = $conf{$ora_sid}->{'ora_home'};
eval "\%conf = ($content);";
if (! defined ($ora_sid) or (! defined ($ora_home)) ) {
printf "Oracle enviroment missing\n";
exit (1);
}
# Set enviroment
$ENV{'PATH'} = $conf{$ora_sid}->{'path'};
$ENV{'ORACLE_SID'} = $ora_sid ;
$ENV{'ORACLE_HOME'} = $ora_home;
#
if ( defined ( $conf{$ora_sid}->{nls_lang} ) ) {
$ENV{'NLS_LANG'} = $conf{$ora_sid}->{nls_lang};
}
#
$dbautil::p = $conf{$ora_sid};
$dbautil::ora_sid = $ora_sid;
$dbautil::ora_home = $ora_home;
$dbautil::run_date = $run_date;
$dbautil::debug = ($conf{$ora_sid}->{'debug'} eq "Y" ) ? 0 : 1;
$dbautil::this_day = $this_day;
}
# Send file to dba
sub send_mail
{
my $mail;
my $mail = `/usr/bin/rmail $conf{$ora_sid}->{'dba_mail'} < $_[0]`;
}
# Send message By Fyodor@aha.ru
sub main::h_send_mail {
my ($header, $bb) = @_;
chomp $bb;
local ($,) = '';
local ($\) = '';
my %h = (%{$header});
foreach ( keys %h) {
chomp $h{$_};
chomp $h{$_};
}
$h{'Cc'} = '';
$h{'CC'} = '';
my $mailer = new Mail::Mailer('sendmail');
$mailer->open(\%h);
local ($\) = "";
print $mailer $bb;
$mailer->close;
return 1;
}
# Run command and compare output with param2
# return true if match,
sub main::RunCmd {
local (*R, *W, *E);
local ($/) = '';
my $output;
open3(\*W, \*R, \*R, $_[0]) or die "Can't run cmd ".$_[0];
while () {
$output = $output.$_;
}
close (R);
close (W);
unless ($dbautil::debug) {
print "Start cmd is \n".$_[0]."Output is \n".$output."End out put\n";
}
unless ($output =~ /$_[1]/i ) {
return 1, $output;
}
return 0, $output;
}
# Copy file to remote.Need checksum and so
# 1 param - cmd , 2 - source 3 target
sub main::cp
{
my ($cmd, $ret, $md5, $output);
$cmd = sprintf ( $_[0],$_[1], $_[2] );
unless ($dbautil::debug) {
print "CP is ".$cmd."\n";
}
open(FILE, $_[1]) || die "Can't open file".$_[1]."\n";
binmode(FILE);
$md5 = Digest::MD5->new->addfile(*FILE)->hexdigest;
close (FILE);
# Run & wait for check md5
($ret, $output) = &main::RunCmd ($cmd, $md5 );
# check for errors
if ( $ret ) {
&main::h_send_mail ({ 'From' => 'statserv',
'To' =>$dbautil::p->{'dba_mail'} ,
'Subject'=> 'Copy failed'},
$_[1].$output );
}
}
# Find latest file in directory Argv0 - directory/filemask - /backup/*.dbf
# Argv1 - days ago
sub main::FindLastFile {
my ($filename, $found, $max_ctime);
$max_ctime = 0;
unless ($dbautil::debug) {
print "Glob is ".$_[0]."\n";
}
while (glob($_[0])) {
$filename = $_;
my ($size, $ctime) = (stat($filename))[7,9];
if ( (time - int($_[1])*24*60*60) > $ctime ) {
if ( $size != 0 && $ctime > $max_ctime ) {
$max_ctime = $ctime;
$found = $filename;
}
}
}
return $found;
}
# Exit OK here
1;