You are here

Hipchat - Notification script

6 posts / 0 new
Last post
pcmerc
pcmerc's picture
Hipchat - Notification script

Created this today on request. Enjoy :D

#!/usr/bin/perl
#
# Mangle by John Gisler <john.gisler@mophie.com>
#
# SYNTAX:
my $usage = qq{
submit_hipchat_script
Copyright (C) 2003-2014 Opsview Limited. All rights reserved

Usage: submit_hipchat_script [-h] [-n]

Where:
-h - this help test
-n - do not send acknowledgements

Uses Nagios environment variables to construct the message to send and calls hipsaint
};

#
# DESCRIPTION:
# Wrapper for hipsaint enabling you to send messages using the Hipchat API
#
# This script requires the 'hipsaint' utility - https://github.com/hannseman/hipsaint.git
#
# Eg:
# python setup.py install
#
#
# LICENCE:
# Copyright (C) 2003-2013 Opsview Limited. All rights reserved
#
# This file is part of Opsview
#
# Opsview is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# Opsview is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Opsview; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#

sub usage {
if ( $_ = shift ) { print "Error: $_\n" }
print $usage;
exit 1;
}

use strict;
use lib '/usr/local/nagios/perl/lib';
use strict;
use Getopt::Std;

my $command;
if ( -x '/usr/bin/hipsaint' ) {
$command = '/usr/bin/hipsaint';
}
else {
$command = '/usr/local/bin/hipsaint';
}
my $opts = {};

getopts( 'hn', $opts );

if ( $opts->{h} ) {
usage();
exit(0);
}

if ( $opts->{n} && $ENV{NAGIOS_NOTIFICATIONTYPE} eq 'ACKNOWLEDGEMENT' ) {
print 'Not sending acknowledgement', $/;
exit 0;
}

my $text;
if ( $ENV{NAGIOS_SERVICEDESC} ) {
$text = "$ENV{NAGIOS_SERVICEDESC}|$ENV{NAGIOS_HOSTALIAS}|$ENV{NAGIOS_LONGDATETIME}|$ENV{NAGIOS_NOTIFICATIONTYPE}|$ENV{NAGIOS_HOSTADDRESS}|$ENV{NAGIOS_SERVICESTATE}|$ENV{NAGIOS_SERVICEOUTPUT}";

# $text = "$SERVICEDESC$|$HOSTALIAS$|$LONGDATETIME$|$NOTIFICATIONTYPE$|$HOSTADDRESS$|$SERVICESTATE$|$SERVICEOUTPUT$";

}
else {
$text = "$ENV{NAGIOS_HOSTNAME}|$ENV{NAGIOS_LONGDATETIME}|$ENV{NAGIOS_NOTIFICATIONTYPE}|$ENV{NAGIOS_HOSTADDRESS}|$ENV{NAGIOS_HOSTSTATE}|$ENV{NAGIOS_HOSTOUTPUT}";

# $text = "$HOSTNAME$|$LONGDATETIME$|$NOTIFICATIONTYPE$|$HOSTADDRESS$|$HOSTSTATE$|$HOSTOUTPUT$";

}

my $pagetype;
if ( $ENV{NAGIOS_SERVICEDESC} ) {
$pagetype = "service";
}
else {
$pagetype = "host";
}

my $token = "APIKEY";

my $room = "ROOMNAME";

# my $number = $ENV{NAGIOS_CONTACTPAGER}
# || die "Must have CONTACTPAGER for this notification script\n";

#my $command = "/bin/echo '$text' | $command -t $token -r $room -T $pagetype -i $text";
my $command = "$command -t $token -r $room -T $pagetype -i '$text' -n";
print "Running $command\n";
system($command) == 0 || die( 'Failed to run command: ', $!, $/ );

smarsh
smarsh's picture
What does your notification

What does your notification method look like for this? Im guessing you need to replace APIKEY and ROOMNAME within the notification script, and not in the notification method via the GUI?

ive tested it out on my system here but cant seem to get it to work - wondering if its a hipsaint bin issue!

Cool stuff though :)

Sam

pcmerc
pcmerc's picture
Hi Sam. The notification

Hi Sam. The notification method is pretty plain.

Name: Hipchat Notification
Command: submit_hipchat_script - as this is the name of the script.

Yes, the ROOM & APIKEY are to be set in the script.

I do believe one could create new attributes to use for ROOM / APIKEY & add those variables to the script instead.

I'll have to test that.

What kind of errors are you seeing, if any?

I have hipsaint installed in /usr/local/bin

John~

smarsh
smarsh's picture
I have no idea why its not

I have no idea why its not working on my personal system, very odd! No notifications are being shown at all in nagios.log when i submit a check result - however when i run it via the command line it works:

root@opsview:/usr/local/nagios/libexec/notifications# ./notify_by_hipchat
Running /usr/local/bin/hipsaint -t apikeyhere -r Opsview -T host -i '|||||' -n
root@opsview:/usr/local/nagios/libexec/notifications#

 

One thing I noticed, which may be due to my lack of CLI fu, is that when i have a room with a space, i.e. "All users", it threw an error:

Running /usr/local/bin/hipsaint -t apikeyhere -r All members -T host -i '|||||' -n
Traceback (most recent call last):
  File "/usr/local/bin/hipsaint", line 9, in <module>
    load_entry_point('hipsaint==0.4.7', 'console_scripts', 'hipsaint')()
  File "/usr/local/lib/python2.7/dist-packages/hipsaint-0.4.7-py2.7.egg/hipsaint/bin/commands.py", line 62, in main
    msg.deliver_payload()
  File "/usr/local/lib/python2.7/dist-packages/hipsaint-0.4.7-py2.7.egg/hipsaint/messages.py", line 49, in deliver_payload
    raw_response = urlopen(self.url, message_params)
  File "/usr/lib/python2.7/urllib2.py", line 126, in urlopen
    return _opener.open(url, data, timeout)
  File "/usr/lib/python2.7/urllib2.py", line 406, in open
    response = meth(req, response)
  File "/usr/lib/python2.7/urllib2.py", line 519, in http_response
    'http', request, response, code, msg, hdrs)
  File "/usr/lib/python2.7/urllib2.py", line 444, in error
    return self._call_chain(*args)
  File "/usr/lib/python2.7/urllib2.py", line 378, in _call_chain
    result = func(*args)
  File "/usr/lib/python2.7/urllib2.py", line 527, in http_error_default
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 404: Not Found
Failed to run command: No such file or directory
root@opsview:/usr/local/nagios/libexec/notifications#

 

Even though I put the room name in double quotes in the script:

my $room = "All members";

But yeah, the script works it appears - its just that my Opsview doesnt want to use it :) Will try on a few other systems!

Cheers,

Sam

pcmerc
pcmerc's picture
Try escaping the spaces,

Try escaping the spaces, example: my $room = "All\ members";

pcmerc
pcmerc's picture
Or use underscores instead of

Or use underscores instead of spaces in the channel name.

Spaces are generally bad, I use underscore instead of spaces as code tends to not translate the usage of spaces easily, sometimes.