Commit a370293cc819580b4e9b64539899329f8f8ec69e

modified David Cox script to Nagios notification via jabber
  
1#!/usr/bin/perl
2#
3# Author David Cox
4# Created from various code examples found on the web
5# Last Modified 08/06/2002
6# Feel free to use or modify as needed to suit your needs
7#
8# Last Modified 11/03/2009 by Joenio Costa <joenio@perl.org.br>
9# - Works with Nagios3
10# - Add option to log DEBUG
11#
12#######################################################
13# MAXWAIT is used because the send message function didn't seem to
14# like being called to fast. The message would be sent unless I waited a second
15# or so. You can experiment with it but I just went with 2 seconds.
16#######################################################
17#
18# released under the terms of the GNU General Public License v3
19
20use strict;
21use Net::Jabber;
22
23use constant SERVER => 'jabber.org';
24use constant PORT => 5222;
25use constant USER => 'username';
26use constant PASSWORD => 'password';
27use constant RESOURCE => 'resource';
28use constant MAXWAIT => 2;
29use constant DEBUGLEVEL => 0;
30use constant DEBUGFILE => "/tmp/notify_by_jabber.log";
31
32open STDERR, '>', DEBUGFILE if DEBUGLEVEL;
33
34if (scalar @ARGV < 2) {
35 die "Usage...\n $0 [jabberid] [message]\n";
36}
37
38my @RECIPIENTS = split(/,/, shift @ARGV);
39my $MESSAGE = "@ARGV";
40
41printf STDERR "Connecting to server %s on port %s\n", SERVER, PORT if DEBUGLEVEL;
42
43my $connection = Net::Jabber::Client->new(debuglevel => DEBUGLEVEL, debugfile => DEBUGFILE . "_net_jabber.log");
44$connection->Connect( "hostname" => SERVER, "port" => PORT ) or die "Cannot connect ($!)\n";
45
46printf STDERR "Anthenticating user %s in resource %s... ", USER, RESOURCE if DEBUGLEVEL;
47
48my @result = $connection->AuthSend( "username" => USER, "password" => PASSWORD, "resource" => RESOURCE );
49
50print STDERR $result[0], "\n" if DEBUGLEVEL;
51
52if ($result[0] ne "ok") {
53 die "Ident/Auth with server failed: $result[0] - $result[1]\n";
54}
55
56foreach ( @RECIPIENTS ) {
57 print STDERR "Sending message to $_\n" if DEBUGLEVEL;
58 print STDERR " $MESSAGE\n" if DEBUGLEVEL;
59 eval {
60 my $message = Net::Jabber::Message->new();
61 $message->SetMessage( "to" => $_, "subject" => "Notification", "type" => "chat", "body" => $MESSAGE );
62 $connection->Send($message);
63 };
64 print STDERR "$@" if $@ && DEBUGLEVEL;
65 sleep(MAXWAIT);
66}
67
68print STDERR "Closing connection\n" if DEBUGLEVEL;
69$connection->Disconnect();
70
71close STDERR if DEBUGLEVEL;
72
73exit 0;