Don't complain when installed version is higher than available version.
[mirror/dsa-nagios.git] / dsa-nagios-checks / checks / dsa-check-running-kernel
1 #!/bin/bash
2
3 # Check if the running kernel has the same version string as the on-disk
4 # kernel image.
5
6 # Copyright 2008 Peter Palfrader
7 # Copyright 2009 Stephen Gran
8 #
9 # Permission is hereby granted, free of charge, to any person obtaining
10 # a copy of this software and associated documentation files (the
11 # "Software"), to deal in the Software without restriction, including
12 # without limitation the rights to use, copy, modify, merge, publish,
13 # distribute, sublicense, and/or sell copies of the Software, and to
14 # permit persons to whom the Software is furnished to do so, subject to
15 # the following conditions:
16 #
17 # The above copyright notice and this permission notice shall be
18 # included in all copies or substantial portions of the Software.
19 #
20 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27
28 OK=0;
29 WARNING=1;
30 CRITICAL=2;
31 UNKNOWN=3;
32
33 get_offset() {
34         local file needle
35
36         file="$1"
37         needle="$2"
38         perl -e '
39                 undef $/;
40                 $i = index(<>, "'"$needle"'");
41                 if ($i < 0) {
42                         exit 1;
43                 };
44                 print $i,"\n"' < "$file"
45 }
46
47 get_avail() {
48         # This is wrong, but leaves room for when we have to care for machines running
49         # myfirstunix-image-0.1-dsa-arm
50         local prefix=linux
51
52         local kervers=$(uname -r)
53
54         local metavers=''
55
56         # DSA uses kernel versions of the form 2.6.29.3-dsa-dl380-oldxeon, where
57         # Debian uses versions of the form 2.6.29-2-amd64
58         if [ "${kervers//dsa}" != "$kervers" ]; then
59                 metavers=$(echo $kervers | sed -r -e 's/^2\.(4|6)\.[0-9]+([\.0-9]+?)-(.*)/2.\1-\3/')
60         else
61                 metavers=$(echo $kervers | sed -r -e 's/^2\.(4|6)\.[0-9]+-[A-Za-z0-9\.]+-(.*)/2.\1-\2/')
62         fi
63
64         # Attempt to track back to a metapackage failed.  bail
65         if [ "$metavers" = "$kervers" ]; then
66                 return 2
67         fi
68
69         # We're just going to give up if we can't find a matching metapackage
70         # I tried being strict once, and it just caused a lot of headaches.  We'll see how
71         # being lax does for us
72
73         local output=$(apt-cache policy ${prefix}-image-${metavers} 2>/dev/null)
74         local metaavailvers=$(echo "$output" | grep '^  Candidate:' | awk '{print $2}')
75         local metainstavers=$(echo "$output" | grep '^  Installed:' | awk '{print $2}')
76
77         if [ -z "$metaavailvers" ] || [ "$metaavailvers" = '(none)' ]; then
78                 return 2
79         fi
80         if [ -z "$metainstavers" ] || [ "$metainstavers" = '(none)' ]; then
81                 return 2
82         fi
83
84         local imagename=0
85         for vers in $(apt-cache --no-all-versions depends ${prefix}-image-${metavers} | grep Depends | awk '{print $2}' | sort -u); do
86                 if dpkg --compare-versions $vers gt $imagename; then
87                         imagename=$vers
88                 fi
89         done
90
91         if [ -z "$imagename" ] || [ "$imagename" = 0 ]; then
92                 return 2
93         fi
94
95         if [ "$imagename" != "${prefix}-image-${kervers}" ]; then
96                 if dpkg --compare-versions "$imagename" lt "${prefix}-image-${kervers}"; then
97                         return 2
98                 fi
99                 echo "$imagename" != "${prefix}-image-${kervers}"
100                 return 1
101         fi
102
103         local availvrs=$(apt-cache policy ${imagename} 2>/dev/null | grep '^  Candidate' | awk '{print $2}')
104         local kernelversion=$(apt-cache policy ${prefix}-image-${kervers} 2>/dev/null | grep '^  Installed:' | awk '{print $2}')
105
106         if [ "$availvrs" = "$kernelversion" ]; then
107                 return 0
108         fi
109
110         echo "$kernelversion != $availvrs"
111         return 1
112 }
113
114 get_image() {
115         local image GZHDR1 GZHDR2 off
116
117         image="$1"
118
119         GZHDR1="\x1f\x8b\x08\x00"
120         GZHDR2="\x1f\x8b\x08\x08"
121
122         off=`get_offset "$image" $GZHDR1`
123         [ "$?" != "0" ] && off="-1"
124         if [ "$off" -eq "-1" ]; then
125                 off=`get_offset "$image" $GZHDR2`
126                 [ "$?" != "0" ] && off="-1"
127         fi
128         if [ "$off" -eq "0" ]; then
129                 zcat < "$image"
130                 return
131         elif [ "$off" -ne "-1" ]; then
132                 (dd ibs="$off" skip=1 count=0 && dd bs=512k) < "$image"  2>/dev/null | zcat 2>/dev/null
133                 return
134         fi
135
136         echo "ERROR: Unable to extract kernel image." 2>&1
137         exit 1
138 }
139
140 searched=""
141 for on_disk in \
142         "/boot/vmlinuz-`uname -r`"\
143         "/boot/vmlinux-`uname -r`"; do
144
145         if [ -e "$on_disk" ]; then
146                 on_disk_version="`get_image "$on_disk" | strings | grep 'Linux version' | head -n1`"
147                 [ -z "$on_disk_version" ] || break
148                 on_disk_version="`cat "$on_disk" | strings | grep 'Linux version' | head -n1`"
149                 [ -z "$on_disk_version" ] || break
150
151                 echo "UNKNOWN: Failed to get a version string from image $on_disk"
152                 exit $UNKNOWN
153         fi
154         searched="$searched $on_disk"
155 done
156
157 if ! [ -e "$on_disk" ]; then
158         echo "WARNING: Did not find a kernel image (checked$searched) - I have no idea which kernel I am running"
159         exit $WARNING
160 fi
161
162
163 running_version="`cat /proc/version`"
164 if [ -z "$running_version" ] ; then
165         echo "UNKNOWN: Failed to get a version string from running system"
166         exit $UNKNOWN
167 fi
168
169 if [ "$running_version" != "$on_disk_version" ]; then
170         echo "WARNING: Running kernel does not match on-disk kernel image: [$running_version != $on_disk_version]"
171         exit $WARNING
172 fi
173
174 ret="$(get_avail)"
175 if [ $? = 1 ]; then
176         echo "WARNING: Kernel needs upgrade [$ret]"
177         exit $WARNING
178 fi
179
180 echo "OK: Running kernel matches on disk image: [$running_version]"
181 exit $OK