check_zarafa.pl
changeset 0 c6e374ba7961
child 1 3ce34c10c732
equal deleted inserted replaced
-1:000000000000 0:c6e374ba7961
       
     1 #! /usr/bin/perl
       
     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>
       
    19 
       
    20 use 5.010;
       
    21 use strict;
       
    22 use warnings;
       
    23 use File::Basename qw(basename);
       
    24 use Pod::Usage;
       
    25 use Getopt::Long;
       
    26 use LWP::Simple;
       
    27 
       
    28 delete @ENV{ grep /^LC_/ => keys %ENV };
       
    29 $ENV{LANG}   = "C";
       
    30 $ENV{LC_ALL} = "C";
       
    31 
       
    32 sub version($$);
       
    33 sub get_last_version($$);
       
    34 sub get_current_version($);
       
    35 sub report($$);
       
    36 
       
    37 my %ERRORS = (
       
    38     OK        => 0,
       
    39     WARNING   => 1,
       
    40     CRITICAL  => 2,
       
    41     UNKNOWN   => 3,
       
    42     DEPENDENT => 4
       
    43 );
       
    44 
       
    45 my $ME      = basename $0;
       
    46 my $NAME    = 'ZARAFA';
       
    47 my $VERSION = '0.1';
       
    48 
       
    49 my %opt = (
       
    50     url     => 'http://www.zarafa.com/download-release',
       
    51     search  => 'http://download.zarafa.com/community/final/[\d\.]+/([\d\.-]+)',
       
    52     package => 'zarafa'
       
    53 );
       
    54 
       
    55 MAIN: {
       
    56     Getopt::Long::Configure('no_ignore_case_always');
       
    57     GetOptions(
       
    58         "u|uerl=s"    => \$opt{url},
       
    59         "s|search=s"  => \$opt{search},
       
    60         "p|package=s" => \$opt{package},
       
    61         "h|help" => sub { pod2usage( -verbose => 1, -exitval => $ERRORS{OK} ) },
       
    62         "m|man" => sub { pod2usage( -verbose => 2, -exitval => $ERRORS{OK} ) },
       
    63         "V|version" => sub { version( $ME, $VERSION ); exit $ERRORS{OK}; }
       
    64     ) or pod2usage( -verbose => 1, -exitval => $ERRORS{CRITICAL} );
       
    65 
       
    66     my $last_verbose = get_last_version( $opt{url}, $opt{search} );
       
    67     my $current_version = get_current_version( $opt{package} );
       
    68 
       
    69     report( $last_verbose, $current_version );
       
    70 }
       
    71 
       
    72 sub get_last_version($$) {
       
    73     my ( $url, $search ) = @_;
       
    74     my @webside      = ();
       
    75     my $last_verbose = "";
       
    76 
       
    77     push @webside, split( "\n", get($url) );
       
    78     foreach (@webside) {
       
    79         /$search/ or next;
       
    80         $last_verbose = $1 and last;
       
    81     }
       
    82     return $last_verbose if ($last_verbose);
       
    83     return "-";
       
    84 }
       
    85 
       
    86 sub get_current_version($) {
       
    87     my ($package) = @_;
       
    88     my $version = undef;
       
    89 
       
    90     my @current_version = grep { /^Version:/ } `dpkg -s $package 2> /dev/null`;
       
    91     if ( $current_version[0] ) {
       
    92         $current_version[0] =~ /^Version:\s+(\S+)/ and $version = $1;
       
    93     }
       
    94     return $version if ($version);
       
    95     return "-";
       
    96 }
       
    97 
       
    98 sub report($$) {
       
    99     my ( $last_verbose, $current_version ) = @_;
       
   100 
       
   101     if ( $last_verbose eq $current_version ) {
       
   102         say "$NAME OK: $opt{package} version $current_version";
       
   103         exit $ERRORS{OK};
       
   104     }
       
   105     else {
       
   106         say
       
   107 "$NAME WARNING: $opt{package} installed version $current_version available version $last_verbose";
       
   108         exit $ERRORS{WARNING};
       
   109     }
       
   110 }
       
   111 
       
   112 sub version($$) {
       
   113     my ( $progname, $version ) = @_;
       
   114     print <<_VERSION;
       
   115 $progname version $version
       
   116 Copyright (C) 2011 by Christian Arnold and Schlittermann internet & unix support.
       
   117 
       
   118 $ME comes with ABSOLUTELY NO WARRANTY. This is free software,
       
   119 and you are welcome to redistribute it under certain conditions.
       
   120 See the GNU General Public Licence for details.
       
   121 _VERSION
       
   122 }
       
   123 
       
   124 __END__
       
   125 
       
   126 =head1 NAME
       
   127 
       
   128 check_zarafa - nagios plugin to check installed zarafa version
       
   129 
       
   130 =head1 SYNOPSIS
       
   131 
       
   132 check_zarafa [B<-u>|B<--url> string] [B<-s>|B<--search> string] [B<-p>|B<--package> string]
       
   133 
       
   134 check_zarafa [B<-h>|B<--help>]
       
   135 
       
   136 check_zarafa [B<-m>|B<--man>]
       
   137 
       
   138 check_zarafa [B<-V>|B<--version>]
       
   139 
       
   140 =head1 OPTIONS
       
   141 
       
   142 =over
       
   143 
       
   144 =item B<-u>|B<--url> I<string>
       
   145 
       
   146 URL where will be look for the I<search> string ( default: http://www.zarafa.com/download-release )
       
   147 
       
   148 =item B<-s>|B<--search> I<string>
       
   149 
       
   150 Perl regular expression to search version string on the url. The content of the round brackets in the
       
   151 regular expression will be used to compare with the installed package version.
       
   152 ( default: 'http://download.zarafa.com/community/final/[\d\.]+/([\d\.-]+)' )
       
   153 
       
   154 =item B<-p>|B<--package> I<string>
       
   155 
       
   156 Installed package name ( default: zarafa )
       
   157 
       
   158 =item B<-h>|B<--help>
       
   159 
       
   160 Print detailed help screen.
       
   161 
       
   162 =item B<-m>|B<--man>
       
   163 
       
   164 Print manual page.
       
   165 
       
   166 =item B<-V>|B<--version>
       
   167 
       
   168 Print version information.
       
   169 
       
   170 =back
       
   171 
       
   172 =head1 DESCRIPTION
       
   173 
       
   174 This nagios plugin I<search> on a specified I<url> for a version string and compare the I<search> result with an installed I<package> version.
       
   175 
       
   176 =head1 VERSION
       
   177 
       
   178 This man page is current for version 0.1 of B<check_zarafa>.
       
   179 
       
   180 =head1 AUTHOR
       
   181 
       
   182 Written by Christian Arnold L<arnold@schlittermann.de>
       
   183 
       
   184 =head1 COPYRIGHT
       
   185 
       
   186 Copyright (C) 2011 by Christian Arnold and Schlittermann internet & unix support.  This is free software, and you are welcome to
       
   187 redistribute it under certain conditions.
       
   188 See the GNU General Public Licence for details.
       
   189 
       
   190 =cut