splitting to more subroutines
authorChristian Arnold <arnold@schlittermann.de>
Sun, 06 Feb 2011 13:31:48 +0100
changeset 2 1be9848b69b6
parent 1 ccfe1fda4a0b
child 3 c442ffe27e48
splitting to more subroutines adding pod documentation fixing options parameters removing critical return status
.perltidyrc
check_release.pl
debian/changelog
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/.perltidyrc	Sun Feb 06 13:31:48 2011 +0100
@@ -0,0 +1,4 @@
+--paren-tightness=2
+--square-bracket-tightness=2
+
+
--- 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 <http://www.gnu.org/licenses/>.
+#
+#    Christian Arnold <arnold@schlittermann.de>
+
 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   = <<EOF;
-Usage: $ME [-u | --url]
-       $ME [-s | --search]
-	   $ME [-f | --file]
-       $ME [-h | --help]
-       $ME [-V | --version]
-EOF
+my $VERSION = "2.0";
 
-my $opt_url    = "http://www.debian.org/releases/stable/index.html";
-my $opt_search = "<p>Debian GNU/Linux";
-my $opt_file   = "/etc/debian_version";
-my ($found, $current_release, $current_majornr, $current_minornr,  $stable_release, $stable_majornr, $stable_minornr, @website);
-
-sub check_status();
-sub print_help();
-sub print_usage();
+my %opt = (
+    url    => "http://www.debian.org/releases/stable/index.html",
+    search => "<p>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(<CR>) {
-		chomp;
-		$current_release = $_;
-		$current_release =~ /^(\d)+.*(\d)+$/;
-		$current_majornr = $1;
-		$current_minornr = $2;
-	}
-	close(CR);
+    open(CR, "<$file");
+    while (<CR>) {
+        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>
+
+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 <<EOF;
-Copyright (c) 2010 Christian Arnold
+=item B<-s>|B<--search> I<string>
+
+search string on the url (default: '<p>Debian')
+
+=item B<-f>|B<--file> I<string>
+
+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: '<p>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<arnold@schlittermann.de>
+
+=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
--- a/debian/changelog	Mon Sep 06 12:32:10 2010 +0200
+++ b/debian/changelog	Sun Feb 06 13:31:48 2011 +0100
@@ -1,3 +1,12 @@
+nagios-plugin-release (2.0) stable; urgency=low
+
+  * splitting to more subroutines
+  * adding pod documentation
+  * fixing options parameters
+  * removing critical return status
+
+ -- Christian Arnold <arnold@schlittermann.de>  Sun, 06 Feb 2011 13:26:00 +0100
+
 nagios-plugin-release (1.2-0) stable; urgency=low
 
   * handling slowly update time for the debian website