Perl script xmpDB2Cfg.pl – DB2 Configuration Parameter Tool
Perl script to create UPDATE DB|DBM CFG statements for databases and instances, prints ordered list of confuguration options. Enables users to „copy“ or „save“ configurations.
#!/usr/local/bin/perl -w
################################################################################
# xmpDB2Cfg.pl - DB2 Configuration Parameter Tool
# OS System - AIX 4.3.x or Sun/Solaris 2.6 or higher
# Database - DB2 UDB V5.x or higher, works with DB2 V7.x
# Called by - Shell
# Calls - xmpPerl.pm - xmp DBA Perl Package
# Getopt - Arguments processing
# Env - Access on environment variables
# English - Aliases on Perl variables
# Options - -d database - Database Name
# -i [name -u -p] - per Instance or remote instance
# -f file - output file
# -c [file] - compare with reference file
#-------------------------------------------------------------------------------
# Autor - GR /25.06.02 - V1.0 first version
# Changes - GR /01.07.02 - V1.1 List sorted, new parm -c
# - GR /05.07.02 - V1.2 with optional attach to remote systems
# - GR /22.02.05 - V2.0 Adapted to DB2 V8.2
# - GR /14.03.05 - V2.1 Added support for DB2 env variables (db2set)
# - GR /18.07.11 - V3.0 Adapted to DB2 V9.7 and several amendments
#-------------------------------------------------------------------------------
# Comments - PERL5LIB environment variable must point to perl module path!
# - Parameter reference files must have been created in advance.
# (store at /opt/xmpdba/ctrl)
################################################################################
################################################################################
# Modules
################################################################################
use strict;
use Getopt::Long;
use vars qw($opt_i $opt_u $opt_p $opt_d $opt_f $opt_c);
use xmpPerl;
use Env;
use English;
use File::stat;
use File::Basename;
use Time::localtime;
################################################################################
# Declare/Init
################################################################################
my $VER = "3.0";
my $work = "";
my @work;
my $inst = "";
my $var;
my $val;
my $cmt;
my $lbr = 0;
my $rbr = 0;
my $hk = '"';
my $stmt = "";
my ($i,$j) = 0;
my $reffile = "";
my $refpos = 0;
my $DB2Version = ""; # V2.0
################################################################################
# Declare/Init: Parm Table
################################################################################
my $p = 0; # DB(M) Parms
my %p_var = (); # Variable
my %p_val = (); # Value
my %p_cmt = (); # Comment
################################################################################
# Check arguments
################################################################################
GetOptions("i:s", "u:s", "p:s", "d=s", "f=s", "c:s");
if (! defined $opt_d && ! defined $opt_i) { # Database check
print "$0 - specify either instance option or database name.\n";
SaySyntax();
}
if (defined $opt_d && defined $opt_i && $opt_i eq "") { # Database check
print "$0 - specify either instance option or database name.\n";
SaySyntax();
}
if (defined $opt_d) {
$opt_d = uc $opt_d; # Upper case
}
if (defined $opt_c && $opt_c ne "" && !-r $opt_c) {
print "$0 - cannot read reference file specified as -c $opt_c.\n";
SaySyntax();
}
################################################################################
# Determine configuration reference, if request and/or provided
################################################################################
if (defined $opt_c) {
if ($opt_c ne "") { # file name provided
$reffile = $opt_c;
if (defined $opt_d) {
$refpos = 9; # column with value
}
else { $refpos = 7; # column with value
}
}
else { if (defined $opt_d) { # database config
$reffile = xmpPerl::getCTRL."/dbref.cfg";
$refpos = 9; # column with value
}
else { $reffile = xmpPerl::getCTRL."/dbmref.cfg";
$refpos = 7; # column with value
}
}
}
################################################################################
# Print script header on terminal
################################################################################
xmpPerl::chkDB2;
xmpPerl::prtHeaderDB2(basename($0)." - DB2 Configuration Tool - V$VER");
$DB2Version = xmpPerl::getDB2Version(); # V2.0
if (defined $opt_i && $opt_i ne "") {
print "Remote Instance .............: $opt_i\n";
}
if (defined $opt_u) {
print "User for remote attach ......: $opt_u\n";
}
if (defined $opt_d) {
print "Database ....................: $opt_d\n";
}
if (defined $opt_f) {
print "UPDATE DB[M] CFG stmt file ..: $opt_f\n";
}
if (defined $opt_c) {
print "Compare to reference file ...: $reffile\n";
}
################################################################################
# Get instance name
################################################################################
$inst = xmpPerl::trim($ENV{DB2INSTANCE});
if (defined $opt_i && $opt_i ne "") {
$inst = $opt_i;
}
################################################################################
# Generate UPDATE CFG Statement into file if requested
################################################################################
if (defined $opt_f) {
############################################################################
# File für Statements öffnen, falls vorhanden
############################################################################
if (! open(hFile, "> $opt_f") )
{ die "$0 cannot write into file $opt_f \n".
"abends";
}
$work = xmpPerl::getTimestamp();
print hFile "#---------------------------------------------------------------\n";
print hFile "# Script created by ".basename($0)." - DB2 Configuration Tool - V$VER\n";
print hFile "# at $work\n";
print hFile "# for DB2 Version $DB2Version\n";
if (defined $opt_d) {
print hFile "# on instance $inst for database $opt_d \n";}
else { print hFile "# on instance $inst \n";}
print hFile "#---------------------------------------------------------------\n";
}
################################################################################
# Attach to (remote) instance, if user specified
################################################################################
if (defined $opt_i && $opt_i ne "" && defined $opt_u) {
$work = xmpPerl::doCmdOut("db2 attach to $opt_i user $opt_u using $opt_p");
if ($work =~ "SQL....N") {
die ">>> $0 - DB2 attach failed!\n".
">>> Instance: $opt_i User: $opt_u, failed with SQLCDOE\n".
">>> $work\n".
">>> abends at";
}
}
################################################################################
# Distinguish between database and database manager config
################################################################################
if (defined $opt_d) {
############################################################################
# Write informational header lines
############################################################################
$work = xmpPerl::doCmdOut("db2 get db cfg for $opt_d");
$stmt = "db2 ".$hk."update db cfg for $opt_d using ";
print "\nUpdatable DB2 Database Configuration Parameters:\n";
print "------------------------------------------------\n";
if (defined $opt_f) {
print hFile "db2 $hk"."RESET DATABASE CONFIGURATION FOR $opt_d"."$hk\n";
print hFile "db2 terminate\n";
}
}
else {
############################################################################
# Write informational header lines
############################################################################
$work = xmpPerl::doCmdOut("db2 get dbm cfg");
$stmt = "db2 ".$hk."update dbm cfg using ";
print "\nUpdatable DB2 Database Manager Configuration Parameters:\n";
print "--------------------------------------------------------\n";
if (defined $opt_f) {
print hFile "db2 $hk"."RESET DATABASE MANAGER CONFIGURATION"."$hk\n";
print hFile "db2 terminate\n";
}
}
################################################################################
# Get Database Manager or DB Configuration
################################################################################
#undef $/;
@work = split("\n",$work);
#print "$#work lines returned from GET DB CFG Command.\n";
linedb: 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) > 60) { # differs!!!
($var,$val) = split(":",$work);
if (! defined $var) {$var = "";}
if (! defined $val) {$val = "";}
}
if ( ($var eq "") and ($val eq "") ) {next linedb};
if ( $work =~ "SQL1611W") {next linedb}; # no data
$var = uc xmpPerl::trim($var);
$val = xmpPerl::trim($val);
#print "=>VAR=$var VAL=$val<=\n";
############################################################################
# Do not process following lines
############################################################################
if ( $var =~ "DATABASE CONFIGURATION FOR DATABASE") {next linedb};
if ( $var =~ "RELEASE LEVEL") {next linedb};
if ( $var =~ "FIRST ACTIVE LOG") {next linedb};
if ( $var =~ "DATABASE TERRITORY") {next linedb};
if ( $var =~ "DATABASE CODE PAGE") {next linedb};
if ( $var =~ "DATABASE CODE SET") {next linedb};
if ( $var =~ "DATABASE COUNTRY CODE") {next linedb};
if ( $var =~ "DATABASE COUNTRY/REGION CODE") {next linedb};
if ( $var =~ "USER EXIT FOR LOGGING STATUS") {next linedb};
if ( $var =~ "LOG RETAIN FOR RECOVERY STATUS") {next linedb};
if ( $var =~ "DATABASE COUNTRY CODE") {next linedb};
if ( $var =~ "MULTI-PAGE FILE ALLOCATION") {next linedb};
if ( $var =~ "BACKUP PENDING") {next linedb};
if ( $var =~ "DATABASE IS CONSISTENT") {next linedb};
if ( $var =~ "ROLLFORWARD PENDING") {next linedb};
if ( $var =~ "RESTORE PENDING") {next linedb};
if ( $var =~ "MULTI-PAGE FILE ALLOCATION") {next linedb};
if ( $var =~ "CHANGED PATH TO LOG FILES") {next linedb};
if ( $var =~ "DATABASE MANAGER CONFIGURATION") {next linedb};
if ( $var =~ "NODE TYPE") {next linedb};
if ( $var =~ "DEFAULT DATABASE MONITOR SWITCHES") {next linedb};
if ( $var =~ "HADR DATABASE ROLE") {next linedb};
if ( $var =~ "DEFAULT NUMBER OF CONTAINERS") {next linedb};
# new with DB2 V9.7
if ( $var =~ "ALL COMMITTED TRANSACTIONS HAVE BEEN WRITTEN TO DISK") {next linedb};
if ( $var =~ "DATABASE COLLATING SEQUENCE") {next linedb};
if ( $var =~ "MONITOR COLLECT SETTINGS") {next linedb};
############################################################################
# Replace following lines
############################################################################
if ( $var =~ "PATH TO LOG FILES") { $var = $var." (NEWLOGPATH)"};
# replacement deactivated with DB2 V9.7
#if ( $var =~ "DATABASE COLLATING SEQUENCE") { $var = $var." (ALT_COLLATE)"};
if (length($val) > 1024) {
$val = "...".substr($val,length($val)-1020);
}
############################################################################
# Replace (textual) values with values tolerated by DB2
############################################################################
if ( ($val =~ "AUTOMATIC")
or ($val =~ "COMPUTED") ) {
# AUTOMATIC must not be replace by -1
}
else {
if ( ($var =~ "INDEX RE-CREATION TIME")
&& ($val =~ "SYSTEM") ) {$val = "SYSTEM"}
if ( ($var =~ "CATALOG CACHE SIZE")
&& ($val =~ "MAXAPPLS") ) {$val = "-1"}
if ( ($var =~ "PACKAGE CACHE SIZE")
&& ($val =~ "MAXAPPLS") ) {$val = "-1"}
if ( ($var =~ "MAX_COORDAGENTS")
&& ($val =~ "(MAXAGENTS - NUM_INITAGENTS)") ) {$val = "-1"}
if ( ($var =~ "MAXCAGENTS")
&& ($val =~ "MAX_COORDAGENTS") ) {$val = "-1"}
if ( ($var =~ "MAX_LOGICAGENTS")
&& ($val =~ "MAX_COORDAGENTS") ) {$val = "-1"}
if ( ($var =~ "NUM_POOLAGENTS")
&& ($val =~ "(calculated)") ) {$val = "-1"}
if ( ($var =~ "MAXDARI")
&& ($val =~ "MAX_COORDAGENTS") ) {$val = "-1"}
if ( ($var =~ "FENCED_POOL")
&& ($val =~ "MAX_COORDAGENTS") ) {$val = "-1"}
if ( ($var =~ "MAX_CONNECTIONS")
&& ($val =~ "MAX_COORDAGENTS") ) {$val = "-1"}
if ( ($var =~ "ALT_COLLATE")
&& ($val =~ "UNIQUE") ) {$val = ""}
if ( ($var =~ "ALT_COLLATE")
&& ($val eq "") ) {next linedb}
if ( ($var =~ "SHEAPTHRES_SHR")
&& ($val =~ "(SHEAPTHRES)") ) {$val = xmpPerl::getDBMCfgValue("SHEAPTHRES")}
}
############################################################################
# Config Defaults
############################################################################
if ($var =~ "CPUSPEED") {
$var = "$val $var";
$val = "-1";
}
if ($var =~ "COMM_BANDWIDTH") {
$var = "$val $var";
$val = "-1";
}
#print "=>VAR=$var VAL=$val\n";
############################################################################
# Config variable name exists in line? "Text text text (VAR) = value"
############################################################################
$lbr = rindex($var,"(");
$rbr = rindex($var,")");
#print "$lbr $rbr\n";
if ( ($lbr > 0) && ($rbr > 0)) {
$cmt = substr($var,0,$lbr-1);
$cmt = xmpPerl::trim($cmt);
$var = substr($var,$lbr+1,$rbr-$lbr-1);
}
else {$cmt = ""; }
if (! defined $val) {$val = "";}
############################################################################
# AUTOMATIC or COMPUTED config value with certain value "Text = AUTOMATIC(value)"
############################################################################
if ( ($val =~ "AUTOMATIC")
or ($val =~ "COMPUTED") ) {
$lbr = rindex($val,"(");
$rbr = rindex($val,")");
#print "$lbr $rbr\n";
if ( ($lbr > 0) && ($rbr > 0)) {
$cmt = $cmt." (".substr($val,$lbr+1,$rbr-$lbr-1).")";
$val = "AUTOMATIC"
}
}
#print "=>VAR=$var VAL=$val CMT=$cmt<=\n";
$p++;
$p_var{$p} = $var;
$p_val{$p} = $val;
$p_cmt{$p} = $cmt;
}
################################################################################
# Sort table
################################################################################
for ($i = 1; $i <= $p; $i++) {
for ($j = $i+1; $j <= $p; $j++) {
if ($p_var{$j} lt $p_var{$i}) {
$var = $p_var{$i};
$val = $p_val{$i};
$cmt = $p_cmt{$i};
$p_var{$i} = $p_var{$j};
$p_val{$i} = $p_val{$j};
$p_cmt{$i} = $p_cmt{$j};
$p_var{$j} = $var;
$p_val{$j} = $val;
$p_cmt{$j} = $cmt;
}
}
}
################################################################################
# Detach from remote instance
################################################################################
if (defined $opt_i && $opt_i ne "" && defined $opt_u) {
$work = xmpPerl::doCmdOut("db2 detach");
}
################################################################################
# Print table
################################################################################
for ($i = 1; $i <= $p; $i++) {
$var = $p_var{$i};
$val = $p_val{$i};
$cmt = $p_cmt{$i};
############################################################################
# Get value of reference configuration file
############################################################################
if (defined $opt_c) {
$work = xmpPerl::doCmdOut("cat $reffile | grep -i ' $var ' | awk '{print \$"."$refpos}'");
#print "=#==>$work<===\n";
########################################################################
# If reference file does not contain a specific value, just ignore
########################################################################
if (defined $work) {
$j = rindex($work,'"');
$work = substr($work,0,$j);
if ($work eq "NULL") {$work=""}
#print "==>$var = $work -- $val ($j)\n";
}
else { $work = $val;}
}
############################################################################
# Print config values on terminal, for 'compare' just prinnt differences
############################################################################
if (!defined $opt_c or (defined $opt_c && $work ne $val )) {
$var = sprintf("%17s",$var);
$val = sprintf("%12s",$val);
print "$var : $val";
if ($cmt ne "" ) {
print " ($cmt)\n";}
else { print "\n";}
if (defined $opt_c) {
print " : ".sprintf("%12s",$work)." (DB2 DEFAULT/REFERENCE VALUE)\n";
}
}
############################################################################
# Print UPDATE CFG Statement into file if requested
############################################################################
if ( (defined $opt_f && !defined $opt_c)
or (defined $opt_f && defined $opt_c && $work ne $val) ) {
$var = sprintf("%17s",$var);
$val = sprintf("%12s",$val);
if ($val eq " ") {
$val = sprintf("%12s","NULL");
}
print hFile $stmt.$var." ".$val.$hk."\n";
}
}
################################################################################
# Distinguish between database and database manager config V2.1
################################################################################
if (!defined $opt_d) {
############################################################################
# Print DB2 Enviroment variables V2.1
############################################################################
#$work = xmpPerl::doCmdOut("db2set| awk '{print "db2set "$1}'|sort");
$work = xmpPerl::doCmdOut("db2set| sort");
@work = split("\n",$work);
#print "$#work lines returned from db2set Command.\n";
print "\nDB2 Environment Variables:\n";
print "--------------------------\n";
if (defined $opt_f) {
print hFile "# DB2 Environment Variables:\n";
print hFile "#---------------------------\n";
}
linedb: foreach $work (@work) {
print $work."\n";
if (defined $opt_f) {
####################################################################
# Do not write workload specific instructions
####################################################################
if ( index($work,"[DB2_WORKLOAD]") > 1 ) {
# do not execute, because value is workload dependent
print hFile "# db2set $work\n";
}
else {
print hFile "db2set $work\n";
}
}
}
}
################################################################################
# Close file if file requested
################################################################################
if (defined $opt_f) {
print hFile "#--------------------------------------------------------\n";
close(hFile);
chmod 0775,$opt_f;
}
print "***End***\n";
################################################################################
# SaySyntax: Show calling syntax
################################################################################
sub SaySyntax {
die "Usage: [perl] $0 {-i [name -u ... -p ...] | -d db} [-f file] [-c [file]]\n".
" -i database manager configuration\n".
" for optional db2 attach to remote system:\n".
" instname remote instance name\n".
" -u user user id\n".
" -p psw user password\n".
" -d database configuration\n".
" -f generate UPDATE DB{M} CFG statements into file\n".
" -c compare to reference configuration\n".
" default refernce files: /opt/xmpdba/ctrl:\n".
" dbmref.cfg for Database Mananager configration\n".
" dbref.cfg for Database configuration\n".
" other file your own\n".
"abends";
}
Comments
Comments are closed.