Last active
February 4, 2022 09:11
-
-
Save gentunian/807c26b15fd8dc94fd3225a09afb4f5c to your computer and use it in GitHub Desktop.
educational purpouses: describe how autoregistration works
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# | |
# Copyright note: | |
# --------------- | |
# | |
# This script is a modification from: | |
# https://support.zabbix.com/secure/attachment/49385/get.sh | |
# written by gleb@irc#zabbix | |
# | |
# The script will connect to the zabbix server trapper port (default: 10051) | |
# in order to retrieve the list of active checks. But zabbix, behind the scenes | |
# will also fire an autoregistration event if the conditions are met. | |
# For more information please take a look at: | |
# | |
# https://www.zabbix.com/documentation/3.2/manual/discovery/auto_registration | |
# | |
# Despite that we are asking for active checks in the request, we want to | |
# fire the auto-registration event. That's the main idea of this script. | |
# Zabbix is expecting something like: <HEADER><DATALEN><DATA> where: | |
# <HEADER> := ZBXD\x01 | |
# <DATALEN> := <little-endian-64bit-hex-value> | |
# <DATA> := <json-object-defining-the-request> | |
# | |
# Asciially: | |
# | |
# +---+---+---+---+----+----+----+----+----+----+----+----+----+-----------------+ | |
# | Z | B | X | D | 01 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | 00 | ............... | | |
# +---+---+---+---+----+----+----+----+----+----+----+----+----+-----------------+ | |
# \____________________/\______________________________________/\________________/ | |
# HEADER (5 bytes) DATALEN (8 bytes) DATA (max: 128MB) | |
# | |
# | |
# <DATALEN> must be the lenght of the <DATA> segment as 64bit HEX little endian byte order. | |
# | |
if [[ $# -lt 2 || $# -gt 3 ]]; then | |
echo | |
echo "Usage: $0 { <zabbix-host-name> <zabbix-server> } [ <zabbix-trapper-port> ]" | |
echo | |
exit 1 | |
fi | |
# The hostname we are trying to mock auto-registration | |
ZBX_HOST=$1 | |
# Zabbix server host/ip | |
ZBX_SERVER=$2 | |
# Zabbix server trapper port (defaults to 10051) | |
ZBX_TRAPPER_PORT=${3:-10051} | |
# <HEADER> value as explained above | |
HEADER="ZBXD\\1" | |
# <DATA> content as explained above. | |
# | |
# We don't check for ZBX_HOST to be empty. Either way if wrong or empty | |
# the request will be OK, but no information may be displayed. | |
DATA="{\"request\": \"active checks\", \"host\": \"${ZBX_HOST}\"}" | |
# <DATALEN> as explained above. | |
# | |
# Create 64bit HEX little endian value equals to the size in bytes of DATA | |
DATALEN=$(printf '%016x' "${#DATA}" | \ | |
awk '{ r=""; for( i=length($0)-1; i > 0; i=i-2) r=r "\\x" substr($0, i, 2); print r}') | |
# Awesome work by gleb: | |
# Create connection | |
exec 3<>/dev/tcp/$ZBX_SERVER/$ZBX_TRAPPER_PORT | |
# Print to file descriptor 3. That was created in the above line. | |
printf "%b%b%s" "${HEADER}" "${DATALEN}" "${DATA}" >&3 | |
# output the result skipping 5 bytes of "ZBXD\\1" header and 8 bytes of message length | |
RESPONSE=$(tail -c+13 <&3) | |
# If python is installed and python version supports/has json module make it human-readable | |
$(python -c "import json" 2> /dev/null) && echo ${RESPONSE} | python -mjson.tool | |
# If no python or json module exists... | |
if [ $? -ne 0 ]; then | |
# just print the response | |
echo ${RESPONSE} | |
fi | |
# Closes file descriptor | |
exec 3>&- | |
exec 3<&- |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment