zabbix-trappers/runtrap

53 lines
987 B
Bash
Executable File

#!/bin/bash
lockfile="/tmp/zabbix.trap.lock"
tempfile=$(mktemp /tmp/zabbix.trap.tmp.XXXXXXXXXX)
trapdir="$(dirname $(readlink -f $0))/trap.d"
errors=0
if [[ -r "${lockfile}" ]]; then
if [[ $(pgrep -f $(readlink -f $0) | wc -l) -gt 0 ]]; then
runs=$(head -n1 ${lockfile})
if [[ $runs -ge 3 ]]; then
echo "need to kill prior poll"
for s in ${trapdir}/*
do
echo "Kill: $s "
pkill -9 -f $s
done
rm -f /tmp/zabbix.trap.lock
rm -f /tmp/zabbix.trap.tmp.*
pkill -9 -f $(readlink -f $0)
else
#let "runs++"
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
zsend=$(zabbix_sender -c /etc/zabbix/zabbix_agentd.conf -i ${tempfile})
if [ $? -ne 0 ]; then
echo "2"
else
echo "${errors}"
fi
rm -f ${tempfile}