Syed Jahanzaib Personal Blog to Share Knowledge !

November 22, 2016

Query Windows SAP Server Instance status via Linux

Filed under: Linux Related, Microsoft Related — Tags: , , — Syed Jahanzaib / Pinochio~:) @ 3:37 PM

sap.jpg

linux_pengiun

Background:

We are using SAP ECC6 on Windows 2008 R2 64bit server in our data center.
The landscape is as below …

  • PRODUCTION
  • DEV [with 2 instances to support our and parent company as well]
  • Q.A  [with 2 instances to support our and parent company as well]
  • DATA GUARD [For PRD Backup/Replication]

Problem:

During the deployment , we were having strange issue that sometimes any single instance at DEV or QAS stops and we get to know it quite late as it was oftenly happening in saturday sundays when no one is available at data center.


Solution:

Therefore I made the following Linux bash script which performs the following functions >

  1. Check Server PING for its availability
  2. Check ORACLE and SAP services and add there name and status in the INFO
  3. Check SAP Instance using sapcontrol command Using WINEXE (Linux utility to execute command on remote windows pc. If it donot find 3 GREEN status in the instance query, then SEND email / SMS to admin.
  4. At next run, check if service/instance is still down, and the alert is already been sent, do not repeat the alert sending until next status changes.
  5. At next run, if the service/instance is UP, then send alert.

Requirements:

  1. Linux (any flavor, I used Ubuntu 12.4 in this guide)
  2. Winexe (Linux utility to execute command on remote windows pc via Linux CLI)
  3. NET RPC tools to check remote windows services via Linux CLI
  4. sendEMAIL tool (To send email using your GMAIL account, you can use your own app too)
  5. KANNEL SMS Gateway (To send sms, you can change it , or disable it if not required)

TO DO LIST


the SCRiPT ~

#!/bin/bash
# Script to check windows based SAP SERVER PING / SERVICES and INSTANCE status via linux query
# and send sms/email accordingly using NET RPC / winexe / sendEmail / Kannel
# Created by SYED JAHANZAIB / aacable at hotmail dot com
# CREATED: 21-NOV-2016
# LAST REVISION: 26-NOV-2016
#set -x

ESC_SEQ="\x1b["
COL_RESET=$ESC_SEQ"39;49;00m"
COL_RED=$ESC_SEQ"31;01m"
COL_GREEN=$ESC_SEQ"32;01m"
COL_YELLOW=$ESC_SEQ"33;01m"
DATE=`date`

# TEMPORARY FOLDER NAME
TEMP="temp"

# Checking if $TEMP folder is previously present or not [To hold all status]. . .
{
if [ ! -d "/$TEMP" ]; then
echo
echo -e "$COL_RED /$TEMP folder not found, Creating it so all TEMPORARY STATUS / HOLDERS will be placed there . . . $COL_RESET"
mkdir /$TEMP
else
echo > /dev/null
fi
}

# SAP-SERVER RELATED INFO
SAPSRVIP="10.0.0.1"
SAPSRVNAME="SAP - DEV-0"
DOMAIN="DOMAIN-OR-WORKGROUP"
ADMINID="ADMINID"
ADMINPASS="PASSWORD"
# You must change following path to point sapcontrol.exe
COMPATH="E:\usr\sap\R3d\DVEBMGS00\exe\sapcontrol"
WINEXE="/usr/sbin/winexe"
# If you have multiple instance, you may change it as required
INSTANCE="0"
SRV1="OracleServiceR3D"
SRV2="OracleServiceODV"
SRV1_STATUS1="/$TEMP/$SAPSRVIP.$INSTANCE.$SRV1.status1.txt"
SRV1_STATUS2="/$TEMP/$SAPSRVIP.$INSTANCE.$SRV1.status2.txt"
SRV2_STATUS1="/$TEMP/$SAPSRVIP.$INSTANCE.$SRV2.status1.txt"
SRV2_STATUS2="/$TEMP/$SAPSRVIP.$INSTANCE.$SRV2.status2.txt"

# COMPANY NAME
COMPANY="-ZAIB-"
FOOTER="Powered by $COMPANY SYS ADMIN"

# KANNEL SMS Gateway Info to send SMS if required
KANNELURL="KANNEL-IP:13013"
KANNELID="kannel"
KANNELPASS="PASSWORD"
# ZAIB CELL
CELL1="03333021909"

# GMAIL DETAILS to send EMAIL alert
GMAILID="YOUR-GMAIL-ID@gmail.com"
GMAILPASS="GMAIL-PASS"
# Add recipient email address below
ADMINMAIL1="syed.jahanzaib@ZAIB"
SENDMAILAPP="/temp/sendEmail-v1.56/sendEmail"

# Various holders to store different Status
SENTSMSRESULT="/$TEMP/$SAPSRVIP.$INSTANCE.txt"
STATUS_HOLDER="/$TEMP/$SAPSRVIP.$INSTANCE.status.txt"
SRV_HOLDER="/$TEMP/$SAPSRVIP.$INSTANCE.srvstatus.txt"
SRV_HOLDER_HEADER="$SAPSRVNAME Services Status ..."
UPMSG="/$TEMP/$SAPSRVIP.$INSTANCE.up.sms"
DOWNMSG="/$TEMP/$SAPSRVIP.$INSTANCE.down.sms"
SAPINSTSTATUS1="/$TEMP/$SAPSRVIP.$INSTANCE.inst-try1.txt"
SAPINSTSTATUS2="/$TEMP/$SAPSRVIP.$INSTANCE.inst-try2.txt"
> $SAPINSTSTATUS1
> $SAPINSTSTATUS2
echo "" > $SRV_HOLDER
touch  $DOWNMSG
> $DOWNMSG
> $UPMSG
touch $STATUS_HOLDER

# Messages which will be sent via email/sms
MSG_UP_SUB="INFO: $SAPSRVNAME - $SAPSRVIP -  Instance $INSTANCE Restored"
MSG_DOWN_SUB="ALERT:$SAPSRVNAME - $SAPSRVIP -  Instance $INSTANCE IS DOWN"
MSG_SRV_DOWN_SUB="ALERT: $SAPSRVNAME - $SAPSRVIP -  One or More Services are Down DOWN"

#######################
### PING SECTION STARTS
#######################

PING_DOWN_MSG="ALERT: $AGPSRVNAME  $SAPSRVIP - PING COMMUNICATION FAILED @ $DATE"
PING_UP_MSG="INFO: $AGPSRVNAME  $SAPSRVIP - PING COMMUNICATION RESTORED - OK @ $DATE"
PING_NORMAL_MSG="INFO: $AGPSRVNAME  $SAPSRVIP - PING COMMUNICATION - OK @ $DATE"
PING_STATUS_HOLDER="/$TEMP/$SAPSRVIP.ping.status"
FINAL_PING_STATUS="1"
# PING Attempts
PING_ATEMPTS="5"

# Check if SERVER is accessibel or not, then inform if sms/email is already been sent / zaib
## IF PING FAILS  then send sms and email, IF NOT ALREAY SENT
echo -e "$COL_YELLOW>Checking PING response at $SAPSRVNAME $SAPSRVIP ... $COL_RESET"
if [[ $(ping -q -c $PING_ATEMPTS $SAPSRVIP) == @(*100% packet loss*) ]]; then
echo ""
FINAL_PING_STATUS="DOWN"
else
echo ""
FINAL_PING_STATUS="UP"
fi

echo $SRV_HOLDER_HEADER >> $SRV_HOLDER

##########################
### PING SECTION ENDS HERE
##########################

# If temporary status holder is not present , then create it,
# forumla is being applied to prevent repeated attempt of file creation / zaib
if [ ! -f $STATUS_HOLDER ]; then
echo -e "Creating Status Holder for first time usage"
touch $STATUS_HOLDER
fi

#######################################
#### QUERY SERVICES SECTION STARTS HERE
#######################################

# $SRV - 1 - CHECK
echo -e "$COL_YELLOW>Checking $SRV1 Service status at $SAPSRVNAME $SAPSRVIP ... Check#1 $COL_RESET"
# IF SAP SERVICE QUERY result is not running, then UPDATE its status accordingly
net rpc service status $SRV1 -I $SAPSRVIP --user=$DOMAIN/$ADMINID%$ADMINPASS |grep running > $SRV1_STATUS1
sleep 5
echo -e "$COL_YELLOW>Checking $SRV1 Service status at $SAPSRVNAME $SAPSRVIP ... Check#2 $COL_RESET"
net rpc service status $SRV1 -I $SAPSRVIP --user=$DOMAIN/$ADMINID%$ADMINPASS |grep running > $SRV1_STATUS2
QSRV1_STATUS1=`cat $SRV1_STATUS1 | grep -o running | wc -l`
QSRV1_STATUS2=`cat $SRV1_STATUS2 | grep -o running | wc -l`
if [[ $QSRV1_STATUS1 -gt 0 ]] && [[ $QSRV1_STATUS1 -gt 0 ]]; then
echo "$SRV1 UP" >> $SRV_HOLDER
else
echo "ALERT: $SRV1 Service is DOWN ...."
echo "$SRV1 DOWN" >> $SRV_HOLDER
fi

# $SRV - 2 - CHECK
# IF SAP SERVICE QUERY result is not running, then UPDATE its status accordingly
echo -e "$COL_YELLOW>Checking $SRV2 Service status at $SAPSRVNAME $SAPSRVIP ... Check#1 $COL_RESET"
net rpc service status $SRV2 -I $SAPSRVIP --user=$DOMAIN/$ADMINID%$ADMINPASS |grep running > $SRV2_STATUS1
sleep 5
echo -e "$COL_YELLOW>Checking $SRV2 Service status at $SAPSRVNAME $SAPSRVIP ... Check#2 $COL_RESET"
net rpc service status $SRV1 -I $SAPSRVIP --user=$DOMAIN/$ADMINID%$ADMINPASS |grep running > $SRV2_STATUS2
QSRV2_STATUS1=`cat $SRV2_STATUS1 | grep -o running | wc -l`
QSRV2_STATUS2=`cat $SRV2_STATUS2 | grep -o running | wc -l`
if [[ $QSRV2_STATUS1 -gt 0 ]] && [[ $QSRV2_STATUS1 -gt 0 ]]; then
echo "$SRV2 UP" >> $SRV_HOLDER
else
echo "ALERT: $SRV2 Service is DOWN ...."
echo "$SRV2 DOWN" >> $SRV_HOLDER
fi

echo "" >> $SRV_HOLDER

# MESSAGES
# SMS and email msg fromat for up n down
NORMSG="$SAPSRVNAME - Oracle Services and SAP instance $INSTANCE QUERY is OK !- All Seems OK @ $DATE!"
LOWMSG="ALERT: $SAPSRVNAME - $SAPSRVIP -  Instance $INSTANCE is DOWN @ $DATE"
OKMSG="INFO: $SAPSRVNAME - $SAPSRVIP -  Instance $INSTANCE is UP now @ $DATE"

##########################################################
#### CHECK FOR RUNNING SERVICES LIKE ORACLE and others ...
##########################################################
CHKGSRVDOWNSTATUS=`cat $SRV_HOLDER | grep -o DOWN | wc -l`
if [[ $CHKGSRVDOWNSTATUS -gt 0 ]]; then
echo -e "$COL_YELLOW >PING STATUS = $FINAL_PING_STATUS"
echo "PING STATUS = $FINAL_PING_STATUS" >> $DOWNMSG
echo "" >> $DOWNMSG
echo ""
echo -e "$COL_RED >ALERT: FOLLOWING SERVICES ARE DOWN ... $COL_RESET"
cat $SRV_HOLDER
NORMSG="$SAPSRVNAME - Oracle Services seems to be $COL_RED DOWN $COL_RESET BUT SAP instance $INSTANCE QUERY is $COL_GREN OK $COL_RESET ! @ $DATE!"

echo "ALERT: FOLLOWING SERVICES ARE DOWN ..." >> $DOWNMSG
cat $SRV_HOLDER >> $DOWNMSG

else
echo -e "$COL_GREEN>INFO: FOLLOWING SERVICES ARE UP at $SAPSRVNAME $SAPSRVIP ... $COL_RESET"
cat  $SRV_HOLDER
fi

#############################################################################
#############################################################################
###
### CHECK SAP INSTANCE FOR 3 GREEN COUNTS and inform if alert is already sent
###
#############################################################################
#############################################################################

# IF SAP INSTANCE QUERY result does not contains 3 GREEN, then send sms and email
# To prevent FALSE alaram, I have added double check : ) HURAAAH, Allah Shuker / zaib
echo -e "$COL_YELLOW> Testing SAP Instance No $INSTANCE ... Check#1 $COL_RESET"
$WINEXE --user=$DOMAIN/$ADMINID%$ADMINPASS //$SAPSRVIP "$COMPATH -nr $INSTANCE -function GetProcessList" > $SAPINSTSTATUS1
sleep 5
echo -e "$COL_YELLOW> Testing SAP Instance No $INSTANCE ... Check#2 $COL_RESET"
$WINEXE --user=$DOMAIN/$ADMINID%$ADMINPASS //$SAPSRVIP "$COMPATH -nr $INSTANCE -function GetProcessList" > $SAPINSTSTATUS2
sleep 1
CHKGREENSTATUS1=`cat $SAPINSTSTATUS1 | grep -o GREEN | wc -l`
CHKGREENSTATUS2=`cat $SAPINSTSTATUS2 | grep -o GREEN | wc -l`

if [[ $CHKGREENSTATUS1 -lt 3 ]] && [[ $CHKGREENSTATUS2 -lt 3 ]]; then
if  [ $(grep -c "TEMP" "$STATUS_HOLDER") -eq 1 ]; then
echo -e "$COL_RED>ALERT: $LOWMSG$COL_RESET"
echo "$LOWMSG" >> $DOWNMSG
echo "SMS/Email for DOWN have already been sent"

fi
fi

if [[ $CHKGREENSTATUS1 -lt 3 ]] && [[ $CHKGREENSTATUS2 -lt 3 ]]; then
if  [ $(grep -c "TEMP" "$STATUS_HOLDER") -eq 0 ]; then
echo  "ALERT: $LOWMSG
SENDING DOWN SMS/Email .... "
echo "$LOWMSG" > $DOWNMSG
echo "" >> $DOWNMSG
echo "PING STATUS = $FINAL_PING_STATUS" >> $DOWNMSG
cat $SRV_HOLDER >> $DOWNMSG
echo "" >> $DOWNMSG
echo "$FOOTER" >> $DOWNMSG
echo "TEMP" > $STATUS_HOLDER

# Sending DOWN Alert SMS/EMAIL
cat $DOWNMSG | curl "http://$KANNELURL/cgi-bin/sendsms?username=$KANNELID&password=$KANNELPASS&to=$CELL1" -G --data-urlencode text@-
$SENDMAILAPP -u "$MSG_DOWN_SUB" -o tls=yes -s smtp.gmail.com:587 -t $ADMINMAIL1 -xu $GMAILID -xp $GMAILPASS -f $GMAILID  -o message-file=$DOWNMSG -o message-content-type=text

fi
else

# SETTING NEW MSG
echo -e "$COL_GREEN $NORMSG ... $COL_RESET"
if  [ $(grep -c "TEMP" "$STATUS_HOLDER") -eq 1 ]; then
echo -e "$OKMSG
SENDING OK UP SMS/Email .... "
echo $MSG_UP_SUB > $UPMSG
echo "" >> $UPMSG
echo "PING STATUS = $FINAL_PING_STATUS" >> $UPMSG
cat $SRV_HOLDER >> $UPMSG
echo "" >> $UPMSG
echo "$FOOTER" >> $UPMSG
sed -i "/TEMP/d" "$STATUS_HOLDER"
# Sending UP INFO SMS/EMAIL
cat $UPMSG | curl "http://$KANNELURL/cgi-bin/sendsms?username=$KANNELID&password=$KANNELPASS&to=$CELL1" -G --data-urlencode text@-
$SENDMAILAPP -u "$MSG_UP_SUB" -o tls=yes -s smtp.gmail.com:587 -t $ADMINMAIL1 -xu $GMAILID -xp $GMAILPASS -f $GMAILID  -o message-file=$UPMSG -o message-content-type=text

fi
fi

# Script Ends Here
# Syed Jahanzaib / aacable @ hotmail . com
# http:// aacable . wordpress . com

Result:

Run the bash script which we created.

CLI RESULT:

1-all-ok

2-all-down

Email Result:

1-down

2-up

SMS  Result:

screenshot_2016-11-22-14-21-49


~ TIPS  ‘N’  TRICKS ~

To install sendEmail tool (using GMAIL account), use following

First install Supporting Libraries
For Ubuntu

apt-get -y install libio-socket-ssl-perl libnet-ssleay-perl perl

for centos

yum -y install perl perl-Crypt-SSLeay perl-IO-Socket-SSL

Now Download and unzip the sendEmail tool

mkdir /temp
cd /temp
wget http://caspian.dotconf.net/menu/Software/SendEmail/sendEmail-v1.56.tar.gz
tar zxvf sendEmail-v1.56.tar.gz
cd sendEmail-v1.56/

To test use following

/temp/sendEmail-v1.56/sendEmail -t DESTINATIONMAIL@hotmail.com -u "Test Email" -s smtp.gmail.com:587 -xu YOURGMAILID@gmail.com -xp GMAILPASS -f YOURGMAILID -o tls=yes -m "hi"
Nov 22 15:16:46 linux sendEmail[12561]: Email was sent successfully!

to install NET tools/command on Ubuntu 12.4 [zaib]

 sudo apt-get install samba-common

to install WINEXE on Ubuntu 12.4 [zaib]

mkdir /temp && cd /temp
apt-get install python-all-dev
wget http://downloads.sourceforge.net/project/winexe/winexe-1.00.tar.gz
tar xzvf winexe-1.00.tar.gz
cd winexe-1.00/source4/
./autogen.sh
./configure
make basics bin/winexe
./bin/winexe -V
# copy winexe binary to /usr/sbin   so that it can be called from any path
# cp /temp/winexe-1.00/source4/bin  /usr/sbin

To query Remote SAP Server instance status use following command

*Make sure you change the path of sapcontrol path/folder to match with your local installation folder structure

# Remote Server with workgroup/standalone
/temp/winexe-1.00/source4/bin/winexe -U ADMINID%PASSWORD //10.0.0.1 "E:\usr\sap\ECC\DVEBMGS00\exe\sapcontrol -nr 0 -function GetProcessList"

# Remote Server with DOMAIN base authentication
winexe --user=DOMAINNAME/ADMINID%PASSWORD //10.0.0.1 "e:\usr\sap\R3d\DVEBMGS00\exe\sapcontrol -nr 0 -function GetProcessList"

To query Remote windows services status , use following command

install the require tool by following

sudo apt-get install samba-common

List all services on remote windows server by following

net rpc service list -I 10.0.0.1 -U DOMAIN/ADMINID%PASSWORD

now query the service STATUS with following

net rpc service status OracleServiceR3D -I 10.0.0.1 -U DOMAIN/ADMINID%PASSWORD

net rpc service status OracleServiceR3D -I 10.0.0.1 -U DOMAIN/ADMINID%PASSWORD | grep running

to stop or start

net rpc service stop SERVICENAME -I IPADDRESS -U USERNAME%PASSWORD
net rpc service start SERVICENAME -I IPADDRESS -U USERNAME%PASSWORD

Regard’s
SYED JAHANZAIB

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: