Site icon Voina Blog (a tech warrior's blog)

Get simple health status of a #SpringBoot application from #bash

Advertisements

Here I describe a bash script to extract only the health status of a Spring Boot application.

As an example I used an adaptor application that interfaces between two external systems.

Note that there several components of the adaptor that need to be monitored:

#curl -s -X GET http://localhost:8083/actuator/health -H 'cache-control: no-cache' | python -m json.tool 2> /dev/null
{
    "components": {
        "controlPoints": {
            "details": {
                "ADAPTER": "UP"
            },
            "status": "UP"
        },
        "dataBase": {
            "details": {
                "Entity Manager(0)": "UP"
            },
            "status": "UP"
        },
        "diskSpace": {
            "details": {
                "exists": true,
                "free": 8830365696,
                "threshold": 10485760,
                "total": 21003583488
            },
            "status": "UP"
        },
        "inputPoints": {
            "details": {
                "SWIFT": "UP"
            },
            "status": "UP"
        },
        "outputPoints": {
            "details": {
                "SWIFT": "UP"
            },
            "status": "UP"
        },
        "ping": {
            "status": "UP"
        }
    },
    "status": "UP"
}

The status returned by the following bash script has the values UP/DOWN/DEGRADED.

#!/bin/bash

LIST=$(curl -s -X GET http://localhost:8083/actuator/health -H 'cache-control: no-cache' \
| python -m json.tool 2> /dev/null \
| grep '\"status\"' \
| cut -d ':' -f 2 \
| sed 's/"/\"/g')

if  [ -z "$LIST" ]; then
    echo "DOWN"
else
    UP="true"
    for value in $LIST
    do
        if [ $value != "\"UP\"" ] && [ $value != "\"UP\"," ]; then
            UP="false"
        fi
    done
    if [ $UP == "true" ]; then
        echo "UP"
    else
        echo "DEGRADED"
    fi
fi

Notes:

Exit mobile version