c4e488466340e3118d9e1f3a4d98449a224630fb
[mirror/dsa-nagios.git] / dsa-nagios-checks / dsa-check-dabackup-server
1 #!/bin/sh
2
3 # some tests weasel uses to check if da-backup is properly configured:
4 # Copyright 2008 Peter Palfrader
5 #
6 # Permission is hereby granted, free of charge, to any person obtaining
7 # a copy of this software and associated documentation files (the
8 # "Software"), to deal in the Software without restriction, including
9 # without limitation the rights to use, copy, modify, merge, publish,
10 # distribute, sublicense, and/or sell copies of the Software, and to
11 # permit persons to whom the Software is furnished to do so, subject to
12 # the following conditions:
13 #
14 # The above copyright notice and this permission notice shall be
15 # included in all copies or substantial portions of the Software.
16 #
17 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24
25
26 set -e
27 set -u
28
29 err=0
30
31 log() {
32         if [ "$1" == "warn" ] && [ "$err" -lt 1 ]; then
33                 err=1
34         elif [ "$1" == "critical" ] && [ "$err" -lt 2 ]; then
35                 err=2
36         elif [ "$1" == "unknown" ] && [ "$err" == 0 ]; then
37                 err=3
38         fi
39 }
40
41 cd /etc/da-backup-manager
42
43 # check that the files have proper names
44 for i in *; do
45         s=`cat "$i" | awk -F = '$1=="source" {print $2}'`
46         if [ "$s" = "master.debian.org/debian" ] && [ "$i" = "listarchive" ]; then
47                 # historical exception
48                 continue
49         fi
50         if [ "`echo $s | tr / -`" != "$i" ]; then
51                 log warn
52                 echo "Mismatch: Filename $i but backup source $s"
53         fi
54 done
55
56 # and that source == destination everywhere
57 for i in *; do
58         s=`cat $i | awk -F = '$1=="source"{print $2}'`
59         d=`cat $i | awk -F = '$1=="destination"{print $2}'`
60         if [ "$d" != "$s" ]; then
61                 log warn
62                 echo "source != dest in $i. ($s != $d)"
63         fi
64 done
65
66 # check that everything that has an entry in authorized_keys has one in /etc/da-backup-manager
67 rsync_targets=`tempfile`
68 backup_manager_sources=`tempfile`
69 trap "rm -f '$rsync_targets' '$backup_manager_sources'" 0 1 2 5 15
70 cat /root/.ssh/authorized_keys |
71         grep '^command=' |
72         sed -e 's/",no-por.*//' -e 's/.*--ignore-errors . //' -e 's#/org/backup.debian.org/staging/##' |
73         sort > "$rsync_targets"
74 grep -h source /etc/da-backup-manager/* | 
75         sed -e 's/source=//' |
76         sort > "$backup_manager_sources"
77 if ! diff "$rsync_targets" "$backup_manager_sources" > /dev/null; then
78         log warn
79         echo "authorized_keys vs. /etc/da-backup-manage mismatch:"
80         diff "$rsync_targets" "$backup_manager_sources"
81 fi
82
83 # check that everything listed as either source or destination by da-backup-manager does exist  -  if stuff is new it might still be missing tho
84 for i in `awk -F = '$1=="source"     {print $2}' /etc/da-backup-manager/* `; do
85         if ! [ -d "/org/backup.debian.org/staging/$i" ]; then
86                 log warn
87                 echo "Warning: source $i does not exist"
88         fi
89 done
90 for i in ` awk -F = '$1=="destination"{print $2}' /etc/da-backup-manager/* `; do
91         if ! [ -d "/org/backup.debian.org/backups/$i" ]; then
92                 log warn
93                 echo "Warning: destination $i does not exist (if it is new, it takes a day)"
94         fi
95 done
96
97 # check that everything that does exist has an entry in da-backup-manager
98 cd /org/backup.debian.org/staging
99 for i in */*; do
100         if ! [ -d "$i" ]; then
101                 log warn
102                 echo "WTF: staging/$i is not a dir"
103                 continue
104         fi
105         if ! grep "source=$i" /etc/da-backup-manager/* > /dev/null; then
106                 log warn
107                 echo "staging/$i is not listed anywhere in /etc/da-backup-manager"
108         fi
109 done
110 cd /org/backup.debian.org/backups
111 for i in */*; do
112         if ! [ -d "$i" ]; then
113                 log warn
114                 echo "WTF: backups/$i is not a dir"
115                 continue
116         fi
117         if ! grep "destination=$i" /etc/da-backup-manager/* > /dev/null; then
118                 log warn
119                 echo "backups/$i is not listed anywhere in /etc/da-backup-manager"
120         fi
121 done
122
123 if [ "$err" = 0 ]; then
124         echo "da-backup seems to be properly configured"
125 fi
126 exit $err