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