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
}
}
Had a similar problem. My solution is using coroutine in ksh and I don't know if it's possible in bash like your flag (but if ksh is installed you can still use it). At least it might gives you some pointers.
#!/bin/ksh
#connecting using coroutine -- Note the |& at the end
{JBOSS_HOME}/bin/jboss-cli.sh --connect --controller={Master_IP}:{Master_Port} --user={UID} --password={Password} |&
#sending request to coroutine
print -p "ls -l /host\n"
#Reading from corouting
while read -p answer; do
##Need to verify when stop reading -- after the prompt is back
if [[ $answer != *domain*:* ]]; then
## If it's not an error, concatenate, space delimited, the response
if [[ $answer == *WFLYCTL* ]]; then
print "$(date +"%Y%m%d%H%M") - FATAL - Error detected - Exiting"
print "$(date +"%Y%m%d%H%M") - Error detected : $answer"
exit
else
Dummy+=$answer" "
fi
elif [[ locFlag -eq 1 ]]; then
locFlag=0
break
else
locFlag=1
fi
done
##Response to array
SvrArray=( $Dummy )
##Do anything with the response, here just printing
for i in ${!SvrArray[@]}; do
print "$i. ${SvrArray[$i]}"
done