Perl Script to automate starting and stopping applications for Documentum Suite on All Unix based platforms.

0

Category:

Perl Script to automate starting and stopping applications for Documentum Suite on All Unix based platforms.


Please do changes as per your environment variables and path settings.

Put it onto your server and call it:
perl dmServices.pl


#!/usr/bin/perl
#########################WARNING-WARNING-WARNING#########################
# Use at your own RISK-please test it on your test environment before you
# jump onto the production .
#########################WARNING-WARNING-WARNING#########################

# Perl Modules required
# If you don't have these modules, this script will not run.
use strict;
use diagnostics;
use File::Basename;
use IPC::Open2;
use strict;
use diagnostics;
use Term::ANSIColor qw(:constants);

use vars qw (*INPUTP *PIPEOUT $CLIENTID $STOP $counter $path);

# Used to abort the script if set to 0;
$STOP = -1;

# Used for the data pipe
$CLIENTID = 0;

#$path = $ENV{'PWD'};
$path = "/tmp/docscript";

sub open_pipe
{
# Open the bi-directional data pipe which is very handy if
# we need to read the output from the command.

printf("Not working yet ... \n");
return;

my $cmd;
$cmd = $_[0];

$cmd='cd /opt;ls';

$CLIENTID = open2(\*PIPEOUT, \*INPUTP, $cmd);
if($CLIENTID == 0)
{ printf("CLIENTID = 0 ...\n");}

if(() eq '')
{ printf("PIPE Connection Error - Must Abort\n"); exit(1);}

printf("PIPE OPEN\n");
}

sub close_pipe
{
# Close all connections

close INPUTP;
close PIPEOUT;

# Kill of the PIPEID process
if($CLIENTID != 0 && kill 0 => $CLIENTID)
{ kill 9 => $CLIENTID;}

waitpid($CLIENTID, 0);
printf("PIPE PID has been terminated\n");
}

sub pipecmd
{
# Commands run through the data pipe
# Takes the input string and pipes out to execute the command

my $str;
$str = $_[0];

# Signal handler in case the pipe is broken.
$SIG{PIPE} = sub {printf("PIPEN has been broken - must Abort\n");};

print INPUTP "$str\n";
}

sub create_path
{
if (! -d $path)
{
my $localcmd;
$localcmd = "mkdir -p $path";
system($localcmd);
}
if (! -d $path) {printf("$path could not be created.\n");exit(0);}
}

sub remove_path
{
if (-d $path)
{
my $localcmd;
my $dir;
$dir = basename $path;
$localcmd = "cd /tmp;rm -rf $dir";
system($localcmd);
}
}

sub promptUser
{
my ($promptString,$defaultValue);
($promptString,$defaultValue) = @_;

$promptString = "->" unless defined $promptString;
$defaultValue = "->" unless defined $defaultValue;

if ($defaultValue) {
print $promptString, "[", $defaultValue, "]: ";
} else {
print $promptString, ": ";
}

$| = 1; # force a flush after our print

# If no activity on the prompt for 2 mins then set the interrupt
# to alarm and we will exit.
eval
{
local $SIG{ALRM} = sub {printf("No activity for 2 mins - Doc Services aborts\n"); exit(0);};
alarm 120;
eval
{
$_ = ; # get the input from STDIN (presumably the keyboard)
# Remove '\n'
chomp;
};
alarm 0;
};
alarm 0;

$_ = 'noentry' unless defined $_;
if($_ =~ /[a-z]/ || $_ =~ /[A-Z]/) {
# This is text;
return $_;
}

if($_ == 0) {return 0;}
if ("$defaultValue") {
return $_ ? $_ : $defaultValue; # return $_ if it has a value
} else {
return $_;
}

}
######################################################################
### Main
######################################################################

&input_handler();
exit(0);

######################################################################
### Subroutines
######################################################################
sub input_handler
{

my $done;
$done = 0;
my ($action, $indicator, $userid);

$userid = getpwuid($>);

while($done == 0)
{
system("clear");

&setupSigHdlrs();
$action =0;

printf("--------------------------------------------\n");
printf(" Welcome $userid to DOC SERVICES\n");
printf("--------------------------------------------\n");
if($userid ne "docinst")
{
printf("--> CURRENT USERID: $userid\n");
printf("---------------------------------------------------\n");
printf("WARNING: To perform maintenance activities on\n");
printf("DOCUMENTUM you MUST be logged in as userid: docinst\n");
printf("otherwise any processes you start or stop will not\n");
printf("operate properly.\n\n");
printf("**********YOUR SESSION IS BEING ABORTED.***********\n");
printf("---------------------------------------------------\n");
exit(0);
}
printf("Please choose one of the options:\n\n");
printf(" --> 1. START Documentum services.\n");
printf(" --> 2. STOP Documentum services.\n");
printf(" --> 3. QUERY Documentum processes.\n");
printf(" --> 4. START Application Server.\n");
printf(" --> 5. START Method Server.\n");
printf(" --> 6. STOP Application Server.\n");
printf(" --> 7. STOP Method Server.\n");
printf(" --> 8. QUERY JAVA Server Process.\n");
printf(" --> 9. START Oracle 10g Server.\n");
printf(" -->10. START Oracle DB Console.\n");
printf(" -->11. STOP DOCBROKER.\n");
printf(" -->12. START DOCBROKER.\n");
printf(" -->13. STOP FULLTEXT SERVER/AGENT.\n");
printf(" -->14. START FULLTEXT SERVER/AGENT.\n");
printf(" -->15. QUERY Index Node Controller.\n");
printf("--------------------------------------------\n");
printf(" --> QUIT: CTRL ^C\n");
printf("--------------------------------------------\n");
printf("--------------------------------------------\n");
$action = &promptUser("Enter numeric option: (1-14): \n");

&setenv_vars();

if($action == 1)
{
system("clear");
&startup_services();
}
elsif($action == 2)
{
system("clear");
&stop_services();
}
elsif($action == 3)
{
system("clear");
my $input = "query";
&terminate_processes($input);
}
elsif($action == 4)
{
system("clear");
my $input = 1;
&handle_apache_server($input);
}
elsif($action == 5)
{
system("clear");
my $input = 4;
&handle_apache_server($input);
}
elsif($action == 6)
{
system("clear");
my $input = 2;
&handle_apache_server($input);
}
elsif($action == 7)
{
system("clear");
my $input = 3;
&handle_apache_server($input);
}
elsif($action == 8)
{
system("clear");
my $input = "java";
&terminate_processes($input);
}
elsif($action == 9)
{
system("clear");

printf("Manually perform the following:\n");
printf(" --> su - oracle\n");
printf(" --> password: oracle1\n");
printf(" --> cd \$ORACLE_HOME\n");
printf(" --> bin/sqlplus /nolog\n");
printf(" --> connect SYS as SYSDBA\n");
printf(" --> oracle1\n");
printf(" --> startup\n");
printf(" --> quit\n");

}
elsif($action == 10)
{
system("clear");

printf("Manually perform the following:\n");
printf(" --> su - oracle\n");
printf(" --> password: oracle1\n");
printf(" --> cd \$ORACLE_HOME\n");
printf(" --> bin/emctl start dbconsole\n");

}
elsif($action == 11)
{
system("clear");
&stop_dmdocbroker();
}
elsif($action == 12)
{
system("clear");
&check_dmdocbroker();
}
elsif($action == 13)
{
system("clear");
&stop_fulltext();
}
elsif($action == 14)
{
system("clear");
&start_fulltext();
}
elsif($action == 15)
{
system("clear");
&get_nctrl();
}

$action = &promptUser("Would you like to exit ?\n");
$action = lc($action);
if($action eq 'y')
{
printf("\n\n\n\n\nTHANK YOU FOR USING DOC SERVICES.\n");
$done = 1;
}
$action = 0;
}}

sub start_oracle
{
printf("Functionality not working yet ...\n");

# Will start the Oracle database or DBconsole
my $inputstr;
$inputstr = $_[0];

&open_pipe();
sleep(1);
&pipecmd("su - oracle;oracle1");

# Setup the environment variables
# $ENV{'ORACLE_HOME'} = '/opt/oracle/oracle/product/10.2.0/db_1';

if($inputstr eq "startup")
{
printf("Performing Oracle Startup ...\n");

# Send in the SQL commands to start up the database
&pipecmd("cd \$ORACLE_HOME;bin/sqlplus /nolog");
&pipecmd("");

while()
{
if(/SQL>/i)
{
# We are in
&pipecmd("connect SYS as SYSDBA");

# Need to sleep 2 secs or we will send in the password before the prompt
# is ready.
sleep(2);
&pipecmd("oracle1");
next;
}
if(/Connected./i)
{
&start_oracle_server();
last;
}
}

sleep(2);
}
if($inputstr eq "console")
{
printf("Performing DBCONSOLE Startup ...\n");

&start_oracle_dbconsole();
}
}

sub start_oracle_server
{
&pipecmd("startup");

while()
{
printf("STARTUP: $_");
}
&pipecmd("quit");
}

sub start_oracle_dbconsole
{
# Startup the oracle database console
&pipecmd("bin/emctl start dbconsole");
while()
{
printf("DBCONSOLE: $_");
}
}

sub terminate_processes
{
# User has the option of passing in parmeter 'query' or 'java' which will
# output processes instead of killing them.

&create_path();
my $pidout = "$path\/pidout.txt";


$counter = 0;

my $input;
$input = $_[0];
$input = 'null' unless defined $input;

my ($pidcnt, $asked, $localcmd, $printonce, $index);
$asked = 'q';
$printonce = 0;
$pidcnt = 0;
$index = 9;

$localcmd = "/bin/ps -fU docinst > $pidout";
system($localcmd);


open(PIDOUT, "< $pidout");
while()
{
if($input eq "java")
{
if($_ =~ /.\/java/i)
{
if ($printonce == 0)
{
printf("\n\n******* DOC Services QUERY JAVA Report *******\n");
$printonce = 1;
}
printf("JAVA: $_\n");
$pidcnt ++;
}
next;
}
if($_ =~ /.\/documentum/i ||
$_ =~ /.\/dmdocbroker/i)
{
$counter ++;

# Documentum processes are still alive.
my @psarray;
undef(@psarray);
@psarray = split(/ /, $_);
#$psarray[2] =~ s/\S+$//;
#$psarray[2] =~ s/^\S+//;
my $i;
for($i=0;$i<=5;$i++)
{
my $value;
$value = $psarray[$i];
if($value =~ s/\d+$//)
{ $index=$i;last;}
}
if ($index >= 5) {printf("Invalid PID ... skipping\n"); next;}
if ($input eq 'query')
{
if ($printonce == 0)
{
printf("\n\n******* DOC Services QUERY Report *******\n");
$printonce = 1;
}
printf("Owner: $psarray[1] PID: $psarray[$index]\n $_\n");
$pidcnt = $counter;
next;
}

if ($asked eq 'q' || $asked eq 'n')
{
$asked = &promptUser("One of more Documentum processes found\n Would you like to terminate ALL ?\n\n");
$asked = lc($asked);
}

if ($asked eq 'y')
{
printf("Terminating process: $psarray[1] : $psarray[$index]\n");
$localcmd = "kill $psarray[$index]";
my $rc = system($localcmd);
if ($rc != 0)
{
printf("[FAILED] Could not terminate process: $psarray[$index]\n");
$STOP = 0;
}
else
{
$counter = $counter -1;
}
}
else
{
printf("[INFO] Process: $psarray[1] : $psarray[$index] lives\n");
$STOP = 0;
}
printf("[INFO] Documentum processes still alive: $counter.\n");
$pidcnt = $counter;
}
}
close(PIDOUT);
if($pidcnt == 0) {printf("\n\nNO PROCESSES FOUND\n");}
&remove_path();
}

sub setupSigHdlrs
{
# Re-define any signal handlers, as necessary.

# Catch Ctrl-C
$SIG{INT} = \&HandleCtrlC;

}

sub HandleCtrlC
{
# A cleanup routine called when the user types ctrl-C.
if($CLIENTID != 0)
{
#&close_pipe();
}
printf("\nControl ^C encountered - exiting.\n");
exit()
}

sub startup_services
{
# start the documentum services
my $localcmd;

printf("INITIATE STARTUP ...\n\n");

# Check for documentum processes
&terminate_processes();

printf(" CHECKING safety indicator ...\n");
# Check if safe to proceed.
&check_safety();

&setenv_vars();
# Added this here as it seems to work instead of letting the
# dm_start_dbdoc_sh19necm1_r01 script try to bring it up.
printf(" STARTING DM_DOCBROKER ...\n\n\n");
system("cd /opt/documentum/dba;./dm_launch_Docbroker");
sleep(2);

printf("\n\n STARTING DATABASE: dbdoc_sh19necm1_r01 ...\n\n\n");
# Repository
$localcmd = ('cd /opt/documentum/dba;./dm_start_dbdoc_sh19necm1_r01');
system($localcmd);

# Start Tomcat for web services.
&handle_apache_server(1);

# Start Method Server.
#&handle_apache_server(4);

&start_fulltext();

&check_dmdocbroker();

printf("\n\n\n\n");
printf("=======================================================\n");
printf("NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE NOTE\n");
printf("=======================================================\n");
printf("You must close your browser and restart using the static\n");
printf("IP address for the DOCUMENTUM services to connect to.\n");
printf("Failure to do so will RESULT in no connectivity.\n");
printf("=======================================================\n");

printf("\n\nSTARTUP INITIATE COMPLETED.\n");
}

sub start_fulltext
{
my $localcmd;

printf(" \n\n STARTING INDEX SERVER ...\n\n\n");
#Index Server
$localcmd = ('cd /opt/documentum/fulltext/IndexServer/bin;./startup.sh');
system($localcmd);

printf(" \n\n STARTING INDEX AGENT ...\n\n\n");
# Index Agent
$localcmd = ('cd /opt/documentum/shared/IndexAgents/IndexAgent1;./startupIndexAgent.sh');
system($localcmd);

&get_nctrl();

}

sub get_nctrl
{
my $localcmd;

printf("Obtaining Node Controller status Info ...");
sleep(3);
$localcmd = ('cd /opt/documentum/fulltext/IndexServer/bin; nctrl sysstatus');
system($localcmd);
}

sub handle_apache_server
{
# Handler to start or stop application server or
# Documentum Method Server.
# INPUT: 1 = START APPLICATION SERVER
# 2 = STOP APPLICATION SERVER
# 3 = STOP METHOD SERVER
# 4 = START METHOD SERVER

my ($localcmd,$input);
$input = $_[0];

if ($input == 1)
{
printf(" STARTING APACHE TOMCAT services ...\n");
# Tomcat for web services.
$ENV{'CATALINA_HOME'} = '/opt/apache/tomcat';
$ENV{'CATALINA_BASE'} = '/opt/apache/tomcat';
$localcmd = 'cd /opt/apache/tomcat/bin;./startup.sh';
system($localcmd);
}
if ($input == 2)
{
printf(" STOPPING APACHE TOMCAT services ...\n");
# Tomcat for web services.
$ENV{'CATALINA_HOME'} = '/opt/apache/tomcat';
$ENV{'CATALINA_BASE'} = '/opt/apache/tomcat';
$localcmd = 'cd /opt/apache/tomcat/bin;./shutdown.sh';
system($localcmd);
}
if ($input == 3)
{
printf(" STOPPING Documentum Method Server ...\n");
printf("\n\n THIS IS NOT RECOMMENDED !!!\n");

$ENV{'CATALINA_HOME'} = '/opt/documentum/shared/tomcat/5.0.28';
$ENV{'CATALINA_BASE'} = '/opt/documentum/shared/tomcat/5.0.28';
$localcmd = 'cd /opt/documentum/shared/tomcat/5.0.28/bin;./shutdown.sh';
system($localcmd);
}
if ($input == 4)
{
printf(" STARTING Documentum Method Server ...\n");
printf("\n\n THIS IS NOT RECOMMENDED !!!\n");
printf(" COMMAND CANCELLED.\n");
$ENV{'CATALINA_HOME'} = '/opt/documentum/shared/tomcat/5.0.28';
$ENV{'CATALINA_BASE'} = '/opt/documentum/shared/tomcat/5.0.28';
$localcmd = 'cd /opt/documentum/shared/tomcat/5.0.28/bin;./startup.sh';
system($localcmd);
}
}
sub check_dmdocbroker
{
my $found;
$found = -1;
&create_path();
system("/bin/ps -ef > $path\/dmdocb.txt");
open(DMOUT, "< $path\/dmdocb.txt");
while()
{
if($_ =~ /.\/dmdocbroker/i)
{
$found = 0;
last;
}
}
close(DMOUT);
&remove_path();
if($found != 0)
{
my $action;
printf("Docbroker failed to start therefore\n");
$action = &promptUser("would you like to start manually ?\n");
$action = lc($action);
if ($action eq 'y')
{
&setenv_vars();
# Docbroker failed to start so kick it manually.
system("cd /opt/documentum/dba;./dm_launch_Docbroker");
printf("\n**DM_DOCBROKER STARTED MANUALLY FROM SCRIPT**\n");
}
else { printf("\n**Please investigate DOCBROKER LOG\n /opt/documentum/dba/log/docbroker...**\n");}
}
}

sub stop_dmdocbroker
{
my $localcmd;

printf(" Stopping DM DOCBROKER ...\n");
$localcmd = 'cd /opt/documentum/dba;./dm_stop_Docbroker';
system($localcmd);
}

sub stop_services
{
my $localcmd;

printf("SHUTDOWN INITIATING ...\n\n");
sleep(2);

&stop_fulltext();

sleep(2);
# Stop Apache and Documentum Method Server
# &handle_apache_server(3); <-- This causes errors so don't do
&handle_apache_server(2);

printf(" Stopping repository ...\n");
$localcmd = 'cd /opt/documentum/dba;./dm_shutdown_dbdoc_sh19necm1_r01';
system($localcmd);

&stop_dmdocbroker();

sleep(5);
# Check for any documentum processes
&terminate_processes();

}

sub stop_fulltext
{
my $localcmd;

printf(" Stopping Index Agent ...\n");
$localcmd = 'cd /opt/documentum/shared/IndexAgents/IndexAgent1;./shutdownIndexAgent.sh';
system($localcmd);

printf(" Stopping Index Server ...\n");
$localcmd = 'cd /opt/documentum/fulltext/IndexServer/bin;./shutdown.sh';
system($localcmd);

&get_nctrl();
}

sub check_safety
{

if ($STOP == 0 && $counter > 0)
{
# Not safe to continue.
printf("ERROR: It is unsafe to continue ... \n");
printf(" Check for previous errors!\n\n");
exit(0);
}
}

sub setenv_vars
{
# Setup the environment just in case we are not being called from
# a profile that has the proper ENV.
$ENV{'ORACLE_SID'} = 'DBDOC';
$ENV{'DOCUMENTUM_SHARED'} = '/opt/documentum/shared';
$ENV{'LD_LIBRARY_PATH'} = '/opt/documentum/fulltext/IndexServer/lib:/opt/documentum/fulltext/fast40:/opt/documentum/shared/dfc:/opt/documentum/shared/IndexAgents/ftintegrity';
$ENV{'LC_ALL'} = 'C';
$ENV{'FASTSEARCH'} = '/opt/documentum/fulltext/IndexServer';
$ENV{'JAVA_HOME'} = '/opt/documentum/shared/java/1.4.2_11';
$ENV{'ORACLE_BASE'} = '/opt/oracle/oracle/product/10.2.0/db_1';
$ENV{'ORACLE_HOME'} = '/opt/oracle/oracle/product/10.2.0/db_1';
$ENV{'DISPLAY'} = 'localhost:1';
$ENV{'DM_HOME'} = '/opt/documentum/product/5.3';
$ENV{'LOGNAME'} = 'docinst';
$ENV{'DOCUMENTUM'} = '/opt/documentum';
$ENV{'PATH'} = '/usr/bin::/usr/local/bin:/usr/openwin/bin';
$ENV{'TNS_ADMIN'} = '/opt/oracle/oracle/product/10.2.0/db_1/network/admin';
}