Wednesday, January 27, 2021

OCI CLI to List all the Database and its Host Lifecycle

 OCI CLI to List all the Database and its Host Lifecycle


This bash scripts needs the tenancy ID to be updated. It loops through all the compartments and then fetches the life cycle of each database and it respective hosts. 

The below code fetches lifecycle of  the hosts where the database is running.

ociCompartmentList=$(oci iam compartment list --compartment-id ocid1.tenancy.oc1..aaaa1111222334)
for c in $(echo "$ociCompartmentList" | jq '.data | keys | .[]')
do
 compartment_ocid=$(echo "$ociCompartmentList" | jq -r ".data[$c].\"id\"")
 ocidbList=$(oci db database list -c $compartment_ocid)
 for i in $(echo "$ocidbList" | jq '.data | keys | .[]')
 do
ocidbnodeList=$(oci db node list -c $compartment_ocid --db-system-id $(echo $ocidbList | jq -r ".data[$i].\"db-system-id\""))
hostname=$(echo "$ocidbnodeList" | jq -r ".data[].\"hostname\"")
lifecycle=$(echo "$ocidbnodeList" | jq -r ".data[].\"lifecycle-state\"")
echo "Hostname: $hostname | LifeCycle: $lifecycle"
 done
done

Hostname: ebstestserver-001| LifeCycle: RUNNING
Hostname: ebstestserver-002| LifeCycle: RUNNING
Hostname: ebstestserver-003| LifeCycle: RUNNING
Hostname: ebstestserver-004| LifeCycle: RUNNING
Hostname: ebstestserver-005| LifeCycle: RUNNING

The below code fetches the lifecycle of the database along with the hostnames

ociCompartmentList=$(oci iam compartment list --compartment-id ocid1.tenancy.oc1..aaaa1111222334)
for c in $(echo "$ociCompartmentList" | jq '.data | keys | .[]')
do
 compartment_ocid=$(echo "$ociCompartmentList" | jq -r ".data[$c].\"id\"")
 ocidbList=$(oci db database list -c $compartment_ocid)
 for i in $(echo "$ocidbList" | jq '.data | keys | .[]')
 do
dbname=$(echo "$ocidbList" | jq -r ".data[$i].\"db-name\"")
dbsystemid=$(echo "$ocidbList" | jq -r ".data[$i].\"db-system-id\"")
lifecycle=$(echo "$ocidbList" | jq -r ".data[$i].\"lifecycle-state\"")
hostname=$(oci db node list -c $compartment_ocid --db-system-id $dbsystemid| jq -r ".data[].\"hostname\"")
echo "Hostname: $hostname | Database: $dbname | LifeCycle: $lifecycle"
 done
done

Hostname: ebstestserver-001| Database: TEST1 |LifeCycle: RUNNING
Hostname: ebstestserver-002| Database: TEST2 |LifeCycle: RUNNING
Hostname: ebstestserver-003| Database: TEST3 |LifeCycle: RUNNING
Hostname: ebstestserver-004| Database: TEST4 |LifeCycle: RUNNING
Hostname: ebstestserver-005| Database: TEST5 |LifeCycle: RUNNING

Note : The script is helps to get the state of single instance vm db systems. for RAC you may have to modify it.

Popular Posts