check_zarafa_version.pl
changeset 9 8bec5bd2e6b2
parent 4 367584867fba
equal deleted inserted replaced
8:2d2de8fa5d70 9:8bec5bd2e6b2
       
     1 #! /usr/bin/perl
       
     2 
       
     3 #   Copyright (C) 2011-2013  Christian Arnold, 2014-2015 Matthias Förste
       
     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    = 'ZARAFAVERSION';
       
    47 my $VERSION = '0.3';
       
    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|url=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     my $side = get($url);
       
    78 
       
    79     return "can't get $url" if ( !$side );
       
    80 
       
    81     push @webside, split( "\n", $side );
       
    82     foreach (@webside) {
       
    83         /$search/ or next;
       
    84         $last_verbose = $1 and last;
       
    85     }
       
    86     return $last_verbose if ($last_verbose);
       
    87     return "-";
       
    88 }
       
    89 
       
    90 sub get_current_version($) {
       
    91     my ($package) = @_;
       
    92     my $version = undef;
       
    93 
       
    94     my @current_version = grep { /^Version:/ } `dpkg -s $package 2> /dev/null`;
       
    95     if ( $current_version[0] ) {
       
    96         $current_version[0] =~ /^Version:\s+(\S+)/ and $version = $1;
       
    97     }
       
    98     return $version if ($version);
       
    99     return "-";
       
   100 }
       
   101 
       
   102 sub report($$) {
       
   103     my ( $last_verbose, $current_version ) = @_;
       
   104 
       
   105     if ( $last_verbose eq $current_version ) {
       
   106         say "$NAME OK: $opt{package} version $current_version";
       
   107         exit $ERRORS{OK};
       
   108     }
       
   109     else {
       
   110         say
       
   111 "$NAME WARNING: $opt{package} installed version $current_version available version $last_verbose";
       
   112         exit $ERRORS{WARNING};
       
   113     }
       
   114 }
       
   115 
       
   116 sub version($$) {
       
   117     my ( $progname, $version ) = @_;
       
   118     print <<_VERSION;
       
   119 $progname version $version
       
   120 Copyright (C) 2011-2013 by Christian Arnold, 2014-2015 by Matthias Förste and
       
   121 Schlittermann internet & unix support
       
   122 
       
   123 $ME comes with ABSOLUTELY NO WARRANTY. This is free software,
       
   124 and you are welcome to redistribute it under certain conditions.
       
   125 See the GNU General Public Licence for details.
       
   126 _VERSION
       
   127 }
       
   128 
       
   129 __END__
       
   130 
       
   131 =head1 NAME
       
   132 
       
   133 check_zarafa_version - nagios plugin to check installed zarafa version
       
   134 
       
   135 =head1 SYNOPSIS
       
   136 
       
   137 check_zarafa_version [B<-u>|B<--url> string] [B<-s>|B<--search> string] [B<-p>|B<--package> string]
       
   138 
       
   139 check_zarafa_version [B<-h>|B<--help>]
       
   140 
       
   141 check_zarafa_version [B<-m>|B<--man>]
       
   142 
       
   143 check_zarafa_version [B<-V>|B<--version>]
       
   144 
       
   145 =head1 OPTIONS
       
   146 
       
   147 =over
       
   148 
       
   149 =item B<-u>|B<--url> I<string>
       
   150 
       
   151 URL where will be look for the I<search> string ( default: http://www.zarafa.com/download-release )
       
   152 
       
   153 =item B<-s>|B<--search> I<string>
       
   154 
       
   155 Perl regular expression to search version string on the url. The content of the round brackets in the
       
   156 regular expression will be used to compare with the installed package version.
       
   157 ( default: 'http://download.zarafa.com/community/final/[\d\.]+/([\d\.-]+)' )
       
   158 
       
   159 =item B<-p>|B<--package> I<string>
       
   160 
       
   161 Installed package name ( default: zarafa )
       
   162 
       
   163 =item B<-h>|B<--help>
       
   164 
       
   165 Print detailed help screen.
       
   166 
       
   167 =item B<-m>|B<--man>
       
   168 
       
   169 Print manual page.
       
   170 
       
   171 =item B<-V>|B<--version>
       
   172 
       
   173 Print version information.
       
   174 
       
   175 =back
       
   176 
       
   177 =head1 DESCRIPTION
       
   178 
       
   179 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.
       
   180 
       
   181 =head1 VERSION
       
   182 
       
   183 This man page is current for version 0.3 of B<check_zarafa_version>.
       
   184 
       
   185 =head1 AUTHOR
       
   186 
       
   187 Written by Christian Arnold L<arnold@schlittermann.de>
       
   188 
       
   189 =head1 COPYRIGHT
       
   190 
       
   191 Copyright (C) 2011-2013 by Christian Arnold, 2014-2015 by Matthias Förste and
       
   192 Schlittermann internet & unix support.  This is free software, and you are
       
   193 welcome to redistribute it under certain conditions.  See the GNU General
       
   194 Public Licence for details.
       
   195 
       
   196 =cut