Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
zbx_functions: Bash functions for Zabbix monitoring
export PATH='/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/sbin'
# Read Zabbix agent config.
ZABBIX_AGENTD_CONF='/etc/zabbix/zabbix_agentd.conf'
ZABBIX_SERVERS=$( grep -v '^#' "${ZABBIX_AGENTD_CONF}" \
| grep ServerActive \
| sed -e 's/\s//g' -e 's/ServerActive=//g' -e 's/,/ /g' )
ZABBIX_SERVER_DEFAULTPORT=10051
MY_HOSTNAME=$( zabbix_agentd -t agent.hostname|cut -d'|' -f2-|sed -e 's/]$//' )
#
# zbx_bulk_sender:
# Read bulk data from stdin and send to Zabbix Servers(for multiple ServerActive).
# Each line of stdin contains whitespace delimited: <hostname> <key> <timestamp> <value>.
# Timestamp should be specified in Unix timestamp format.
# SEE ALSO:
# https://www.zabbix.com/documentation/2.2/manpages/zabbix_sender
#
function zbx_bulk_sender() {
local ZABBIX_SERVER
local SERVER_ADDR
if [ -t 0 ]; then
DATA=""
else
DATA=$( cat - )
fi
for ZABBIX_SERVER in ${ZABBIX_SERVERS}; do
SERVER_ADDR=$( echo ${ZABBIX_SERVER}|cut -d':' -f1 )
echo ${ZABBIX_SERVER}|grep ':' \
&& SERVER_PORT=$( echo ${ZABBIX_SERVER}|cut -d':' -f2 ) \
|| SERVER_PORT=${ZABBIX_SERVER_DEFAULTPORT}
echo "${DATA}" \
| sed -e 's/^"*-"* /"'"${MY_HOSTNAME}"'" /' \
| timeout 5 zabbix_sender -vvv -z ${SERVER_ADDR} -p ${SERVER_PORT} -T -i -
done
}
#
# zbx_sender:
# Send data to Zabbix Servers(for multiple ServerActive).
# $1: item key.
# $2: item value.
#
function zbx_sender() {
local ZABBIX_SERVER
local SERVER_ADDR
local ITEM_KEY="$1"
local VALUE="$2"
for ZABBIX_SERVER in ${ZABBIX_SERVERS}; do
SERVER_ADDR=$( echo ${ZABBIX_SERVER}|cut -d':' -f1 )
echo ${ZABBIX_SERVER}|grep ':' \
&& SERVER_PORT=$( echo ${ZABBIX_SERVER}|cut -d':' -f2 ) \
|| SERVER_PORT=${ZABBIX_SERVER_DEFAULTPORT}
timeout 5 zabbix_sender -vvv -z ${SERVER_ADDR} -p ${SERVER_PORT} \
-s "${MY_HOSTNAME}" -k "${ITEM_KEY}" -o "${VALUE}"
done
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment