1 | #!/usr/bin/perl -w |
---|
2 | |
---|
3 | # |
---|
4 | # This script compares the version of the currently running kernel |
---|
5 | # with the one defined in the Quattor profile for the current host |
---|
6 | # If they are different, a warning is returned |
---|
7 | # |
---|
8 | # Later: also compare GRUB config; if the kernel version in the Quattor |
---|
9 | # |
---|
10 | |
---|
11 | use strict; |
---|
12 | use Getopt::Long; |
---|
13 | |
---|
14 | my $verbose=0; |
---|
15 | my $help; |
---|
16 | my $hostname; |
---|
17 | |
---|
18 | my $TIMEOUT = 20; |
---|
19 | |
---|
20 | GetOptions("help" => \$help, |
---|
21 | "hostname|H:s" => \$hostname, |
---|
22 | "timeout|t:i" => \$TIMEOUT, |
---|
23 | "verbose|v+" => \$verbose ); |
---|
24 | |
---|
25 | my %ERRORS=(OK=>0, |
---|
26 | WARNING=>1, |
---|
27 | CRITICAL=>2, |
---|
28 | UNKNOWN=>3, |
---|
29 | DEPENDENT=>4); |
---|
30 | |
---|
31 | |
---|
32 | my $KERNEL_VERSION_PATH = '/system/kernel/version'; |
---|
33 | my $NCM_QUERY = "/usr/bin/ncm-query"; |
---|
34 | ( -x $NCM_QUERY ) or die "$NCM_QUERY: $!\n"; |
---|
35 | |
---|
36 | my $result = $ERRORS{"UNKNOWN"}; |
---|
37 | my $message = ""; |
---|
38 | |
---|
39 | # Just in case of problems, let's not hang Nagios |
---|
40 | $SIG{'ALRM'} = sub { |
---|
41 | print ("ERROR: No response from probe (alarm timeout)\n"); |
---|
42 | exit $ERRORS{"UNKNOWN"}; |
---|
43 | }; |
---|
44 | |
---|
45 | alarm($TIMEOUT); |
---|
46 | |
---|
47 | ############################################################################## |
---|
48 | # compare kernel versions |
---|
49 | # |
---|
50 | |
---|
51 | my $running_kv = &get_running_kernel_version; |
---|
52 | my $configured_kv = &get_configured_kernel_version; |
---|
53 | if ( ! defined $running_kv ) { |
---|
54 | $message = "Failed to determine running kernel"; |
---|
55 | } |
---|
56 | elsif ( ! defined $configured_kv ) { |
---|
57 | $message = "Failed to determine configured kernel"; |
---|
58 | } |
---|
59 | else { |
---|
60 | # both versions are defined; compare them |
---|
61 | if ( $running_kv eq $configured_kv ) { |
---|
62 | $result = $ERRORS{"OK"}; |
---|
63 | } |
---|
64 | else { |
---|
65 | $result = $ERRORS{"WARNING"}; |
---|
66 | $message = "Running kernel $running_kv differs from configured version $configured_kv"; |
---|
67 | } |
---|
68 | } |
---|
69 | |
---|
70 | alarm(0); |
---|
71 | |
---|
72 | |
---|
73 | # Write output and return exit code; |
---|
74 | if ( $result == $ERRORS{"OK"} ) { |
---|
75 | $message = "OK: kernel version = $running_kv"; |
---|
76 | } |
---|
77 | |
---|
78 | print "$message\n"; |
---|
79 | exit($result); |
---|
80 | |
---|
81 | |
---|
82 | |
---|
83 | |
---|
84 | # determine the version of the kernel configured in the Quattor profile |
---|
85 | # return: string with kernel version or undef |
---|
86 | sub get_configured_kernel_version { |
---|
87 | my $cmd = 'sudo ' . $NCM_QUERY . ' ' . $KERNEL_VERSION_PATH; |
---|
88 | my @output = `$cmd`; |
---|
89 | if ( $? == 0 ) { |
---|
90 | if ( $output[@output-1] =~ /^\$.*\'(.+)\'$/ ) { |
---|
91 | return $1; |
---|
92 | } |
---|
93 | } |
---|
94 | return undef; |
---|
95 | } |
---|
96 | |
---|
97 | |
---|
98 | # determine the version of the running kernel |
---|
99 | # return: string with kernel version or undef |
---|
100 | sub get_running_kernel_version { |
---|
101 | my $cmd = 'uname -r'; |
---|
102 | my $version = `$cmd`; |
---|
103 | if ( $? == 0 ) { |
---|
104 | chomp $version; |
---|
105 | return $version; |
---|
106 | } |
---|
107 | else { |
---|
108 | return undef; |
---|
109 | } |
---|
110 | } |
---|
111 | |
---|