#!/usr/bin/perl -w

#
# Envoi de notifications par Jabber
# Arguments : destinataire (Jabber ID), hote, service, etat, output, date
# (c) 11/2004 X. Dusart pour IMA

use Net::Telnet;

my $destinataire = shift || &usage();
my $notificationtype = shift || &usage();
my $hote = shift || &usage();
my $service = shift || &usage();
my $etat = shift || &usage();
my $output = shift || &usage();
my $date = shift || &usage();

sub usage {
    print "Tous les parametres n'ont pas ete fournis !\n";
    print "\n";
    print "Commande d'envoi de messages Jabber pour Nagios\n";
    print "Usage : send_jabber.pl <Jabber ID> <type> <hote> <service> <etat> <detail> <date>\n";
    exit 1;
}

my $hello = '<stream:stream to=\'domaine.com\' xmlns=\'jabber:client\' xmlns:stream=\'http://etherx.jabber.org/streams\'>';
my $auth = '<iq id=\'auth2\' type=\'set\'>\n<query xmlns=\'jabber:iq:auth\'>\n<username>nagios</username>\n<password>serveur</password>\n<resource>telnet</resource>\n</query>\n</iq>';
#my $presence = '<presence/>';
my $presence = '';
my $message = "<message from='domaine.com' to='$destinataire'>\n<subject>Nagios $notificationtype $hote/$service</subject>\n<body>Hote : $hote\nService : $service\nEtat : $etat\nDate : $date\nInformations complementaires : $output\n</body>\n</message>";
my $quit = '</stream:stream>';

my $telnet = new Net::Telnet(Telnetmode => 0);
if ($telnet->open(Host => 'localhost', Port => 5222)) {
    my $line;
    $telnet->print($hello);
    $line = $telnet->get;
    $telnet->print($auth);
    $line = $telnet->get;
    $telnet->print($presence);
    $telnet->print($message);
    $telnet->print($quit);
    $telnet->close;
}
