#!/usr/db/bin/perl -w
#{{{ POD
# -------------------------------------------------------------------------
# $Id: fc_db_rename.pl,v 1.3.2.36 2014/04/02 08:02:29 Andreas_A_Kempf Exp $
# -------------------------------------------------------------------------
# Copyright: (c) IBM Deutschland Business Services GmbH
# Author   : $Author: Andreas_A_Kempf $
# Changes  : $Date: 2014/04/02 08:02:29 $
# Revision : $Revision: 1.3.2.36 $
# -------------------------------------------------------------------------
# $Log: fc_db_rename.pl,v $
# Revision 1.3.2.36  2014/04/02 08:02:29  Andreas_A_Kempf
# Bugfix after Test with RAC Cluster
#
# Revision 1.3.2.35  2014/02/11 07:58:34  Andreas_A_Kempf
# change ignore error Code from sqlplus  for create Redo Logs
# insert LogInfo for create SPFILE with cluster_database=true
#
# Revision 1.3.2.34  2014/02/07 11:35:21  Andreas_A_Kempf
# insert new investigation for REDO Logs
# and insert new wirte  and create REDO Logs
#
# Revision 1.3.2.33  2013/11/04 12:02:55  Andreas_A_Kempf
# Bugfix after roleout
#
# Revision 1.3.2.31  2013/09/16 09:08:12  Andreas_A_Kempf
# create controlfile with archivelog Mode
#
# Revision 1.3.2.30  2013/03/13 17:20:21  Andreas_A_Kempf
# change create spfile with mirror control file
#
# Revision 1.3.2.29  2013/03/05 10:40:00  Andreas_A_Kempf
# change pfile controlfie in create_new_controlfile
#
# Revision 1.3.2.28  2013/03/05 10:33:43  Andreas_A_Kempf
# insert mirror controlfile
#
# Revision 1.3.2.27  2012/12/10 09:06:56  de139708
# insert check, whether redo.cfg is possible
#
# Revision 1.3.2.25  2012/10/02 13:19:43  de139708
# change MAXDATAFILES from 200 to 400
#
# Revision 1.3.2.24  2012/08/10 05:19:18  de139708
# change the definition for the Produktion  Diskgroup redo and data
#
# Revision 1.3.2.23  2012/06/21 09:53:33  de139708
# modify delete REDO Log Files
# ignore SPFILE in REDO Log Directory
#
# Revision 1.3.2.22  2012/06/21 09:00:15  de139708
# modify create spfile into ASM
# inspect, is database running
# inspect is database running with spfile
#
# Revision 1.3.2.21  2012/06/21 08:33:38  de139708
# modify create spfile into ASM
#
# Revision 1.3.2.20  2012/06/20 12:48:59  de139708
# remove check error for startup with pfile by delete spfile
#
# Revision 1.3.2.19  2012/06/19 19:05:57  de139708
# change create_spfile
# insert move spfile to ASM Diskgroup
#
# Revision 1.3.2.18  2012/05/15 10:01:59  de139708
# change for SSCD
#
# Revision 1.3.2.17  2012/05/14 13:40:33  de139708
# change for Produktion
#
# Revision 1.3.2.15  2012/05/10 13:00:33  de139708
# change for RAC Flashcopy
# insert add logfile thread 4
#
# Revision 1.3.2.14  2012/05/08 17:43:00  de139708
# remve alter add redolog for create clone Controlfile
#
# Revision 1.3.2.13  2012/05/08 09:01:30  de139708
# first check in into CVS
#
# Revision 1.3.2.12  2012/04/27 08:55:10  de139708
# finale for SCDI. Change Diskgroupname
#
# Revision 1.3.2.11  2012/04/27 06:43:42  de139708
# rename scdp_mount to scdp_startup_mount
#
# Revision 1.3.2.10  2012/04/27 06:14:09  de139708
# remove mirror and change the flashcopy process
#
# Revision 1.3.2.7  2012/04/03 13:28:15  de139708
# insert changes for Flashcopy SCDI
#
# Revision 1.3.2.5  2012/03/22 13:59:48  de139708
# remove use RCM::MakeScript;
# this make a XML:DOM error
#
# Revision 1.3.2.4  2012/03/22 11:35:17  de139708
# removed use XML::DOM;
#
# Revision 1.3.2.3  2012/03/22 11:02:32  de139708
# insert test, is bcvop successfully started
#
# Revision 1.3.2.2  2012/03/22 06:22:19  de139708
# released Version off start 2.0
#
# Revision 1.3  2012/03/14 19:54:02  de139708
# fix man bugfies
# insert check routine
#
# Revision 1.2  2012/03/09 14:37:45  de139708
# insert execute tasknumber and auto run funktion. Ask for y / n for run Task
#
# Revision 1.1  2012/02/28 14:45:54  de139708
# new load
#
# -------------------------------------------------------------------------
# -*-perl-*-
# vim:ai:et:ts=4:sw=4:sts=4:tw=72:spell:spelllang=en_us
#}}}
use strict;
use Pod::Text;
use IPC::Open2;
use File::Copy;
use lib qw( /usr/db/RCM/Classes );
use lib qw( /applications/oracle/scripts/2.0/perllib );
use lib qw( /applications/oracle/scripts/2.0/perllib/rcm );
use lib qw( /applications/DWSfc/lib);
use lib "/applications/oracle/scripts/2.0/perllib/$^O";

use ORAenv;		# Must be the first module to load!
use ORAlog;
use ORArcm;
use ORAocw;
use Data::Dumper;
use Getopt::Long;
use POSIX;
use File::Basename;
#use DWSfcLog;
use DWSsf;
use vars qw($rcm);
use vars qw($opt_d $dbflag $oratab_path $isClusterNode);
use vars qw($opt_abort %opt_instlist);

use ORAtools;
use ORAocw;

# receive message from requests
my $errortxt;
my @messages;
my @rslt;
# flag to indicate whether the getconfig request was successful
my $gotdata = 0;
my $sf = DWSsf -> new ();

###########################{{{DIE
# DIE
###########################
$SIG{__DIE__} = sub {
	print "error: @_\n";
	exit 1;
};
#}}}
#{{{ init
###########################
# unset ENV
###########################
delete $ENV{'LANG'};

###########################
#
# get opts
###########################
my ($debug,$go);
my $hostname = qx[uname -n];
chomp($hostname);
my ($opt_help,$help,$DB_NAME,$interactive,$opt_d,$exit_code,$auto,$tasknumber,$all);
my ($mount_database,$change_DBID);
my $LogFile;
my $mirror_nr;
my $print;
my $start;
my $stop;

my $optctl = GetOptions (
	"debug|d" => \$opt_d,
	"dbname=s" => \$DB_NAME,
	"print" => \$print,
	"help|h" => \$help,
	"interactive|i" => \$interactive,
	"start=s" => \$start,
	"stop=s" => \$stop,
	"auto|a" => \$auto,
	"mount_database" => \$mount_database,
	"change_DBID" => \$change_DBID,
	"exit_code=s" => \$exit_code,
	"tasknumber|t=s" => \$tasknumber,
	"all" => \$all,
	);
	
if ($opt_d) {
	$debug = "1";
} else {
	$debug = "0";
};
$sf->init('debug' => "$debug");

if ($help) { pod2text($0); exit 0;};
unless($DB_NAME){
	pod2text($0);
	exit 1;
}

$exit_code = "1" unless($exit_code);
$auto = "0" unless($auto);
$LogFile = "" unless($LogFile);
$DB_NAME = uc($DB_NAME);

###########################
# date / time
###########################
my $expr	= 900;					# Zeit f�r ablauf
my $expr_sec 	= time() - $expr;			# aktuelle Zeit in sec - $expr
my $date_sec 	= time();				# aktuelle Zeit in sec

my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$mon+=1;
if ($sec < 10) { $sec = "0$sec"; }
if ($min < 10) { $min = "0$min"; }
if ($hour < 10) { $hour = "0$hour"; }
if ($mon < 10) { $mon = "0$mon"; }
if ($mday < 10) { $mday = "0$mday"; }
$year = $year+1900;

my $time_stamp = "$year-$mon-$mday". "__". "$hour.$min.$sec";
#}}}
##########################{{{ main
main: {
###########################
	#{{{ read mapfile
	my ($dbid,$admin_host,$prefix,$member,$order,$db,$db_instance,$Instance_prefix,$mirror);
	my ($hash_1);
	my $arg_dat_file =  "/applications/DWSfc/conf/DWS_map_FC.txt.develop";
	my $SCDP = "0";

	open (READ_CONFIG, $arg_dat_file) || die "(func_read) ERROR: Could not open the config / mapping file: $!";
	while (<READ_CONFIG>) {
		my $line  = $_;
		chomp($line);
		next if ( $line =~ /^[ 	]*$/ or $line =~ /^[ 	]*#/);
		if($line =~ /^([\d+\w+]*)\s+([\d+\w+]*)\s+([\d+\w+|-]*[:]?[\d+\w+]*)\s+([\d+\w+]*)\s+([\d+\w+]*)\s+([\d+\w+]*)\s+([\d+\w+]*)\s+([\d+\w+]*)\s+([\d+\w+]*)\s+([\d+\w+]*)\s+([\d+\w+]*)\s+([\d+\w+|-]*)\s+([\d+\w+|-]*)/){
			$admin_host = $1;
			$db = $2;
			$prefix = $3;
			$mirror = $4;
			$member = $5;
			$order = $6;
			$db = uc($db);
			$Instance_prefix = $11;
			$dbid = $13;

			if($db eq "$DB_NAME"){
				if($mirror eq "primary"){
					$mirror_nr = "2";	
				} elsif($mirror eq "mirror"){
					$mirror_nr = "1";
				} else {
					$mirror_nr = "0";
				}
			
				if("$hostname" eq "$admin_host"){
					$db_instance = $db ."1";
				} elsif ($hostname eq "$member") {
					$db_instance = $db ."2";
				}
				$sf->init('DB_NAME' => "$DB_NAME",
					'LogFile' => "$LogFile",
					'db_instance' => "$db_instance",
				);

				if($prefix =~ /(.)(.):(.)(.)/){
					$hash_1->{ "1" } =  {} unless exists($hash_1->{ "01" });
					$hash_1 -> {"1" } = {
						'prefix'  => "$1",
						'type'  => "$2",
						'Instance_prefix'  => "$Instance_prefix",
						'Instance'  => "$db_instance",
						'order'  => "$order",
						'dbid'  => "$dbid",
						'DB_NAME'  => "$DB_NAME",
					};
					$hash_1 -> {"2"} = {
						'prefix'  => "$3",
						'type'  => "$4",
						'Instance_prefix'  => "$Instance_prefix",
						'Instance'  => "$db_instance",
						'order'  => "$order",
						'dbid'  => "$dbid",
						'DB_NAME'  => "$DB_NAME",
					};
				} elsif ($prefix =~ /(.)(.)/ && $prefix !~ /RAC/){
					$hash_1->{ "1" } =  {} unless exists($hash_1->{ "01" });
					$hash_1 -> {"1" } = {
						'prefix'  => "$1",
						'type'  => "$2",
						'Instance_prefix'  => "$Instance_prefix",
						'Instance'  => "$db_instance",
						'order'  => "$order",
						'dbid'  => "$dbid",
						'DB_NAME'  => "$DB_NAME",
					};
				} elsif ($prefix =~ /RAC/){
					$hash_1->{ "1" } =  {} unless exists($hash_1->{ "01" });
					$hash_1 -> {"1" } = {
						'prefix'  => "",
						'type'  => "m",
						'Instance_prefix'  => "$Instance_prefix",
						'Instance'  => "$db_instance",
						'order'  => "$order",
						'dbid'  => "$dbid",
						'DB_NAME'  => "$DB_NAME",
					};
					$hash_1 -> {"2"} = {
						'prefix'  => "",
						'type'  => "p",
						'Instance_prefix'  => "$Instance_prefix",
						'Instance'  => "$db_instance",
						'order'  => "$order",
						'dbid'  => "$dbid",
						'DB_NAME'  => "$DB_NAME",
					};
				}
			}
		}
	}
	close(READ_CONFIG);
	$sf->log( "msg" => "db_instance for $hostname not found ", "Level" => ERROR) unless ($db_instance);
	print "Dump: hash_1\n"if($debug);
	print Dumper($hash_1)if($debug);

	#}}}
my $is_ocw_installed = ORAocw::is_ocw_installed($db_instance);
my $execution_plan;

$execution_plan = { 	'10'	=>	"--dbname $DB_NAME --stop database",
			'20'	=>	"--dbname $DB_NAME --mount_database",
			'30'	=>	"--dbname $DB_NAME --change_DBID",
			'50'	=>	"--dbname $DB_NAME --stop database",
			'60'	=>	"--dbname $DB_NAME --start database ",
		};
		
unless($is_ocw_installed){
	$execution_plan -> {'10'} = "--dbname $DB_NAME --stop instance";
	$execution_plan -> {'50'} = "--dbname $DB_NAME --stop instance";
	$execution_plan -> {'60'} = "--dbname $DB_NAME --start instance";
}
#{{{ print menu
	if($print){
		system("clear");
		printf "%-5s | %-25s\n", "Nr.","task";
		print "------+------------------------------------------------------\n";
		for my $key ( sort {$a<=>$b} keys (%$execution_plan)) {
			my $task = $execution_plan -> {"$key"};
			printf "%-5s | %-25s\n", $key,$0 ." ". $task;
		}
		exit 0;
	}

	if($interactive){
		while (1) {
			printf "%-5s | %-25s\n", "Nr.","task";
			print "------+------------------------------------------------------\n";
			for my $key ( sort {$a<=>$b} keys (%$execution_plan)) {
				my $task = $execution_plan -> {"$key"};
				printf "%-5s | %-25s\n", $key,$0 ." ". $task;
			}
			print "Press keys to see their Task Number execution.  Use Ctrl-C to quit.\n";
			my $key = <STDIN>;
			chomp ($key);
			if ( exists ($execution_plan ->{ "$key" })){
				run_task('key' => $key, 'execution_plan' => $execution_plan,'auto' => $auto);
			}elsif($key =~ /(\d+)-(\d+)/) {
				my $start = $1;
				my $end = $2;
				my $key;


				for(my $key = $start; $key <= $end; $key++) {
					run_task('key' => $key, 'execution_plan' => $execution_plan,'auto' => $auto);
				}
			}
		}
		exit 0;
	}

	if($tasknumber){
		my @array=split(/,/,$tasknumber);
		my $count = @array;

		if($count > "1"){
			foreach my $key (@array){
				run_task('key' => $key, 'execution_plan' => $execution_plan,'auto' => $auto);
			}
		} elsif($tasknumber =~ /(\d+)-(\d+)/) {
			my $start = $1;
			my $end = $2;
			my $key;

			for(my $key = $start; $key <= $end; $key++) {
				run_task('key' => $key, 'execution_plan' => $execution_plan,'auto' => $auto);
			}
		} else {
			my $task = $execution_plan -> {"$tasknumber"};
			run_task('key' => $tasknumber, 'execution_plan' => $execution_plan,'auto' => $auto);
		}
		exit 0;
	}

	if($all){
		for my $key ( sort {$a<=>$b} keys (%$execution_plan)) {
			my $task = $execution_plan -> {"$key"};
			printf "%-5s | %-25s\n", "Nr.","task";
			print "------+------------------------------------------------------\n";
			printf "%-5s | %-25s\n", $key,$0 ." ". $task;
			$sf->log( "msg" => "start:". $key." ".$task, "Level" => INFO);
			my $pid = open2(\*Reader, \*Writer, "$0 $task") or error_exit "Could not launch command '$0 $task'!";
			my (@result,$rc);

			while(<Reader>) {
				chomp $_;
				$sf->log( "msg" => "$_", "Level" => DEBUG);
				push @result, $_;
			}

			close Writer;
			close Reader;
			my $wpid = waitpid $pid, 0;
			$rc = $? >> 8;
			if($rc){
				if($rc eq "100"){
					print join("\n",@result);
					print "------+------------------------------------------------------\n";
					$sf->log( "msg" => "ok:". $key." ".$task, "Level" => INFO);
				} else {
					print "Task: $key $0 $task failed\n".join("\n",@result);
					print "------+------------------------------------------------------\n";
					$sf->log( "msg" => "faild:". $key." ".$task, "Level" => INFO);
					exit $rc;
				}
			} else {
				$sf->log( "msg" => "ok:". $key." ".$task, "Level" => INFO);
			}
			print "------+------------------------------------------------------\n";
		}
		exit 0;
	}
#}}}

		my $rc = "0";
		$Instance_prefix = $hash_1 -> {"1"}->{"Instance_prefix"};
		$Instance_prefix = "" unless($Instance_prefix);

		if($start){
			if($start eq "database"){
				ORAtools::start_ora_database($db_instance);
			} elsif ($start eq "instance"){
				start_instance('DB_INSTANCE_NAME' => $db_instance, 'exit_code' => $exit_code);
			} else {
				print "Task $start is not defined\n";
				pod2text($0);
				exit 1;
			}
		} elsif ($stop){
			if($stop eq "database"){
				ORAtools::stop_ora_database($db_instance);
			} elsif ($stop eq "instance"){
				stop_instance('DB_INSTANCE_NAME' => $db_instance, 'exit_code' => $exit_code);
			} else {
				print "Task $stop is not defined\n";
				pod2text($0);
				exit 1;
			}
		} else {
			if($mount_database){
				$rc = mount_database('DB_INSTANCE_NAME' => $db_instance, 'exit_code' => $exit_code);
			} elsif ($change_DBID){
				$rc = change_DBID('Object' => $hash_1, 'exit_code' => $exit_code);
			} else {
				print "give the objekt database or diskgroup for \n";
				pod2text($0);
				exit 1;
			}
		}

		$rc = "0" unless($rc);
		exit $rc;
	#$rc= $sf->query_split('DB_NAME' => $DB_NAME, 'hash_1' => $hash_1);
#}}}
###########################
} # END from MAIN
###########################
#{{{ stop_instance
sub stop_instance {
	my (%arg) =  @_;
	my ($DB_INSTANCE_NAME) = $arg{'DB_INSTANCE_NAME'};
	my ($exit_code) = $arg{'exit_code'};

	ORAtools::stop_ora_db($DB_INSTANCE_NAME);
	return($exit_code);
}
#}}}
#{{{ start_instance
sub start_instance {
	my (%arg) =  @_;
	my ($DB_INSTANCE_NAME) = $arg{'DB_INSTANCE_NAME'};
	my ($exit_code) = $arg{'exit_code'};
	$sf->log( "msg" => "start: $DB_INSTANCE_NAME","Level"  => INFO);

	ORAtools::start_ora_db($DB_INSTANCE_NAME);
	return($exit_code);

}
#}}}
#{{{ run_task
sub run_task {
	my (%arg) =  @_;
	my ($key) = $arg{'key'};
	my ($execution_plan) = $arg{'execution_plan'};
	my ($auto) = $arg{'auto'};
	my $go = "0";
	my $msg;
	my $reply = "0";
	$auto = "0" unless($auto);
	return unless ( exists ($execution_plan ->{ "$key" }));

	printf "%-5s | %-25s\n", "Nr.","task";
	print "------+------------------------------------------------------\n";
	my $task = $execution_plan -> {"$key"};
	printf "%-5s | %-25s\n", $key,$0 ." ". $task;
	print "------+------------------------------------------------------\n";
	unless($auto){
		print "Press (y|Y) to start this Task\n";
		print "Press (e|E) to exit (abort the complete Script)\n";
		print "Press (n|N) to skip this Task: ";

		while ($reply !~ /[yYnNeE]/ ) {
			$reply = <STDIN>;
			chop ($reply);
		}
	} else {
		$reply = "y";
	}

	if ($reply =~ /[yY]/){
		$sf->log( "msg" => "start:". $key." ".$task, "Level" => INFO);
		
		my $pid = open2(\*Reader, \*Writer, "$0 $task --exit_code $key") or error_exit "Could not launch command '$0 $task'!";
		my (@result,$rc);

		while(<Reader>) {
			chomp $_;
			$sf->log( "msg" => "$_", "Level" => DEBUG);
			push @result, $_;
		}

		close Writer;
		close Reader;
		my $wpid = waitpid $pid, 0;
		$rc = $? >> 8;
		if($rc){
			if($rc eq "100"){
				print join("\n",@result);
				print "------+------------------------------------------------------\n";
				$sf->log( "msg" => "ok:". $key." ".$task, "Level" => INFO);
			} else {
				print join("\n",@result);
				print "Task: $key failed: code:$rc\n";;
				print "------+------------------------------------------------------\n";
				$sf->log( "msg" => "faild:". $key." ".$task, "Level" => ERROR);
			}
		} else {
			$sf->log( "msg" => "ok:". $key." ".$task, "Level" => INFO);
		}
	} elsif ($reply =~ /[nN]/){
		print "skip the Task: $key \n";
		print "------+------------------------------------------------------\n";
	} elsif($reply =~ /[eE]/){
		$sf->log( "msg" => "abort Script:", "Level" => ERROR);
	}

	return();
}
#}}}
#{{{ mount_database
sub mount_database {
	my (%arg) =  @_;
	my ($DB_INSTANCE_NAME) = $arg{'DB_INSTANCE_NAME'};
	my ($exit_code) = $arg{'exit_code'};
	$sf->log( "msg" => "mount_database: $DB_INSTANCE_NAME","Level"  => INFO);

	my ($errcount,@result);
	my (@known_ora_error,@sql,@lines);
	my $go = "0";

	@known_ora_error = ('');
	@sql = ("set linesize 1000", "select 'SQLOUT:' || STATUS, DATABASE_STATUS, INSTANCE_ROLE, ", "THREAD#, ACTIVE_STATE from v\\\\\\\$instance;");
	($errcount, @result) = ORAtools::run_sql_command $DB_INSTANCE_NAME, "", @sql;

	if($errcount != 0) {
		foreach(@result) {
			chomp $_;
			ORAtools::dbout $_ if not $_ =~ /^\s*$/;
		}
	}

	@lines = grep /^SQLOUT:/, @result;
	my $linecount = @lines;
	if($linecount != 0) {
		print "-------------- Instance state is not down -------------\n";
		print "----------------- please stop database ----------------\n\n";
		return 3;
	}
	
	@sql = ("startup mount;");
	($errcount,@result) = ORAtools::run_sql_command($DB_INSTANCE_NAME, @known_ora_error,@sql);

	@sql = ("set linesize 1000", "select 'SQLOUT:' || STATUS, DATABASE_STATUS, INSTANCE_ROLE, ", "THREAD#, ACTIVE_STATE from v\\\\\\\$instance;");
	($errcount, @result) = ORAtools::run_sql_command $DB_INSTANCE_NAME, "", @sql;

	if($errcount != 0) {
		foreach(@result) {
			chomp $_;
			ORAtools::dbout $_ if not $_ =~ /^\s*$/;
		}
	}

	@lines = grep /^SQLOUT:/, @result;
	my ($istatus, $dbstatus, $irole, $thread, $active) = ($lines[0] =~ /SQLOUT:(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)/);
	if($istatus !~ /MOUNTED/){
		print "--------------- Instance is not MOUNTED ---------------\n";
		return 2;
	}
	#print "--------------- Instance status: $istatus\n";
	return(0);

}
#}}}
#{{{ change_DBID
sub change_DBID {
	my (%arg) =  @_;
	my ($Object) = $arg{'Object'};
	my ($exit_code) = $arg{'exit_code'};
	my $dbid = $Object  -> {"1"} ->  {"dbid"};
	my $DB_INSTANCE_NAME = $Object  -> {"1"} ->  {"Instance"};
	my $DB_NAME = $Object  -> {"1"} ->  {"DB_NAME"};
	my $startup = "0";
	
	my ($errcount,@result);
	my (@known_ora_error,@sql,@lines);
	my $go = "0";

	my $get_status = get_status('DB_NAME' => $DB_NAME);
	if($get_status eq "2"){
		if($dbid){
			@known_ora_error = ('');
			@sql = ("@/applications/DWSfc/sql/change_dbid.sql $dbid;");
			($errcount, @result) = ORAtools::run_sql_command $DB_INSTANCE_NAME, "", @sql;
		
			@lines = grep /info: dbid old and new ist equal/, @result;
			my $linecount = @lines;
			if($linecount >= 1) {
				print "info: dbid old and new ist equal\n";
				print "nothing to change\n";
			} else {
				$startup = "1";
				foreach(@result) {
					print $_;
				}
			}
		}
		if($startup){
			@sql = ("alter database open resetlogs;");
			($errcount, @result) = ORAtools::run_sql_command $DB_INSTANCE_NAME, "", @sql;
			foreach(@result) { print $_; }
		} else {
			@sql = ("alter database open;");
			ORAtools::run_sql_command $DB_INSTANCE_NAME, "", @sql;
		}
		my $get_status = get_status('DB_NAME' => $DB_NAME);
		print "\n";
		return(100);
	} else {
		print "Database is not in mount state \n";
	}
	return 1;

}
#}}}
#{{{ get_status
sub get_status {
	my (%arg) =  @_;
	my ($DB_NAME) = $arg{'DB_NAME'};
	my ($exit_code);
	my $errcount;
	my @result;
	my @sql = ("set linesize 1000", "select 'SQLOUT:' || STATUS, DATABASE_STATUS, INSTANCE_ROLE, ", "THREAD#, ACTIVE_STATE from v\\\\\\\$instance;");

	($errcount, @result) = ORAtools::run_sql_command $DB_NAME, "", @sql;

	if($errcount != 0) {
		foreach(@result) {
			chomp $_;
			ORAtools::dbout $_ if not $_ =~ /^\s*$/;
		}
		#ORAtools::error_exit "Could not query instance state!";
	}

	my @lines = grep /^SQLOUT:/, @result;

	my $linecount = @lines;
	if($linecount != 1) {
		print "-------------- Instance state down -------------\n";
		return 3;
	}
	my ($istatus, $dbstatus, $irole, $thread, $active) = ($lines[0] =~ /SQLOUT:(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)/);
	if($istatus =~ /MOUNTED/){
		print "-------------- Instance state MOUNTED -------------\n";
		return 2;
	}
	print "-------------- Instance state -------------\n";
	printf "Instance: %-20s Status: %s\n", $DB_NAME, $istatus;
	printf "DB state: %-20s Role:   %s\n", $dbstatus, $irole;
	printf "Thread#:  %-20d Activation state: %s\n", $thread, $active;
	print "\n";

	return (100) ;

}
#}}}
##############################{{{ POD

=pod

=head1 NAME

change_dbid.pl

=head1 SYNOPSIS

rename the DBID

=head1 DESCRIPTION

rename the DBID

=head1 OPTIONS

=item B<--print print all steps for the Database>

=item B<--interactive / -i start interactive>

=item B<--tasknumber / -t number from the Task. Use --print for show Task>

=item B<--all  start all Tasks>

=over 4

=item B<--start / --stop database>

=item B<--start / --stop instance>

=item B<--mount_database>

=item B<--change_dbid>

=over 4

=item B<--debug/-d Debug Modus>

=item B<--SID/-s Database>

=item B<--help/-h help page>

=back

=head1 EXAMPLE

=item B<change_dbid --dbname SCDT3P7 --stop database>

=item B<change_dbid --dbname SCDT3P7 --mount_database>

=item B<change_dbid --dbname SCDT3P7 --change_dbid>

=item B<change_dbid --dbname SCDP --start instance>

=item B<change_dbid --dbname SCDT3P7 --stop instance>

=item B<change_dbid --dbname SCDT3P7 --start diskgroup>

=item B<change_dbid --dbname SCDT3P7 --start database>

=item B<change_dbid --dbname SCDT3P7 -t 20,30>

=over 4

=head1 ENVIRONMENT

standard environment of the underlying OS

=head1 BUGS

none so far

=head1 AUTHOR

S<Andreas Kempf>,
Last revision by:

$Author: Andreas_A_Kempf $
$Revision: 1.3.2.36 $
$Date: 2014/04/02 08:02:29 $

=cut

#}}}
