Unix Code - Detail View


Date

Name

Plattform

Language

Kurzbeschreibung

Owner

Link

20.10.2002

xmpTBSCUsage.pl

Unix

Perl

Zeigt und prüft Auslastung der Tablespaces, generiert Mail bei Threshold-Überschreitung

Gernot Ruban

 

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

#!/usr/local/bin/perl -w
################################################################################
# xmpTBSCUsage.pl - Check Tablespace Usage
# 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  - xmpTBSCUsage.pl -d databaseDatenbank-Name
#                             -t tablespace     (opt) Tablespace
#                             -g n=m            n=Usage, m=RC bei Überschreitung
#                             -m id|list        Mail Recipient ID oder Liste
#-------------------------------------------------------------------------------
# Autor     - GR /09.11.00 - V1.0 erste Version
# Changes   - GR /13.11.00 - V1.1 mit -t Tablespace Option
#             GR /10.01.01 - V1.2 Unterschiedl. Pagesizes berücksichtigen,
#                                 High Water Mark, Grand Totals in KBytes
#             GR /18.12.01 - V2.0 Mail-Versand bei Überschreitung des Schwellwertes
#             GR /29.01.02 - V2.1 Delete temporary file when work has been done
#             GR /12.02.02 - V2.1 Checked problem with temp files, no change
#-------------------------------------------------------------------------------
# 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_t $opt_g $opt_m);
use xmpPerl;
use Env;
use English;
use File::Basename;

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

my $work    = "";# for TS List
my @work;# for TS List
my $work2   = "";# for Container List
my @work2;# for Container List
my $tempfile;# temporary file
my $hFile;# File handle
my $rl;my $rc;

my $var;
my $val;
my $MSG;

my $opt_usage   = 0;
my $opt_rc      = 0;

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_pgsize   = (); my $f4;
my %ts_hwm      = (); my $f5;
my %ts_pgtotal  = (); my $f6;
my %ts_pgused   = (); my $f7;
my %ts_pgfree   = (); my $f8;
my %ts_usage    = (); my $f9;
my %ts_threshold= (); my $fa;

my $ts_total_alloc   = 0;# Grand Total
my $ts_total_used    = 0;# Grand Total
my $ts_total_free    = 0;# Grand Total
my $ts_total_usage= 0;# Grand Total

my $ts_count= 0;# reached Threshold


################################################################################
# Ausgabe-Formate
################################################################################
format KOPF =
----------LIST OF TABLESPACES----------------------------------
 Tablespace--------- Page    High ---------Page Usage----------
 ID Name        Type Size    Mark   Total    Used    Free     %
---------------------------------------------------------------
.

format ZEILE =
@>> @<<<<<<<<<< @<<  @>>  @>>>>>> @>>>>>> @>>>>>> @>>>>>> @>>>> @<<<<<<<<<<<<<<<<<<<<<
$f1,$f2,        $f3, $f4, $f5,    $f6,    $f7,    $f8,    $f9,  $fa
.

format LINE =
---------------------------------------------------------------
.

format FUSS =
Grand Total:   Allocated Space ..:  @>>>>>>>> KBytes
$ts_total_alloc
               Free DB2 Space ...:  @>>>>>>>> KBytes
$ts_total_free
               Used Space / Usage:  @>>>>>>>> KBytes  = @>>>> %
$ts_total_used,$ts_total_usage
-----------------END LIST OF TABLESPACES-----------------------
.

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

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;

if (defined $opt_t ) {$opt_t = uc $opt_t}# Specific Tablespace

if (! defined $opt_g ){undef $opt_m;}# Schwellwert

################################################################################
# Check DB2 Existenz und Informationsblock ausgeben
################################################################################
xmpPerl::chkDB2();
xmpPerl::prtHeaderDB2("$0 - Check Tablespace Usage Utility - 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";}

################################################################################
# Die anderen Parameter noch ausgeben
################################################################################
if (defined $opt_t ) {
print "Tablespace ..................: $opt_t\n";}
if (defined $opt_g ) {
($opt_usage,$opt_rc) = split("=",$opt_g);
print "Usage Threshold and RC Value : $opt_usage >= RC=$opt_rc\n";
if (defined $opt_m ) {
print "- Mail ID|List when exceeded : $opt_m\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
}
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 =~ "TOTAL")    {$ts_pgtotal{$ts}   = $val}
if ($var =~ "USED")     {
$ts_pgused{$ts}  = $val;
        $ts_usage{$ts}   = sprintf("%5.2f",$ts_pgused{$ts} / $ts_pgtotal{$ts} * 100);
}
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 =~ "HIGH WATER MARK"){
if ($ts_type{$ts} eq "DMS") {
$ts_hwm{$ts}  = $val; }
else {
$ts_hwm{$ts}  = "N/A"; }
}

}

################################################################################
# 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_pgsize{$ts};
$f5 = $ts_hwm{$ts};
$f6 = $ts_pgtotal{$ts};
$f7 = $ts_pgused{$ts};
$f8 = $ts_pgfree{$ts};
$f9 = $ts_usage{$ts};

########################################################################
# Alle oder nur angegebenen Tablespace checken
########################################################################
if ((! defined $opt_t)# keine -t Option
    or($ts_name{$ts} =~ $opt_t)  ){# oder Tablespace gefunden

################################################################
# Tablespace markieren, wenn Schwellwert erreicht
################################################################
if (  ($ts_usage{$ts} > $opt_usage)
   && (defined $opt_g)
   && ($ts_type{$ts} ne "SMS")) {
$fa = "<==";
$ts_threshold{$ts} = "X";    # threshold exceeded
$ts_count++;}
else {  $fa = "";
$ts_threshold{$ts} = "";    # threshold not exceeded
}

################################################################
# Korrektur erforderlich, wenn Utility Pending (z.B. Load vorher abgebr.)
################################################################
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} );
if ($ts_pgfree{$ts} ne "N/A") {
        $ts_total_free= $ts_total_free +
    ( $ts_pgfree{$ts} * $ts_pgsize{$ts} );
}

}
}
$ts--;

# Fusszeile ausgeben ###########################################################
$FORMAT_NAME = "FUSS"; # $~
$FORMAT_TOP_NAME = "LINE";# $^
$FORMAT_LINES_PER_PAGE= 5;# $=

$ts_total_usage = $ts_total_used / $ts_total_alloc * 100;

write;

################################################################################
# Abbruch, wenn mind. ein Container den Schwellwert überschritten hat
################################################################################
if ($ts_count > 0) {
print "$0 - mindestens $ts_count Tablespace(s) hat/haben den \n".
"Schwellwert von $opt_usage % Space-Auslastung erreicht oder überschritten.\n".
"abends now, (RC=$opt_rc)\n";
########################################################################
# Send mail, if requested
########################################################################
if ( defined $opt_m ) {
################################################################
# Temporary File
################################################################
   $tempfile="/tmp/".basename($0).".$$";
#print ">>>$tempfile\n";
if (! open(hFile, "> $tempfile") )
{  die "$0 cannot open $tempfile for write operation. \n".
    "abends";
}
print hFile basename($0)." - Tablespace Usage Check\n\n";
print hFile "Usage of one or more tablespaces in database $opt_d exceeds \n".
    "usage threshold of $opt_usage percent. Please check!\n\n";
$work = $ts;
for ($ts = 1; $ts <= $work; $ts++) {
if ( $ts_threshold{$ts} eq "X" ) {
print hFile "Tablespace ".$ts_name{$ts}.":\n";
print hFile "           High Water Mark .........: ".$ts_hwm{$ts}." pages\n";
print hFile "           Total Pages Allocated ...: ".$ts_pgtotal{$ts}."\n";
print hFile "           Currently Used Pages ....: ".$ts_pgused{$ts}."\n";
print hFile "           Usage (in percent) ......: ".$ts_usage{$ts}."%\n";
}
}
$ts--;
print hFile "***end***\n";
close(hFile);
################################################################
# Send temporary file to recipients
################################################################
$rl = xmpPerl::MailText("Tablespace Usage exceeded!" , $opt_m , $tempfile);
$rc = $?;
if ($rc != 0) {
   print "$0 failed to create/send E-Mail using $opt_m $tempfile ($rc).\n";
}
print "E-Mail has been sent to\n$rl\n";
unlink($tempfile);
}
########################################################################
# Return with specified return code
########################################################################
exit $opt_rc;
}

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

exit 0;


#################################################################################
########################################################## Sub Routines #########
# SaySyntax #####################################################################
sub SaySyntax {
die "Usage: [perl] $0 -d db [-u parm -p parm -t ts -g n=m -m [id|list]]\n".
"-d database \n".
"-u User (opt)\n".
"-p Password (opt)\n".
"-t Tablespace / Search Value (opt)\n".
"-g usage=rc (abends if real usage>value)\n".
"   -m mail id|list when usage exceeded\n".
"abends";
}

© Gernot Ruban