1 | #!/bin/bash |
---|
2 | MONDIR=/var/lib/mon |
---|
3 | |
---|
4 | #resolution of modify times is only 2 second on some filesystems, so prevent race conditions by touching it now and sleeping 2 seconds |
---|
5 | touch $MONDIR/.zabbix-time |
---|
6 | echo "Sleeping..." |
---|
7 | sleep 2 |
---|
8 | |
---|
9 | #traveled back in time? (e.g. systemclock is adjusted) |
---|
10 | if [ $MONDIR/.zabbix-time -ot $MONDIR/zabbix-latest ]; then |
---|
11 | #just resend everything |
---|
12 | rm $MONDIR/zabbix-latest |
---|
13 | fi |
---|
14 | |
---|
15 | UPDATES="" |
---|
16 | |
---|
17 | #Let zabbix rediscover all syn-3 facilities if something changed: |
---|
18 | if [ $MONDIR/alerts -nt $MONDIR/zabbix-latest ]; then |
---|
19 | echo "Rediscovering services..." |
---|
20 | cd $MONDIR/alerts |
---|
21 | JSON= |
---|
22 | for FACILITY in *; do |
---|
23 | #JOBTTL=`cat $JOBNAME` |
---|
24 | if [ "$JSON" != "" ]; then |
---|
25 | JSON=$JSON', ' |
---|
26 | fi |
---|
27 | JSON=$JSON'{ "{#FACILITY}": "'$FACILITY'" } ' |
---|
28 | done |
---|
29 | MODIFY_TIME=`stat --format=%Y .` |
---|
30 | |
---|
31 | #low level discovery takes time, so send it and then wait a minute to process |
---|
32 | echo -en '- syn3.discovery { "data": [ '$JSON' ] }\n' | zabbix_sender -vv -c /etc/zabbix/zabbix_agentd.conf -i - || exit 1 |
---|
33 | echo "Waiting for zabbix to process discovery..." |
---|
34 | sleep 65 |
---|
35 | fi |
---|
36 | |
---|
37 | #Find all facility states that have changed since last succesfull send |
---|
38 | cd $MONDIR/alerts |
---|
39 | for FACILITY in *; do |
---|
40 | #NOTE: msg-modifytime only changes if something actually changed, while state-modifytime always changes when syn3-state is called. |
---|
41 | if [ $FACILITY/msg -nt $MONDIR/zabbix-latest ]; then |
---|
42 | echo "Facility $FACILITY has changed since last send." |
---|
43 | STATE=`cat $FACILITY/state` |
---|
44 | MODIFY_TIME=`stat --format=%Y $FACILITY/state` |
---|
45 | MSG=`cat $FACILITY/msg | tr '\n' ' '` |
---|
46 | UPDATES="$UPDATES""- syn3.state[$FACILITY] $STATE, $MSG\n" |
---|
47 | fi |
---|
48 | done |
---|
49 | |
---|
50 | |
---|
51 | if [ "$UPDATES" != "" ]; then |
---|
52 | echo "Sending updates to zabbix:" |
---|
53 | echo -en "$UPDATES" |
---|
54 | if echo -en "$UPDATES" | zabbix_sender -vv -c /etc/zabbix/zabbix_agentd.conf -i -; then |
---|
55 | #only if everything went ok, we update the timestamp of the latest-file. otherwise everything will be send again. |
---|
56 | mv $MONDIR/.zabbix-time $MONDIR/zabbix-latest |
---|
57 | echo "All updates sent ok." |
---|
58 | else |
---|
59 | echo "Error while sending, will try again later." |
---|
60 | exit 1 |
---|
61 | fi |
---|
62 | else |
---|
63 | echo "No changes found" |
---|
64 | fi |
---|
65 | |
---|