diff options
Diffstat (limited to 'libexec/check_xmpp')
-rw-r--r-- | libexec/check_xmpp | 117 |
1 files changed, 117 insertions, 0 deletions
diff --git a/libexec/check_xmpp b/libexec/check_xmpp new file mode 100644 index 0000000..c628c13 --- /dev/null +++ b/libexec/check_xmpp @@ -0,0 +1,117 @@ +#!perl +# +# Connect to an XMPP service to check it's working. +# +# Author: Tom Ryder <tom@sanctum.geek.nz> +# License: MIT +# +package main; + +# Force me to write this properly +use strict; +use warnings; +use utf8; + +# Old Perl should be OK +use 5.006; + +# Import required modules +use English '-no_match_vars'; +use Monitoring::Plugin '%ERRORS'; +use Net::XMPP; + +# Decree package version +our $VERSION = '1.00'; + +our $DESCRIPTION = <<'EOF'; +This plugin uses Perl Net::XMPP to connect to an XMPP server, to test whether +it is accepting connections. It does not perform any authentication. +EOF +our $LICENSE = <<'EOF'; +MIT License <https://opensource.org/licenses/MIT> +EOF + +# Add custom options +our @OPTS = ( + { + spec => 'hostname|H=s', + label => 'HOSTNAME', + help => 'Hostname or IP address of device to check', + }, + { + spec => 'port|p=i', + label => 'PORT', + help => 'TCP port to check', + }, + { + spec => 'tls|s', + help => 'Use a secure connection', + }, +); + +# Build Monitoring::Plugin object +my $mp = Monitoring::Plugin->new( + usage => 'Usage: %s' + . ' [--hostname|-H HOSTNAME]' + . ' [--port|-p PORT]' + . ' [--tls|-s]', + version => $VERSION, + blurb => $DESCRIPTION, + license => $LICENSE, +) or die "Failed plugin build\n"; + +# Anything fatal in here exits UNKNOWN +eval { + + # Add and read custom options + for my $opt (@OPTS) { + $mp->add_arg( %{$opt} ); + } + $mp->getopts(); + + # Start counting down to timeout + alarm $mp->opts->timeout(); + + # Create XMPP object + my $xmpp = Net::XMPP::Client->new() + or die "Failed object construct\n"; + + # Build connection parameters based on options + my %conn; + for my $opt (qw(hostname port tls)) { + defined( my $val = $mp->opts->get($opt) ) + or next; + $conn{$opt} = $val; + } + + # If TLS is being used, get certificate authority details + if ( $conn{tls} ) { + require IO::Socket::SSL; + my %default_ca = IO::Socket::SSL::default_ca(); + for my $key ( keys %default_ca ) { + $conn{ lc $key } = $default_ca{$key}; + } + } + + # Attempt connection and add an appropriate message + my ( $code, $message ); + if ( $xmpp->Connect(%conn) ) { + ( $code, $message ) = ( $ERRORS{OK}, 'Successful XMPP connection' ); + $xmpp->Disconnect(); + } + else { + ( $code, $message ) = ( $ERRORS{CRITICAL}, 'Failed XMPP connection' ); + } + $mp->add_message( $code, $message ); + + # Exit with the status of our most severe error + $mp->plugin_exit( + $mp->check_messages( + join => ', ', + join_all => ', ', + ), + ); + +} or $mp->plugin_die($EVAL_ERROR); + +1; |