a08fec76946a03628250b584a7944fec3312f891
[mirror/dsa-nagios.git] / dsa-nagios-checks / 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 AUTHKEYSFILE=/etc/ssh/userkeys/root
30
31 err=0
32
33 log() {
34         if [ "$1" = "warn" ] && [ "$err" -lt 1 ]; then
35                 err=1
36         elif [ "$1" = "critical" ] && [ "$err" -lt 2 ]; then
37                 err=2
38         elif [ "$1" = "unknown" ] && [ "$err" = 0 ]; then
39                 err=3
40         fi
41 }
42
43 cd /etc/da-backup-manager
44
45 # check that the files have proper names
46 for i in *; do
47         s=`cat "$i" | awk -F = '$1=="source" {print $2}'`
48         if [ "$s" = "master.debian.org/debian" ] && [ "$i" = "listarchive" ]; then
49                 # historical exception
50                 continue
51         fi
52         if [ "`echo $s | tr / -`" != "$i" ]; then
53                 log warn
54                 echo "Mismatch: Filename $i but backup source $s"
55         fi
56 done
57
58 # and that source == destination everywhere
59 for i in *; do
60         s=`cat $i | awk -F = '$1=="source"{print $2}'`
61         d=`cat $i | awk -F = '$1=="destination"{print $2}'`
62         if [ "$d" != "$s" ]; then
63                 log warn
64                 echo "source != dest in $i. ($s != $d)"
65         fi
66 done
67
68 # check that everything that has an entry in authorized_keys has one in /etc/da-backup-manager
69 rsync_targets=`tempfile`
70 backup_manager_sources=`tempfile`
71 trap "rm -f '$rsync_targets' '$backup_manager_sources'" 0 1 2 5 15
72 cat "$AUTHKEYSFILE" |
73         grep '^command=' |
74         sed -e 's/",no-por.*//' -e 's/.*--ignore-errors . //' -e 's#/org/backup.debian.org/staging/##' |
75         grep -v 'command="/usr/lib/da-backup/da-backup-ssh-wrap ' |
76         sort > "$rsync_targets"
77
78 wildcard_hosts=`cat "$AUTHKEYSFILE" | grep 'command="/usr/lib/da-backup/da-backup-ssh-wrap ' | sed -e 's/.*da-backup-ssh-wrap \([a-z0-9.]*\)".*/\1/' | tr '\n' '|' | sed 's/|$//'`
79 grep -h '^source' /etc/da-backup-manager/* |
80         sed -e 's/source=//' |
81         egrep -v "^($wildcard_hosts)/" |
82         sort > "$backup_manager_sources"
83 if ! diff "$rsync_targets" "$backup_manager_sources" > /dev/null; then
84         log warn
85         echo "authorized_keys vs. /etc/da-backup-manage mismatch:"
86         diff "$rsync_targets" "$backup_manager_sources"
87 fi
88
89 # 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
90 for i in `awk -F = '$1=="source"     {print $2}' /etc/da-backup-manager/* `; do
91         if ! [ -d "/org/backup.debian.org/staging/$i" ]; then
92                 log warn
93                 echo "Warning: source $i does not exist"
94         fi
95 done
96 for i in ` awk -F = '$1=="destination"{print $2}' /etc/da-backup-manager/* `; do
97         if ! [ -d "/org/backup.debian.org/backups/$i" ]; then
98                 log warn
99                 echo "Warning: destination $i does not exist (if it is new, it takes a day)"
100         fi
101 done
102
103 # check that everything that does exist has an entry in da-backup-manager
104 cd /org/backup.debian.org/staging
105 for i in */*; do
106         if ! [ -d "$i" ]; then
107                 log warn
108                 echo "WTF: staging/$i is not a dir"
109                 continue
110         fi
111         if ! grep "source=$i" /etc/da-backup-manager/* > /dev/null; then
112                 log warn
113                 echo "staging/$i is not listed anywhere in /etc/da-backup-manager"
114         fi
115 done
116 cd /org/backup.debian.org/backups
117 for i in */*; do
118         if ! [ -d "$i" ]; then
119                 log warn
120                 echo "WTF: backups/$i is not a dir"
121                 continue
122         fi
123         if ! grep "destination=$i" /etc/da-backup-manager/* > /dev/null; then
124                 log warn
125                 echo "backups/$i is not listed anywhere in /etc/da-backup-manager"
126         fi
127 done
128
129 if [ "$err" = 0 ]; then
130         echo "da-backup seems to be properly configured"
131 fi
132 exit $err