#!/usr/bin/perl

use warnings;
use IO::Socket;
use Getopt::Std;

my $hostname = gethostbyname('nagiosproxy.incas.de');
if($hostname eq "")
{
        print "Could not resolve Hostname";
        exit 2;
}

getopts('h:t:g:s:m:') or die "Ungueltige Option!\n";

$message = "{";

if($opt_t)
{
        $message = $message . "\"token\":\"" . $opt_t . "\",";
}
else
{
        &printusage;
}
if($opt_h)
{
        $message = $message . "\"host\":\"" . $opt_h . "\",";
}
else
{
        &printusage;
}
if($opt_g)
{
        $message = $message . "\"group\":\"" . $opt_g . "\",";
}
else
{
        &printusage;
}
if($opt_s)
{
        $message = $message . "\"service\":\"" . $opt_s . "\",";
}
if($opt_m)
{
        $message = $message . "\"message\":\"" . $opt_m . "\",";
}
else
{
        &printusage;
}
$message = $message . "\"type\":\"notifikation:\",";


$message = substr($message, 0, - 1);
$message = $message . "}";


my $sh;
socket($sh, PF_INET, SOCK_STREAM, getprotobyname('tcp')) or die $!;
  if(connect($sh, sockaddr_in(9182, $hostname)))
        {
                print $sh $message;
                $sh->autoflush();
                close $sh;
        }
        else
        {
                print "Could not connect to Nagmon-proxy";
                exit 2;
        }

sub printusage
{
        print $ARGV[0]. " -t Notify-token -g Hostgroupname -h Hostname [-s Servicename] -m Notificationmessage";
        exit;
}
