Hi,

I set up nagios-notifications via jabber.
I have a jabber-server running on my Nagios box (really easy to set up, jabber). I used the notify_via_jabber script from David Cox found on the Nagiosexchange. It's a little perl script to transforms your message to a valid jabber message and sends it to the jabber-server.

Now, we made some enhancements to the script, so that you can add html tags to the text if you add --html. When you invoke the script without --html, it behaves as before.

example.

'old' behaviour'
enhanced_notify_by_jabber jabberid "this is my text"

'new' behaviour
enhanced_notify_by_jabber jabberid --html "this is my text, this is <strong>bold</strong>"

Can someone pass me the e-mail address of David Cox, so I can ask hem to put the new script on Nagiosexchange, because I tried to add it as a comment, but I had a lottle of trouble with html-tags...

Frederik
#!/usr/bin/perl
#
# Author David Cox
# Created from various code examples found on the web
# Last Modified 2006-01-19
# History 
# HTML-changes based on 08/06/2002
# adapted d.d. 2006-01-19 by Dieter Demerre to allow html encoded messages
#
# Feel free to use or modify as needed to suit your needs
#######################################################
# MAXWAIT is used because the send message function didn't seem to
# like being called too fast. The message would be sent unless I waited a second
# or so. You can experiment with it but I just went with 2 seconds.
#######################################################

use strict;
use Net::Jabber qw(Client) ;
use Net::Jabber qw(Message) ;
use Net::Jabber qw(Protocol) ;
use Net::Jabber qw(Presence) ;

use HTML::Parse ;
require HTML::FormatText;

my $len = scalar @ARGV;
my $htmlmessage = my $mymessage = "";

if ((2 ne $len) && (($len ne 3) || ($ARGV[1] ne "--html")))
{
   die "Usage...\n notify <jabberid>[,<jabberid>]* [--html] <message>\n";
}

my @field=split(/,/,$ARGV[0]);

use constant RECIPIENT => $ARGV[0];
use constant SERVER    => 'your-jabber-server';
use constant PORT      => 5222;
use constant USER      => 'your-jabber-id';
use constant PASSWORD  => 'your-jabber-password';
use constant RESOURCE  => ' ';
use constant MAXWAIT   => 2 ;

if ("$ARGV[1]" eq "--html")
{
  $htmlmessage  = $ARGV[2];
  # printf("pre de-html-ize: htmlmessage = \"$htmlmessage\".\n");
  $mymessage = 
HTML::FormatText->new->format(HTML::Parse::parse_html("$htmlmessage\n"));
} else {
  $htmlmessage   = $ARGV[1];
  $mymessage   = "$htmlmessage";
}
 # printf("htmlmessage = \"$htmlmessage\".\n");
 # printf("message = \"$mymessage\".\n");

my $connection = Net::Jabber::Client->new();
$connection->Connect( "hostname" => SERVER,"port" => PORT )  or die
"Cannot connect ($!)\n";

my @result = $connection->AuthSend( "username" => USER,"password" =>
PASSWORD,"resource" => RESOURCE );
if ($result[0] ne "ok") {
 die "Ident/Auth with server failed: $result[0] - $result[1]\n";
}

foreach ( @field ) {
  my $message = Net::Jabber::Message->new();
  $message->SetTo($_);
  $message->SetSubject('Notification');
  $message->SetType('chat');
  $message->SetBody($mymessage);
  # printf("Will send text \"%s\".\n",$message->GetBody());
  if ("$ARGV[1]" eq "--html")
  {
    $message->InsertRawXML("<html 
xmlns='http://jabber.org/protocol/xhtml-im'><body 
xmlns='http://www.w3.org/1999/xhtml'>$htmlmessage</body></html>");
  }

  $connection->Send($message);
  sleep(MAXWAIT);
}
$connection->Disconnect();
exit;

Reply via email to