Skip to content

Instantly share code, notes, and snippets.

@veysby
Last active April 20, 2020 10:08
Show Gist options
  • Save veysby/cf89534dc023f668c499 to your computer and use it in GitHub Desktop.
Save veysby/cf89534dc023f668c499 to your computer and use it in GitHub Desktop.
Activemq: check health status using REST API
$curl http://localhost:8161/api/jolokia/read/org.apache.activemq:type=Broker,brokerName=localhost,service=Health
{"timestamp":1449776947,"status":200,"request":{"mbean":"org.apache.activemq:brokerName=localhost,service=Health,type=Broker","type":"read"},"value":{"CurrentStatus":"Good"}}
@jmtscaff
Copy link

jmtscaff commented Jan 19, 2018

Reading the status doesn't actually checks the status it just reads the last know status. It is better to execute the method healthStatus() that will check the status and returns the new status.

Here is the final script I'm using:

#!/bin/bash

# Check health status
STATUS="$(curl -u admin:admin -s http://localhost:8161/api/jolokia/exec/org.apache.activemq:type=Broker,brokerName=localhost,service=Health/healthStatus | jq -r '.value')"

if [ "$STATUS" != "Good" ]; then
   ACTIVEMQ_PID="$(cat /opt/activemq/data/activemq.pid)"
   echo "WARNING: Killing ActiveMQ with $ACTIVEMQ_PID"
   # Killing the process
   # systemd should restart the process as I have the property restart=Always
   # if not make sure to start the process again
   kill -9 $ACTIVEMQ_PID 
else
   echo "INFO: ActiveMQ up and running."
fi

@xxfroxx
Copy link

xxfroxx commented Aug 28, 2018

Hi,

What Can I use to replace this "jq -r" from the healthcheck?, I read somewhere that sed will do the same, but I´m not familiar with it.

Thanks.

Copy link

ghost commented Sep 25, 2018

sed /can/ do the same thing, but sed doesn't understand json. You'll have to do some regex-fu in order to get a similar result. jq is like sed but with the added luxury of understanding json, so regardless of extra lines or other possible strange formatting, jq, in this case, will output the whole value of the 'value' member of the json being fed in (or nothing/error if the json is invalid or doesn't contain a member named 'value')

@petr-ujezdsky
Copy link

petr-ujezdsky commented Oct 2, 2019

I do not have jq installed in my docker image so I use much simpler version:

curl --silent --show-error "http://localhost:8161/api/jolokia/exec/org.apache.activemq:type=Broker,brokerName=localhost,service=Health/healthStatus" | tee /dev/tty | grep --silent 'Good'

The exit status give OK/KO condition.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment