2011-05-17 15:21:47 +00:00
#!/usr/bin/env perl
# IBM(c) 2007 EPL license http://www.eclipse.org/legal/epl-v10.html
#
#####################################################
#
2011-05-27 15:30:15 +00:00
# This script will run a reorg on the DB2 table(s)
2011-05-17 15:21:47 +00:00
# It is dependent on the setup of DB2 by the db2sqlsetup script
# It is to be set in a cron job on the xCAT Management Node and
# requires xCAT to be installed because it uses xCAT Perl libraries
# and xCAT commands
#
#####################################################
BEGIN
{
$::XCATROOT = $ENV{'XCATROOT'} ? $ENV{'XCATROOT'} : '/opt/xcat';
$::XCATDIR = $ENV{'XCATDIR'} ? $ENV{'XCATDIR'} : '/etc/xcat';
}
use lib "$::XCATROOT/lib/perl";
2011-05-17 17:01:27 +00:00
use Getopt::Long;
2011-05-17 15:21:47 +00:00
use strict;
use warnings;
# Use runsqlcmd to run the SQL query to read the list of tables in
# the XCATDB schema
# Then use runsqlcmd to run the reorg on the list of tables,
# allow read/write by other applications during reorg
2011-05-17 17:01:27 +00:00
#
2011-05-18 13:16:45 +00:00
my $tablelist;
my $help;
my $cmd;
my @tablist;
GetOptions( 'V|verbose' => \$::VERBOSE,
't=s' => \$tablelist,
'h|help' => \$help,);
if ($help) {
print "DB2 Table Reorganization utility.\n";
print
2013-02-12 19:39:20 +00:00
"This script can be set as a cron job or run on the command line to reorg the xcatdb DB2 database tables. It automatically added as a cron job, if you use the db2sqlsetup command to create your DB2 database setup for xCAT. \n";
2011-05-18 13:16:45 +00:00
print "Usage:\n";
print "\t--V - Verbose mode\n";
print "\t--h - usage\n";
print
"\t--t -comma delimitated list of tables.\n Without this flag it reorgs all tables in the xcatdb database .\n";
print "\n";
2013-03-14 14:18:54 +00:00
print "Author: Lissa Valletta\n";
2011-05-18 13:16:45 +00:00
exit 0;
}
2013-03-13 23:01:17 +00:00
require xCAT::Utils;
2011-05-19 12:05:20 +00:00
# check to see if running DB2
my $DBname = xCAT::Utils->get_DBName;
if ($DBname ne "DB2") {
print " Reorg of table only supported for DB2 database\n";
2012-04-12 05:47:36 +00:00
`logger -p local4.err -txcat " reorgtbls:Only supports DB2 database"` ;
2011-05-19 12:05:20 +00:00
exit 1;
}
2011-05-18 13:16:45 +00:00
if ($tablelist) { # input list of tables
@tablist=split(/\,/, $tablelist);
} else { # get all tables
$cmd="$::XCATROOT/sbin/runsqlcmd \"select tabname from syscat.tables where TABSCHEMA='XCATDB';\"";
@tablist = xCAT::Utils->runcmd($cmd, 0);
if ($::RUNCMD_RC != 0)
{
2012-04-12 05:47:36 +00:00
`logger -p local4.err -txcat " reorgtbls:error in select tabname from syscat.tables"` ;
2011-05-18 13:16:45 +00:00
exit 1;
}
2011-05-17 15:21:47 +00:00
}
# reorg each table
my $foundheader=0;
foreach my $table (@tablist) {
chomp $table;
2011-05-18 13:16:45 +00:00
# skip lines untils we find the header unless -t option
if (!($tablelist)) {
if ($foundheader==0) {
if ( !($table =~ /TABNAME/)) {
next;
} else {
$foundheader=1;
next;
}
2011-05-17 15:21:47 +00:00
}
}
# skip blanks and -- lines
$table =~ s/\s*//g; # remove blanks
if ($table =~ /^\s*$/) { # skip blanks
next;
}
2011-05-17 17:01:27 +00:00
if ($table =~ m/[^a-zA-Z0-9_]/) # skip non alphanumeric lines not underscore
2011-05-17 15:21:47 +00:00
{
next;
}
2011-05-18 13:16:45 +00:00
$table =~ tr/a-z/A-Z/; # convert to upper
2011-05-17 17:01:27 +00:00
if ($::VERBOSE) {
print " Reorg of table $table\n";
}
2011-05-17 15:21:47 +00:00
$cmd="$::XCATROOT/sbin/runsqlcmd \"reorg indexes all for table $table allow write access;\"";
xCAT::Utils->runcmd($cmd, 0);
if ($::RUNCMD_RC != 0)
{
2012-04-12 05:47:36 +00:00
`logger -p local4.warning -txcat " reorgtbls:error $cmd"`;
2011-05-17 15:21:47 +00:00
} else {
2012-04-12 05:47:36 +00:00
`logger -p local4.info -txcat " reorgtbls:reorg indexes for $table"`;
2011-05-17 15:21:47 +00:00
}
$cmd="$::XCATROOT/sbin/runsqlcmd \"reorg table $table inplace allow write access;\"";
xCAT::Utils->runcmd($cmd, 0);
if ($::RUNCMD_RC != 0)
{
2012-04-12 05:47:36 +00:00
`logger -p local4.warning -txcat " reorgtbls:error $cmd"`;
2011-05-17 15:21:47 +00:00
} else {
2012-04-12 05:47:36 +00:00
`logger -p local4.info-txcat " reorgtbls:reorg $table inplace"`;
2011-05-17 15:21:47 +00:00
}
}
exit 0;