-
Notifications
You must be signed in to change notification settings - Fork 3
/
notify_by_xmpp
executable file
·148 lines (124 loc) · 3.91 KB
/
notify_by_xmpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!/usr/bin/perl -w
# script for nagios notify via Jabber / Google Talk Instant Messaging
# using XMPP protocol and SASL PLAIN authentication.
#
# author: Jason Rivers <[email protected]>
# based on work by Andrew Elwell <[email protected]>
# based on work by Thus0 <[email protected]> and David Cox
#
# released under the terms of the GNU General Public License v2
# Copyright 2018 Jason Rivers
use strict;
use Net::XMPP;
use Getopt::Std;
## Configuration defaults
my $xmpp_username = '';
my $xmpp_password = '';
my $xmpp_resource = 'NagiosCore';
my $xmpp_server = '';
my $xmpp_port = 5222;
my $xmpp_componentname = '';
my $xmpp_connectiontype = 'tcpip';
my $xmpp_tls = 1;
## End of configuration
## Check arguments
my $opts = {};
getopts( 'hu:p:r:s:P:c:t:T:', $opts );
if ( $opts->{h} ) {
showUsage();
}
if ( $opts->{u} ) {
$xmpp_username = $opts->{u};
} else {
showUsage();
}
if ( $opts->{p} ) {
$xmpp_password = $opts->{p};
} else {
showUsage();
}
if ( $opts->{r} ) {
$xmpp_resource = $opts->{r};
}
if ( $opts->{s} ) {
$xmpp_server = $opts->{s};
} else {
showUsage();
}
if ( $opts->{P} ) {
$xmpp_port = $opts->{P};
}
if ( $opts->{c} ) {
$xmpp_componentname = $opts->{c};
}
if ( $opts->{T} ) {
$xmpp_connectiontype = $opts->{T};
}
if ( $opts->{t} ) {
$xmpp_tls = $opts->{t};
}
my $xmpp_msgto = $ENV{NAGIOS_CONTACTEMAIL} || die "You must set CONTACTEMAIL for the user to use this script\n";
## End of Arguments
sub showUsage {
if ( $_ = shift ) { print "Error: $_\n"}
print "$0 \n";
print "Licence: MIT\n";
print "Copyright 2016 Jason Rivers\n\n";
print "Usage: notify_by_xmpp [Options]\n\n";
print "Options:\n\n";
print " -h shows this help\n";
print " -u xmpp username\n";
print " -p xmpp password\n";
print " -r xmpp resource\n";
print " -s xmpp server\n";
print " -c xmpp component name (eg domain of your username)\n";
print " -P xmpp port number (default 5222)\n";
print " -T connection type (default tcpip) \n";
print " -t use TLS (default 1)\n\n";
print "Example:\n";
print "notify-by-xmpp -u nagios -p NagiosPassword -s xmpp.example.com -c example.com\n\n";
print "Uses Nagios environment variables to send XMPP message\n\n";
print "Nagios environment variables used:\n";
print "NAGIOS_CONTACTEMAIL\n";
print "NAGIOS_NOTIFICATIONTYPE\n";
print "NAGIOS_SERVICEOUTPUT\n";
print "NAGIOS_SERVICEDESC\n";
print "NAGIOS_SERVICESTATE\n";
print "NAGIOS_HOSTNAME\n";
print "NAGIOS_SHORTDATETIME\n";
exit (0);
}
## Construct Text for message
my $xmpp_text="";
if ( $ENV{NAGIOS_SERVICEDESC} ) {
$xmpp_text = "$ENV{NAGIOS_NOTIFICATIONTYPE}: $ENV{NAGIOS_SERVICEDESC} on $ENV{NAGIOS_HOSTNAME} is $ENV{NAGIOS_SERVICESTATE}: $ENV{NAGIOS_SERVICEOUTPUT} ($ENV{NAGIOS_SHORTDATETIME})";
} else {
$xmpp_text = "$ENV{NAGIOS_NOTIFICATIONTYPE}: $ENV{NAGIOS_HOSTNAME} is $ENV{NAGIOS_HOSTSTATE}: $ENV{NAGIOS_HOSTOUTPUT} ($ENV{NAGIOS_SHORTDATETIME})";
}
#------------------------------------
my $Connection = new Net::XMPP::Client();
my $status = $Connection->Connect(
hostname => $xmpp_server, port => $xmpp_port,
componentname => $xmpp_componentname,
connectiontype => $xmpp_connectiontype, tls => $xmpp_tls);
if (!(defined($status))) {
print "ERROR: XMPP connection failed.\n";
print " ($!)\n";
exit(0);
}
my $sid = $Connection->{SESSION}->{id};
$Connection->{STREAM}->{SIDS}->{$sid}->{hostname} = $xmpp_componentname;
my @result = $Connection->AuthSend(
username => $xmpp_username, password => $xmpp_password,
resource => $xmpp_resource);
if ($result[0] ne "ok") {
print "ERROR: Authentication failed: $result[0] - $result[1]\n";
exit(0);
}
# Send messages
$Connection->MessageSend(
to => "$xmpp_msgto",
resource => $xmpp_resource,
type => "chat",
body => $xmpp_text);
$Connection->Disconnect();