Skip to content

Instantly share code, notes, and snippets.

@bageljp
Last active May 6, 2019 01:34
Show Gist options
  • Select an option

  • Save bageljp/5844641 to your computer and use it in GitHub Desktop.

Select an option

Save bageljp/5844641 to your computer and use it in GitHub Desktop.
# 'host-notify-by-jabber' command
define command{
command_name host-notify-by-jabber
command_line /usr/bin/perl $USER1$/notify_via_jabber.pl $CONTACTPAGER$ "`/usr/bin/printf \"***** Nagios *****\nHost '$HOSTALIAS$' is $HOSTSTATE$ - Info: $HOSTOUTPUT$\"`"
}
# 'host-notify-by-jabber' command to groupchat
define command{
command_name host-notify-by-jabber-groupchat
command_line /usr/bin/perl $USER1$/notify_via_jabber.pl $CONTACTPAGER$ "`/usr/bin/printf \"***** Nagios *****\nHost '$HOSTALIAS$' is $HOSTSTATE$ - Info: $HOSTOUTPUT$\"`" groupchat
}
# 'service-notify-by-jabber' command
define command{
command_name service-notify-by-jabber
command_line /usr/bin/perl $USER1$/notify_via_jabber.pl $CONTACTPAGER$ "`/usr/bin/printf \"***** Nagios *****\n$NOTIFICATIONTYPE$: $HOSTADDRESS$ ** Service: $SERVICEDESC$ ** State: $SERVICESTATE$\nInfo: $SERVICEOUTPUT$\nDate/Time: $LONGDATETIME$\"`"
}
# 'service-notify-by-jabber' command to groupchat
define command{
command_name service-notify-by-jabber-groupchat
command_line /usr/bin/perl $USER1$/notify_via_jabber.pl $CONTACTPAGER$ "`/usr/bin/printf \"***** Nagios *****\n$NOTIFICATIONTYPE$: $HOSTADDRESS$ ** Service: $SERVICEDESC$ ** State: $SERVICESTATE$\nInfo: $SERVICEOUTPUT$\nDate/Time: $LONGDATETIME$\"`" groupchat
}
#!/usr/bin/perl -w
#
# script for nagios notify via Jabber / Google Talk Instant Messaging
# using XMPP protocol and SASL PLAIN authentication.
#
# author: 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 2007 Andrew Elwell.
use strict;
use Net::XMPP;
use Encode 'decode';
## Configuration
my $username = "account(ex. nagios)";
my $password = "password(ex. password)";
my $resource = "resource(ex. nagios)";
## End of configuration
my $len = scalar @ARGV;
if ($len lt 2) {
die "Usage...\n $0 [jabberid] [message] [groupchat]\n";
}
my @field=split(/,/,$ARGV[0]);
#------------------------------------
# Google Talk & Jabber parameters :
my $hostname = 'jabber_server(ex. jabber.example.com';
my $port = 5222;
my $componentname = 'domain(ex. example.com)';
my $connectiontype = 'tcpip';
my $tls = 1;
#------------------------------------
my $Connection = new Net::XMPP::Client();
# Connect to talk.google.com
my $status = $Connection->Connect(
hostname => $hostname, port => $port,
componentname => $componentname,
connectiontype => $connectiontype, tls => $tls);
if (!(defined($status))) {
print "ERROR: XMPP connection failed.\n";
print " ($!)\n";
exit(0);
}
# Change hostname
my $sid = $Connection->{SESSION}->{id};
$Connection->{STREAM}->{SIDS}->{$sid}->{hostname} = $componentname;
# Authenticate
my @result = $Connection->AuthSend(
username => $username, password => $password,
resource => $resource);
if ($result[0] ne "ok") {
print "ERROR: Authorization failed: $result[0] - $result[1]\n";
exit(0);
}
if ($len lt 3 || $ARGV[2] ne "groupchat") {
# Send messages
foreach ( @field ) {
$Connection->MessageSend(
to => "$_\@$componentname",
resource => $resource,
subject => "Notification",
type => "chat",
body => decode('UTF-8',$ARGV[1]));
}
} else {
# Send messages groupchat
foreach ( @field ) {
$Connection->MessageSend(
to => "$_\@conference.$componentname",
resource => $resource,
subject => "Notification",
type => "groupchat",
body => decode('UTF-8',$ARGV[1]));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment