7d2cb3ab4c8a84bfe8460e636dd87b9b8cfcbb7b
[mirror/dsa-nagios.git] / dsa-nagios-nrpe-config / 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         elif [ "$off" -ne "-1" ]; then
63                 (dd ibs="$off" skip=1 count=0 && dd bs=512k) < "$image"  2>/dev/null | zcat 2>/dev/null
64         fi
65
66         echo "ERROR: Unable to extract kernel image." 2>&1
67         exit 1
68 }
69
70 on_disk="/boot/vmlinuz-`uname -r`"
71
72 if ! [ -e "$on_disk" ] ; then
73         echo "WARNING: $on_disk does not exist - I have no idea which kernel I am running"
74         exit $WARN
75 fi
76
77 on_disk_version="`get_image $on_disk | strings | grep 'Linux version' | head -n1`"
78 if [ -z "$on_disk_version" ] ; then
79         echo "UNKNOWN: Failed to get a version string from image $on_disk"
80         exit $UNKNOWN
81 fi
82
83 running_version="`cat /proc/version`"
84 if [ -z "$running_version" ] ; then
85         echo "UNKNOWN: Failed to get a version string from running system"
86         exit $UNKNOWN
87 fi
88
89 if [ "$running_version" != "$on_disk_version" ]; then
90         echo "WARNING: Running kernel does not match on-disk kernel image: [$running_version != $on_disk_version]"
91         exit $WARNING
92 else
93         echo "OK: Running kernel matches on disk image: [$running_version]"
94         exit $OK
95 fi