File: //opt/OV/bin/oadbutil.pl
#!/opt/OV/nonOV/perl/a/bin/perl
use Data::Dumper;
use strict;
use Getopt::Long;
use Scalar::Util qw(looks_like_number);
my ($cmd, $res);
my ($OvInstallDir, $OvDataDir, $ddflbdFile, $OADBFolder, $OADB_MF) ;
my (@dataSource, @logFile, @classes) ;
my $verbose=0;
my $silent=0;
my $OS = $^O;
my $win64 = 0;
my $startOAcore = 0;
sub usage() {
print " USAGE : \n",
" -d|datasource <value> [-c|class <value>] \n\n";
print "Cleans up meta data and records of given datasource and class.\n";
print "If only datasource is specified, all classes under the datasource are removed.\n";
}
sub setOAPath {
if ( $OS =~ m/^MSWin/ ) {
my $instdir = $ENV{'OvInstallDir'};
$ENV{PATH} = "$ENV{PATH}:${instdir}bin";
my $parch= $ENV{'PROCESSOR_ARCHITECTURE'};
if ( $parch =~ m/^AMD64/ ) {
$ENV{PATH} = "$ENV{PATH}:${instdir}bin\\win64";
$win64 = 1;
}
} else {
my $uname=`uname -s`;
if ( $uname =~ m/AIX/ ) {
$ENV{PATH} = "$ENV{PATH}:/usr/lpp/OV/bin:/usr/lpp/perf/bin";
} else {
$ENV{PATH} = "$ENV{PATH}:/opt/OV/bin:/opt/perf/bin";
}
}
}
sub GetDataDir {
open (OVPATHEXE, "ovpath -datadir|") or die("unable to run ovpath binary: \n");
my $out = <OVPATHEXE>;
close OVPATHEXE;
chomp $out;
$out = $out . '/';
return $out;
}
sub GetInstallDir {
open (OVPATHEXE, "ovpath -instdir|") or die("unable to run ovpath binary: \n");
my $out = <OVPATHEXE>;
close OVPATHEXE;
chomp $out;
$out = $out . '/';
return $out;
}
sub GetOutputVal {
my ($cmd) = "@_" ;
open (FD, "$cmd|") or die("unable to run cmd: \n");
my $out = <FD>;
close FD;
chomp $out;
return $out;
}
sub ExecuteOACmd {
my $cmdLine = @_[0];
my $out = "";
chomp ($cmdLine);
my @args = split(/ /,$cmdLine) ;
my $n = @args;
if ($n < 1) { die(" Error : cmd is empty.\n"); }
my ($exCmd) = "";
if ($win64) {
($exCmd) = "\"". $OvInstallDir . "bin/win64/" . $args[0] ."\"" ;
} else {
($exCmd) = "\"". $OvInstallDir . "bin/" . $args[0] ."\"" ;
}
foreach my $index (1..$#args) {
$exCmd = $exCmd . " " . $args[$index];
}
if($verbose){
print "Executing : $exCmd" ;
}
open (FD, "$exCmd|") or die("unable to run cmd: \n");
while (<FD>)
{
my $outLine = $_;
$out = $out . $outLine;
}
if($verbose){ print "Result:\n$out\n" ; }
close FD;
return $out;
}
sub restartoacore {
#Step 5 : Restart oacore
if($startOAcore) {
if (!$silent){ print "Starting oacore\n"; }
$cmd = "ovc -start oacore";
$res = &ExecuteOACmd($cmd);
}
}
sub GetConfigVal {
my $namespace = @_[0];
my $confvar = @_[1];
$cmd = "ovconfget " . $namespace . " " . $confvar;
$res = &ExecuteOACmd($cmd);
chomp($res);
return $res;
}
#Read input arguments
my $help = '';
my $dsn = '';
my $mc = '';
my $dsnId = '';
my $mcId = '';
my $silent = '';
my $force = '';
my $cla = GetOptions("datasource|d=s" => \$dsn, "class|c=s" => \$mc, "help|h|?" => \$help, "force|f" => \$force, "silent|s" => \$silent) or usage ();
if ($help) {
usage ();
exit (0);
}
if (!$dsn){
print " Error : Data source name is empty.\n";
usage ();
exit (0);
}
&setOAPath;
$OvInstallDir = &GetInstallDir ;
$OvDataDir = &GetDataDir ;
if ( $OS =~ m/^MSWin/ ) {
$OADBFolder = $OvDataDir."\\databases\\oa\\";
$OADB_MF = $OADBFolder."oa.db";
}
else{
$OADBFolder = $OvDataDir."databases/oa/";
$OADB_MF = $OADBFolder."oa.db";
}
if($verbose){
print "InstallDir $OvInstallDir \n";
print "DataDir $OvDataDir \n";
print "OADBFolder = $OADBFolder \n";
print "OADB_MF = $OADB_MF \n";
print "Data source = $dsn \n";
print "Class = $mc \n";
}
my $confvalue = GetConfigVal("coda","SSL_SECURITY");
$cmd = "ovc -status oacore";
$res = &ExecuteOACmd($cmd);
chomp($res);
if ($res){
my @resStrs = split(/ /,$res) ;
my $len=@resStrs;
if( $len > 0 ){
my $status = $resStrs[$len-1];
if (lc($status) eq lc("Running")) {
$startOAcore = 1;
}
if($startOAcore) {
if($confvalue eq "ALL") {
$cmd = "ovcodautil -showds -https";
}
else {
$cmd = "ovcodautil -showds";
}
$res = &ExecuteOACmd($cmd);
my @dslines = split(/\n/,$res) ;
my $dsFound=0;
foreach my $dsline (@dslines) {
chomp ($dsline);
if (lc($dsline) eq lc($dsn)) {
$dsFound=1;
last;
}
}
if( $dsFound == 0 )
{
print " Error : Invalid data source - $dsn.\n";
exit(-1);
}
if($mc)
{
if($confvalue eq "ALL") {
$cmd = "ovcodautil -obj -ds " . $dsn . " -o " . $mc . " -https";
}
else {
$cmd = "ovcodautil -obj -ds " . $dsn . " -o " . $mc ;
}
$res = &ExecuteOACmd($cmd);
my @mclines = split(/\n/,$res) ;
my $mcFound=0;
foreach my $mcline (@mclines) {
chomp ($mcline);
if ($mcline =~ /^NumMetrics/ ) {
$mcFound=1;
last;
}
}
if( $mcFound == 0 )
{
print " Error : Invalid Class - $mc.\n";
exit(-1);
}
}
}
}
if(!$force){
my $answer="";
print "Warning: All the performance information and data will be erased for the data source - $dsn ";
if ($mc){ print " and class - $mc. "; }
print ".\nDo you really want to proceed? y/n : ";
chomp(my $answer = <>);
#print "\n";
if( lc $answer ne "y"){
exit (-1);
}
}
if($startOAcore) {
if (!$silent){ print "Stopping oacore\n"; }
$cmd = "ovc -stop oacore";
$res = &ExecuteOACmd($cmd);
}
else {
if (!$silent){ print "oacore is not Running, will not be restarted \n"; }
}
}
#Step 2 : Read Meta Info
$cmd = "sqlite3 \"$OADB_MF\" \"select Id, Name from Domains where UPPER(Name) = UPPER('$dsn'); \"";
$res = &ExecuteOACmd($cmd);
chomp($res);
my @dataRow = split /\|/, $res;
#print Dumper(@dataRow);
my $n = @dataRow;
if ($n != 2)
{
print " Error : Invalid data source - $dsn.\n";
restartoacore();
exit(-1);
}
my $dIdStr = $dataRow[0];
my $dStr = $dataRow[1];
chomp ($dIdStr);
chomp ($dStr);
$dsnId = $dIdStr;
if($verbose){ print " Data source Info from Database is : $dsnId and $dStr \n"; }
if (!looks_like_number($dsnId) )
{
print " Error : Invalid Data source - $dsn.\n";
restartoacore();
exit(-1);
}
if ($mc){
$cmd = "sqlite3 \"$OADB_MF\" \"select Id, Name from ManagedClasses where DomainId = '$dsnId' and UPPER(Name) = UPPER('$mc'); \"";
}
else{
$cmd = "sqlite3 \"$OADB_MF\" \"select Id, Name from ManagedClasses where DomainId = '$dsnId'; \"";
}
$res = &ExecuteOACmd($cmd);
chomp($res);
if ($mc){
if (!$res){
print " Error : Invalid Class - $mc.\n";
restartoacore();
exit(-1);
}
}
my $mcIdList="";
my $mcStr = "";
my $mcIdStr = "";
my @lines = split(/\n/,$res) ;
my $lineIndex=0;
foreach my $line (@lines) {
my @dataRow = split /\|/, $line;
#print Dumper(@dataRow);
my $n = @dataRow;
if ($n != 2) {
print " Error : Invalid Class - $mc.\n";
restartoacore();
exit(-1);
}
$mcIdStr = $dataRow[0];
$mcStr = $dataRow[1];
chomp ($mcIdStr);
chomp ($mcStr);
if($lineIndex) { $mcIdList = $mcIdList . ",".$mcIdStr; }
else { $mcIdList=$mcIdStr; }
$lineIndex++;
}
if($verbose){ print "Metric Id List : $mcIdList" ; }
#Step 3 : Delete meta Information
$cmd = "sqlite3 \"$OADB_MF\" \"BEGIN TRANSACTION; ";
if( $mcIdList ne "") {
$cmd = $cmd . " DELETE FROM Metrics WHERE BelongsToClassId IN ( $mcIdList ) ; " ;
$cmd = $cmd . " DELETE FROM ManagedClasses WHERE ID IN ( $mcIdList ) ; " ;
}
if (!$mc){ $cmd = $cmd . " DELETE FROM Domains WHERE ID = $dsnId ; " ; }
$cmd = $cmd . " END TRANSACTION; \" " ;
if (!$silent){ print "Cleanning Meta Information of ". $dStr . "::" . $mcStr ."\n" } ;
if($verbose){ print "Executing $cmd \n"; }
$res = &ExecuteOACmd($cmd);
#Step 4 : Delete db files
my $dir = $OADBFolder;
opendir(DIR, $dir) or die $!;
my $fPattern="";
if ($mc){
$fPattern = "dml_".$dStr."__".$mcStr;
}else{
$fPattern = "dml_".$dStr."__";
}
my @dots
= grep {
/$fPattern/
} readdir(DIR);
# Loop through the array printing out the filenames
my $fCount = @dots;
if( $fCount > 0 && (!$silent) ) { print "Removing Files : \n"; }
foreach my $file (@dots) {
my $fileToDel = "$OADBFolder" . $file;
if (!$silent){ print " $fileToDel \n"; }
if($verbose){ print "File To Delete - $fileToDel \n"; }
unlink $fileToDel or warn "Could not unlink $fileToDel: $!";
}
closedir(DIR);
#Step 5 : Restart oacore
restartoacore();
exit (0);