check_release.pl
changeset 27 6a562a51e1d8
parent 26 a03a6dab1176
equal deleted inserted replaced
26:a03a6dab1176 27:6a562a51e1d8
     1 #!/usr/bin/perl -w
       
     2 
       
     3 #    Copyright (C) 2011  Christian Arnold
       
     4 #    Copyright (C) 2015  Heiko Schlittermann
       
     5 #
       
     6 #    This program is free software: you can redistribute it and/or modify
       
     7 #    it under the terms of the GNU General Public License as published by
       
     8 #    the Free Software Foundation, either version 3 of the License, or
       
     9 #    (at your option) any later version.
       
    10 #
       
    11 #    This program is distributed in the hope that it will be useful,
       
    12 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    13 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    14 #    GNU General Public License for more details.
       
    15 #
       
    16 #    You should have received a copy of the GNU General Public License
       
    17 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
       
    18 #
       
    19 #    Christian Arnold <arnold@schlittermann.de>
       
    20 
       
    21 use strict;
       
    22 use File::Basename;
       
    23 use Getopt::Long;
       
    24 use LWP::Simple;
       
    25 use Pod::Usage;
       
    26 
       
    27 my %ERRORS = (
       
    28     OK        => 0,
       
    29     WARNING   => 1,
       
    30     CRITICAL  => 2,
       
    31     UNKNOWN   => 3,
       
    32     DEPENDENT => 4
       
    33 );
       
    34 
       
    35 sub get_local_release;
       
    36 sub get_stable_release($$);
       
    37 sub report($$);
       
    38 sub version($$);
       
    39 
       
    40 my $ME      = basename $0;
       
    41 my $VERSION = "2.9";
       
    42 
       
    43 my %opt = (
       
    44     url    => "https://www.debian.org/releases/stable/index.html",
       
    45     search => qr/<p>Debian\b.*?([\d.]+)/,
       
    46     file   => "/etc/debian_version",
       
    47     ok     => 0
       
    48 );
       
    49 
       
    50 MAIN: {
       
    51     Getopt::Long::Configure('bundling');
       
    52     GetOptions(
       
    53         "u|url=s"    => \$opt{url},
       
    54         "s|search=s" => \$opt{search},
       
    55         "f|file=s"   => \$opt{file},
       
    56 		"o|ok"       => \$opt{ok},
       
    57         "h|help" => sub { pod2usage(-verbose => 1, -exitval => $ERRORS{OK}) },
       
    58         "m|man" => sub { pod2usage(-verbose => 2, -exitval => $ERRORS{OK}) },
       
    59         "V|version" => sub { version($ME, $VERSION); exit $ERRORS{OK}; }
       
    60     ) or pod2usage(-verbose => 1, -exitval => $ERRORS{CRITICAL});
       
    61 
       
    62     my ($local_release, $stable_release) = eval {
       
    63 	get_local_release($opt{file}),
       
    64 	get_stable_release($opt{url}, $opt{search});
       
    65     };
       
    66     if ($@) {
       
    67 	print "RELEASE CRITICAL: $@\n";
       
    68 	exit $ERRORS{CRITICAL};
       
    69     }
       
    70     report($local_release, $stable_release);
       
    71 }
       
    72 
       
    73 sub get_local_release {
       
    74 
       
    75     my $local_release = do {
       
    76 	local @ARGV = grep { -r } @_
       
    77 	    or die "@_: $!\n";
       
    78 	<>;
       
    79     };
       
    80 
       
    81     chomp $local_release;
       
    82     return $local_release;
       
    83 }
       
    84 
       
    85 sub get_stable_release($$) {
       
    86     my ($url,   $search)         = @_;
       
    87     my $website;
       
    88 
       
    89     $website = get($url)
       
    90 	or die "Failed to get $url\n";
       
    91 
       
    92     $website =~ /$opt{search}/s
       
    93 	or die  "an't parse $url\n";
       
    94 
       
    95     return $1;
       
    96 }
       
    97 
       
    98 sub compare_versions {
       
    99     my ($left, $right) = @_;
       
   100     my @left = split '.', $left;
       
   101     my @right = split '.', $right;
       
   102 
       
   103 }
       
   104 
       
   105 sub report($$) {
       
   106     my ($local_release, $stable_release) = @_;
       
   107     my ($local_major, $stable_major) = map { int } $local_release, $stable_release;
       
   108 
       
   109 
       
   110     if ($opt{ok} or $local_release eq $stable_release or $local_major > $stable_major) {
       
   111         print "RELEASE OK: $local_release/$stable_release\n";
       
   112         exit $ERRORS{OK};
       
   113     }
       
   114 
       
   115     if ($local_major - $stable_major < -1) {
       
   116         print "RELEASE CRITICAL: $local_release / $stable_release\n";
       
   117         exit $ERRORS{CRITICAL};
       
   118     }
       
   119 
       
   120     print "RELEASE WARNING: $local_release / $stable_release\n";
       
   121     exit $ERRORS{"WARNING"};
       
   122 
       
   123 }
       
   124 
       
   125 sub version($$) {
       
   126     my ($progname, $version) = @_;
       
   127 
       
   128     print <<_VERSION;
       
   129 $progname version $version
       
   130 Copyright (C) 2011-2013 by Christian Arnold and Schlittermann internet & unix support.
       
   131 Copyright (C) 2015 by Schlittermann internet & unix support.
       
   132 
       
   133 $ME comes with ABSOLUTELY NO WARRANTY. This is free software,
       
   134 and you are welcome to redistribute it under certain conditions.
       
   135 See the GNU General Public Licence for details.
       
   136 _VERSION
       
   137 }
       
   138 
       
   139 __END__
       
   140 
       
   141 =head1 NAME
       
   142 
       
   143 check_release - nagios plugin to checks the current debian release number
       
   144 
       
   145 =head1 SYNOPSIS
       
   146 
       
   147 check_release [-u|--url url]
       
   148               [-s|--search string]
       
   149               [-f|--file path]
       
   150               [-o|--ok]
       
   151               [-h|--help]
       
   152               [-m|--man]
       
   153               [-V|--version]
       
   154 
       
   155 =head1 OPTIONS
       
   156 
       
   157 =over
       
   158 
       
   159 =item B<-u>|B<--url> I<url>
       
   160 
       
   161 URL where to search for the search string (default: https://www.debian.org/releases/stable/index.html)
       
   162 
       
   163 =item B<-s>|B<--search> I<string>
       
   164 
       
   165 search string on the url (default: /<p>Debian\b.*?([\d.]+)/)
       
   166 
       
   167 =item B<-f>|B<--file> I<string>
       
   168 
       
   169 file with current release informations (default: /etc/debian_version)
       
   170 
       
   171 =item B<-o>|B<--ok>
       
   172 
       
   173 Exit status always ok.
       
   174 
       
   175 =item B<-h>|B<--help>
       
   176 
       
   177 Print detailed help screen.
       
   178 
       
   179 =item B<-m>|B<--man>
       
   180 
       
   181 Print manual page.
       
   182 
       
   183 =item B<-V>|B<--version>
       
   184 
       
   185 Print version information.
       
   186 
       
   187 =back
       
   188 
       
   189 =head1 DESCRIPTION
       
   190 
       
   191 This plugin checks the current debian release number.
       
   192 
       
   193 =head1 VERSION
       
   194 
       
   195 This man page is current for version 2.3 of check_release.
       
   196 
       
   197 =head1 AUTHOR
       
   198 
       
   199 Written by Christian Arnold L<arnold@schlittermann.de>
       
   200 
       
   201 =head1 COPYRIGHT
       
   202 
       
   203 Copyright (C) 2011 by Christian Arnold and Schlittermann internet & unix support.
       
   204 This is free software, and you are welcome to redistribute it under certain conditions.
       
   205 See the GNU General Public Licence for details.
       
   206 
       
   207 =cut