#!/usr/bin/perl -w use strict; use warnings; use lib "/usr/local/libexec/nagios/"; use utils qw($TIMEOUT %ERRORS &print_revision &support); use Getopt::Long; use Net::Jabber; my ( $opt_version, $opt_help, $opt_port, $opt_host, $opt_verbose, $opt_uname, $opt_pass, $opt_ssl, ); my $state = 'UNKNOWN'; my $PROGNAME = "check_jabber_login"; sub print_help (); sub print_usage (); %ENV = (); Getopt::Long::Configure('bundling'); GetOptions( "V|version" => \$opt_version, "h|help" => \$opt_help, "v|verbose+" => \$opt_verbose, "H|hostname=s" => \$opt_host, "P|port=i" => \$opt_port, "s|ssl" => \$opt_ssl, "u|username=s" => \$opt_uname, "p|password=s" => \$opt_pass ); $opt_port ||= 5222; # -h means display verbose help screen if ($opt_help) { print_usage(); exit $ERRORS{'OK'}; } # -V means display version number if ($opt_version) { print_revision( $PROGNAME, '$Revision: 1.1 $ ' ); exit $ERRORS{'OK'}; } if ( !$opt_host || !$opt_port || !$opt_uname || !$opt_pass ) { print_usage(); exit $ERRORS{'UNKNOWN'}; } if ( !utils::is_hostname($opt_host) ) { print "$opt_host is not a valid host name\n"; print_usage(); exit $ERRORS{"UNKNOWN"}; } $SIG{'ALRM'} = sub { print("ERROR: No response from server (alarm)\n"); exit $ERRORS{"UNKNOWN"}; }; alarm($TIMEOUT); checklogin(); sub checklogin { my $jabbercm = $opt_host; my $jabbercmport = $opt_port; my $username = $opt_uname; my $password = $opt_pass; my $is_ssl = 0; my $debug = 0; $is_ssl = '1' if defined $opt_ssl; $debug = '2' if defined $opt_verbose; my $ocon = new Net::Jabber::Client( debuglevel => $debug, file => 'stdout' ); if ( !$ocon->Connect( 'hostname' => $jabbercm, 'port' => $jabbercmport ) ) { print "CRITICAL: Connection error $jabbercm:$jabbercmport\n"; exit $ERRORS{'CRITICAL'}; } my @resp = $ocon->AuthSend( username => $username, password => $password, resource => 'Nagios', tls => $is_ssl ); if ( $resp[0] ne "ok" ) { print "WARNING: Login error for $username\n"; exit $ERRORS{'WARNING'}; } $ocon->Disconnect(); print "OK: Login for $username successful\n"; exit $ERRORS{'OK'}; } sub print_usage () { print "Usage: \n"; print " $PROGNAME -H host -P port -u username -p password [-s] [--ssl]\n"; print " $PROGNAME [-h | --help]\n"; print " $PROGNAME [-V | --version]\n"; }