Commit a370293cc819580b4e9b64539899329f8f8ec69e
- Diff rendering mode:
- inline
- side by side
notify_via_jabber
(73 / 0)
|   | |||
| 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 | |||
| 20 | use strict; | ||
| 21 | use Net::Jabber; | ||
| 22 | |||
| 23 | use constant SERVER => 'jabber.org'; | ||
| 24 | use constant PORT => 5222; | ||
| 25 | use constant USER => 'username'; | ||
| 26 | use constant PASSWORD => 'password'; | ||
| 27 | use constant RESOURCE => 'resource'; | ||
| 28 | use constant MAXWAIT => 2; | ||
| 29 | use constant DEBUGLEVEL => 0; | ||
| 30 | use constant DEBUGFILE => "/tmp/notify_by_jabber.log"; | ||
| 31 | |||
| 32 | open STDERR, '>', DEBUGFILE if DEBUGLEVEL; | ||
| 33 | |||
| 34 | if (scalar @ARGV < 2) { | ||
| 35 | die "Usage...\n $0 [jabberid] [message]\n"; | ||
| 36 | } | ||
| 37 | |||
| 38 | my @RECIPIENTS = split(/,/, shift @ARGV); | ||
| 39 | my $MESSAGE = "@ARGV"; | ||
| 40 | |||
| 41 | printf STDERR "Connecting to server %s on port %s\n", SERVER, PORT if DEBUGLEVEL; | ||
| 42 | |||
| 43 | my $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 | |||
| 46 | printf STDERR "Anthenticating user %s in resource %s... ", USER, RESOURCE if DEBUGLEVEL; | ||
| 47 | |||
| 48 | my @result = $connection->AuthSend( "username" => USER, "password" => PASSWORD, "resource" => RESOURCE ); | ||
| 49 | |||
| 50 | print STDERR $result[0], "\n" if DEBUGLEVEL; | ||
| 51 | |||
| 52 | if ($result[0] ne "ok") { | ||
| 53 | die "Ident/Auth with server failed: $result[0] - $result[1]\n"; | ||
| 54 | } | ||
| 55 | |||
| 56 | foreach ( @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 | |||
| 68 | print STDERR "Closing connection\n" if DEBUGLEVEL; | ||
| 69 | $connection->Disconnect(); | ||
| 70 | |||
| 71 | close STDERR if DEBUGLEVEL; | ||
| 72 | |||
| 73 | exit 0; |

