Friday, July 27, 2018

Shell script to automate diagnostic agent startup

 
Following  shell script  will be helpful to automate the startup of diagnostic  agent in case it is stopped.
 
You can schedule  the script  as a  crontab entry under the OS user that runs diagnostic agent.  The script will perform the status check and  if the diagnostic agent is found stopped it will be automatically started by the script.  You will be able to overcome the manual intervention needed.
 
Machine generated alternative text: . : - dadadm 52> crontab —1
DO NOT EDIT THIS FILE — edit the master and reinstall.
(/tmp/crontab.XXXX733cfz installed on Mon Apr 6 12:14:32 2015)
k (Cron version V5.0 —— $Id: crontab.c,v 1.12 2004/01/23 18:56:42 vixie Exp $)
* * * * /home/dadadm/chec]c diagnostic agent.sh
cipxlnl:dadadm 53> EI
 
Cron entry for  scheduling the script in every 5 minutes :   */5 * * * * /home/dadadm/check_diagnostic_agent.sh
 
In my case , DAD is the SID of the diagnostic agent. So I  have installed the script under the OS user dadadm.
 
The script is not intended to replace standard SAP process of starting the agent during OS startup.
If you observe that the agent  does not start up  during OS startup, check the following
 
1) whether  sapstartsrv service for the diagnostic agent has been configured  in sapservices file.  sapservices  can be located in /usr/sap directory
 
Machine generated alternative text: -- -- ‘ 54> cat /usr/5ap/5apervices
4 ! /bin/sh
LDLIBRARYPATH/usr/sap/EA/SMDA97/exe: $LD LIBRARY PATH; export LD LIBRARY PATH; /usr/sapf /SMDA97/exe/sapstartsrv pf/usr/sap/DAA/SYS/profile/DAA SMDA97_ci -. .l -D
—u •--- -‘•
 
2)sapinit  is placed in /etc/init.d directory (this is for Linux systems)
Machine generated alternative text: :dadadm 54> cd /etc/init.d
Directory: /etc/mt.d
:dadadm 55> is —itr sapinit
—rwxr—x——— 1 root sapsys 12235 Oct 24 2014 sapinit
‘-‘
 
3)Autostart parameter  has been set to 1 in instance profile of diagnostic agent
 
 
#SCRIPT STARTS HERE - COPY FROM THE LINE BELOW
 
#!/bin/sh
SID=""
profile_dir=""
LOG="$HOME/check_diagnostic_agent.log"
#DIR_LIBRARY=/usr/sap/DAD/SYS/exe/run
#=================================================================
startExecution()
{
start_timestamp=`date +"%Y-%m-%d %H:%M:%S"`
echo "[ Execution start timestamp:  ${start_timestamp}" >> "$LOG"
}
 
#=================================================================
exitOnError()
{
error_timestamp=`date +"%Y-%m-%d %H:%M:%S"`
echo "Execution end timestamp:  ${error_timestamp} ]" >> "$LOG"
exit 1
}
 
#=================================================================
endExecution()
{
end_timestamp=`date +"%Y-%m-%d %H:%M:%S"`
echo "Execution end timestamp:  ${end_timestamp} ]" >> "$LOG"
}
 
##################
##Main Function
startExecution
#================================================================
#Export Environment variables
if [ -f $HOME/.profile ]; then
     . $HOME/.profile
   #echo "Environment profile found..." >> "$LOG"
else
   echo "ERROR: No environment profile found!!!" >> "$LOG"
   exitOnError
fi
#================================================================
#Check Environment variable SAPSYSTEMNAME
if [ ! -z $SAPSYSTEMNAME ]
then
 SID="${SAPSYSTEMNAME}"
 profile_dir="/usr/sap/${SAPSYSTEMNAME}/SYS/profile"
 #echo "Diagnostic agent SID id ${SAPSYSTEMNAME}" >> "$LOG"
else
  echo "ERROR: SAPSYSTEMNAME environment variable not found...exiting!!!" >> "$LOG"
  exitOnError
fi
#================================================================
#check whether startsap exists
STARTSAP_DIR=""
for dir in `echo $LD_LIBRARY_PATH | sed 's/:/ /g'`
do
   if [ -x "${dir}/startsap" ]
   then
    STARTSAP_DIR="$dir"  
    break
   fi
done
#echo "STARTSAP_DIR=${STARTSAP_DIR}"
if [ -z "$STARTSAP_DIR" ]
then
 echo "ERROR: startsap executable not found...exiting!!!" >> "$LOG"
 exitOnError
fi
#=================================================================
#check if the SAP System is a diagnostic agent
if [ -d /usr/sap/$SAPSYSTEMNAME/SMDA[0-9][0-9] ]
then
 echo "SAP System ${SAPSYSTEMNAME} is a diagnostic agent..." >> "$LOG"
else
  echo "ERROR: Dignostic agent is not installed..." >> "$LOG"
  exitOnError
fi
#=================================================================
if [ -d "$profile_dir" ]
then
  #echo "File system /usr/sap/${SAPSYSTEMNAME}/SYS/profile exists..." >> "$LOG"
  echo "Checking if diagnostic agent is running...." >> "$LOG"
  if [ `$STARTSAP_DIR/startsap check|grep -c "is running"` -ge 1 ]
  then
      # check whether diagnostic agent processes are running
      ps -ef | grep "/usr/sap/$SAPSYSTEMNAME/SMDA[0-9][0-9]/exe/jstart" > /dev/null 2>&1
      rc1=$?
      ps -ef | grep "/usr/sap/$SAPSYSTEMNAME/SMDA[0-9][0-9]/exe/sapstartsrv" > /dev/null 2>&1
      rc2=$?
      rc=`expr $rc1 + $rc2`
      if [ $rc -eq 0 ]; then
        echo "diagnostic agent is already running..." >> "$LOG"
      else
        echo "RC=$rc...Probably diagnostic agent is not running..Executing startsap...." >> "$LOG"
        $STARTSAP_DIR/startsap
      fi
   else
     echo "Probably diagnostic agent is not running..Executing startsap...." >> "$LOG"
     $STARTSAP_DIR/startsap
  fi
else
  echo "File system does not /usr/sap/${SAPSYSTEMNAME}/SYS/profile exist..." >> "$LOG"
  exitOnError
fi
endExecution
 
# END OF SCRIPT
 

Friday, July 20, 2018

Shell script to decypt files using gpg

#!/bin/bash
#PURPOSE: This shell script is indented to decrypt the encrypted files(*.gpg files) using gpg command. we have .gpg files (encrypted) and public key of the party who encrypted the files.. we want to decrypt the files and keep a copy of encrypted files as well.


###########################
#Change the directories according to your requirement, don't change the variables :)
HOME=/home/myuser
#location of encrypted files
SRC="$HOME/ENCRYYPTED"
#location of archiving
ARCHIVE="$HOME/ARCHIVED"
#location of decrypted files
DEST="$HOME/DECRYPTED"
#public key file - to be used to decrypt
KEYFILE="$SRC/key.asc"
#Encrypted file listing - temporary use
ENC_FILES="$SRC/encrypt_files.txt"
#log file
LOGFILE="$SRC/gpg_decrypt.log"
####################################

PATH=.:/sbin:/usr/sbin:/usr/local/sbin:/root/bin:/usr/local/bin:/usr/bin:/bin:$PATH
############
#Functions
############
check_dirs()
{
#check dirs
if [ ! -d "$SRC" -o ! -d "$DEST" -o ! -d "$ARCHIVE" ]; then
  echo "ERROR: All required directories are not available" | tee -a "$LOGFILE"
  exit 1
fi
#check key file
if [ ! -r "$KEYFILE" ]; then
  echo "ERROR: Key file $KEYFILE is not avaiable" | tee -a "$LOGFILE"
  exit 1
fi
}
copy_files_to_dest()
{
local files
local file
local timestamp
local extn
local cpfile
#change working dir to source dir
cd "$SRC"
if [ $? -ne 0 ]; then
  echo "ERROR:change to dir $SRC is not successful" | tee -a "$LOGFILE"
  exit 1
fi
#copy the gpg files from SRC dir to ARCHIVE dir
files=`ls -1 *.gpg 2>/dev/null`
echo "$files" > "$ENC_FILES"
if [ -r "$ENC_FILES" ]; then
while IFS=$'\n' read -r file
 do
   if [ -f "$file" ]; then  # if a file
    timestamp=`ls -l --time-style="long-iso" "$file" |awk '{print $6 "-" $7}'|sed "s/:/-/"`
    filename=`echo "$file" | awk -F . '{if (NF > 1) {print $1}'}`
    extn=`echo "$file" | awk -F . '{if (NF > 1) {print $NF}'}`
    cpfile="${filename}_${timestamp}.${extn}"
    echo "File name : $file " | tee -a "$LOGFILE"
    echo "Timestamp : $timestamp " | tee -a "$LOGFILE"
    echo "Target file name :  $cpfile" | tee -a "$LOGFILE"
    echo ""  | tee -a "$LOGFILE"
    cp -p "$SRC/$file" "$ARCHIVE/$cpfile"
    if [ $? -ne 0 ]; then
   echo "ERROR: Copy of $file from $SRC to $ARCHIVE failed " | tee -a "$LOGFILE"
   exit 1
    else
   echo "$file copied to $cpfile in $ARCHIVE " | tee -a "$LOGFILE"
    fi
   fi
 done < "$ENC_FILES"
fi
}
import_public_key()
{
#import gpg public key
gpg --import --no-verbose "$KEYFILE"
if [ $? -ne 0 ]
 then
   echo "Error in importing public key . Check the key " | tee -a "$LOGFILE"
   exit 1
fi
}
decrypt_files()
{
local file
local filename
local newfile
#decrypt files
while IFS=$'\n' read -r file
do
  if [ -f "$file" ]; then # proceed if it is a file
   filename=`echo "$file" | awk -F . '{if (NF > 1) {print $1}'}`
   newfile="$filename.txt"
   gpg --decrypt "$SRC/$file" > "$DEST/$newfile"
   if [ $? -eq 0 ]; then
  echo "$SRC/$file decrypted in $DEST/$newfile" | tee -a "$LOGFILE"
  rm "$SRC/$file"
  echo "$file removed from $SRC" | tee -a "$LOGFILE"
   fi
  fi
done < "$ENC_FILES"
}
del_tmp_file()
{
#delete temporary file
if [ -f "$ENC_FILES" ]
then
   rm "$ENC_FILES"
fi
}
init_log()
{
 NAME=`basename $0`
 echo "*******Executing $NAME at $(date +"%Y-%m-%d %T") ********" | tee -a "$LOGFILE"
}
###########
#Main()
###########
check_dirs
init_log
copy_files_to_dest
import_public_key
decrypt_files
del_tmp_file


#SCRIPT ENDS here




How to use the script
  • Place the encrypted *.gpg files in /home/myuser/ENCRYYPTED folder
  • Place the publick key in  /home/myuser/ENCRYYPTED folder. Name it as key.asc
  • Place the script in any location that suits your requirement
  • Run the script.
  • Copy of encrypted files will go in /home/myuser/ARCHIVED folder
  • Decrypted files will go in /home/myuser/DECRYPTED folder
  • Logfile of the script will be in /home/myuser