DB2 ist ein Produkt der
IBM Corporation. Bitte Copyright- und Trademark-Hinweise beachten!
#!/usr/local/bin/perl -w
################################################################################
# xmpTBSCList.pl - List Tablespace und Tablespace Container Informationen
# Called by - Command Level oder DBA Scripts
# Calls - xmpPerl.pm - xmp DBA Perl Package
# Getopt - Argument Verwaltung
# Env - Zugriff auf Umgebungsvariablen
# English - Aliase für Perl Variablen
# Optionen - xmpListTBSC.pl -d databaseDatenbank-Name
# -c Container Liste zusätzlich
# -f file File für Redirected Restore
#-------------------------------------------------------------------------------
# Autor - GR /05.01.00 - V1.0 erste Version
# Changes - GR /11.01.00 - V1.1 jetzt mit Package Perl
# - GR /17.01.00 - V1.2 mit Container Anzeige (Option -c)
# - GR /29.05.00 - V1.3 Anpassung an neue Script-Strategie,
# Ausgabe des Grand Total
# - GR /06.06.00 - V1.3.1 Korrektur des Ausgabe-Formats
# - GR /13.06.00 - V1.3.2 Korrektur, Fehler bei Utility Pending
# - GR /08.11.00 - V1.4 mit Option -f für Redirected Restores,
# Formatanweisungen geändert,
# renamed von xmpListTBSC.pl
# - GR /14.11.00 - V1.4.1 Korrektur des erzeugten SET Statements,
# Berücksichtigung von Raw Devices
# - GR /04.12.00 - V1.4.2 Redirected Restore Output File Name
# - GR /12.12.00 - V1.4.3 Schlüsselwort im Redirected Restore korrigiert
# - GR /10.01.01 - V1.5 Grand Total in KBytes
#-------------------------------------------------------------------------------
# Comments - PERL5LIB Environment Variable muss auf Perl Module Pfad zeigen!
################################################################################
################################################################################
# Modules
################################################################################
use strict;
use Getopt::Std;
use vars qw($opt_d $opt_u $opt_p $opt_c $opt_f);
use xmpPerl;
use Env;
use English;
################################################################################
# Declare/Init
################################################################################
my $VER = "1.4.5";
my $work = "";# for TS List
my @work;# for TS List
my $work2 = "";# for Container List
my @work2;# for Container List
my $var;
my $val;
my $MSG;
my $ts= 0;# no. of Tablespaces
my %ts_id= (); my $f1;# Tablespace Structure
my %ts_name= (); my $f2;
my %ts_type = (); my $f3;
my %ts_content = (); my $f4;
my %ts_state = (); my $f5;
my %ts_pgtotal = (); my $f6;
my %ts_pguseable= (); my $f7;
my %ts_pgused = (); my $f8;
my %ts_pgfree = (); my $f9;
my %ts_pgsize = (); my $fa;
my %ts_extsize = (); my $fb;
my %ts_pfsize = (); my $fc;
my %ts_container= (); my $fd;
my $tsc= 0;# no. of Containers
my %tsc_tsid= (); #$f1# Tablespace ID
my %tsc_id = ();#$f2# Container ID
my %tsc_name = ();#$f3# Container Name
my %tsc_type = ();#$f4
my %tsc_pgtotal = ();#$f5
my %tsc_pguseable=();#$f6
my %tsc_pgaccess=();#$f7
my %tsc_tsname = ();#$f8# Tablespace Name
my $ts_total_alloc = 0;# Grand Total
my $ts_total_used = 0;# Grand Total
my $ts_total_useable = 0;# Grand Total
my $hFile;# File Handle
################################################################################
# Ausgabe-Formate
################################################################################
format KOPF =
------------------------LIST OF TABLESPACES-------------------------------------
Tablespace--------------------- --------Page Usage------------- ---Sizes--- Con
ID Name Type Cont State Total Useable Used Free PG Ext PF tnr
--------------------------------------------------------------------------------
.
format ZEILE =
@>> @<<<<<<<<<< @<< @<<< @<<<<< @>>>>>> @>>>>>> @>>>>>> @>>>>>> @>> @>> @>> @>>
$f1,$f2, $f3, $f4, $f5, $f6, $f7, $f8, $f9, $fa,$fb,$fc,$fd
.
format LINE =
--------------------------------------------------------------------------------
.
format FUSS =
Grand Total: Allocated Space ..: @>>>>>>>> KBytes in @>>>>> Tablespaces
$ts_total_alloc,$ts
Useable DB2 Space : @>>>>>>>> KBytes
$ts_total_useable
Used Space .......: @>>>>>>>> KBytes
$ts_total_used
------------------------END LIST OF TABLESPACES---------------------------------
.
format CKOPF =
-------------------------LIST OF CONTAINERS-------------------------------------
TS Ct Container ----Page Usage-----
ID ID File Name Type Total Useabl Acc
--------------------------------------------------------------------------------
.
format CZEILE =
@>> @>> @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< @<<<< @>>>>>> @>>>>>> @>>
$f1,$f2,$f3, $f4, $f5, $f6, $f7
.
format CFUSS =
@>>>>> Container Files
$tsc
-------------------------END LIST OF CONTAINERS---------------------------------
.
################################################################################
# Argumente/Options prüfen
################################################################################
getopts("d:u:p:cf:");
if (! defined $opt_d )# Datenbank Check
{print "$0 - Datenbank nicht angegeben\n";
SaySyntax();
}
if (! defined $opt_u )# User Check
{$opt_u = xmpPerl::getDBAUser();
if ($opt_u eq "")
{print "$0 - User nicht angegeben und keine lP_LUS Variable definiert\n";
SaySyntax(); }
}
if (! defined $opt_p )# User Password Check
{$opt_p = xmpPerl::getDBAPsw();
if ($opt_p eq "")
{print "$0 - User's Password fehlt und keine lP_LPW Variable definiert\n";
SaySyntax(); }
}
$opt_d = uc $opt_d;
################################################################################
# Check DB2 Existenz und Informationsblock ausgeben
################################################################################
xmpPerl::chkDB2();
xmpPerl::prtHeaderDB2("$0 - List Tablespace and Container Utility - V$VER");
print "Database / DB2 User ID ......: $opt_d / $opt_u";
################################################################################
# DB2 Connect zur Datenbank
################################################################################
$work = xmpPerl::doCmdOut("db2 connect to $opt_d user $opt_u using $opt_p ");
if ($work =~ "Connection Information")
{print " (Connect erfolgreich)\n";}
else
{die ">>>Kann keine Verbindung zur Datenbank $opt_d mit User $opt_u herstellen\n".
">>>Meldung: $work\n".
">>>ABEND";}
if (defined $opt_f) {
print "File für Redirected Restore : $opt_f\n";
}
################################################################################
# DB2 List Commands ausführen
################################################################################
$work = xmpPerl::doCmdOut("db2 list tablespaces show detail");
undef $/;
@work = split("\n",$work);
#print "$#work lines returned from DB2 List Tablespace Command.\n";
foreach $work (@work) {
($var,$val) = split("=",$work);
if (! defined $var) {$var = "";}
if (! defined $val) {$val = "";}
$var = uc xmpPerl::trim($var);
$val = uc xmpPerl::trim($val);
########################################################################
# Korrektur erforderlich, wenn Utility Pending (z.B. Load vorher abgebr.)
########################################################################
if ($var =~ "CHANGE"){
$var = "";
$val = "";
}
#if ($var ne "" || $val ne "") {
#print "$var\t$val\n";
#}
########################################################################
# Beginn eines neuen Tablespace-Blocks
########################################################################
if ($var =~ "TABLESPACE ID"){
################################################################
# Tablespace Nummer erfassen
################################################################
$ts++;
$ts_id{$ts} = $val;# neue TSpace ID
#print "Neuer TS $var $val\n";
}
#######################################################################
# andere Suchbegriffe
#######################################################################
if ($var =~ "NAME") {
$ts_name{$ts} = $val;
################################################################
# Container Infos angefordert (08.11.00: verschoben von ID, s.o.)
################################################################
if ($opt_c # Container Info requested
|| $opt_f ){# Redirected Restore requested
$f1 = $ts_id{$ts};
$f2 = "db2 list tablespace containers for $f1 show detail";
#print "TS-ID=$f1\n";
$work2= xmpPerl::doCmdOut($f2);
#print "$work2\n";
@work2 = split("\n",$work2);
#print "$#work2 lines returned from DB2 List Container Command.\n";
foreach $work2 (@work2) {
($var,$val) = split("=",$work2);
if (! defined $var) {$var = "";}
if (! defined $val) {$val = "";}
$var = uc xmpPerl::trim($var);
$val = xmpPerl::trim($val);
#$val = uc xmpPerl::trim($val);
if ($var =~ "CONTAINER ID") {
$tsc++;
$tsc_tsid{$tsc} = $f1;# Tablespace ID
$tsc_id{$tsc} = $val;# neue Container ID
$tsc_tsname{$tsc} = $ts_name{$ts} # TS Name
}
if ($var =~ "NAME"){$tsc_name{$tsc} = $val}
if ($var =~ "TYPE") {$tsc_type{$tsc} = uc $val};
if ($var =~ "TOTAL"){$tsc_pgtotal{$tsc}= $val}
if ($var =~ "USEABLE"){$tsc_pguseable{$tsc}= $val}
if ($var =~ "ACCESS"){$tsc_pgaccess{$tsc}= uc $val}
}
}
}
#######################################################################
# Und der Rest der anderen Suchbegriffe
#######################################################################
if ($var =~ "TYPE") {
if ($val =~ "SYSTEM") {$ts_type{$ts} = "SMS"};
if ($val =~ "DATA") {$ts_type{$ts} = "DMS"};
}
if ($var =~ "CONTENTS") {
if ($val =~ "ANY") {
$ts_content{$ts} = "ANY"}
else {$ts_content{$ts} = $val}
}
if ($var =~ "STATE"){$ts_state{$ts} = $val}
if ($var =~ "TOTAL") {$ts_pgtotal{$ts} = $val}
if ($var =~ "USEABLE") {$ts_pguseable{$ts} = $val}
if ($var =~ "USED") {$ts_pgused{$ts} = $val}
if ($var =~ "FREE") {
if ($val =~ "NOT APPLIC") {
$ts_pgfree{$ts} = "N/A"}
else {$ts_pgfree{$ts} = $val}
}
if ($var =~ "PAGE SIZE") {$ts_pgsize{$ts} = $val/1024}
if ($var =~ "EXTENT") {$ts_extsize{$ts} = $val}
if ($var =~ "PREFETCH") {$ts_pfsize{$ts} = $val}
if ($var =~ "CONTAINERS"){$ts_container{$ts} = $val}
}
################################################################################
# DB2 Connection abbauen
################################################################################
$work = xmpPerl::doCmdOut("db2 connect reset");
################################################################################
# Ergebnis formattiert ausgeben
################################################################################
$FORMAT_NAME = "ZEILE"; # $~
$FORMAT_TOP_NAME = "KOPF";# $^
$FORMAT_FORMFEED= "";# $^L
$FORMAT_LINES_PER_PAGE= $ts+4;# $=, plus Lines f’r Kopf
$work = $ts;
for ($ts = 1; $ts <= $work; $ts++) {
$f1 = $ts_id{$ts};
$f2 = $ts_name{$ts};
$f3 = $ts_type{$ts};
$f4 = $ts_content{$ts};
$f5 = $ts_state{$ts};
$f6 = $ts_pgtotal{$ts};
$f7 = $ts_pguseable{$ts};
$f8 = $ts_pgused{$ts};
$f9 = $ts_pgfree{$ts};
$fa = $ts_pgsize{$ts};
$fb = $ts_extsize{$ts};
$fc = $ts_pfsize{$ts};
$fd = $ts_container{$ts};
write;
########################################################################
# Grant Totals von Pages in KBytes umrechnen
########################################################################
$ts_total_alloc = $ts_total_alloc +
( $ts_pgtotal{$ts} * $ts_pgsize{$ts} );
$ts_total_used= $ts_total_used +
( $ts_pgused{$ts} * $ts_pgsize{$ts} );
$ts_total_useable= $ts_total_useable +
( $ts_pguseable{$ts} * $ts_pgsize{$ts} );
}
$ts--;
# Fusszeile ausgeben ###########################################################
$FORMAT_NAME = "FUSS"; # $~
$FORMAT_TOP_NAME = "LINE";# $^
$FORMAT_LINES_PER_PAGE= 5;# $=
write;
################################################################################
# Container Ergebnis, wenn gewünscht, formatiert ausgeben
################################################################################
if ($opt_c) {
$FORMAT_NAME = "CZEILE"; # $~
$FORMAT_TOP_NAME = "CKOPF";# $^
$FORMAT_LINES_PER_PAGE= $tsc+4;# $=
$work = $tsc;
for ($tsc = 1; $tsc <= $work; $tsc++) {
$f1 = $tsc_tsid{$tsc};
$f2 = $tsc_id{$tsc};
$f3 = $tsc_name{$tsc};
$f4 = $tsc_type{$tsc};
$f5 = $tsc_pgtotal{$tsc};
$f6 = $tsc_pguseable{$tsc};
$f7 = $tsc_pgaccess{$tsc};
write;
}
$tsc--;
# Fusszeile ausgeben ###################################################
$FORMAT_NAME = "CFUSS"; # $~
$FORMAT_TOP_NAME = "LINE";# $^
$FORMAT_LINES_PER_PAGE= 2;# $=
write;
}
################################################################################
# Anweisungen für Redirected Restore in File schreiben
################################################################################
if ($opt_f) {
if (! open(hFile, "> $opt_f") )
{ die "$0 kann nicht in $opt_f schreiben. \n".
"abends";
}
########################################################################
# Vorspann für Restore Anweisungen
########################################################################
print hFile "UPDATE COMMAND OPTIONS USING S ON Z ON $opt_f.out V ON;\n";
print hFile "SET CLIENT ATTACH_NODE 0;\n";
print hFile "SET CLIENT CONNECT_NODE 0;\n";
$work = xmpPerl::getTimestamp();
print hFile "ECHO \@----------------------------------------------------\@;\n";
print hFile "ECHO \@Script created ........: $work\@;\n";
print hFile "ECHO \@Script File ...........: $opt_f\@;\n";
print hFile "ECHO \@Database to be restored: $opt_d (Redirected Restore)\@;\n";
print hFile "ECHO \@Disclaimer: Check/edit this script to avoid loss of data!\@;\n";
print hFile "------------------------------------------------------------\n";
print hFile "RESTORE DB $opt_d \n".
" FROM /back-up-path\n".
" TAKEN AT yyyymmddhhmmss\n".
" TO /new-db-path\n".
" INTO new-db\n".
" NEWLOGPATH /new-logpath\n".
" WITH 2 BUFFERS BUFFER 2048\n".
" REDIRECT;\n";
print hFile "----------------------------------------------------\n";
print hFile "--RESTORE DB $opt_d ABORT;\n";
########################################################################
# Anweisungen für jeden Tablespace Container oder Pfad
########################################################################
$work = $tsc;
$work2 = "";
for ($tsc = 1; $tsc <= $work; $tsc++) {
$f1 = $tsc_tsid{$tsc};
$f2 = $tsc_id{$tsc};
$f3 = $tsc_name{$tsc};
$f4 = $tsc_type{$tsc};
$f5 = $tsc_pgtotal{$tsc};
$f6 = $tsc_pguseable{$tsc};
$f7 = $tsc_pgaccess{$tsc};
$f8 = $tsc_tsname{$tsc};
if ($f1 ne $work2){# TS-ID unchanged?
if ($tsc gt 1) {print hFile " );\n"};
print hFile "-------- $f1 / $f2 / $f8 --------\n";
print hFile "SET TABLESPACE CONTAINERS FOR $f1 USING (\n";
if ($f4 eq "FILE") {print hFile " $f4 $f3 $f5 \n";}
if ($f4 eq "PATH") {print hFile " $f4 $f3 \n";}
if ($f4 eq "DISK") {print hFile " DEVICE $f3 $f5 \n";}
}
else{
if ($f4 eq "FILE") {print hFile " ,$f4 $f3 $f5 \n";}
if ($f4 eq "PATH") {print hFile " ,$f4 $f3 \n";}
if ($f4 eq "DISK") {print hFile " ,DEVICE $f3 $f5 \n";}
}
$work2 = $f1;
}
print hFile " );\n";
$tsc--;
print hFile "----------------------------------------------------\n";
print hFile "RESTORE DB $opt_d CONTINUE;\n";
print hFile "-- ROLLFORWARD DATABASE $opt_d TO END OF LOGS;\n";
print hFile "-- ROLLFORWARD DATABASE $opt_d STOP;\n";
########################################################################
# File für Redirected Restore schließen und für andere freigeben
########################################################################
close(hFile);
chmod 0775,$opt_f;
print "Redirected Restore File successfully created!\n";
}
print "***Ende***\n";
exit 0;
#################################################################################
########################################################## Sub Routines #########
# SaySyntax #####################################################################
sub SaySyntax {
die "Usage: [perl] $0 -d db [-u parm -p parm -c -f file]\n".
"-d database \n".
"-u User (opt)\n".
"-p Password (opt)\n".
"-c Container-Liste (opt)\n".
"-f file for DB2 Redirected Restore\n".
"abends";
}
© Gernot Ruban