· 9 years ago · Jun 26, 2017, 02:44 AM
1#!/usr/bin/perl -w
2#
3# script for nagios notify via Jabber / Google Talk Instant Messaging
4# using XMPP protocol and SASL PLAIN authentication.
5#
6# author: Andrew Elwell <A.Elwell@physics.gla.ac.uk>
7# based on work by Thus0 <Thus0@free.fr> and David Cox
8#
9# released under the terms of the GNU General Public License v2
10# Copyright 2007 Andrew Elwell.
11
12use strict;
13use Net::XMPP;
14
15## Configuration
16my $username = "";
17my $password = "";
18my $resource = "nagios";
19## End of configuration
20
21
22my $len = scalar @ARGV;
23if ($len ne 2) {
24 die "Usage...\n $0 [jabberid] [message]\n";
25}
26my @field=split(/,/,$ARGV[0]);
27#------------------------------------
28
29# Google Talk & Jabber parameters :
30
31my $hostname = 'talk.google.com';
32my $port = 443;
33my $componentname = 'gmail.com';
34my $connectiontype = 'tcpip';
35my $ssl = 1;
36
37#------------------------------------
38
39my $Connection = new Net::XMPP::Client();
40
41# Connect to talk.google.com
42my $status = $Connection->Connect(
43 hostname => $hostname, port => $port,
44 componentname => $componentname,
45 connectiontype => $connectiontype, ssl => $ssl);
46
47if (!(defined($status))) {
48 print "ERROR: XMPP connection failed.\n";
49 print " ($!)\n";
50 exit(0);
51}
52
53# Change hostname
54my $sid = $Connection->{SESSION}->{id};
55$Connection->{STREAM}->{SIDS}->{$sid}->{hostname} = $componentname;
56
57# Authenticate
58my @result = $Connection->AuthSend(
59 username => $username, password => $password,
60 resource => $resource);
61
62if ($result[0] ne "ok") {
63 print "ERROR: Authorization failed: $result[0] - $result[1]\n";
64 exit(0);
65}
66
67# Send messages
68foreach ( @field ) {
69$Connection->MessageSend(
70 to => "$_\@$componentname",
71 resource => $resource,
72 subject => "Notification",
73 type => "chat",
74 body => $ARGV[1]);
75}