diff -r ccfe1fda4a0b -r 1be9848b69b6 check_release.pl --- a/check_release.pl Mon Sep 06 12:32:10 2010 +0200 +++ b/check_release.pl Sun Feb 06 13:31:48 2011 +0100 @@ -1,120 +1,203 @@ #!/usr/bin/perl -w +# Copyright (C) 2011 Christian Arnold +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +# +# Christian Arnold + use strict; use File::Basename; use Getopt::Long; use LWP::Simple; +use Pod::Usage; +use if $ENV{DEBUG} => "Smart::Comments"; -use lib "/usr/lib/nagios/plugins"; -use utils qw (%ERRORS &print_revision &support); +my %ERRORS = ( + OK => 0, + WARNING => 1, + CRITICAL => 2, + UNKNOWN => 3, + DEPENDENT => 4 +); + +sub get_current_release($); +sub get_stable_release($$); +sub report($$); +sub version($$); my $ME = basename $0; -my $VERSION = "1.1"; -my $USAGE = < "http://www.debian.org/releases/stable/index.html", + search => "

Debian", + file => "/etc/debian_version" +); MAIN: { Getopt::Long::Configure('bundling'); GetOptions( - "u|url" => \$opt_url, - "s|search" => \$opt_search, - "f|file" => \$opt_file, - "h|help" => sub { print_help(); exit $ERRORS{OK}; }, - "V|version" => - sub { print_revision( $ME, $VERSION ); exit $ERRORS{OK}; }, - ); + "u|url=s" => \$opt{url}, + "s|search=s" => \$opt{search}, + "f|file=s" => \$opt{file}, + "h|help" => sub { pod2usage(-verbose => 1, -exitval => $ERRORS{OK}) }, + "m|man" => sub { pod2usage(-verbose => 2, -exitval => $ERRORS{OK}) }, + "V|version" => sub { version($ME, $VERSION); exit $ERRORS{OK}; } + ) or pod2usage(-verbose => 1, -exitval => $ERRORS{CRITICAL}); + + ### %opt - # get current release number - unless ( -r $opt_file ) { - print "RELEASE CRITICAL: $opt_file not exists or not read permission is granted\n"; - exit $ERRORS{"CRITICAL"}; - } + my $current_release = get_current_release($opt{file}); + my $stable_release = get_stable_release($opt{url}, $opt{search}); + report($current_release, $stable_release); +} + +sub get_current_release($) { + my $file = shift; + my $current_release = undef; + + unless (-r $file) { + print +"RELEASE CRITICAL: $file not exists or not read permission is granted\n"; + exit $ERRORS{CRITICAL}; + } - open(CR, "<$opt_file"); - while() { - chomp; - $current_release = $_; - $current_release =~ /^(\d)+.*(\d)+$/; - $current_majornr = $1; - $current_minornr = $2; - } - close(CR); + open(CR, "<$file"); + while () { + chomp; + $current_release = $_; + } + close(CR); - # download the website - push @website, split("\n", get($opt_url)); + ### $current_release + + return $current_release; +} + +sub get_stable_release($$) { + my ($url, $search) = @_; + my ($found, $stable_release) = undef; + my @website = (); - foreach (@website) { - unless ($found) { - /$opt_search/ or next; - $found = $_; - next; - } - } + push @website, split("\n", get($url)); - # get stable release number - if ($found =~ /$opt_search\s+([\d\.]+).*/) { - $stable_release = $1; - $stable_release =~ /^(\d)+.*(\d)+$/; - $stable_majornr = $1; - $stable_minornr = $2; - } + foreach (@website) { + unless ($found) { + /$search/ or next; + $found = $_; + next; + } + } - check_status(); + if ($found =~ /$opt{search}\s+([\d\.]+).*/) { + $stable_release = $1; + } + ### $stable_release + + return $stable_release; } -sub check_status() { - if ($stable_release eq $current_release) { - print "RELEASE OK: current release number $current_release\n"; - exit $ERRORS{"OK"}; - # Debian-Project is a little bit slowly to update his websites - } elsif (++$stable_minornr eq $current_minornr) { +sub report($$) { + my ($current_release, $stable_release) = @_; + + if ($current_release eq $stable_release) { print "RELEASE OK: current release number $current_release\n"; exit $ERRORS{"OK"}; - } elsif ($current_majornr < $stable_majornr) { - print "RELEASE CRITICAL: current release number $current_release / stable release number $stable_release\n"; - exit $ERRORS{"CRITICAL"}; - } else { - print "RELEASE WARNING: current release number $current_release / stable release number $stable_release\n"; - exit $ERRORS{"WARNING"}; - } + } + else { + print +"RELEASE WARNING: current release number $current_release / stable release number $stable_release\n"; + exit $ERRORS{"WARNING"}; + } +} + +sub version($$) { + my ($progname, $version) = @_; + + print <<_VERSION; +$progname version $version +Copyright (C) 2011 by Christian Arnold and Schlittermann internet & unix support. + +$ME comes with ABSOLUTELY NO WARRANTY. This is free software, +and you are welcome to redistribute it under certain conditions. +See the GNU General Public Licence for details. +_VERSION } +__END__ -sub print_usage() { print $USAGE } +=head1 NAME + +check_release - nagios plugin to checks the current debian release number + +=head1 SYNOPSIS + +check_release [-u|--url url] + [-s|--search string] + [-f|--file path] + [-h|--help] + [-m|--man] + [-V|--version] + +=head1 OPTIONS + +=over + +=item B<-u>|B<--url> I + +URL where to search for the search string (default: http://www.debian.org/releases/stable/index.html) -sub print_help() { - print_revision( $ME, $VERSION ); - print <|B<--search> I + +search string on the url (default: '

Debian') + +=item B<-f>|B<--file> I + +file with current release informations (default: /etc/debian_version) + +=item B<-h>|B<--help> + +Print detailed help screen. + +=item B<-m>|B<--man> + +Print manual page. + +=item B<-V>|B<--version> + +Print version information. + +=back + +=head1 DESCRIPTION This plugin checks the current debian release number. -$USAGE - -u, --url - URL where to search for the search string (default: http://www.debian.org/releases/stable/index.html) - -s, --search - search string on the url (default: '

Debian GNU/Linux') - -f, --file - file with current release informations (default: /etc/debian_version) - -h, --help - print detailed help screen - -V, --version - print version information -EOF - support(); -} +=head1 VERSION + +This man page is current for version 2.0 of check_release. + +=head1 AUTHOR + +Written by Christian Arnold L + +=head1 COPYRIGHT + +Copyright (C) 2011 by Christian Arnold and Schlittermann internet & unix support. +This is free software, and you are welcome to redistribute it under certain conditions. +See the GNU General Public Licence for details. + +=cut