#!/bin/bash MONDIR=/var/lib/mon #resolution of modify times is only 2 second on some filesystems, so prevent race conditions by touching it now and sleeping 2 seconds touch $MONDIR/.zabbix-time echo "Sleeping..." sleep 2 #traveled back in time? (e.g. systemclock is adjusted) if [ $MONDIR/.zabbix-time -ot $MONDIR/zabbix-latest ]; then #just resend everything rm $MONDIR/zabbix-latest fi UPDATES="" #Let zabbix rediscover all syn-3 facilities if something changed: if [ $MONDIR/alerts -nt $MONDIR/zabbix-latest ]; then echo "Rediscovering services..." cd $MONDIR/alerts JSON= for FACILITY in *; do #JOBTTL=`cat $JOBNAME` if [ "$JSON" != "" ]; then JSON=$JSON', ' fi JSON=$JSON'{ "{#FACILITY}": "'$FACILITY'" } ' done MODIFY_TIME=`stat --format=%Y .` #low level discovery takes time, so send it and then wait a minute to process echo -en '- syn3.discovery { "data": [ '$JSON' ] }\n' | zabbix_sender -vv -c /etc/zabbix/zabbix_agentd.conf -i - || exit 1 echo "Waiting for zabbix to process discovery..." sleep 65 fi #Find all facility states that have changed since last succesfull send cd $MONDIR/alerts for FACILITY in *; do #NOTE: msg-modifytime only changes if something actually changed, while state-modifytime always changes when syn3-state is called. if [ $FACILITY/msg -nt $MONDIR/zabbix-latest ]; then echo "Facility $FACILITY has changed since last send." STATE=`cat $FACILITY/state` MODIFY_TIME=`stat --format=%Y $FACILITY/state` MSG=`cat $FACILITY/msg | tr '\n' ' '` UPDATES="$UPDATES""- syn3.state[$FACILITY] $STATE, $MSG\n" fi done if [ "$UPDATES" != "" ]; then echo "Sending updates to zabbix:" echo -en "$UPDATES" if echo -en "$UPDATES" | zabbix_sender -vv -c /etc/zabbix/zabbix_agentd.conf -i -; then #only if everything went ok, we update the timestamp of the latest-file. otherwise everything will be send again. mv $MONDIR/.zabbix-time $MONDIR/zabbix-latest echo "All updates sent ok." else echo "Error while sending, will try again later." exit 1 fi else echo "No changes found" fi