cookbook-zabbix/files/default/traps/runtrap.debugpost
2017-02-07 01:02:03 -05:00

109 lines
2.2 KiB
Bash
Executable file

#!/bin/bash
state=${1:-live}
if [[ "$state" == *report* ]]; then
report=1
state="${state#*-}"
fi
agentconf=${2:-/etc/zabbix/zabbix_agentd.conf}
lockfile="/tmp/zabbix.trap.${state}.lock"
tempfile=$(mktemp /tmp/zabbix.trap.${state}.tmp.XXXXXXXXXX)
trap 'rm -f "$tempfile"' EXIT
trapdir="$(dirname $(readlink -f $0))/${state}"
errors=0
debug=false
post=false
if [[ "$0" == *".debug" ]]; then
debug=true
elif [[ "$0" == *".debugpost" ]]; then
debug=true
post=true
fi
if [[ ! -d "$trapdir" ]]; then
echo "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
echo "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 $state >> ${tempfile} 2> /dev/null
if [ $? -ne 0 ]; then
errors=1
if [[ $report -eq 1 ]]; then
echo "WARN: ${s}"
fi
else
if [[ $report -eq 1 ]]; then
echo "OK: ${s}"
fi
fi
done
#cat ${tempfile}
#rm -f ${tempfile}
#echo
#echo "Errors: $errors"
#exit 0
if $debug; then
cat ${tempfile}
if $post; then
zabbix_sender -c ${agentconf} -i ${tempfile} -vv
ret=$?
if [[ $ret -ne 0 ]]; then
echo "ERROR: $ret"
zabbix_sender -c ${agentconf} -vv -k "agent.traps" -o "0"
else
if [[ $errors -gt 0 ]]; then
echo "WARN: $errors"
zabbix_sender -c ${agentconf} -vv -k "agent.traps" -o "1"
else
echo "OK"
zabbix_sender -c ${agentconf} -vv -k "agent.traps" -o "2"
fi
fi
else
echo "- agent.traps 1"
fi
else
zsend=$(zabbix_sender -c ${agentconf} -i ${tempfile})
if [ $? -ne 0 ]; then
echo "ERROR"
zabbix_sender -c ${agentconf} -k "agent.traps" -o "0" > /dev/null 2>&1
else
if [[ $errors -gt 0 ]]; then
echo "WARN"
zabbix_sender -c ${agentconf} -k "agent.traps" -o "1" > /dev/null 2>&1
else
echo "OK"
zabbix_sender -c ${agentconf} -k "agent.traps" -o "2" > /dev/null 2>&1
fi
fi
fi
rm -f ${tempfile}
rm -f ${lockfile}