Last active
August 29, 2015 14:01
-
-
Save alice-xu/bf89c5f1174a3e793ff8 to your computer and use it in GitHub Desktop.
zbx_functions: Bash functions for Zabbix monitoring
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
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