I have found a reliable workaround for this issue.
What I realized is that the check_jmx script on the NagiosXI machine would run just as well on the remote machine I was trying to monitor. I copied that and jmxquery.jar to the remote machine.
I had to make a couple modifications. Normally, java is installed on the NagiosXI machine so that the check can run. On my remote machine, each instance I'm monitoring has its own version of java locally, so I just pointed at one of those. If you have java globally you won't need to make this edit. You'll also notice though, the one change you HAVE to make: on the next line after the java call, add "echo $?". That echoes the actual error code back to the script on the NagiosXI server.
Code: Select all
#!/bin/sh
#
# Nagios plugin to monitor Java JMX (http://java.sun.com/jmx)attributes.
#
RDIR=`dirname $0`
/path/to/local/java/bin/java -cp $RDIR/jmxquery.jar org.nagios.JMXQuery $@
echo $?
Then, on the NagiosXI host, I created a script called check_jmx_remote
Code follows, but the explanation is simple: I simply establish an ssh connection to the remote host and execute the check_jmx script over there. The result is returned to me, along with the original exit code.
In order to make this work, you'll need to export your NagiosXI key to the remote server. You'll need to have root access, as you'll need to su nagios from root. Then cd ~/.ssh
ssh-copy-id -i
[email protected]
It'll ask you for the password for the user on the remote host. This all allows you to make the ssh connection without having to supply the password, thus allowing it in a script.
You'll note in my code that I use -p in my ssh command as we use a nonstandard port, which i've subbed with 55555, but you can delete that if you're on 22.
Code: Select all
#!/bin/bash
#
# Nagios plugin to monitor Java JMX (http://java.sun.com/jmx)attributes.
#
theCommand="$@"
theHost=$(cut -d'/' -f7 <<<"$theCommand")
theHost="$(cut -d':' -f1 <<<$theHost)"
# echo "$theHost"
sendCommand="$(ssh -p 55555 myuser@$theHost /usr/local/nagios/libexec/check_jmx $theCommand)"
theResponse=$(echo "$sendCommand"|head -1)
#echo "$theResponse"
theExitCode=$(echo "$sendCommand"|tail -n1)
#echo "$theExitCode"
if [ "$theExitCode" == "0" ]; then
echo "OK - Status: $theResponse"
exit 0;
elif [ "$theExitCode" == "2" ]; then
echo -e "CRITICAL - Status: $theResponse"
exit 2;
else
echo -e "WARNING - Status: $theResponse"
exit 1;
fi
I've left some lines commented that you can uncomment if you need to run it locally on the cli to check output.
You''ll need to create a new Command for this, mine is called check_jmx_local, just like the file. You can literally duplicate the check_jmx command and change the name, and the file it's pointing to instead of creating one from scratch.
Then you just set up the services. The one thing that's different about the command in $ARG1$ is that you don't put any single quotes. So instead of this:
-a '-U service:jmx:rmi:///jndi/rmi://$HOSTADDRESS$:9999/jmxrmi -O java.lang:type=Memory -A HeapMemoryUsage -K used -I HeapMemoryUsage -J used -vvvv -w 7516188920 -c 8589930194'
You put this:
-a -U service:jmx:rmi:///jndi/rmi://$HOSTADDRESS$:9999/jmxrmi -O java.lang:type=Memory -A HeapMemoryUsage -K used -I HeapMemoryUsage -J used -vvvv -w 7516188920 -c 8589930194
I hope this helps someone. It's taken me about a week to work this all out.