Nagios Plugin to monitor BOSH services
Enjoy!
#!/usr/bin/perl -w
#
# check_bosh plugin for nagios
#
# usage:
# check_bosh -U url -H host
#
# Check if bosh service running at specified address
#
# initial version: 3 May 2000 by Truongchinh Nguyen and Karl DeBisschop
#
# current status: $Revision: 1196 $
#
# Copyright Notice: GPL
# $Id: check_bosh.pl 1196 2009-06-17 15:20:46Z s.strigler $
#
use strict;
use lib "/usr/lib/nagios/plugins";
use utils qw($TIMEOUT %ERRORS &print_revision &support);
use vars qw($PROGNAME);
my ($verbose,$host,$response,$state);
my ($opt_V,$opt_H,$opt_h,$opt_U);
$opt_V = $opt_h, $opt_h = $opt_U = '';
$state = 'UNKNOWN';
$PROGNAME = "check_bosh";
sub print_help ();
sub print_usage ();
$ENV{'BASH_ENV'}='';
$ENV{'ENV'}='';
$ENV{'PATH'}='';
$ENV{'LC_ALL'}='C';
use Getopt::Long;
Getopt::Long::Configure('bundling');
GetOptions(
"V" => \$opt_V, "version" => \$opt_V,
"h" => \$opt_h, "help" => \$opt_h,
"U=s" => \$opt_U, "url=s" => \$opt_U,
"H=s" => \$opt_H, "host=s" => \$opt_H,
"v+" => \$verbose, "verbose+" => \$verbose
);
# -h means display verbose help screen
if ($opt_h) { print_help(); exit $ERRORS{'OK'}; }
# -V means display version number
if ($opt_V) {
print_revision($PROGNAME,'$Revision: 1196 $ ');
exit $ERRORS{'OK'};
}
# -U means URL
unless ($opt_U) { print_usage(); exit $ERRORS{'UNKNOWN'}; }
# -H means host name
unless ($opt_H) { print_usage(); exit $ERRORS{'UNKNOWN'}; }
if (! utils::is_hostname($opt_H)){
print "$opt_H is not a valid host name\n";
print_usage();
exit $ERRORS{"UNKNOWN"};
}
# Just in case of problems, let's not hang Nagios
$SIG{'ALRM'} = sub {
print ("ERROR: No response from RPC server (alarm)\n");
exit $ERRORS{"UNKNOWN"};
};
alarm($TIMEOUT);
$state = &check_bosh($opt_U, $opt_H);
if ($state eq 'OK') {
print "$state: BOSH service at $opt_U up and running\n";
}elsif ($state eq 'WARNING') {
print "$state: BOSH service not working correctly. Bad hostname given?\n";
}else{
print "$state: BOSH service at $opt_U is not running\n";
}
exit $ERRORS{$state};
sub check_bosh() {
my $url = shift;
my $host = shift;
use LWP::UserAgent;
my $ua = LWP::UserAgent->new;
my $req = HTTP::Request->new(POST => $url);
$req->content_type('text/xml; charset=utf-8');
my $payload = "
print “SEND: “, $payload, “\n” if($verbose);
$req->content($payload);
my $res = $ua->request($req);
if ($res->is_success) {
print “RECV: “, $res->content, “\n” if ($verbose);
if ($res->content =~ /type=’terminate’/) {
return ‘WARNING’;
} elsif ($res->content =~ /sid=’([^']+)’/) {
$req = HTTP::Request->new(POST => $url);
$req->content_type(’text/xml; charset=utf-8′);
$payload = “