Published on | Updated on

Scripts & oneLiner

When you are on the command line in Linux, you often have to implement “special tasks” from time to time. Fortunately, this can be done in many ways with the possibilities of a shell in Linux. I definitely prefer Bash, even though the user of the SAP application usually gets a csh as default shell.

Here I would like to introduce a few “oneliners” that you can use for one task or another. The term “oneliner” means that in principle I only have to issue one command. However, I don’t take that too closely in the following, and especially for readability, more than one line is used in places.

Configure SAP Hostagent with HTTPS

The following commands can be used to configure the SAP Hostagent with HTTPS. In the meantime the SAP Hostagent has an SSL autoconfiguration, so you have to check if you should delete it before.

sudo if test ! -d /usr/sap/hostctrl/exe/sec/; then sudo mkdir /usr/sap/hostctrl/exe/sec/; else echo "Achtung das Zielverzeichnis existiert bereits!" && pause; fi
export SECUDIR=/usr/sap/hostctrl/exe/sec/
export PATH=$PATH:/usr/sap/hostctrl/exe
sudo env PATH=$PATH SECUDIR=$SECUDIR rm -rf $SECUDIR/SAPSSLS.pse
# History ausschalten, damit das Kennwort nicht darin auftaucht
set +o history

# Kennwort austauschen beim Parameter -x!
sudo env PATH=$PATH SECUDIR=$SECUDIR sapgenpse gen_pse -p SAPSSLS \
-r /usr/sap/hostctrl/$(hostname).req -x “Sicheres Kennwort” \
-k GN-dNSName:$(hostname -f) -k GN-dNSName:$HOSTNAME \
"DNS=$(hostname), CN=$(hostname), O=SAP Basis, C=DE"
sudo env PATH=$PATH SECUDIR=$SECUDIR sapgenpse seclogin -p SAPSSLS -O sapadm
sudo env SECUDIR=$SECUDIR chown -R sapadm:sapsys $SECUDIR
sudo env SECUDIR=$SECUDIR chmod 640 $SECUDIR/cred_v2
sudo cat /usr/sap/hostctrl/$(hostname).req

# Manuelle Erstellung des Zertifikats und Bereitstellung des Zertifikats in /tmp 
sudo env PATH=$PATH SECUDIR=$SECUDIR sapgenpse import_own_cert -c /tmp/certnew.p7b -p SAPSSLS
sudo /usr/sap/hostctrl/exe/saphostexec -restart
sudo rm -f /tmp/certnew.p7b
set -o history

Install HANA database

The special feature here is that the installation takes place under a logical host name. The prerequisite is that the physical hostname is, for example, hostname01 and the logical hostname is hostname02.

The SID of the database consists of D (= Database) and a number. In this example D00. This number is then also used as the instance number.

The downloads directory must contain the SAPCAR and the installation file for the HANA database server.

# Parameter setzen
DOWNLOADS=/install/HANADB/
INSTHOST=${HOSTNAME:0:7}$(printf %02d $(echo $((10#${HOSTNAME: -3} + 10#01))))
SID=D00
set +o history
DB_KENNWORT=”Ein sicheres Kennwort“
set -o history

cd ${DOWNLOADS}
if test -d ${DOWNLOADS}/HANAEXT; then rm -rf ${DOWNLOADS}/HANAEXT; fi
# IMDB_SERVER*SAR entpacken
${DOWNLOADS}/SAPCAR -x -manifest SIGNATURE.SMF -f "HANADB/IMDB_*SAR" -R ${DOWNLOADS}/HANAEXT
# Ein Template erstellen und das Kennwort einfügen. Dieses Kennwort wird für alle Benutzer und als Masterkennwort verwendet
sudo ${DOWNLOADS}/HANAEXT/SAP_HANA_DATABASE/hdblcm --action=install --dump_configfile_template=${DOWNLOADS}/HDB_install.cfg
sudo cp ${DOWNLOADS}/HDB_install.cfg.xml ${DOWNLOADS}/HDB_install.pwd.xml
sudo sed  -i "s/\*\*\*/${DB_KENNWORT}/g" ${DOWNLOADS}/HDB_install.pwd.xml
sudo chmod 600 HDB_install.*
 
sudo cat ${DOWNLOADS}/HDB_install.pwd.xml | sudo ${DOWNLOADS}/HANAEXT/SAP_HANA_DATABASE/hdblcm \
--read_password_from_stdin=xml  \
--sid=${SID} --number=${SID: -2} --userid=33${SID: -2} \
--hostname=${HOSTNAME:0:7}$(printf %02d $(echo $((10#${HOSTNAME: -3} + 10#01)))) \
--autostart=off --system_usage=development --batch

Uninstall HANA DB

## Deinstall ##
SID=D00
sudo /hana/shared/${SID}/hdblcm/hdblcm --uninstall --batch

Adapt Stack.xml to other system

Yes, for an SAP update the Maintenance Planner is used, all systems are included via a track in a Stack.xml file, which the SUM can then use.

So much for the theory. Sometimes you have configured Stack.xml for one system and don’t want to (or can’t) create a new Stack.xml for all SAP systems. Then the stack.xml must be adjusted by hand.

# SAP System Identifier Old / New
OSID=
NSID=
# ASCS Instance Number
OASCSNR=
NASCSNR=
# Dialog Instance Number
ODINR=
NDINR=
# SAP Hostname
OSAPHOST=
NSAPHOST=
sed -e "/>${OSID}</>${NSID}</g" -e "/>${OASCSNR}</>${NASCSNR}</g" -e "/>${ODINR}</>${DINR}</g"  -e "/>${OSAPHOST}</>${NSAPHOST}</g" "MP_Stack*.xml" > MP_Stack_${NSID}.xml

Search and zip log files

Find files that are not older than 2h, search and zip them.

LOGDIR=./server?/log
find ${LOGDIR} -mmin -120 -print | zip /tmp/Serverlogs_$(date +'%F') -@ 

Log file size count

The question with this command is, how big are the logfiles, which were generated within a certain time (here 24h).

SAPDB=$(grep HDB /usr/sap/sapservices | awk -F/ '{print $4}')
ZEIT=1440
find /sysbackup/${SAPDB}/log/ -type f -mmin -${ZEIT}  -printf '%s\n' | awk '{ T += $1 } END { print T/1024/1024/1024,"GB"}'

Monitor sybase log area

The following script was created because the log area of a sybase database sporadically increased to 100% and the database and therefore the SAP Java system was down.

By e-mail, this script should warn at a defined threshold. You could also include an automated flush here.

Excerpt:

...
# Aktuellen freien Speicher ermitteln
PERCFREE=$(tail -n 1 "${DATALOGPATH}/${DATALOGFILE}" | awk '{print $7}')

# Fehlerbehandlung wenn ein kritischer Wert überschritten wurde
# floating is a mess mit der Bash. Wir verwenden daher bc
if (( $(echo "${PERCFREE} >= ${PERCCRIT}" | bc) ));
then
  echo "Kritischer Bereich ${CPERCRIT} nicht erreicht. ${PERCFREE} pages free." >> /tmp/sybtest.txt

else

cat <(printf "ACHTUNG!\nDer Logbereich der sybase Datenbank auf ${HOSTNAME} läuft in einen kritischen Bereich. Es sind nur noch ${PERCFREE} % pages des ${DB} Logbereiches frei. \
		\n\n---------- Logdatei ${DATALOGPATH}/${DATALOGFILE} ----------\n")  \
	<(head -n 30 "${DATALOGPATH}/${DATALOGFILE}") <(printf "\n...\n\n") <(tail -n 24 "${DATALOGPATH}/${DATALOGFILE}") \
	<(printf "\n\n\n---------- Logdatei /sybase/${DB}/ASE-16_0/install/${DB}.log ----------\n...\n") <(tail -n 30 "/sybase/${DB}/ASE-16_0/install/${DB}.log") \
	<(printf "\n---------- Ende der Logdatei ----------\n\n\n-- \nMit freundlichen Grüßen\n\nDas SAP-Basis Team")   \
	| mailx -r ${EMAIL_SENDER} -s "Kritscher DB Zustand auf ${HOSTNAME}" ${EMAIL_EMPFAENGER}

fi

Shut down all SAP systems of a server

Here the pure print version

awk -F'/' '/^[^#].*\/[A-Z]{3,4}[0-9]{2}/ {print $4,$5, substr($5, length($5) -1) }' /usr/sap/sapservices | sort -u -k1,2 | awk -F' ' '{print "source /home/"tolower($1)"adm/.sapenv.sh;", "sapcontrol -nr", $3, "-function StopSystem ALL; sleep 60"}'

Automatically update SAP Cloud Connector

The following script automatically updates the SAP Cloud Connector on the development and test systems. An e-mail is then sent to a defined distribution list. The production system is updated manually.

Excerpt:

...

fn_check_new_version() {
	# Download SCC
	echo -e "\033[1mDownload der aktuellsten SCC-Version.\033[0m"
	wget -q --header "Cookie: eula_3_1_agreed=tools.hana.ondemand.com/developer-license-3_1.txt" -r -l1 --no-parent -A 'sapcc-*linux-x64.zip' https://tools.hana.ondemand.com/ -P /tmp

	# installierte Version - zwei Ansaetze die Version zu finden
	# 1) größer als Version >=2.13 "scc.version=?.?.?"
	VERSION_NOW=$(grep '^scc.version=' /opt/sap/scc/version.properties | grep -oE '[0-9]{1,2}\..*')
	if [ $? -ne 0 ]; then
		# 2) vor Version <2.13 "version=?.?.?"  
	    VERSION_NOW=$(grep '^version=' /opt/sap/scc/version.properties | grep -oE '[0-9]{1,2}\..*')
	    echo -e "Installierte Version: \033[1;32m${VERSION_NOW} \033[0m"
	else
        echo -e "Installierte Version: \033[1;32m${VERSION_NOW} \033[0m"
	fi
	
	# heruntergeladene Version
	VERSION_DOWNLOAD=$(echo /tmp/tools.hana.ondemand.com/additional/sapcc* | grep -oE '([0-9]{1,2}\.?){4}')
	echo -e "Heruntergeladene Version: \033[1;32m${VERSION_DOWNLOAD} \033[0m\n"
	
	# Prüfung, ob neue Version vorhanden oder aktuellste schon installiert -> wenn keine neue Version, dann grün angezeigt
	if [[ "${VERSION_DOWNLOAD}" = "${VERSION_NOW}" ]];
	then
		echo -e "\033[36mKein Update notwendig! \033[0m"
		fn_end 
	fi	
	
	case ${SYSTYPE} in
	TEST)
		# Prüfung, ob die heruntergeladene mit der geplanten Version übereinstimmt und nicht zwischenzeitlich noch eine neuere Version zur Verf¸gung steht
		if [[ -f "${PLANNED_VERSION}" ]]
		then
			PLANVERSION=$(head -n 1 ${PLANNED_VERSION})
			if [[ "${VERSION_DOWNLOAD}" > "${PLANVERSION}" ]]
			then
				# Im Test-System dann nochmal warten
				echo -e "\033[36mBisher geplante Version ${PLANVERSION}. Neue Version ${VERSION_DOWNLOAD} wurde gefunden, aber der Systemtyp ist ${SYSTYPE}. Die Installation wird um ${DELAY} Tage verzoegert. \033[0m"
				echo ${VERSION_DOWNLOAD} > ${PLANNED_VERSION}
				echo ${EPOCHTIME} > ${CHECKFILE}
				fn_end
			fi
		else	
			echo ${VERSION_DOWNLOAD} > ${PLANNED_VERSION}
		fi ;;
		
	PROD)
		# Prüfung, ob die heruntergeladene mit der geplanten Version übereinstimmt und nicht zwischenzeitlich noch eine neuere Version zur Verfügung steht
		# und ob wirklich installiert werden soll
		if [[ ( "${VERSION_DOWNLOAD}" > "${PLANNED_VERSION}" ) && ${EXECUTE_PROD_UPDATE} == true ]]
		then
			# Im Produktivsystem dann auf das verschobene Installationsfile zurückgreifen
			# aktuell heruntergeladenes Installationsfile verschieben
			mv /tmp/tools.hana.ondemand.com/additional/*.zip "${VERSIONPATH}"
			# Installationsfile zur geplanten Version wiederherstellen
			if [[ -f "/tmp/tools.hana.ondemand.com/scc_versions/sapcc-${PLANNED_VERSION}-linux-x64.zip" ]]
			then
				cp ${VERSIONPATH}/sapcc-${PLANNED_VERSION}-linux-x64.zip /tmp/tools.hana.ondemand.com/additional/
			else
				echo "Die geplante Version ist nicht verfuegbar und muss manuell unter '/tmp/tools.hana.ondemand.com/additional' zur Verfuegung gestellt werden."
				fn_end
			fi
		fi ;;
	esac
} # End of function

...

Comment on the post

Please write a comment.

Your comment will be moderated by an admin before the activation.