Do my database services run on the preferred instance (s)?

Today, I just want to share with you a little script that I use, which allows me to quickly check if my database services are running on the configured “Preferred instances” in RAC environments.


for service in $(srvctl config service -d DATABASE_NAME|awk 'BEGIN {SERVICE=""; PREF=""; OFS=";"} { if($0 ~ /Service name:/) SERVICE=$NF; if($0 ~ /Preferred instances:/) {PREF=$NF;print SERVICE,PREF}}');do
SERVICE=$(echo ${service}|cut -d ";" -f1)
INSTANCES=$(echo ${service}|cut -d ";" -f2)
RUNNINGINSTANCES=$(srvctl status service -d DATABASE_NAME -s ${SERVICE}|awk '{print $NF}')

You just need to replace “DATABASE_NAME” with the name of the database you used to register it with “srvctl”. Then, you can copy/paste the script directly in your shell.

Sample output

MYDB: OK (Preferred: MYDB1,MYDB2,MYDB3,Running: MYDB1,MYDB2,MYDB3)
MYDB_APP01: OK (Preferred: MYDB2,Running: MYDB2)
MYDB_APP02: OK (Preferred: MYDB3,Running: MYDB3)
MYDB_APP03: OK (Preferred: MYDB1,Running: MYDB1)
MYDB_APP04: OK (Preferred: MYDB2,Running: MYDB2)
MYDB_APP05: OK (Preferred: MYDB3,Running: MYDB3)
MYDB_APP06: OK (Preferred: MYDB1,Running: MYDB1)
MYDB_APP07: OK (Preferred: MYDB2,Running: MYDB2)
MYDB_APP08: OK (Preferred: MYDB3,Running: MYDB3)
MYDB_APP09: OK (Preferred: MYDB1,Running: MYDB1)
MYDB_APP10: OK (Preferred: MYDB2,Running: MYDB2)
MYDB_APP11: OK (Preferred: MYDB3,Running: MYDB3)
MYDB_APP12: OK (Preferred: MYDB1,Running: MYDB1)
MYDB_APP13: OK (Preferred: MYDB2,Running: MYDB2)
MYDB_APP14: OK (Preferred: MYDB3,Running: MYDB3)
MYDB_APP15: OK (Preferred: MYDB1,Running: MYDB1)
MYDB_APP16: OK (Preferred: MYDB2,Running: MYDB2)
MYDB_APP17: OK (Preferred: MYDB1,Running: MYDB1)

If the service is running on the “Preferred instances” configured, the script returns “OK”. Otherwise, it will return “KO”. You can easily customize the output to integrate with your monitoring solution.

The script can be a bit slow. This is due to the command “srvctl config” which takes a while to display all the information about the services.

I hope you find this script useful. Stay tuned for more DBA stuff!

Leave a Reply

Your email address will not be published. Required fields are marked *