b68efb87919ccc131ee019b1de0b40b04a6f2698
[matthijs/upstream/backupninja.git] / src / ninjareport.in
1 #!@BASH@
2 # -*- mode: sh; sh-basic-offset: 3; indent-tabs-mode: nil; -*-
3 #
4 #
5 # Ninjareport - generate a single simple report for a lot of hosts
6 #
7 # requires logtail
8 #
9 # Copyright (C) 2007 - riseup.net -- property is theft.
10
11 # TODO:
12 #
13 #  . check for logs that are never updating and warn
14 #  . change constantly updating logs (maildir) to be more friendly
15 #  . documentation
16 #  . maybe make config option that lists all hosts that should report-in, and if 
17 #    one doesn't then warn
18 #  . restrict rsync somehow?
19 #  . abstract path for logtail
20 #  . on the report master, the reportdirectory should be set where the reports will be going, note this
21
22 mail=1
23 display=0
24
25 process() {
26
27 # look in the logfile for any lines like the following:
28 # Jan 20 01:02:46 Info: FINISHED: 2 actions run. 0 fatal. 0 error. 0 warning.
29 #
30 # note: some backups never finish, such as the maildir one, need to handle these
31 # perhaps by looking for FAILED messages?
32 # note2: what about logs that aren't being updated? this is a failure case and 
33 # should be looked for
34 # note3: there are also these entries:
35 # Jan 20 14:00:01 Fatal: No backup actions configured in '/etc/backup.d', run ninjahelper!
36
37 # The following has to be done without invoking a subshell (see BashFAQ #24)
38 logupdates=`maketemp ninjadata`
39
40 /usr/sbin/logtail -f $host > $logupdates
41 grep FINISHED $logupdates | 
42 (
43         fatal=0
44         warning=0
45         error=0
46
47         while read line
48         do
49                 line_array=($line)
50                 fatal=$(($fatal + ${line_array[8]}))
51                 error=$(($error + ${line_array[10]}))
52                 warning=$(($warning + ${line_array[12]}))
53         done
54         if (( $fatal || $warning || $error )); then
55                 echo "`basename $host .log`: $fatal fatals found, $error errors found, $warning warnings found" >> $statusfile
56                 echo "" >> $reportappend
57                 echo "`basename $host .log` log entries since last ninjareport" >> $reportappend
58                 echo "---------" >> $reportappend
59                 cat $logupdates >> $reportappend
60                 rm $logupdates
61         fi
62 )
63
64 }
65
66 generatereport() {
67
68 reportfile=`maketemp ninjareport`
69
70 # Generate a report, only if there are failures
71 if [ -s $statusfile ]; then
72         echo "         backupninja mission failures - `date`" >> $reportfile
73         echo "       --------------------------------------------------------------" >> $reportfile
74         echo "" >> $reportfile
75         cat $statusfile | column -t >> $reportfile
76         echo "" >> $reportfile
77         echo "         log entries from failed reports" >> $reportfile
78         echo "       -----------------------------------" >> $reportfile
79         cat $reportappend >> $reportfile
80 fi
81
82 }
83
84 usage() {
85         cat << EOF
86 This script generates a backupninja status report for all configured
87 systems. It requires that each status report is placed in a spot where
88 ninjareport can read it, reports are mailed to the reportemail
89 configured in @CFGDIR@/backupninja.conf.
90
91 The following options are available:
92 -h, --help              This usage message
93 -f, --conffile FILE     Use FILE for the configuration instead
94                         of @CFGDIR@/backupninja.conf
95 -m, --mail <email>      Mail the report to this address
96 -o, --out               Don't mail the report, just display it
97
98 EOF
99 }
100
101 #####################################################
102 ## MAIN
103
104 conffile="@CFGDIR@/backupninja.conf"
105
106 ## process command line options
107
108 while [ $# -ge 1 ]; do
109         case $1 in
110                 -h|--help) 
111                         usage
112                         exit 0
113                         ;;
114                 -f|--conffile)
115                         if [ -f $2 ]; then
116                                 conffile=$2
117                         else
118                                 echo "-f|--conffile option must be followed by an existing filename"
119                                 fatal "-f|--conffile option must be followed by an existing filename"
120                                 usage
121                         fi
122                         # we shift here to avoid processing the file path 
123                         shift
124                         ;;
125                 -m|--mail)
126                         reportemail=$2
127                         shift
128                         ;;
129                 -o|--out)
130                         mail=0
131                         display=1
132                         ;;
133                 *)
134                         echo "Unknown option $1"
135                         usage
136                         exit
137                         ;;
138         esac
139         shift
140 done                                                                                                                                                                                                            
141
142 ## Load and confirm basic configuration values
143
144 # bootstrap
145 if [ ! -r "$conffile" ]; then
146         echo "Configuration file $conffile not found." 
147         fatal "Configuration file $conffile not found."
148 fi
149
150 # find $libdirectory
151 libdirectory=`grep '^libdirectory' $conffile | awk '{print $3}'`
152 if [ -z "$libdirectory" ]; then
153    if [ -d "@libdir@" ]; then
154       libdirectory="@libdir@"
155    else
156       echo "Could not find entry 'libdirectory' in $conffile." 
157       exit 1
158    fi
159 else
160    if [ ! -d "$libdirectory" ]; then
161       echo "Lib directory $libdirectory not found." 
162       exit 1
163    fi
164 fi
165
166 # include shared functions
167 . $libdirectory/tools
168
169 setfile $conffile
170
171 getconf reportdirectory
172 getconf reportemail
173
174 ## Process each configuration file
175
176 hosts=`find $reportdirectory -follow -mindepth 1 -maxdepth 1 -type f ! -name '*.offset' | sort -n`
177
178 if [ -z "$hosts" ]; then
179         echo "Fatal: No backupninja reports found in '$reportdirectory'!"
180         mail=0
181 fi
182
183 statusfile=`maketemp ninjastatus`
184 reportappend=`maketemp ninjaappend`
185
186 for host in $hosts; do
187         [ -f "$host" ] || continue
188         # Check somehow that the file is a valid report file
189         process $host
190 done
191
192 generatereport
193
194 ## mail the report to the report address or display it
195
196 if [ -s $reportfile ]; then
197     if [ $mail == 1 ]; then
198         mail -s "backupninja mission failure report" $reportemail < $reportfile
199     fi
200 fi
201
202 if [ $display == 1 ]; then
203         cat $reportfile
204 fi
205