Doc/Monitoring/NagiosProbes: check_kernel_version.NIKHEF

File check_kernel_version.NIKHEF, 2.5 KB (added by /C=GR/O=HellasGrid/OU=auth.gr/CN=Christos Triantafyllidis, 13 years ago)

check_kernel_version by NIKHEF

Line 
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
11use strict;
12use Getopt::Long;
13
14my $verbose=0;
15my $help;
16my $hostname;
17
18my $TIMEOUT = 20;
19
20GetOptions("help" => \$help,
21           "hostname|H:s" => \$hostname, 
22           "timeout|t:i" => \$TIMEOUT,
23           "verbose|v+" => \$verbose );
24
25my %ERRORS=(OK=>0,
26            WARNING=>1,
27            CRITICAL=>2,
28            UNKNOWN=>3,
29            DEPENDENT=>4);
30
31
32my $KERNEL_VERSION_PATH = '/system/kernel/version';
33my $NCM_QUERY = "/usr/bin/ncm-query";
34( -x $NCM_QUERY ) or die "$NCM_QUERY: $!\n";
35
36my $result = $ERRORS{"UNKNOWN"};
37my $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
45alarm($TIMEOUT);
46
47##############################################################################
48# compare kernel versions
49#
50
51my $running_kv = &get_running_kernel_version;
52my $configured_kv = &get_configured_kernel_version;
53if ( ! defined $running_kv ) {
54    $message = "Failed to determine running kernel";
55}
56elsif ( ! defined $configured_kv ) {
57    $message = "Failed to determine configured kernel";
58}
59else {
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
70alarm(0);
71
72
73# Write output and return exit code;
74if ( $result == $ERRORS{"OK"} ) {
75    $message = "OK: kernel version = $running_kv";
76}
77
78print "$message\n";
79exit($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
86sub 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
100sub 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