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 = "";
print "SEND: ", $payload, "\n" if($verbose);
$req->content($payload);

$res = $ua->request($req);

if ($res->is_success) {
print "RECV: ", $res->content, "\n" if ($verbose);
}
else {
print $res->status_line, "\n" if ($verbose);
return 'CRITICAL';
}
}
}
else {
print $res->status_line, "\n" if ($verbose);
return 'CRITICAL';
}

return 'OK';
}

sub print_help() {
print_revision($PROGNAME,'$Revision: 1196 $ ');
print "Copyright (c) 2009 Stefan Strigler\n";
print "adopted from check_rpc by Karl DeBisschop/Truongchinh Nguyen/Subhendu Ghosh\n";
print "\n";
print "Check if bosh service is up and running\n";
print "\n";
print_usage();
print "\n";
print " URL of bosh service\n";
print " XMPP host name\n";
print " [-v] Verbose \n";
print " [-v -v] Verbose - will print supported programs and numbers \n";
print "\n";
support();
}

sub print_usage () {
print "Usage: \n";
print " $PROGNAME -U url -H host [-v]\n";
print " $PROGNAME [-h | --help]\n";
print " $PROGNAME [-V | --version]\n";
}

2 Responses to Nagios Plugin to monitor BOSH services


Comments

  1. Comment by ag3nt911 | 2009/09/22 at 07:12:51

    Just thought I’d drop a comment and say thanks for creating a great jabber javascript library!

    Just curious, I noticed on the svn you made recent commits to the jsjac library. Are you still working actively on it I assume?

  2. Comment by Steve | 2009/09/22 at 08:57:35

    yes of course I am. but it’s more about fixing bugs than actively developing on new features.


Comments are closed