Unix Code - Detail View


Date

Name

Plattform

Language

Kurzbeschreibung

Owner

Link

20.10.2002

xmpMonitor.pl

Unix

Perl

DB2 Monitoring Informationen auswerten und in einer speziellen Monitoring Datenbank speichern

Gernot Ruban

 

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

#!/usr/local/bin/perl -w
################################################################################
# xmpMonitor.pl - DB2 Monitoring Instruments
# Called by - Command Level
# Calls     - xmpPerl.pm   - xmp DBA Perl Package
#           - DBI::DB2     - DB2 Perl Interace 
#           - Getoptions   - Argument Processing, Long Version
#           - Env          - accessing environment variables
#           - English      - aliases for Perl variables
# Options   - xmpMonitor.pl   -tdb databaseTarget DB (Monitor DB)
#                             -tu  user         User ID
#                             -tp  psw          User Psw
#                             -mp  project      Monitor Project Name
#                             -mdb database     [opt.] Database to monitor
#      -clear [nnn]      [opt.] first deletes info
#      -reset            [opt.] takes info, then resets
#      -resetonly        [opt.] only resets monitor info
#      -activate         [opt.] only activates databases
#      -deactivate       [opt.] deactivates db after snapshot
#-------------------------------------------------------------------------------
# Autor     - GR /15.02.02 - V1.0 first version
# Changes   - GR /15.03.02 - V1.1 extended by lock section, CLEAR and RESET option,
#           - GR /20.03.02 - V2.0 GROUP_ID for Dynamic SQL, count rows inserted,
#                                 one ore more db arguments, ACTIVATE option
#           - GR /27.03.02 - V2.1 COMMIT before starting new section
#           - GR /09.04.02 - V2.2 New option -deactivate, -clear extended by nnn
#           - GR /06.05.02 - V2.3 Additionally takes instance snapshot, new table
#           - GR /14.05.02 - V2.3 New col LOCK_OBJNO in SNAP_LOCK
#-------------------------------------------------------------------------------
# Comments  - PERL5LIB variable must point to path containg perl.pm module!
#           - DB2 MONITOR SWITCH TABLE must be on
#           - DB2 RESET MONITOR eliminates monitoring buffers
#           - $drh = DBI Driver Handle
#             $dbh = DBD Database Handle
#             $sth = SQL Statement Handle
#           - Start database and keep database alive using "ACTIVATE DATABASE".
#             Only an active db provides snapshot information, deactivation
#             or disconnection the last db connection destroys snapshot monitor
#      information.
#    - RESET/RESETONLY only impacts current attachment. Other views of
#             monitoring areas of other attachments are not affected.
################################################################################

################################################################################
# Modules
################################################################################
use strict;
use Getopt::Long;
use vars qw($opt_tdb $opt_tu $opt_tp $opt_mp $opt_mdb $opt_clear $opt_reset $opt_resetonly $opt_activate $opt_deactivate);
use xmpPerl;
use DBI;
use DBD::DB2::Constants;
use DBD::DB2;
use Env;
use English;

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

my ($dbh, $sth, $stmt);

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;

################################################################################
# Switches
################################################################################
my ($f_db, $f_bp, $f_ds, $f_appl, $f_ts, $f_tb, $f_lock, $f_inst);
my $f_done;# record switch
my ($i,$n);

################################################################################
# Counters
################################################################################
my $n_total = 0;
my $n_db = 0;
my $n_bp = 0;
my $n_ds = 0;
my $n_appl = 0;
my $n_ts = 0;
my $n_tb = 0;
my $n_lock = 0;
my $n_inst = 0;

################################################################################
# Table of databases
################################################################################
my $tdb = 0;
my @tdb_name;

################################################################################
# Database records
################################################################################
my $database;
my $bufferpool;
my $tablespace;
my $applname;# application name
my $applhandle;# application handle
my $lock_objno;# lock object number V2.3
my $table;# table name
my $snap_ts;# Monitor Timestamp
my $group_id;                                           # Dynamic SQL Stmt
my $instance;# instance name
my $valc;# character string
my $valn;# numeric value

################################################################################
# Check arrguments/options
################################################################################
GetOptions("tdb=s","tu=s", "tp=s", "mp=s", "mdb:s", "clear:i", "reset", "resetonly", "activate", "deactivate");

if ((! defined $opt_activate)# not ACTIVATE
   and  (! defined $opt_resetonly)   ){# not RESETONLY
if (! defined $opt_tdb ){# Datenbank Check
print "$0 - target database not specified\n";
        SaySyntax();
}

if (! defined $opt_tu ){# User Check
$opt_tu = xmpPerl::getDBAUser();
       if ($opt_tu eq ""){
print "$0 - user not and lP_LUS env variable not specified\n";
        SaySyntax();  }
}

if (! defined $opt_tp ){# User Password Check
$opt_tp = xmpPerl::getDBAPsw();
if ($opt_tp eq ""){
print "$0 - user's password and lP_LPW env variable not specified\n";
        SaySyntax();  }
}

if (! defined $opt_mp ){# Monitor Project
print "$0 - monitor project description/name not specified\n";
         SaySyntax();
}
}

if (! defined $opt_mdb ){# Database to monitor
$opt_mdb = "*";
}

$opt_tdb = uc $opt_tdb;
$opt_mp  = uc $opt_mp;
$opt_mdb = uc $opt_mdb;

################################################################################
# Check DB2 Existenz und Informationsblock ausgeben
################################################################################
xmpPerl::chkDB2();
xmpPerl::prtHeaderDB2("$0 - DB2 Monitoring Instruments - Version $VER");

if ((! defined $opt_activate)# not ACTIVATE
   and  (! defined $opt_resetonly)   ){# not RESETONLY
print "Monitor Database / User ID ..: $opt_tdb / $opt_tu\n";
print "Monitor Projekt .............: $opt_mp\n";
print "Database(s) to be process ...: $opt_mdb\n";
$snap_ts = xmpPerl::getTimestamp();
print "Monitor Snapshot Timestamp ..: $snap_ts (generated)\n";

if (defined $opt_deactivate) {
print "Database Deactivation .......: requested, see protocol below!\n";
}
if (defined $opt_clear) {
print "Clearing Monitor Database ...: retention period $opt_clear day(s)\n";
}
}

################################################################################
# OPTION -MDB: Determine database(s) to process
################################################################################
if ($opt_mdb eq "*") {
########################################################################
# Get DB Directory, FOR ALL Databases is not equivalent to list of DB's
########################################################################
$work = xmpPerl::doCmdOut("db2 list db directory | egrep -i 'database alias|entry type'");
undef $/;
@work = split("\n",$work);
#print "$#work lines returned from DB2 List db dir Command.\n";
if ($work =~ "not found") {
die ">>> DB2 Commands (z.B. list db dir) nicht ausführbar!\n".
">>> Meldung: $work\n".
">>> ABEND ";
}
foreach $work (@work) {
($var,$val) = split(" = ",$work);
################################################################
# Store local database, but not the monitor database
################################################################
if ($var =~ "alias") {
$val = uc xmpPerl::trim($val);
if ($val ne $opt_tdb) {
$tdb++;
$tdb_name[$tdb] = $val;
}
}
################################################################
# Local databases only
################################################################
if ($var =~ "entry type") {
if ($val ne "Indirect") {
$tdb_name[$tdb] = "";
$tdb--;
}
}
}
}
else {
########################################################################
# One or more database names
########################################################################
undef $/;
@work = split(" ",$opt_mdb);
foreach $work (@work) {
$tdb++;
$tdb_name[$tdb] = $work;
}
}

################################################################################
# OPTION -ACTIVATE: Reset and activate db(s) for monitoring
################################################################################
if (defined $opt_activate) {
print "Database Activation .........: requested\n";
for ($i = 1; $i<=$tdb ; $i++) {
#$work = xmpPerl::doCmdOut("db2 reset monitor for db ".$tdb_name[$i]);
#print "RESET ".$tdb_name[$i]."=$work\n";
$work = xmpPerl::doCmdOut("db2 activate db ".$tdb_name[$i]);
if (   ($work =~ 'DB20000I')
   or  ($work =~ 'SQL1490W')
   or  ($work =~ 'SQL1494W') ) {
print "                               ".$tdb_name[$i]." activated! (".substr($work,0,8).")\n";}
else {print "                               ".$tdb_name[$i]." failed! $work\n";
}
}

########################################################################
# Skip if RESETONLY requested
########################################################################
if (! defined $opt_resetonly) {
print "***this is the end***\n";
exit 0;
}
}

################################################################################
# OPTION -RESETONLY:Reset DB2 internal monitor areas requested
################################################################################
if (defined $opt_resetonly) {
print "Reset DB2 monitor areas .....: requested ...\n";

########################################################################
# Global or distinct RESET
########################################################################
if ($opt_mdb eq "*") {
$work = xmpPerl::doCmdOut("db2 reset monitor all");
if ($work =~ 'DB20000I') {
print "                               reset for ALL!\n";}
else {print "                               failed for ALL! $work\n";
}
}
else {
################################################################
# Reset all databases
################################################################
for ($i = 1; $i<=$tdb ; $i++) {
$work = xmpPerl::doCmdOut("db2 reset monitor for db ".$tdb_name[$i]);
if ($work =~ 'DB20000I') {
print "                               ".$tdb_name[$i]." reset!\n";}
else {print "                               ".$tdb_name[$i]." reset failed! $work\n";
}

}
}
print "***this is the end***\n";
exit 0;
}

################################################################################
# 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 DWH Source Database holding Delta Processing Control Table
################################################################################
$dbh = DBI->connect("dbi:DB2:$opt_tdb",
                    $opt_tu,
                    $opt_tp,
                    { 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 Troubles to connect to $opt_tdb\n>>>".$DBI::errstr;

################################################################################
# OPTION -CLEAR [nnn]: Housekeeping (CLEAR) requested
################################################################################
if (defined $opt_clear) {
$stmt = "DELETE FROM xmpDBA.SNAP_DB ".
"WHERE  (PROJECT = '$opt_mp' OR '*' = '$opt_mp') ".
"AND    SNAP_TS <= CURRENT TIMESTAMP - $opt_clear DAYS";
$sth = $dbh->prepare($stmt) || die "Statement $stmt";
$sth->execute() || print "Statement $stmt\n".$dbh->errstr."\n";
#doSQLCommit();
$dbh->commit || die $dbh->errstr;
print "                               - SNAP_DB done!\n";

$stmt = "DELETE FROM xmpDBA.SNAP_BP ".
"WHERE  (PROJECT = '$opt_mp' OR '*' = '$opt_mp') ".
"AND    SNAP_TS <= CURRENT TIMESTAMP - $opt_clear DAYS";
$sth = $dbh->prepare($stmt) || die "Statement $stmt";
$sth->execute() || print "Statement $stmt\n".$dbh->errstr."\n";
#doSQLCommit();
$dbh->commit || die $dbh->errstr;
print "                               - SNAP_BP done!\n";

$stmt = "DELETE FROM xmpDBA.SNAP_DYNSQL ".
"WHERE  (PROJECT = '$opt_mp' OR '*' = '$opt_mp') ".
"AND    SNAP_TS <= CURRENT TIMESTAMP - $opt_clear DAYS";
$sth = $dbh->prepare($stmt) || die "Statement $stmt";
$sth->execute() || print "Statement $stmt\n".$dbh->errstr."\n";
#doSQLCommit();
$dbh->commit || die $dbh->errstr;
print "                               - SNAP_DYNSQL done!\n";

$stmt = "DELETE FROM xmpDBA.SNAP_APPL ".
"WHERE  (PROJECT = '$opt_mp' OR '*' = '$opt_mp') ".
"AND    SNAP_TS <= CURRENT TIMESTAMP - $opt_clear DAYS";
$sth = $dbh->prepare($stmt) || die "Statement $stmt";
$sth->execute() || print "Statement $stmt\n".$dbh->errstr."\n";
#doSQLCommit();
$dbh->commit || die $dbh->errstr;
print "                               - SNAP_APPL done!\n";

$stmt = "DELETE FROM xmpDBA.SNAP_TS ".
"WHERE  (PROJECT = '$opt_mp' OR '*' = '$opt_mp') ".
"AND    SNAP_TS <= CURRENT TIMESTAMP - $opt_clear DAYS";
$sth = $dbh->prepare($stmt) || die "Statement $stmt";
$sth->execute() || print "Statement $stmt\n".$dbh->errstr."\n";
#doSQLCommit();
$dbh->commit || die $dbh->errstr;
print "                               - SNAP_TS done!\n";

$stmt = "DELETE FROM xmpDBA.SNAP_TB ".
"WHERE  (PROJECT = '$opt_mp' OR '*' = '$opt_mp') ".
"AND    SNAP_TS <= CURRENT TIMESTAMP - $opt_clear DAYS";
$sth = $dbh->prepare($stmt) || die "Statement $stmt";
$sth->execute() || print "Statement $stmt\n".$dbh->errstr."\n";
#doSQLCommit();
$dbh->commit || die $dbh->errstr;
print "                               - SNAP_TB done!\n";

$stmt = "DELETE FROM xmpDBA.SNAP_LOCK ".
"WHERE  (PROJECT = '$opt_mp' OR '*' = '$opt_mp') ".
"AND    SNAP_TS <= CURRENT TIMESTAMP - $opt_clear DAYS";
$sth = $dbh->prepare($stmt) || die "Statement $stmt";
$sth->execute() || print "Statement $stmt\n".$dbh->errstr."\n";
#doSQLCommit();
$dbh->commit || die $dbh->errstr;
print "                               - SNAP_LOCK done!\n";

$stmt = "DELETE FROM xmpDBA.SNAP_INST ".
"WHERE  (PROJECT = '$opt_mp' OR '*' = '$opt_mp') ".
"AND    SNAP_TS <= CURRENT TIMESTAMP - $opt_clear DAYS";
$sth = $dbh->prepare($stmt) || die "Statement $stmt";
$sth->execute() || print "Statement $stmt\n".$dbh->errstr."\n";
#doSQLCommit();
$dbh->commit || die $dbh->errstr;
print "                               - SNAP_INST done!\n";

print "                               done!\n";

########################################################################
# Disconnect from DWH Source Database
########################################################################
$sth->finish()   || die $dbh->errstr;
$dbh->commit     || die $dbh->errstr;
}

################################################################################
# Execute SNAPSHOT for database(s) determined
################################################################################
if ($tdb == 0) {
die ">>> This system does not contain any databases!\n".
    ">>> Cannot process DB2 SNAPSHOT Command.\n".
    ">>> ABEND ";
}

################################################################################
# Database Manager Snapshot# V 2.3
################################################################################
$work = xmpPerl::doCmdOut("db2 get snapshot for database manager");
undef $/;
@work = split("\n",$work);
print "$#work lines returned from GET SNAPSHOT Command.\n";

linedbm: foreach $work (@work) {
########################################################################
# Split line, remove leading and trailing blanks
########################################################################
($var,$val) = split("=",$work);

if (! defined $var) {$var = "";}
if (! defined $val) {$val = "";}

if (length($var) > 50) {# differs!!!
($var,$val) = split(":",$work);
if (! defined $var) {$var = "";}
if (! defined $val) {$val = "";}
}

if ( ($var eq "") and ($val eq "") ) {next linedbm};
if ( $work =~ "SQL1611W") {next linedbm}; # no data

$var = uc xmpPerl::trim($var);
$val = xmpPerl::trim($val);

if (length($val) > 1024) {
$val = "...".substr($val,length($val)-1020);
}

#print "=>VAR=$var VAL=$val\n";
$f_done = 0;

########################################################################
# Kind of information
########################################################################
if ($var =~ "DATABASE MANAGER SNAPSHOT"){# V2.3
$f_inst = 1;
($f_db, $f_bp, $f_ds, $f_appl, $f_ts, $f_tb, $f_lock) = 0;
print "INSTANCE SECTION ............. --------\n";
$instance = "";
$dbh->commit || die $dbh->errstr;
}

########################################################################
# Instance / Database Manager Section# V 2.3
########################################################################
if (    ($f_inst)
   and  ($var =~ "INSTANCE NAME")
   and  (defined $val)
   and  ($val ne "")) {
$instance = $val;
print "- Instance info detected for : $instance\n";
$f_done   = 1;
}

########################################################################
# other snapshot information
########################################################################
if (    (! $f_done)
   and(defined $val)
   and  ($val ne "")){

################################################################
# split line: left text (char), right number (if numerical)
################################################################
#print "$var=$val ---";
$valc = $val;
$valn = whenisnum($valc);
$valc =~ s/[']/´/g;# format '->´
#print "CHAR=$valc NUM=$valn\n";

################################################################
# insert record into SNAP_INST table# V 2.3
################################################################
if ($f_inst) {
$stmt = "INSERT INTO xmpDBA.SNAP_INST ".
"(PROJECT, INSTANCE, SNAP_TS,VAR,VALC,VALN) ".
"VALUES('$opt_mp', '$instance',".
"'$snap_ts','$var','$valc',$valn)";
$sth = $dbh->prepare($stmt) || die "Statement $stmt";
$sth->execute() || die "Statement $stmt\n".$dbh->errstr;
$n_total++;
$n_inst++;
}
}
}


################################################################################
# Process databases
################################################################################
for ($i = 1; $i<=$tdb ; $i++) {

#print "==================> ".$tdb_name[$i]."\n";
$work = xmpPerl::doCmdOut("db2 get snapshot for all on ".$tdb_name[$i]);
undef $/;
@work = split("\n",$work);
#print "$#work lines returned from GET SNAPSHOT Command.\n";

line: foreach $work (@work) {

########################################################################
# Split line, remove leading and trailing blanks
########################################################################
($var,$val) = split("=",$work);
if (! defined $var) {$var = "";}
if (! defined $val) {$val = "";}

if (length($var) > 45) {
($var,$val) = split(":",$work);
if (! defined $var) {$var = "";}
if (! defined $val) {$val = "";}
}

if ( ($var eq "") and ($val eq "") ) {next line};
if ( $work =~ "SQL1611W") {next line}; # no data

$var = uc xmpPerl::trim($var);
$val = xmpPerl::trim($val);

if (length($val) > 1024) {
$val = "...".substr($val,length($val)-1020);
}

#print "=>VAR=$var VAL=$val\n";
$f_done = 0;

########################################################################
# Kind of information
########################################################################
if ($var =~ "DATABASE SNAPSHOT"){
$f_db = 1;
($f_inst, $f_bp, $f_ds, $f_appl, $f_ts, $f_tb, $f_lock) = 0;
print "DATABASE SECTION ............. --------\n";
$database = "";
$dbh->commit || die $dbh->errstr;
}

if ($var =~ "BUFFERPOOL SNAPSHOT"){
$f_bp = 1;
($f_inst, $f_db, $f_ds, $f_appl, $f_ts, $f_tb, $f_lock) = 0;
print "BUFFERPOOL SECTION ...........\n";
$bufferpool = "";
$dbh->commit || die $dbh->errstr;
}

if ($var =~ "DYNAMIC SQL SNAPSHOT"){
$f_ds = 1;
($f_inst, $f_db, $f_bp, $f_appl, $f_ts, $f_tb, $f_lock) = 0;
print "DYNAMIC SQL SECTION ..........\n";
$group_id = 0;
$dbh->commit || die $dbh->errstr;
}

if ($var =~ "APPLICATION SNAPSHOT"){
$f_appl = 1;
($f_inst, $f_db, $f_bp, $f_ds, $f_ts, $f_tb, $f_lock) = 0;
print "APPLICATION SECTION ..........\n";
$applname = "";
$applhandle = "";
$dbh->commit || die $dbh->errstr;
}

if ($var =~ "TABLESPACE SNAPSHOT"){
$f_ts = 1;
($f_inst, $f_db, $f_bp, $f_ds, $f_appl, $f_tb, $f_lock) = 0;
print "TABLESPACE SECTION ...........\n";
$tablespace = "";
$dbh->commit || die $dbh->errstr;
}

if ($var =~ "TABLE SNAPSHOT"){
$f_tb = 1;
($f_inst, $f_db, $f_bp, $f_ds, $f_appl, $f_ts, $f_lock) = 0;
print "TABLE SECTION ................\n";
$table = "";
$dbh->commit || die $dbh->errstr;
}

if ($var =~ "DATABASE LOCK SNAPSHOT") {
$f_lock = 1;
($f_inst, $f_db, $f_bp, $f_ds, $f_appl, $f_ts, $f_tb) = 0;
print "LOCKING SECTION ...............\n";
$database = "";
$applname = "";
$applhandle = 0;
$lock_objno = 0;# V2.3
$dbh->commit || die $dbh->errstr;
}

########################################################################
# Database Section
########################################################################
if (    (($f_db) or ($f_bp) or ($f_ds) or ($f_ts) or ($f_tb) or ($f_lock))
   and  ($var =~ "DATABASE NAME")
   and  (defined $val)
   and  ($val ne "")) {
$database = $val;
print "- Database info detected for : $database\n";
$f_done   = 1;
}

########################################################################
# Bufferpool Section
########################################################################
if (    ($f_bp)
   and  ($var =~ "BUFFERPOOL")
   and  (defined $val)
   and  ($val ne "")) {
$bufferpool = $val;
print "- Bufferpool info detected ..: $bufferpool\n";
$f_done     = 1;
}

########################################################################
# Application or Lock Section
########################################################################
if ( ($f_appl) or ($f_lock) ) {
###############################################################
# Application Name
###############################################################
if (     ($var =~ "APPLICATION NAME")
    and  (defined $val)
      and  ($val ne "")) {
$applname = $val;
print "- Application info detected  : $applname $applhandle\n";
#$f_done     = 1;
########################################################
# rename previous entries, if existing
########################################################
if ($f_appl) {
$stmt = "UPDATE xmpDBA.SNAP_APPL ".
"SET APPLNAME = '$applname' ".
"WHERE PROJECT = '$opt_mp' ".
"AND   SNAP_TS = '$snap_ts' ".
"AND   APPLNAME = '$applhandle'";
}
if ($f_lock) {
$stmt = "UPDATE xmpDBA.SNAP_LOCK ".
"SET APPLNAME = '$applname' ".
"WHERE PROJECT = '$opt_mp' ".
"AND   DATABASE = '$database' ".
"AND   SNAP_TS = '$snap_ts' ".
"AND   APPLNAME = '$applhandle' ".
"AND   APPLHANDLE = $applhandle";
}

$sth = $dbh->prepare($stmt) || die "Statement $stmt";
$sth->execute() || die "Statement $stmt\n".$dbh->errstr;
}
###############################################################
# Application Handle
###############################################################
if (     ($var =~ "APPLICATION HANDLE")
    and  (defined $val)
      and  ($val ne "")) {
$applname = $val;
$applhandle = $val;
$f_done     = 1;
}
###############################################################
# Lock Object Number# V2.3
###############################################################
if (     ($var =~ "LOCK OBJECT NAME")
    and  (defined $val)
      and  ($val ne "")) {
$lock_objno = $valn;
}
}

########################################################################
# Tablespace Section
########################################################################
if (    ($f_ts)
   and($var =~ "TABLESPACE NAME")
   and  (defined $val)
   and  ($val ne "")) {
$tablespace = $val;
print "- Tablespace info detected ..: $tablespace\n";
$f_done     = 1;
}

########################################################################
# Table Section
########################################################################
if (    ($f_tb)
   and($var =~ "TABLE NAME")
   and(defined $val)
   and($val ne "")) {
$table = $val;
print "- Table info detected .......: $table\n";
$f_done     = 1;
}

########################################################################
# Part of Dynamic SQL Statement Snapshot
########################################################################
if (    ($f_ds)
   and($var =~ "NUMBER OF EXECUTIONS")
   and(defined $val)
   and($val ne "")) {
$group_id++;
print "- Dynamic SQL Stmt detected  : $group_id\n";
}


########################################################################
# other snapshot information
########################################################################
if (    (! $f_done)
   and(defined $val)
   and  ($val ne "")){

################################################################
# split line: left text (char), right number (if numerical)
################################################################
#print "$var=$val ---";
$valc = $val;
$valn = whenisnum($valc);
$valc =~ s/[']/´/g;# format '->´
#print "CHAR=$valc NUM=$valn\n";

################################################################
# insert record into SNAP_DB table
################################################################
if ($f_db) {
$stmt = "INSERT INTO xmpDBA.SNAP_DB ".
"(PROJECT, DATABASE, SNAP_TS,VAR,VALC,VALN) ".
"VALUES('$opt_mp', '$database',".
"'$snap_ts','$var','$valc',$valn)";
$sth = $dbh->prepare($stmt) || die "Statement $stmt";
$sth->execute() || die "Statement $stmt\n".$dbh->errstr;
$n_total++;
$n_db++;
}
################################################################
# insert record into SNAP_BP table
################################################################
if ($f_bp) {
$stmt = "INSERT INTO xmpDBA.SNAP_BP ".
"(PROJECT, DATABASE, BUFFERPOOL, SNAP_TS,VAR,VALC,VALN) ".
"VALUES('$opt_mp', '$database', '$bufferpool',".
"'$snap_ts','$var','$valc',$valn)";
$sth = $dbh->prepare($stmt) || die "Statement $stmt";
$sth->execute() || die "Statement $stmt\n".$dbh->errstr;
$n_total++;
$n_bp++;
}
################################################################
# insert record into SNAP_DYNSQL table
################################################################
if ($f_ds) {
$stmt = "INSERT INTO xmpDBA.SNAP_DYNSQL ".
"(PROJECT, DATABASE, SNAP_TS, GROUP_ID, VAR,VALC,VALN) ".
"VALUES('$opt_mp', '$database', ".
"'$snap_ts', $group_id, '$var','$valc',$valn)";
$sth = $dbh->prepare($stmt) || die "Statement $stmt";
$sth->execute() || die "Statement $stmt\n".$dbh->errstr;
$n_total++;
$n_ds++;
}
################################################################
# insert record into SNAP_APPL table
################################################################
if ($f_appl) {
#print "$opt_mp $database $applname $var $valc\n";
$stmt = "INSERT INTO xmpDBA.SNAP_APPL ".
"(PROJECT, APPLNAME, APPLHANDLE, SNAP_TS,VAR,VALC,VALN) ".
"VALUES('$opt_mp','$applname', $applhandle, '$snap_ts','$var','$valc',$valn)";
$sth = $dbh->prepare($stmt) || die "Statement $stmt";
$sth->execute() || die "Statement $stmt\n".$dbh->errstr;
$n_total++;
$n_appl++;
}
################################################################
# insert record into SNAP_TS table
################################################################
if ($f_ts) {
$stmt = "INSERT INTO xmpDBA.SNAP_TS ".
"(PROJECT, DATABASE, TABLESPACE, SNAP_TS,VAR,VALC,VALN) ".
"VALUES('$opt_mp', '$database', '$tablespace', ".
"'$snap_ts','$var','$valc',$valn)";
$sth = $dbh->prepare($stmt) || die "Statement $stmt";
$sth->execute() || die "Statement $stmt\n".$dbh->errstr;
$n_total++;
$n_ts++;
}
################################################################
# insert record into SNAP_TB table
################################################################
if ($f_tb) {
#print "$opt_mp $database $table $var $valc\n";
$stmt = "INSERT INTO xmpDBA.SNAP_TB ".
"(PROJECT, DATABASE, TABLE, SNAP_TS,VAR,VALC,VALN) ".
"VALUES('$opt_mp', '$database', '$table',".
"'$snap_ts','$var','$valc',$valn)";
$sth = $dbh->prepare($stmt) || die "Statement $stmt";
$sth->execute() || die "Statement $stmt\n".$dbh->errstr;
$n_total++;
$n_tb++;
}
################################################################
# insert record into SNAP_LOCK table
################################################################
if ($f_lock) {
#print "$opt_mp $database $applname $var $valc\n";
$stmt = "INSERT INTO xmpDBA.SNAP_LOCK ".
"(PROJECT, DATABASE, APPLNAME, APPLHANDLE,".
" LOCK_OBJNO, SNAP_TS,VAR,VALC,VALN) ".
"VALUES('$opt_mp','$database','$applname', $applhandle, $lock_objno, '$snap_ts','$var','$valc',$valn)";
$sth = $dbh->prepare($stmt) || die "Statement $stmt";$sth->execute() || die "Statement $stmt\n".$dbh->errstr;
$n_total++;
$n_lock++;
}

}
}

########################################################################
# OPTION -DEACTIVATE: Deactivate database, if requested
########################################################################
if (defined $opt_deactivate) {
print "Database Deactivation .......: requested ...\n";
$work = xmpPerl::doCmdOut("db2 deactivate db ".$tdb_name[$i]);
if (   ($work =~ 'DB20000I')
   or  ($work =~ 'SQL1495W')
   or  ($work =~ 'SQL1496W') ) {
print "                               ".$tdb_name[$i]." deactivated! (".substr($work,0,8).")\n";}
else {print "                               ".$tdb_name[$i]." deactivation failed, $work\n";
}
}

}

################################################################################
# Disconnect from DWH Source Database
################################################################################
if ($n_total > 0) {
$sth->finish()   || die $dbh->errstr;
}
$dbh->commit     || die $dbh->errstr;
$dbh->disconnect || die $dbh->errstr;

print "Final Report ................: (no. of records inserted into $opt_tdb)\n";
print "Records inserted totally ....: $n_total\n";
print "- Instance records ..........: $n_inst\n";# V2.3
print "- Database records ..........: $n_db\n";
print "- Bufferpool records ........: $n_bp\n";
print "- Application records .......: $n_appl\n";
print "- Locking records ...........: $n_lock\n";
print "- Dynamic SQL records .......: $n_ds\n";
print "- Tablespace records ........: $n_ts\n";
print "- Table records .............: $n_tb\n";

################################################################################
# OPTION -RESET: Reset DB2 internal monitor areas requested
################################################################################
if (defined $opt_reset) {
print "Reset DB2 monitor areas .....: requested ...\n";
if ($opt_mdb eq "*") {
$work = xmpPerl::doCmdOut("db2 reset monitor all");
if ($work =~ 'DB20000I') {
print "                               reset for ALL!\n";}
else {print "                               failed for ALL! $work\n";
}
}
else {
################################################################
# Reset all databases
################################################################
for ($i = 1; $i<=$tdb ; $i++) {
$work = xmpPerl::doCmdOut("db2 reset monitor for db ".$tdb_name[$i]);
if ($work =~ 'DB20000I') {
print "                               ".$tdb_name[$i]." reset!\n";}
else {print "                               ".$tdb_name[$i]." reset failed! $work\n";
}

}
}
}

print "***this is the end***\n";

exit 0;


#################################################################################
########################################################## Sub Routines #########
#################################################################################

#################################################################################
# whenisnum: return numerical value, otherwise return 0
#################################################################################
sub whenisnum {
my $val = $_[0];
my $valn = 0;
#########################################################################
# Numerical, leading/trailing sign, decimal point or comma
#########################################################################
#print "VAL=$val<===";
if (   ($val !~ /[A-Za-z]/  )# not alpha
   and ($val !~ /\:/  )# no :'s
   and ($val !~ /\//  )# no /'s
   and ($val !~ " " )    # no blank in digits
   #and ($val le "2147483647")# V2.1
   #and (length($val) <= 15)# V2.1 (DEC(23,6)!!!)
   and (  ($val =~ /\d+/ )    # trivial digits
       or ($val =~ /[+-]?\d+\.?\d+?/ ) # signed dotted digits
   )   ) {
$valn = $val;
}
else {$valn = 0;
#print "VAL=$val<=== not numerical <=-=-=-=\n";
}
return $valn;
}

#################################################################################
# doSQLCommit: Executes an SQL COMMIT statement
#################################################################################
sub doSQLCommit {
$stmt = "COMMIT";
$sth = $dbh->prepare($stmt) || die "Statement $stmt";
$sth->execute() || die "Statement $stmt\n".$dbh->errstr."\n";
}

#################################################################################
# SaySyntax: Show calling syntax
#################################################################################
sub SaySyntax {
die "Usage: [perl] $0 -tdb db [-u parm -p parm] -mp text [-mdb db|*] [options]\n".
"-tdb  monitor information database \n".
"-tu   authorized user (or lP_LUS)\n".
"-tp   password (or lP_LPW)\n".
"-mp   monitor project (textual description)\n".
"-mdb  [opt.] database(s) to be monitored, or * (dflt.)\n".
"-clear [n]  [opt.] for housekeeping activities (also use -mp),\n".
"    n=retention period in days,\n".
"    use -clear in combination with -mp '*'\n".
"-reset      [opt.] after taking the snapshot\n".
"-resetonly  [opt.] reset w/o taking a snapshot\n".
"-activate   [opt.] only activates database(s)\n".
"-deactivate [opt.] deactivates database(s) after taking\n".
"           the monitor snapshot\n".
"abends";
}

© Gernot Ruban