File check_source-service.pl of Package monitoring-plugins-source-service
#!/usr/bin/perl -w
# nagios: -epn
#
# check_source-service - nagios plugin
#
# Copyright (C) 2010, Novell, Inc.
# Author: Lars Vogdt
#
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# * Neither the name of the Novell nor the names of its contributors may be
# used to endorse or promote products derived from this software without
# specific prior written permission.
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
# $Id: check_source-service.pl,v 1.2 2010/10/25 21:49:30 lrupp Exp lrupp $
#
##
use strict;
use warnings;
use Getopt::Long;
use Pod::Usage;
use Time::HiRes qw( clock );
use WWW::Curl::Simple;
use XML::Simple;
# cleanup the environment
$ENV{'PATH'} = '/bin:/usr/bin:/sbin:/usr/sbin:';
$ENV{'BASH_ENV'} = '';
$ENV{'ENV'} = '';
our $conf = {
'VERSION' => '0.1',
'PROGNAME' => 'check_source-service',
'timeout' => '60',
'critical' => '2',
'warning' => '1',
'debug' => 0,
'curl' => '/usr/bin/curl',
'protocol' => 'http',
'hostname' => 'build.opensuse.org',
'port' => '5352',
'service' => 'service',
};
our $print_version = 0;
our $print_help = 0;
our $exitcode = 0;
our %ERRORS = (
'OK' => 0,
'WARNING' => 1,
'CRITICAL' => 2,
'UNKNOWN' => 3,
'DEPENDENT' => 4
);
#######################################################################
# Functions
#######################################################################
sub DEBUG($) {
my ($output) = @_;
print "DEBUG: $output\n" if ( $conf->{'debug'} );
}
sub print_myrevision ($$) {
my ($commandName,$pluginRevision) = @_;
print "$commandName v$pluginRevision\n";
}
#######################################################################
# Main
#######################################################################
Getopt::Long::Configure('bundling');
GetOptions(
"H=s" => \$conf->{'hostname'},
"hostname=s" => \$conf->{'hostname'},
"v" => \$print_version,
"version" => \$print_version,
"h" => \$print_help,
"help" => \$print_help,
"d" => \$conf->{'debug'},
"debug" => \$conf->{'debug'},
"w=f" => \$conf->{'warning'},
"warning=f" => \$conf->{'warning'},
"c=f" => \$conf->{'critical'},
"critical=f" => \$conf->{'critical'},
"p=i" => \$conf->{'port'},
"port=i" => \$conf->{'port'},
"protocoli=s" => \$conf->{'protocol'},
"s=s" => \$conf->{'service'},
"service=s" => \$conf->{'service'},
"t=i" => \$conf->{'timeout'},
"timeout=i" => \$conf->{'timeout'},
) or pod2usage(2);
pod2usage(
-exitstatus => 0,
-verbose => 2, # 2 to print full pod
) if $print_help;
# Just in case of problems, let's not hang Nagios
$SIG{'ALRM'} = sub {
print "UNKNOWN - Plugin timed out\n";
exit $ERRORS{'UNKNOWN'};
};
alarm( $conf->{'timeout'} );
if ($print_version) {
print_myrevision( $conf->{'PROGNAME'}, $conf->{'VERSION'} );
exit $ERRORS{'OK'};
}
# check the options...
if ( $conf->{'warning'} >= $conf->{'critical'} ) {
print "CONFIG ERROR - critical value ("
. $conf->{'critical'}
. ") must be higher than warning value ("
. $conf->{'warning'} . ")\n";
pod2usage(2);
alarm(0);
exit $ERRORS{'UNKNOWN'};
}
if ( !defined($conf->{'hostname'}) ) {
print "ERROR - no hostname given\n";
pod2usage(2);
alarm(0);
exit $ERRORS{'UNKNOWN'};
}
my $ret_str='UNKNOWN';
my $error='UNKNOWN';
my $url=$conf->{'protocol'}."://".$conf->{'hostname'}.":".$conf->{'port'}."/".$conf->{'service'};
if ($conf->{'debug'}){
use Data::Dumper;
print STDERR Data::Dumper->Dump([$conf]);
DEBUG("URL: $url");
}
# do not confuse the enduser with warnings from WWW::Curl::Simple if the host is not reachable
# do a simple check via curl before (yes, not nice but it's late here...)
my $check=`$conf->{'curl'} $url 2>/dev/null || echo -n 1`;
if ($check eq '1'){
print "UNKNOWN: Could not connect to $url\n";
alarm(0);
exit $ERRORS{'UNKNOWN'};
}
# Start the actual request
my $curl = WWW::Curl::Simple->new(fatal=>1);
my $clock0 = clock();
my $retcode = $curl->get($url);
my $clock1 = clock();
my $clockd = $clock1 - $clock0;
DEBUG("retcode:\n".Data::Dumper->Dump([$retcode])."\n");
# Looking at the results...
if ($retcode->{'_rc'} != '200') {
$ret_str = "CRITICAL: Got response code ".$retcode->{'_rc'};
$error = 'CRITICAL';
} else {
my $xml = XMLin("$retcode->{'_content'}", ForceArray => 1,
ForceContent => 1,
KeyAttr => 'body',
KeepRoot => 0,);
DEBUG("content:\n".Data::Dumper->Dump([$xml])."\n");
my $i=-1;
foreach my $section (@{$xml->{'service'}}){
$i++;
next if ($xml->{'service'}[$i]->{'name'} !~ /download_url/);
if ($clockd >= $conf->{'critical'}){
$ret_str = "CRITICAL : Source Service needs $clockd seconds";
$error = 'CRITICAL';
}
elsif ($clockd >= $conf->{'warning'}){
$ret_str = "WARNING : Source Service needs $clockd seconds";
$error = 'CRITICAL';
}
else {
$ret_str = "OK : Source Service returns in $clockd seconds";
$error = 'OK';
}
}
}
print "$ret_str | time=".$clockd."s;$conf->{'warning'};$conf->{'critical'};0.0\n";
$exitcode=$ERRORS{$error};
alarm(0);
exit $exitcode;
__END__
=head1 check_source-service
check_source-service is a Nagios Plugin, checking the availability of a source-service.
=head1 SYNOPSIS
./check_source-service -H $HOSTNAME$ -w1 -c2 -p5352
Options:
-H <HOSTNAME> | --hostname <HOSTNAME>
-w <float> | --warning <float>
-c <float> | --critical <float>
-p <int> | --port <int>
| --protocol <string>
-s <string> | --service <string>
-t <int> | --timeout <int>
-h | --help
-d | --debug
=head1 OPTIONS
=over 8
=item B<--hostname> F<hostname>
The name of the host.
=item B<--critical> F<float>
(Mili)Seconds befor the check should return critical.
=item B<--warning> F<float>
(Mili)Seconds befor the check should return warning.
=item B<--port> F<int>
Port number (default: 5352)
=item B<--protocol> F<string>
Use the given protocol instead of http
=item B<--service> -F<string>
Use this service (URL) instead of the default name service
=item B<--timeout> F<int>
General timeout for the script.
=item B<--help>
Produces this output.
=item B<--debug>
Print debug output on console.
=back
=head1 DESCRIPTION
B<check_source-service> allows you to check the availability of a Source Service.
=head1 AUTHORS
Written by Lars Vogdt <Lars.Vogdt@novell.com>
=head1 SUPPORT
Please use https://bugzilla.novell.com to submit patches or suggest improvements.
Include version information with all correspondence (when possible use output from
the --version option of the plugin itself).
=cut