I want to create a bash/shell script which monitors the JVM memory usage of Wildfly by using the jboss-cli Therefor I need to get the hosts and get the Wildfly servers per host in a for loop. However, starting/connecting the jboss-cli takes several seconds and stresses the CPU. This makes the script very slow. Sending commands in an interactive jboss-cli session is quite fast.
Is there a way to connect to the jboss-cli only once and send the input commands to that active session? I need the output of the commands to continue with the script.
Current script:
# List all hosts:
hosts="$(jboss-cli.sh -c --controller=servername:9990 --command=":read-children-names(child-type=host)" | grep " " | awk '{print $1}' | sed 's/,//g' | sed 's/"//g')"
# Loop through hosts results:
for host in $hosts
{
#List all servers:
servers="$(jboss-cli.sh -c --controller=servername:9990 --command="/host=$host:read-children-names(child-type=server)" | grep " " | awk '{print $1}' | sed 's/,//g' | sed 's/"//g')"
# Loop through server results:
for server in $servers
{
# check if server is running:
serverstate=$(jboss-cli.sh -c --controller=servername:9990 --command="/host=$host/server=$server:read-attribute(name=server-state)" | grep "result" | awk '{print $3}' | sed 's/"//g')
if [ $serverstate = "running" ]
then
#Do a check etc. etc.
fi
}
}