#!/usr/bin/perl # # Author David Cox # Created from various code examples found on the web # Last Modified 08/06/2002 # Feel free to use or modify as needed to suit your needs # # Last Modified 11/03/2009 by Joenio Costa # - Works with Nagios3 # - Add option to log DEBUG # ####################################################### # MAXWAIT is used because the send message function didn't seem to # like being called to fast. The message would be sent unless I waited a second # or so. You can experiment with it but I just went with 2 seconds. ####################################################### # # released under the terms of the GNU General Public License v3 use strict; use Net::Jabber; use constant SERVER => 'jabber.org'; use constant PORT => 5222; use constant USER => 'username'; use constant PASSWORD => 'password'; use constant RESOURCE => 'resource'; use constant MAXWAIT => 2; use constant DEBUGLEVEL => 0; use constant DEBUGFILE => "/tmp/notify_by_jabber.log"; open STDERR, '>', DEBUGFILE if DEBUGLEVEL; if (scalar @ARGV < 2) { die "Usage...\n $0 [jabberid] [message]\n"; } my @RECIPIENTS = split(/,/, shift @ARGV); my $MESSAGE = "@ARGV"; printf STDERR "Connecting to server %s on port %s\n", SERVER, PORT if DEBUGLEVEL; my $connection = Net::Jabber::Client->new(debuglevel => DEBUGLEVEL, debugfile => DEBUGFILE . "_net_jabber.log"); $connection->Connect( "hostname" => SERVER, "port" => PORT ) or die "Cannot connect ($!)\n"; printf STDERR "Anthenticating user %s in resource %s... ", USER, RESOURCE if DEBUGLEVEL; my @result = $connection->AuthSend( "username" => USER, "password" => PASSWORD, "resource" => RESOURCE ); print STDERR $result[0], "\n" if DEBUGLEVEL; if ($result[0] ne "ok") { die "Ident/Auth with server failed: $result[0] - $result[1]\n"; } foreach ( @RECIPIENTS ) { print STDERR "Sending message to $_\n" if DEBUGLEVEL; print STDERR " $MESSAGE\n" if DEBUGLEVEL; eval { my $message = Net::Jabber::Message->new(); $message->SetMessage( "to" => $_, "subject" => "Notification", "type" => "chat", "body" => $MESSAGE ); $connection->Send($message); }; print STDERR "$@" if $@ && DEBUGLEVEL; sleep(MAXWAIT); } print STDERR "Closing connection\n" if DEBUGLEVEL; $connection->Disconnect(); close STDERR if DEBUGLEVEL; exit 0;