1

I want to monitor the below health check URL. I need generate a alert if mongodb goes to disconnected state. I wrote a bash script. Seems its still not working. Any can help on this ? Thanks

When do health check to https://www.example.com/healthz result as below response: {"mongodb":"connected","redis":"connected"}

And my Bash script is:

#!/bin/bash
data=$(wget --timeout 5 -O - -q -t 1 https://www.example.com/healthz)
message=$(jq '.Disconnected' <<< "$data")
if [[ "$message" == '"Success"' ]] ;  then
    echo something
else
    send email
2

1 Answer 1

0

There is no .Disconnected key in the JSON response.

To check the .mongodb key for the string connected you may do this:

#!/bin/sh

if wget --timeout 5 -O - -q -t 1 https://www.example.com/healthz |
   jq --exit-status '.mongodb == "connected"' >/dev/null
then
    # code for the case when MongoDB is connected
else
    # code for the case when MongoDB is *not* connected
fi

This passes the response directly from wget to jq instead of going the roundabout way of storing it in an intermediate variable.

Using --exit-status (or -e) with jq makes the utility exit with a successful exit status (zero) if it does not output nil or false. The expression we give to jq will output true if the .mongodb key has the value connected.

We then use the exit status of jq in the if statement to choose the branch to take.

1
  • Thanks a lot Kusalananda .its worked
    – tech sl
    Mar 29, 2020 at 13:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .