[project @ peter@palfrader.org-20080505174137-50lwpqcqpvj61871]
[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 on_disk_compressed="/boot/vmlinuz-`uname -r`"
70 on_disk_uncompressed="/boot/vmlinux-`uname -r`"
71
72 if [ -e "$on_disk_compressed" ]; then
73         on_disk="$on_disk_compressed"
74         on_disk_version="`get_image "$on_disk" | strings | grep 'Linux version' | head -n1`"
75 elif [ -e "$on_disk_uncompressed" ]; then
76         on_disk="$on_disk_uncompressed"
77         on_disk_version="`cat "$on_disk" | strings | grep 'Linux version' | head -n1`"
78 else
79         echo "WARNING: Neither $on_disk_uncompressed nor $on_disk_compressed do exist - I have no idea which kernel I am running"
80         exit $WARNING
81 fi
82
83 if [ -z "$on_disk_version" ] ; then
84         echo "UNKNOWN: Failed to get a version string from image $on_disk"
85         exit $UNKNOWN
86 fi
87
88 running_version="`cat /proc/version`"
89 if [ -z "$running_version" ] ; then
90         echo "UNKNOWN: Failed to get a version string from running system"
91         exit $UNKNOWN
92 fi
93
94 if [ "$running_version" != "$on_disk_version" ]; then
95         echo "WARNING: Running kernel does not match on-disk kernel image: [$running_version != $on_disk_version]"
96         exit $WARNING
97 else
98         echo "OK: Running kernel matches on disk image: [$running_version]"
99         exit $OK
100 fi