#!/usr/bin/perl -w
# SMTP-handler for integrating OpsMgr into Nagios
# Version 0.1-BETA 
# Copyright 2007 Ingo Lantschner, Vienna, Austria
# License: GPL
# Please send feedback to ingo@boxbe.com

use strict;
use warnings;

my $commandfile = "/usr/local/nagios/var/rw/nagios.cmd";
my $debug = 1;

my @input=<STDIN>;
my ($service,$state,$output,$host);

foreach (@input) {
	if ($debug==1) {
		open DEBUGLOG, ">> /tmp/smtpdebug.log" or die "Can not open /tmp/smtpdebug.log\n";
			print DEBUGLOG "$_\n";
			close DEBUGLOG;
	} 
    
	if (/^[tT]o:\ (.+)@/) {
        $service = $1;  # Service ist set to the userpart of the destination-address
    }

    if (/^[fF]rom:\ .+@(.+)[>]*/) {
        $host = $1;  # Host is set to the host-part of the from-address
    }

    if (/^[sS]ubject:\ .*Alert:\ (.*)/) {
        $output = $1;  # output is set to all text after "Alert: " in the subject-line
    }

    $state = 2;  # each email received sets the service in Nagios to CRITICAL 
				 # (this could be tweaked later)
	
}
my $return=system("/usr/local/nagios/libexec/eventhandlers/submit_check_result", $host, $service, $state, "$output");

if ($debug==1) {
	open DEBUGOUT, ">> /tmp/smtpdebug" or die "Can't open /tmp/smtpdebug\n";
       print DEBUGOUT "PROCESS_SERVICE_CHECK_RESULT;$host;$service;$state;\"$output\"\n";
       close DEBUGOUT;
}
exit $return;

