Skip to content

Commit

Permalink
ncr: TM-693: log rotation script (#1111)
Browse files Browse the repository at this point in the history
* add archive logs script

* update var

* fix

* fix

* fix

* fix
  • Loading branch information
drobinson-moj authored Nov 14, 2024
1 parent 23b72c6 commit 920d92f
Show file tree
Hide file tree
Showing 9 changed files with 162 additions and 81 deletions.
2 changes: 1 addition & 1 deletion ansible/roles/ncr-bip/defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ sap_bip_unpack_base_directory: "/u02/software/{{ sap_bip_rar_base }}"
sap_bip_extraction_directory: "/u02/software/{{ sap_bip_rar_base }}"
sap_bip_responsefiles_directory: "/u02/software/ResponseFiles"
sap_bip_installation_directory: /u01/app/bobj/BIP4
sap_web_archived_logs_directory: /u02/Archived_Logs
sap_bip_archived_logs_directory: /u02/Archived_Logs
sap_promotion_management_directory: /u02/tmpAJS

sap_bip_rar_base: BIPLATS4301P_1200-70002683 # without _P1.EXE and _P2.RAR extension
Expand Down
25 changes: 13 additions & 12 deletions ansible/roles/ncr-bip/tasks/install_bip.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@
path: /u01/app/bobj/BIP4/sap_bobj
register: bip_installed_check

- block:
- name: Create installation directory
ansible.builtin.file:
path: "{{ item }}"
state: directory
mode: 0775
owner: bobj
group: binstall
with_items:
- "{{ sap_bip_installation_directory }}"
- name: Create bobj owned directories
ansible.builtin.file:
path: "{{ item }}"
state: directory
mode: 0775
owner: bobj
group: binstall
with_items:
- "{{ sap_bip_installation_directory }}"
- "{{ sap_bip_archived_logs_directory }}"

- block:
- name: Perform pre-requesite checks
become_user: bobj
ansible.builtin.shell: |
Expand Down Expand Up @@ -49,11 +50,11 @@
# block
when: not bip_installed_check.stat.exists

- name: Copy SAPBOBJEnterpriseXI40 script
- name: Copy SAPBOBJEnterpriseXI40 scripts
ansible.builtin.template:
src: "{{ item }}"
dest: "/{{ item }}"
mode: 0775
mode: 0755
loop:
- usr/local/bin/SAPBOBJEnterpriseXI40

Expand Down
11 changes: 11 additions & 0 deletions ansible/roles/ncr-bip/tasks/setup_bobj.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,17 @@
loop:
- home/bobj/.bash_profile

- name: Copy bobj scripts
ansible.builtin.template:
src: "{{ item }}"
dest: "/{{ item }}"
owner: bobj
group: binstall
mode: 0755
loop:
- home/bobj/archive_logs.sh
- home/bobj/sap_restart.sh

- name: Copy bobj biprws helper scripts
ansible.builtin.template:
src: "{{ item }}"
Expand Down
4 changes: 2 additions & 2 deletions ansible/roles/ncr-bip/tasks/setup_tomcat_restart.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
group: binstall
state: directory
loop:
- "{{ sap_web_archived_logs_directory }}"
- "{{ sap_bip_archived_logs_directory }}"

- name: Copy tomcat restart script
ansible.builtin.template:
Expand All @@ -34,7 +34,7 @@
minute: "{{ 60 | random(seed=inventory_hostname) }}"
hour: "3"
weekday: "MON,WED,FRI"
job: "find {{ sap_web_archived_logs_directory }} -mtime +365 -prune -exec rm -rf {} \\;"
job: "find {{ sap_bip_archived_logs_directory }} -mtime +365 -prune -exec rm -rf {} \\;"
user: bobj

# block
Expand Down
42 changes: 42 additions & 0 deletions ansible/roles/ncr-bip/templates/home/bobj/archive_logs.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/bin/bash
# Managed in code modernisation-platform-configuration-management repo, ncr-bip role
# To dryrun, run like this: DRYRUN=1 ./archive-logs.sh

BOBJEDIR="{{ sap_bip_installation_directory }}/sap_bobj"
BOBJUSER="bobj"
CURRENTUSER=$(whoami)
DATESTAMP=$(date +%Y%m%d_%H%M)
ARCHLOGDIR="{{ sap_bip_archived_logs_directory }}/Archived_Logs_${DATESTAMP}"
DRYRUN=${DRYRUN:-0}

if [[ "$BOBJUSER" != "$CURRENTUSER" ]]; then
echo "ERROR: This script must be run as $BOBJUSER"
[[ $DRYRUN == 0 ]] && exit 1
fi

if pgrep -u bobj java > /dev/null; then
echo "ERROR: Please stop bobj processes before running"
[[ $DRYRUN == 0 ]] && exit 1
fi

mv_files() {
files=$(find "$1" -name "$2" 2>/dev/null)
if [[ -n $files ]]; then
if [[ ! -d $ARCHLOGDIR ]]; then
echo "Creating log file archive $ARCHLOGDIR"
[[ $DRYRUN == 0 ]] && mkdir -p "$ARCHLOGDIR"
fi
num_files=$(wc -l <<< "$files")
echo "Moving $num_files files from $1/$2 to $ARCHLOGDIR/"
[[ $DRYRUN == 0 ]] && mv "$1/"$2 "$ARCHLOGDIR/"
fi
}

if [[ -d $ARCHLOGDIR ]]; then
echo "Not archiving logs as $ARCHLOGDIR already exists"
else
mv_files "$BOBJEDIR/logging" "*.*"
mv_files "$BOBJEDIR/tomcat/logs" "*.*"
mv_files "$BOBJEDIR/tomcat/bin" "TraceLog_*"
mv_files ~ "SBOPWebapp_*"
fi
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/bash

# Script for pulling each SI_KIND set of objects in turn from the biprws interface.
# Script for pulling each SI_KIND set of objects in turn from the biprws interface.
# NOTE: SQL may need something like `SELECT TOP 10000` to ensure all objects returned

apps="AdminConsole
Expand Down
68 changes: 68 additions & 0 deletions ansible/roles/ncr-bip/templates/home/bobj/sap_restart.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#!/bin/bash
# Managed in code modernisation-platform-configuration-management repo, ncr-bip role
# This script stops SAP processes (Tomcat/CCM), archives logs, and then restarts SAP processes

BOBJEDIR="{{ sap_bip_installation_directory }}/sap_bobj"
BOBJUSER="bobj"
CURRENTUSER=$(whoami)
DRYRUN=${DRYRUN:-0}

stop_processes() {
if [ -f "$BOBJEDIR"/ccm.config ]; then
if [[ $DRYRUN != 0 ]]; then
echo "DRYRUN: Stopping servers"
else
"$BOBJEDIR"/ccm.sh -stop all
logger -p daemon.info -t bobj "Stopped servers"
fi
fi
if [ -d "$BOBJEDIR"/tomcat ]; then
if [[ $DRYRUN != 0 ]]; then
echo "DRYRUN: Stopping Tomcat"
else
"$BOBJEDIR"/tomcatshutdown.sh
logger -p daemon.info -t bobj "Stopped Tomcat"
fi
fi
}

archive_logs() {
if [[ -x /home/$BOBJUSER/archive_logs.sh ]]; then
if [[ $DRYRUN != 0 ]]; then
DRYRUN=$DRYRUN /home/$BOBJUSER/archive_logs.sh
else
echo "Archiving Logs"
/home/$BOBJUSER/archive_logs.sh | logger -p daemon.info -t bobj
fi
fi
}

start_processes() {
if [ -d "$BOBJEDIR"/tomcat ]; then
if [[ $DRYRUN != 0 ]]; then
echo "DRYRUN: Starting Tomcat"
else
"$BOBJEDIR"/tomcatstartup.sh
logger -p daemon.info -t bobj "Started Tomcat"
fi
fi
if [ -f "$BOBJEDIR"/ccm.config ]; then
if [[ $DRYRUN != 0 ]]; then
echo "DRYRUN: Starting servers"
else
"$BOBJEDIR"/ccm.sh -start all
logger -p daemon.info -t bobj "Started servers"
fi
fi
}

if [[ "$BOBJUSER" != "$CURRENTUSER" ]]; then
echo "ERROR: This script must be run as $BOBJUSER"
[[ $DRYRUN == 0 ]] && exit 1
fi

if pgrep -u bobj java > /dev/null; then
stop_processes
fi
archive_logs
start_processes
51 changes: 3 additions & 48 deletions ansible/roles/ncr-bip/templates/home/bobj/tomcat_restart.sh
Original file line number Diff line number Diff line change
@@ -1,50 +1,5 @@
#!/bin/bash
# Version 2
# Managed in code modernisation-platform-configuration-management repo, ncr-bip role
# Scheduled to run periodically via bobj cron
. /home/bobj/.bash_profile

BOBJEDIR={{ sap_bip_installation_directory }}/sap_bobj/
LOGDIR=${BOBJEDIR}logging/Tomcat_Restart_Logs/
BOBJUSER=bobj
BOXNAME={{ ec2.tags['Name'] }}
CURRENTUSER=`id | sed -e 's|).*$||' -e 's|^.*(||' `
DATESTAMP=`date +%Y%m%d_%H%M`
LOGFILE=${LOGDIR}TomcatRestart_${BOXNAME}_${DATESTAMP}.log
ARCHLOGDIR={{ sap_web_archived_logs_directory }}/Archived_Logs_${DATESTAMP}/
export LOGDIR BOBJEDIR BOXNAME LOGFILE BOBJUSER

mkdir -p $LOGDIR 2>&1 | tee -a $LOGFILE
mkdir -p $ARCHLOGDIR 2>&1 | tee -a $LOGFILE

echo "Restarting Tomcat on server -" $BOXNAME | tee -a $LOGFILE
echo "Log file:" $LOGFILE
echo "Current user:" $CURRENTUSER | tee -a $LOGFILE

if [ "$BOBJUSER" -a "$BOBJUSER" != "$CURRENTUSER" ]; then
echo "ERROR: This script must be run as" $BOBJUSER | tee -a $LOGFILE
exit 1
fi

TOMCATPID=`ps -aux | grep tomcat | grep -v grep | grep -v $(basename -- "$0") | awk '{print $2}'`
if [ -n "$TOMCATPID" ]; then
echo "Stopping Tomcat (PID = ${TOMCATPID})..." | tee -a $LOGFILE
exec ${BOBJEDIR}tomcatshutdown.sh | tee -a $LOGFILE

TOMCATPID=`ps -aux | grep tomcat | grep -v grep | grep -v $(basename -- "$0") | awk '{print $2}'`
if [ -n "$TOMCATPID" ]; then
echo "Tomcat is still running. Attempting to kill PID =" $TOMCATPID
kill -9 $TOMCATPID
fi
echo "Tomcat stopped" | tee -a $LOGFILE
else
echo "Tomcat is not running" | tee -a $LOGFILE
fi

echo "Moving log files to " $ARCHLOGDIR | tee -a $LOGFILE
mv ~/SBOPWebapp_* $ARCHLOGDIR 2>&1 | tee -a $LOGFILE
mv $BOBJEDIR/tomcat/logs/*.* $ARCHLOGDIR 2>&1 | tee -a $LOGFILE
mv $BOBJEDIR/tomcat/bin/TraceLog_* $ARCHLOGDIR 2>&1 | tee -a $LOGFILE

echo "Starting Tomcat..." | tee -a $LOGFILE
exec ${BOBJEDIR}tomcatstartup.sh | tee -a $LOGFILE
echo "Tomcat has been started" | tee -a $LOGFILE

/home/bobj/sap_restart.sh
38 changes: 21 additions & 17 deletions ansible/roles/ncr-bip/templates/usr/local/bin/SAPBOBJEnterpriseXI40
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
# On RedHat AS 3 and above, use runuser rather than su
SU="su"
if [ -f "/etc/redhat-release" -a -x "/sbin/runuser" ]; then
SU="runuser"
SU="runuser"
fi

if [ -f "/etc/redhat-release" ]; then
Expand All @@ -43,8 +43,9 @@ BOBJEDIR="{{ sap_bip_installation_directory }}/sap_bobj"
case "$1" in
'start')

# Source the env.sh file
# $SU - "$BOBJEUSERNAME" ". $BOBJEDIR/setup/env.sh"
if [ -f "/home/$BOBJEUSERNAME"/archive_logs.sh ]; then
$SU - "$BOBJEUSERNAME" -c "/home/$BOBJEUSERNAME"/archive_logs.sh | logger -p daemon.info -t bobj
fi

if [ -f "$BOBJEDIR"/ccm.config ]; then
# Source the ccm.config file
Expand All @@ -59,18 +60,17 @@ case "$1" in

if [ -d "$BOBJEDIR"/tomcat ]; then
$SU - "$BOBJEUSERNAME" -c "$BOBJEDIR"/tomcatstartup.sh
logger -p daemon.info -t bobj "Started Tomcat"
logger -p daemon.info -t bobj "Started Tomcat"
fi

if [ -f "$BOBJEDIR"/sqlanywhere_startup.sh ]; then
$SU - "$BOBJEUSERNAME" -c "$BOBJEDIR"/sqlanywhere_startup.sh
logger -p daemon.info -t bobj "Started SQL Anywhere"
fi
if [ -f "$BOBJEDIR"/sqlanywhere_startup.sh ]; then
$SU - "$BOBJEUSERNAME" -c "$BOBJEDIR"/sqlanywhere_startup.sh
logger -p daemon.info -t bobj "Started SQL Anywhere"
fi

if [ -f "$BOBJEDIR"/ccm.config ]; then
$SU - "$BOBJEUSERNAME" -c "$BOBJEDIR/ccm.sh -start all"

logger -p daemon.info -t bobj "Started servers"
$SU - "$BOBJEUSERNAME" -c "$BOBJEDIR/ccm.sh -start all"
logger -p daemon.info -t bobj "Started servers"
fi

if [ -f "/etc/redhat-release" ]; then
Expand All @@ -95,19 +95,23 @@ case "$1" in
fi

if [ -f "$BOBJEDIR"/ccm.config ]; then
$SU - "$BOBJEUSERNAME" -c "$BOBJEDIR/ccm.sh -stop all"
logger -p daemon.info -t bobj "Stopped servers"
$SU - "$BOBJEUSERNAME" -c "$BOBJEDIR/ccm.sh -stop all"
logger -p daemon.info -t bobj "Stopped servers"
fi

if [ -d "$BOBJEDIR"/tomcat ]; then
$SU - "$BOBJEUSERNAME" -c "$BOBJEDIR"/tomcatshutdown.sh
logger -p daemon.info -t bobj "Stopped Tomcat"
logger -p daemon.info -t bobj "Stopped Tomcat"
fi

if [ -f "$BOBJEDIR"/sqlanywhere_shutdown.sh ]; then
$SU - "$BOBJEUSERNAME" -c "$BOBJEDIR"/sqlanywhere_shutdown.sh
logger -p daemon.info -t bobj "Stopped SQL Anywhere"
fi
$SU - "$BOBJEUSERNAME" -c "$BOBJEDIR"/sqlanywhere_shutdown.sh
logger -p daemon.info -t bobj "Stopped SQL Anywhere"
fi

if [ -f "/home/$BOBJEUSERNAME"/archive_logs.sh ]; then
$SU - "$BOBJEUSERNAME" -c "/home/$BOBJEUSERNAME"/archive_logs.sh | logger -p daemon.info -t bobj
fi

if [ -f "/etc/redhat-release" ]; then
rm -f $Lockfile
Expand Down

0 comments on commit 920d92f

Please sign in to comment.