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 memory – memory used as follows:
[standalone@127.0.0.1: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
Recommended Articles
Querying Active Services in WildFly using CLI and JMX
Learn how to check available services with WildFly using the CLI and JMX, including a step-by-step guide on querying the jgroups service.
Connecting VisualVM to WildFly with Java 21: A Step-by-Step Guide
Learn how to monitor and profile your WildFly application server using VisualVM, a free tool for Java applications.
Monitor Your Java Application Server with Hawtio: A Step-by-Step Guide
Learn how to use Hawtio as an open-source monitoring solution for JVM middleware like JBoss Enterprise Application Server and WildFly, with a single point of management across multiple middleware.
Monitor and Invalidate HTTP Sessions in WildFly Application Server Using CLI and Micrometer Metrics
Learn how to monitor and invalidate HTTP sessions in WildFly using CLI and Micrometer metrics. #WildFly #Java #Middleware #CloudNative