zabbix-trappers/runtrap

76 lines
1.3 KiB
Bash
Executable File

#!/bin/bash
state=${1:-live}
agentconf=${2:-/etc/zabbix/zabbix_agentd.conf}
lockfile="/tmp/zabbix.trap.${state}.lock"
tempfile=$(mktemp /tmp/zabbix.trap.${state}.tmp.XXXXXXXXXX)
trapdir="$(dirname $(readlink -f $0))/trap.d/${state}"
errors=0
debug=false
log() {
echo "$(date +"%x %T") $*"
}
if [[ "$0" == *".debug" ]]; then
debug=true
fi
if [[ ! -d "$trapdir" ]]; then
log "ERROR: Trap directory doesn't exist: $trapdir "
exit 5
fi
if [[ -r "${lockfile}" ]]; then
if [[ $(pgrep -f $(readlink -f $0) | wc -l) -gt 0 ]]; then
runs=$(head -n1 ${lockfile})
if [[ $runs -ge 3 ]]; then
for s in ${trapdir}/*
do
log "Kill: $s "
pkill -9 -f $s
done
rm -f ${lockfile}
rm -f /tmp/zabbix.trap.${state}.*
pkill -9 -f $(readlink -f $0)
else
echo "$[ ++runs ]" > ${lockfile}
exit 0
fi
fi
fi
echo 1 > ${lockfile}
for s in ${trapdir}/*
do
$s >> ${tempfile} 2> /dev/null
if [ $? -ne 0 ]; then
errors=1
fi
done
#cat ${tempfile}
#rm -f ${tempfile}
#echo
#echo "Errors: $errors"
#exit 0
if $debug; then
cat ${tempfile}
else
zsend=$(zabbix_sender -c ${agentconf} -i ${tempfile})
if [ $? -ne 0 ]; then
log "ERROR"
else
if [[ $errors -gt 0 ]]; then
log "WARN"
else
log "OK"
fi
fi
fi
rm -f ${tempfile}
rm -f ${lockfile}