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.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 by Christian Arnold and Schlittermann internet & unix support. |
|
121 |
|
122 $ME comes with ABSOLUTELY NO WARRANTY. This is free software, |
|
123 and you are welcome to redistribute it under certain conditions. |
|
124 See the GNU General Public Licence for details. |
|
125 _VERSION |
|
126 } |
|
127 |
|
128 __END__ |
|
129 |
|
130 =head1 NAME |
|
131 |
|
132 check_zarafa - nagios plugin to check installed zarafa version |
|
133 |
|
134 =head1 SYNOPSIS |
|
135 |
|
136 check_zarafa [B<-u>|B<--url> string] [B<-s>|B<--search> string] [B<-p>|B<--package> string] |
|
137 |
|
138 check_zarafa [B<-h>|B<--help>] |
|
139 |
|
140 check_zarafa [B<-m>|B<--man>] |
|
141 |
|
142 check_zarafa [B<-V>|B<--version>] |
|
143 |
|
144 =head1 OPTIONS |
|
145 |
|
146 =over |
|
147 |
|
148 =item B<-u>|B<--url> I<string> |
|
149 |
|
150 URL where will be look for the I<search> string ( default: http://www.zarafa.com/download-release ) |
|
151 |
|
152 =item B<-s>|B<--search> I<string> |
|
153 |
|
154 Perl regular expression to search version string on the url. The content of the round brackets in the |
|
155 regular expression will be used to compare with the installed package version. |
|
156 ( default: 'http://download.zarafa.com/community/final/[\d\.]+/([\d\.-]+)' ) |
|
157 |
|
158 =item B<-p>|B<--package> I<string> |
|
159 |
|
160 Installed package name ( default: zarafa ) |
|
161 |
|
162 =item B<-h>|B<--help> |
|
163 |
|
164 Print detailed help screen. |
|
165 |
|
166 =item B<-m>|B<--man> |
|
167 |
|
168 Print manual page. |
|
169 |
|
170 =item B<-V>|B<--version> |
|
171 |
|
172 Print version information. |
|
173 |
|
174 =back |
|
175 |
|
176 =head1 DESCRIPTION |
|
177 |
|
178 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. |
|
179 |
|
180 =head1 VERSION |
|
181 |
|
182 This man page is current for version 0.3 of B<check_zarafa>. |
|
183 |
|
184 =head1 AUTHOR |
|
185 |
|
186 Written by Christian Arnold L<arnold@schlittermann.de> |
|
187 |
|
188 =head1 COPYRIGHT |
|
189 |
|
190 Copyright (C) 2011 by Christian Arnold and Schlittermann internet & unix support. This is free software, and you are welcome to |
|
191 redistribute it under certain conditions. |
|
192 See the GNU General Public Licence for details. |
|
193 |
|
194 =cut |
|