Move files into specific directories in source
[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 #
8 # Permission is hereby granted, free of charge, to any person obtaining
9 # a copy of this software and associated documentation files (the
10 # "Software"), to deal in the Software without restriction, including
11 # without limitation the rights to use, copy, modify, merge, publish,
12 # distribute, sublicense, and/or sell copies of the Software, and to
13 # permit persons to whom the Software is furnished to do so, subject to
14 # the following conditions:
15 #
16 # The above copyright notice and this permission notice shall be
17 # included in all copies or substantial portions of the Software.
18 #
19 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
23 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26
27 OK=0;
28 WARNING=1;
29 CRITICAL=2;
30 UNKNOWN=3;
31
32 get_offset() {
33         local file needle
34
35         file="$1"
36         needle="$2"
37         perl -e '
38                 undef $/;
39                 $i = index(<>, "'"$needle"'");
40                 if ($i < 0) {
41                         exit 1;
42                 };
43                 print $i,"\n"' < "$file"
44 }
45
46 get_image() {
47         local image GZHDR1 GZHDR2 off
48
49         image="$1"
50
51         GZHDR1="\x1f\x8b\x08\x00"
52         GZHDR2="\x1f\x8b\x08\x08"
53
54         off=`get_offset "$image" $GZHDR1`
55         [ "$?" != "0" ] && off="-1"
56         if [ "$off" -eq "-1" ]; then
57                 off=`get_offset "$image" $GZHDR2`
58                 [ "$?" != "0" ] && off="-1"
59         fi
60         if [ "$off" -eq "0" ]; then
61                 zcat < "$image"
62                 return
63         elif [ "$off" -ne "-1" ]; then
64                 (dd ibs="$off" skip=1 count=0 && dd bs=512k) < "$image"  2>/dev/null | zcat 2>/dev/null
65                 return
66         fi
67
68         echo "ERROR: Unable to extract kernel image." 2>&1
69         exit 1
70 }
71
72 searched=""
73 for on_disk in \
74         "/boot/vmlinuz-`uname -r`"\
75         "/boot/vmlinux-`uname -r`"; do
76
77         if [ -e "$on_disk" ]; then
78                 on_disk_version="`get_image "$on_disk" | strings | grep 'Linux version' | head -n1`"
79                 [ -z "$on_disk_version" ] || break
80                 on_disk_version="`cat "$on_disk" | strings | grep 'Linux version' | head -n1`"
81                 [ -z "$on_disk_version" ] || break
82
83                 echo "UNKNOWN: Failed to get a version string from image $on_disk"
84                 exit $UNKNOWN
85         fi
86         searched="$searched $on_disk"
87 done
88
89 if ! [ -e "$on_disk" ]; then
90         echo "WARNING: Did not find a kernel image (checked$searched) - I have no idea which kernel I am running"
91         exit $WARNING
92 fi
93
94
95 running_version="`cat /proc/version`"
96 if [ -z "$running_version" ] ; then
97         echo "UNKNOWN: Failed to get a version string from running system"
98         exit $UNKNOWN
99 fi
100
101 if [ "$running_version" != "$on_disk_version" ]; then
102         echo "WARNING: Running kernel does not match on-disk kernel image: [$running_version != $on_disk_version]"
103         exit $WARNING
104 else
105         echo "OK: Running kernel matches on disk image: [$running_version]"
106         exit $OK
107 fi