db_updater: Put parentheses back
[merlin.git] / push_logs.sh
blob47b9cea50cc8f40d3b67d346772bbb422119830d
1 #!/bin/sh
3 # Copyright(c) 2008 op5 AB
4 # License: GPLv2
7 master=
8 ssh_key=
9 newest_archived=
10 last_sent_file=/opt/monitor/op5/log_pusher/last_sent
11 config_file=/opt/monitor/etc/nagios.cfg
12 archive_dir=$(sed -n 's/^log_archive_path=\([^ \t]*\).*$/\1/p' $config_file)
13 log_file=$(sed -n 's/^log_file=\([^ \t]*\).*$/\1/p' $config_file)
14 test "$log_file" || log_file=/opt/monitor/var/nagios.log
15 fpart="${log_file##*/}"
16 test "$archive_dir" || archive_dir=$(echo $log_file | sed 's#/$fpart.*##g')
17 test -d "$archive_dir" && \
18 newest_archived=$(/bin/ls --color=none -rt1 $archive_dir/*.log 2>/dev/null | tail -1)
19 #echo "fpart: $fpart; log_file: $log_file"
20 #echo "archive_dir: $archive_dir"
21 #echo "newest_archived: $newest_archived"
22 #exit 0
24 get_lines()
26 grep --no-filename \
27 -e ' HOST ALERT:' -e ' SERVICE ALERT:' \
28 -e ' INITIAL HOST STATE:' -e ' INITIAL SERVICE STATE:' \
29 -e ' CURRENT HOST STATE:' -e ' CURRENT SERVICE STATE:' \
30 -e ' HOST DOWNTIME ALERT:' -e ' SERVICE DOWNTIME ALERT:' \
31 "$@" 2>/dev/null | sort
34 tmpfile=$(mktemp)
35 trap "rm -f $tmpfile" 0 1 2 3 15
36 test -f "$last_sent_file" && last_sent=$(cat "$last_sent_file" 2>/dev/null)
37 if [ "$last_sent" ]; then
38 get_lines $newest_archived $log_file \
39 | sed -n "/^.$last_sent/,\$p" | sed "/^.$last_sent/d" > $tmpfile
40 else
41 last_sent=0
42 get_lines "$archive_dir"/* $log_file > $tmpfile
45 if ! [ -s "$tmpfile" ]; then
46 echo "No updates to send."
47 exit 0
50 cur_sent=$(sed -n '$s/^.\([0-9]*\).*/\1/p' $tmpfile)
52 gzip -9f $tmpfile
53 trap "rm -f $tmpfile $tmpfile.gz" 0 1 2 3 15
54 tmpfile="$tmpfile.gz"
56 dest_name="/opt/monitor/pushed_logs/$(hostname).$last_sent-$cur_sent.log.gz"
57 scp -i $ssh_key $tmpfile monitor@$master:$dest_name && \
58 echo "$cur_sent" > "$last_sent_file"