Monitor WildFly with CLI and your bash skills

There are actually many free and opensource tools for monitoring the jboss/wildfly application server. In this article we will learn how to create a monitoring shell using as little as jboss cli and bash and awk.

In some scenarios you have to monitor one or two key attributes of the application server configuration and receive an alert if the attribute has reached a critical value. In such scenario it is not worthy installing a complex tool, especially if you have some basic linux shell scripting.

We will learn how to monitor a few key attributes such as the available datasources and the free heap memory on the application server.

Let’s start from the CLI commands. Here is how to monitor the number of available connections in a DB pool:

$ ./jboss-cli.sh -c "/subsystem=datasources/data-source=MYDS/statistics=pool:read-attribute(name="AvailableCount")"  
{     "outcome" => "success",     "result" => "100" }

So, what we want to capture is the value of “result”. With as little as some awk skills we can do that:

availpool=$(./jboss-cli.sh -c "/subsystem=datasources/data-source=MYDS/statistics=pool:read-attribute(name="AvailableCount")" | awk '/result/{gsub("\"", "", $3); print $3}') 

if [ $availpool -le 5 ] then   
   echo "Database Connections Pool getting low! Reamining connections : $availpool" | mail -s "JBoss AS Alert" adminATadmin.com 
fi

Sometimes metrics can be calculated by simple arithmetic of other metrics. For example, if you want to be informed about the free memory on a server, you have to compute: max memorymemory used as follows:

[[email protected]:9990 /] /core-service=platform-mbean/type=memory:read-attribute(name=heap-memory-usage)

{

    "outcome" => "success",

    "result" => {

        "init" => 67108864L,

        "used" => 158278728L,

        "committed" => 341835776L,

        "max" => 477102080L

    }

}

We will need to variables for this purpose:

heapused=$(./jboss-cli.sh -c "/core-service=platform-mbean/type=memory:read-attribute(name=heap-memory-usage)" | grep "used" | awk '{print $3}' | sed 's/L,//') 

heapmax=$(./jboss-cli.sh -c "/core-service=platform-mbean/type=memory:read-attribute(name=heap-memory-usage)" | grep "max" | awk '{print $3}' | sed 's/L//') 

freememory=$((heapmax - heapused)) 

if [ $freememory -le 128000000 ] then   
   echo "JVM Heap memory getting low: Remaining: $freememory bytes" | mail -s "JBoss AS Alert" adminATadmin.com 
fi

In the above script, we are sending an alert by mail if the amount of free heap memory drops below 128 MB. Easy peasy !

Please note that we have assumed that the jboss-cli.sh script is going to connect automatically to the loopback address. If you don’t want to include the IP Address and port of the Management interface in the script, we suggest entering the default value for it in jboss-cli.xml

Found the article helpful? if so please follow us on Socials