Snippet - List tenants of an SAP HANA MDC

I thought I would share a snippet of code that I have found useful for discovering all of the SIDs and Tenants running on an SAP HANA system running with Multitenant Database Containers (MDC). If you are running HANA 2.0 this is your default operating mode.

Disclaimer: I do not consider myself a HANA expert, whilst day-to-day I work with HANA systems I focus more on OS level operations and automation.

I have found occasion to find out all the instances running on a server, what their SIDs are and any tenants that may exist. There’s an assumption that you will find your HANA installation in /usr/sap - it is here you will find where instances are installed. You can print all the SIDs from this directory:

#!/usr/bin/env bash

#
# Find:
#   maxdepth = 1, we only want to find stuff in this directory level.
#   regextype = sed, we will use `sed` regular expressions to find our SIDs.
#   regex = '.*/[A-Z0-9]\{3\}', a SID is a 3 uppercase alphanumeric characters.
#   printf = '%f\n', just print the filename.
#
find /usr/sap \
    -maxdepth 1 \
    -regextype sed \
    -regex '.*/[A-Z0-9]\{3\}' \
    -printf '%f\n'

We should expect output such as below:

XMD
XMQ
XMP

So now we have a list of SIDs with our find command, we can get the instance number from each SID. Conveniently the instance number is found appended to the end of a directory prefixed with “HDB”, eg: HDB00 for instance 00.

#!/usr/bin/env bash

SIDS=$(find /usr/sap \
    -maxdepth 1 \
    -regextype sed \
    -regex '.*/[A-Z0-9]\{3\}' \
    -printf '%f\n'
)

for sid in ${SIDS} ; do
    echo -n "${sid} : "
    #
    # Find:
    #   Same as before, but this time we are using a different regex.
    #
    #   regex = '.*/HDB[0-9]\{2\}', find all directories prefixed with HDB
    #       these should be suffixed with 2 numbers.
    #
    find "/usr/sap/${sid}" \
        -maxdepth 1 \
        -regextype sed \
        -regex '.*/HDB[0-9]\{2\}' \
        -printf '%f\n' | sed 's/HDB//'
done

Our expected output from the above should be a SID and it’s instance number:

XMD : 10
XMQ : 05
XMP : 00

Now we can look to see if it is an MDC and check how many tenants each instance has. Conveniently there is a file that holds information about tenant containers on the filesystem. You’ll likely find it here: /usr/sap/${sid}/SYS/global/hdb/mdc/databases.lst

#!/usr/bin/env bash

SIDS=$(find /usr/sap \
    -maxdepth 1 \
    -regextype sed \
    -regex '.*/[A-Z0-9]\{3\}' \
    -printf '%f\n'
)

for sid in ${SIDS} ; do
    echo -n "${sid} : "

    find "/usr/sap/${sid}" \
        -maxdepth 1 \
        -regextype sed \
        -regex '.*/HDB[0-9]\{2\}' \
        -printf '%f\n' | sed 's/HDB//'

    DBLIST="/usr/sap/${sid}/SYS/global/hdb/mdc/databases.lst"
    if [ -f "${DBLIST}" ] ; then
        #
        # Grep 1:
        #   Remove all comments out of the file
        # Grep 2:
        #   Look for a line beginning with a SID style string, it should end
        #   with a 'yes' to signify it is active. Fields are delimited with
        #   colons (:)
        # Awk:
        #   Use the colon delimeter (:) to select and print the first field.
        # Sed 1:
        #   Prefix each line with indentation and a hyphen
        # Sed 2:
        #   Suffix each tenant with the SID.
        #
        grep -v '#' "${DBLIST}" | \
        grep -Ei '^[A-Z0-9]{3}:.*:yes$' | \
        awk -F: '{ print $1 }' | \
        sed 's/^/  - /' | \
        sed "s/$/@${sid}/"
    fi
done

So now we look for tenants for our database. The expected output should be something like the below:

XMD : 10
  - [email protected]
  - [email protected]
XMQ : 05
  - [email protected]
XMP : 00

It’s not the most elegant output but you can get some basic information this way. You may also find it useful for gathering information on a system that you do not have SYSTEM user access to.