check_release.pl
changeset 2 1be9848b69b6
parent 1 ccfe1fda4a0b
child 3 c442ffe27e48
equal deleted inserted replaced
1:ccfe1fda4a0b 2:1be9848b69b6
     1 #!/usr/bin/perl -w
     1 #!/usr/bin/perl -w
       
     2 
       
     3 #    Copyright (C) 2011  Christian Arnold
       
     4 #
       
     5 #    This program is free software: you can redistribute it and/or modify
       
     6 #    it under the terms of the GNU General Public License as published by
       
     7 #    the Free Software Foundation, either version 3 of the License, or
       
     8 #    (at your option) any later version.
       
     9 #
       
    10 #    This program is distributed in the hope that it will be useful,
       
    11 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    12 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    13 #    GNU General Public License for more details.
       
    14 #
       
    15 #    You should have received a copy of the GNU General Public License
       
    16 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
       
    17 #
       
    18 #    Christian Arnold <arnold@schlittermann.de>
     2 
    19 
     3 use strict;
    20 use strict;
     4 use File::Basename;
    21 use File::Basename;
     5 use Getopt::Long;
    22 use Getopt::Long;
     6 use LWP::Simple;
    23 use LWP::Simple;
     7 
    24 use Pod::Usage;
     8 use lib "/usr/lib/nagios/plugins";
    25 use if $ENV{DEBUG} => "Smart::Comments";
     9 use utils qw (%ERRORS &print_revision &support);
    26 
       
    27 my %ERRORS = (
       
    28     OK        => 0,
       
    29     WARNING   => 1,
       
    30     CRITICAL  => 2,
       
    31     UNKNOWN   => 3,
       
    32     DEPENDENT => 4
       
    33 );
       
    34 
       
    35 sub get_current_release($);
       
    36 sub get_stable_release($$);
       
    37 sub report($$);
       
    38 sub version($$);
    10 
    39 
    11 my $ME      = basename $0;
    40 my $ME      = basename $0;
    12 my $VERSION = "1.1";
    41 my $VERSION = "2.0";
    13 my $USAGE   = <<EOF;
    42 
    14 Usage: $ME [-u | --url]
    43 my %opt = (
    15        $ME [-s | --search]
    44     url    => "http://www.debian.org/releases/stable/index.html",
    16 	   $ME [-f | --file]
    45     search => "<p>Debian",
    17        $ME [-h | --help]
    46     file   => "/etc/debian_version"
    18        $ME [-V | --version]
    47 );
    19 EOF
       
    20 
       
    21 my $opt_url    = "http://www.debian.org/releases/stable/index.html";
       
    22 my $opt_search = "<p>Debian GNU/Linux";
       
    23 my $opt_file   = "/etc/debian_version";
       
    24 my ($found, $current_release, $current_majornr, $current_minornr,  $stable_release, $stable_majornr, $stable_minornr, @website);
       
    25 
       
    26 sub check_status();
       
    27 sub print_help();
       
    28 sub print_usage();
       
    29 
    48 
    30 MAIN: {
    49 MAIN: {
    31     Getopt::Long::Configure('bundling');
    50     Getopt::Long::Configure('bundling');
    32     GetOptions(
    51     GetOptions(
    33         "u|url"    => \$opt_url,
    52         "u|url=s"    => \$opt{url},
    34         "s|search" => \$opt_search,
    53         "s|search=s" => \$opt{search},
    35 		"f|file"   => \$opt_file,
    54         "f|file=s"   => \$opt{file},
    36         "h|help"   => sub { print_help(); exit $ERRORS{OK}; },
    55         "h|help" => sub { pod2usage(-verbose => 1, -exitval => $ERRORS{OK}) },
    37         "V|version" =>
    56         "m|man" => sub { pod2usage(-verbose => 2, -exitval => $ERRORS{OK}) },
    38           sub { print_revision( $ME, $VERSION ); exit $ERRORS{OK}; },
    57         "V|version" => sub { version($ME, $VERSION); exit $ERRORS{OK}; }
    39     );
    58     ) or pod2usage(-verbose => 1, -exitval => $ERRORS{CRITICAL});
    40 
    59 
    41 	# get current release number
    60     ### %opt
    42 	unless ( -r $opt_file ) {
    61 
    43 		print "RELEASE CRITICAL: $opt_file not exists or not read permission is granted\n";
    62     my $current_release = get_current_release($opt{file});
    44 		exit $ERRORS{"CRITICAL"};
    63     my $stable_release = get_stable_release($opt{url}, $opt{search});
    45 	}
    64     report($current_release, $stable_release);
    46 
    65 }
    47 	open(CR, "<$opt_file");
    66 
    48 	while(<CR>) {
    67 sub get_current_release($) {
    49 		chomp;
    68     my $file            = shift;
    50 		$current_release = $_;
    69     my $current_release = undef;
    51 		$current_release =~ /^(\d)+.*(\d)+$/;
    70 
    52 		$current_majornr = $1;
    71     unless (-r $file) {
    53 		$current_minornr = $2;
    72         print
    54 	}
    73 "RELEASE CRITICAL: $file not exists or not read permission is granted\n";
    55 	close(CR);
    74         exit $ERRORS{CRITICAL};
    56 
    75     }
    57 	# download the website
    76 
    58 	push @website, split("\n", get($opt_url));
    77     open(CR, "<$file");
    59 
    78     while (<CR>) {
    60 	foreach (@website) {
    79         chomp;
    61 		unless ($found) {
    80         $current_release = $_;
    62 			/$opt_search/ or next;
    81     }
    63 			$found = $_;
    82     close(CR);
    64 			next;
    83 
    65 		}
    84     ### $current_release
    66 	}
    85 
    67 
    86     return $current_release;
    68 	# get stable release number
    87 }
    69 	if ($found =~ /$opt_search\s+([\d\.]+).*/) {
    88 
    70 		$stable_release = $1;
    89 sub get_stable_release($$) {
    71 		$stable_release =~ /^(\d)+.*(\d)+$/;
    90     my ($url,   $search)         = @_;
    72 		$stable_majornr = $1;
    91     my ($found, $stable_release) = undef;
    73 		$stable_minornr = $2;
    92     my @website = ();
    74 	}
    93 
    75 
    94     push @website, split("\n", get($url));
    76 	check_status();
    95 
    77 
    96     foreach (@website) {
    78 }
    97         unless ($found) {
    79 
    98             /$search/ or next;
    80 sub check_status() {
    99             $found = $_;
    81 	if ($stable_release eq $current_release) {
   100             next;
    82 		print "RELEASE OK: current release number $current_release\n";
   101         }
    83 		exit $ERRORS{"OK"};
   102     }
    84 	# Debian-Project is a little bit slowly to update his websites
   103 
    85      } elsif (++$stable_minornr eq $current_minornr) {
   104     if ($found =~ /$opt{search}\s+([\d\.]+).*/) {
       
   105         $stable_release = $1;
       
   106     }
       
   107 
       
   108     ### $stable_release
       
   109 
       
   110     return $stable_release;
       
   111 }
       
   112 
       
   113 sub report($$) {
       
   114     my ($current_release, $stable_release) = @_;
       
   115 
       
   116     if ($current_release eq $stable_release) {
    86         print "RELEASE OK: current release number $current_release\n";
   117         print "RELEASE OK: current release number $current_release\n";
    87         exit $ERRORS{"OK"};
   118         exit $ERRORS{"OK"};
    88 	} elsif ($current_majornr < $stable_majornr) {
   119     }
    89 		print "RELEASE CRITICAL: current release number $current_release / stable release number $stable_release\n";
   120     else {
    90 		exit $ERRORS{"CRITICAL"};
   121         print
    91 	} else {
   122 "RELEASE WARNING: current release number $current_release / stable release number $stable_release\n";
    92 		print "RELEASE WARNING: current release number $current_release / stable release number $stable_release\n";
   123         exit $ERRORS{"WARNING"};
    93 		exit $ERRORS{"WARNING"};
   124     }
    94 	}
   125 }
    95 }
   126 
    96 
   127 sub version($$) {
    97 
   128     my ($progname, $version) = @_;
    98 sub print_usage() { print $USAGE }
   129 
    99 
   130     print <<_VERSION;
   100 sub print_help() {
   131 $progname version $version
   101     print_revision( $ME, $VERSION );
   132 Copyright (C) 2011 by Christian Arnold and Schlittermann internet & unix support.
   102     print <<EOF;
   133 
   103 Copyright (c) 2010 Christian Arnold
   134 $ME comes with ABSOLUTELY NO WARRANTY. This is free software,
       
   135 and you are welcome to redistribute it under certain conditions.
       
   136 See the GNU General Public Licence for details.
       
   137 _VERSION
       
   138 }
       
   139 
       
   140 __END__
       
   141 
       
   142 =head1 NAME
       
   143 
       
   144 check_release - nagios plugin to checks the current debian release number
       
   145 
       
   146 =head1 SYNOPSIS
       
   147 
       
   148 check_release [-u|--url url]
       
   149               [-s|--search string]
       
   150               [-f|--file path]
       
   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: http://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')
       
   166 
       
   167 =item B<-f>|B<--file> I<string>
       
   168 
       
   169 file with current release informations (default: /etc/debian_version)
       
   170 
       
   171 =item B<-h>|B<--help>
       
   172 
       
   173 Print detailed help screen.
       
   174 
       
   175 =item B<-m>|B<--man>
       
   176 
       
   177 Print manual page.
       
   178 
       
   179 =item B<-V>|B<--version>
       
   180 
       
   181 Print version information.
       
   182 
       
   183 =back
       
   184 
       
   185 =head1 DESCRIPTION
   104 
   186 
   105 This plugin checks the current debian release number.
   187 This plugin checks the current debian release number.
   106 
   188 
   107 $USAGE
   189 =head1 VERSION
   108     -u, --url
   190 
   109         URL where to search for the search string (default: http://www.debian.org/releases/stable/index.html)
   191 This man page is current for version 2.0 of check_release.
   110     -s, --search
   192 
   111         search string on the url (default: '<p>Debian GNU/Linux')
   193 =head1 AUTHOR
   112     -f, --file
   194 
   113         file with current release informations (default: /etc/debian_version)
   195 Written by Christian Arnold L<arnold@schlittermann.de>
   114     -h, --help
   196 
   115         print detailed help screen
   197 =head1 COPYRIGHT
   116     -V, --version
   198 
   117         print version information
   199 Copyright (C) 2011 by Christian Arnold and Schlittermann internet & unix support.
   118 EOF
   200 This is free software, and you are welcome to redistribute it under certain conditions.
   119     support();
   201 See the GNU General Public Licence for details.
   120 }
   202 
       
   203 =cut