Perl Code - Detail View


Date

Name

Plattform

Language

Kurzbeschreibung

Owner

Link

20.10.2002

xmpTBSCCheck.pl

Unix

Perl

DB2 Tablespace und Container Check

Gernot Ruban

 

DB2 ist ein Produkt der
IBM Corporation. Bitte
Copyright-  und Trademark-Hinweise beachten!

#!/usr/local/bin/perl -w
################################################################################
# xmpTBSCCheck.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  - -d xxx       - Database
#             -u xxx       - User
#             -p xxx       - Password
#-------------------------------------------------------------------------------
# Autor     - GR /19.10.00 - V1.0 Kopiert aus xmpListTBSC.pl und angepasst
# Changes   - GR /08.11.00 - V1.0 renamed von xmpCheckTBSC.pl
#           - GR /11.12.00 - V1.1 neue Option -c für COUNT(*) der Table Rows je TS
#           - GR /22.01.01 - V1.2 COUNT(*) nur bei -c, sonst nicht
#-------------------------------------------------------------------------------
# Comments  - PERL5LIB Environment Variable muss auf Perl Module Pfad zeigen!
#           - $drh = DBI Driver Handle
#             $dbh = DBD Database Handle
#             $sth = SQL Statement Handle
################################################################################

################################################################################
# Modules
################################################################################
use strict;
use Getopt::Std;
use vars qw($opt_d $opt_u $opt_p $opt_c);
use xmpPerl;
use DBI;
use DBD::DB2::Constants;
use DBD::DB2;
use Env;
use English;

################################################################################
# Declare/Init
################################################################################
my $VER     = "1.1";

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 $f_any= "";# Any abnormal State

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 $f6;# Table Row Count
my ($creator,$name);  my $f7;# Table Name

my $dbh;# Database Handle
my ($sth, $sth2);# Statement Handle
my ($stmt1, $stmt2);# SQL Statements

################################################################################
# Ausgabe-Formate
################################################################################
format KOPF =
------LIST OF TABLESPACES-------
 Tablespace---------------------------------------------------------------------
 ID Name        Type Cont State            
--------------------------------------------------------------------------------
.

format ZEILE =
@>> @<<<<<<<<<< @<<  @<<< @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$f1,$f2,        $f3, $f4, $f5
.

format CZEILE =
    Rows @>>>>>>>>> Table @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
         $f6,                 $f7
.


################################################################################
# Argumente/Options prüfen
################################################################################
getopts("d:u:p:c");

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 Status - Version $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_c) {
print "Option 'Count Table Rows' ...: YES\n";
}

################################################################################
# DB2 List Commands ausführen
################################################################################
$work = xmpPerl::doCmdOut("db2 list tablespaces");
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}
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"){
if ($val ne "0X0000") {
#print "$var\n";
$f_any = "1";
}
$work = xmpPerl::doCmdOut("db2tbst ".$val);
$ts_state{$ts}     = $val." ".$work;
}

}

################################################################################
# DB2 Connection abbauen
################################################################################
$work = xmpPerl::doCmdOut("db2 connect reset");

################################################################################
# DBI Switches
################################################################################
my $switch = DBI->internal;
$switch->debug(0);                     # 1=normal debug, 2=detailed handle trace
#print "DBI Version and Attributes ..: $switch->{'Attribution'}, $switch->{'Version'}\n";
#print "DBI Drivers .................: ",join(", ",DBI->available_drivers()),"\n";

################################################################################
# Connect to Database, if counting rows required
################################################################################
if (defined $opt_c) {
$dbh = DBI->connect("dbi:DB2:$opt_d",
                    $opt_u,
                    $opt_p,
                    { PrintError => 0, # don't print error by default
                      RaiseError => 0, # don't print error and die by default
                      AutoCommit => 0, # don't commit after each statement
                      ChopBlanks => 0, # don't remove trailing blanks on CHAR-Types
                    } )
        || die ">>>$0 Problem mit Connect to $opt_d\n>>>".$DBI::errstr;
}

################################################################################
# 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 = xmpPerl::trim($ts_state{$ts});
write;
if (   ($f5 eq "0X0000 State = Normal")
            && (defined $opt_c)                 ){
$stmt1 = "SELECT CREATOR,NAME FROM SYSIBM.SYSTABLES WHERE TBSPACE='$f2' AND TBSPACE ^= 'SYSCATSPACE' ORDER BY NAME";
$sth   = $dbh->prepare($stmt1) || die "Statement #1: $stmt1\n".$dbh->errstr;
$sth->execute() || die "Statement #1: $stmt1\n".$dbh->errstr;
while ( (($creator,$name) = $sth->fetchrow()) ) {
$creator = uc xmpPerl::trim($creator);
$name    = uc xmpPerl::trim($name);
$stmt2   = "SELECT COUNT(*) FROM $creator.$name";
#print "$stmt2\n";
 $sth2    = $dbh->prepare($stmt2) || die "Statement #2: $stmt2\n".$dbh->errstr;
if (! $sth2->execute()) {
die "Statement #2: $stmt2\n".$dbh->errstr; }
else {$f6 = $sth2->fetchrow();
$f7 = "$creator.$name";
print "    Object: $f7 Rows: $f6\n";
}

}
}

}

################################################################################
# Disconnect from Database
################################################################################
if (defined $opt_c) {
$sth->finish()   || die $dbh->errstr;
$sth2->finish()  || die $dbh->errstr;
$dbh->commit     || die $dbh->errstr;
$dbh->disconnect || die $dbh->errstr;
}

################################################################################
# Ein Tablespace Status ist nicht in Ordnung
################################################################################
if ($f_any eq "1") {
die "$0 - ein Tablespace Status ist abnormal \n".
    "abends";
}

print "***Ende***\n";

exit 0;


#################################################################################
########################################################## Sub Routines #########
# SaySyntax #####################################################################
sub SaySyntax {
die "Usage: [perl] $0 -d db [-u ... -p ...] [-c]\n".
"-d database \n".
"-u User (opt)\n".
"-p Password (opt)\n".
"-c counts rows in tables of tablespaces\n".
"abends";
}

© Gernot Ruban