See www.zabbix.com for the official Zabbix site.

Docs/howto/zabbix get jmx

From Zabbix.org
Jump to: navigation, search
Note: zabbix_get cannot be used to query Java gateway since versions 2.2.17, 3.0.7, 3.2.3 and 3.3.0. See ZBX-11528 for more detail.
Note: The protocol for JMX items has been changed in Zabbix 3.4. See the upgrade notes for more detail.

Intro

While an undocumented feature, you can actually use the zabbix_get command to test out the Zabbix Java gateway. This is very helpful to be able to manually test out items, without having to wait for Zabbix to automatically poll them, just like you can with zabbix_get for items from the agent.

Script

Below is a small Bash script that you can use to wrap the zabbix_get command

#!/usr/bin/env bash

ZBXGET="/usr/bin/zabbix_get"
if [ $# != 5 ]
then
    echo "Usage: $0 <JAVA_GATEWAY_HOST> <JAVA_GATEWAY_PORT> <JMX_SERVER> <JMX_PORT> <KEY>"
    exit;
fi

QUERY="{\"request\": \"java gateway jmx\",\"conn\": \"$3\",\"port\": $4,\"keys\": [\"$5\"]}"

$ZBXGET -s $1 -p $2 -k "$QUERY"

Copy the above code into a new file, make it executable and when you call the script you will see the JSON response from the gateway. Example below:

user@host # ./zabbix_get_jmx localhost 10052 java_application_server 9001 'jmx[\"java.lang:type=Threading\",ThreadCount]'
{"response":"success","data":[{"value":"2516"}]}

NOTE: Its important to correctly escape the quotes in the keys section, or the script will fail with an error, that it was unable to parse the JSON object.

Another script

But why?! Why would you use undocumented features? Here is a script that mimics the one above (including requirement to escape the quotes manually) but does not depend on zabbix_get.

#!/usr/bin/env bash

if [ $# != 5 ]
then
	echo "Usage: $0 <JAVA_GATEWAY_HOST> <JAVA_GATEWAY_PORT> <JMX_SERVER> <JMX_PORT> <KEY>"
	exit;
fi

# create connection
exec 3<>/dev/tcp/$1/$2

# compose message
MSG="{\"request\": \"java gateway jmx\", \"conn\": \"$3\", \"port\": $4, \"keys\": [\"$5\"]}"

# write message length as zero-padded 16-digit hexadecimal number
printf -v LEN '%016x' "${#MSG}"

# prepare message length in little endian representation
BYTES=""
for i in {0..14..2}
do
	BYTES="\\x${LEN:$i:2}$BYTES"
done

# prepend protocol header and message length
printf "ZBXD\\1$BYTES%s" "$MSG" >&3

# output the result skipping 5 bytes of "ZBXD\\1" header and 8 bytes of message length
tail -c+13 <&3

Where to go next?

If you are still here you may be interested in voting for ZBXNEXT-3764 to get a zabbix_get-like utility for testing Java gateway out of the box.