#!/usr/bin/perl
#notify_by_opalert
#Description:
#       OpMon Notification via OpAlert
#Author:
#	Estevan Ramalho (estevan.ramalho@opservices.com.br)
#Version:
#	1.0 18-07-2013: Initial release

use strict;
use Getopt::Long;
use POSIX;

$ENV{"USER"}="opuser";
$ENV{"HOME"}="/home/opuser";

our $version = "1.0";
our $name = "notify_by_opalert";
our $path = "/usr/local/opmon/libexec/opservices/";

our ($opt_version, $opt_help, $opt_debug, $opt_from, $opt_to, $opt_Houtput, $opt_Soutput);
our ($opt_date, $opt_Ntype, $opt_Hname, $opt_Haddr, $opt_Hstate, $opt_Sname, $opt_Sstate);

our $usage ='
Usage: '. $0 .' [OPTION]...
Send a SMS notification via OpAlert system, for hosts and services.

$USER1$/notify_by_opalert.pl -t \'$CONTACTPAGER$\' --Ntype \'$NOTIFICATIONTYPE$\' -D \'$SHORTDATETIME$\' -H \'$HOSTNAME$\' --Haddr \'$HOSTADDRESS$\' --Hstate \'$HOSTSTATE$\' --Sname \'$SERVICEDESC$\' --Sstate \'$SERVICESTATE$\' --Houtput \'$HOSTOUTPUT$\' --Soutput \'$SERVICEOUTPUT$\'

Plugin automatic detects Host and Service alerts

Extra Options:

        -h, --help              Display this help and exit
        -v, --version           Output version information and exit
        -d, --debug     	1 = Debug
                        	2 = Generate log file

Send email to suporte@opservices.com.br if you have questions
regarding use of this software. To submit patches or suggest improvements,
send email to suporte@opservices.com.br.
Please include version information with all correspondence (when possible,
use output from the --version option of the plugin itself).

';

sub main {
	# Get options
	getOptions();
	logger($opt_debug, "----- Starting -----");

	logger($opt_debug, "----- Environmental Variables -----");
	logger($opt_debug, $opt_Ntype); 
	logger($opt_de7bug, $opt_date);
	logger($opt_debug, $opt_Hname);
	# Get the Ticket Description
	my $msg = get_msg();
	logger($opt_debug, "Message:\n$msg");
	# Send Message
	logger($opt_debug, "----- Send Message -----");
	my $return = send_msg($msg);
	logger($opt_debug, "OK - The message has been sent");
	exit (0);
}

sub send_msg {
	my $msg = shift;
	my $sms_msg = "From: OpAlert\nTo: 55$opt_to\n\n$msg\n";
	my $sms_msg_file =  "/var/spool/sms/outgoing/opalert.$$";
	logger($opt_debug,"FILE: $sms_msg_file");
	logger($opt_debug,"MESSAGE:\n$sms_msg");
	open(SMS,">$sms_msg_file");
	print SMS $sms_msg;
	close(SMS);
	return(0);
}

sub getOptions { 
	Getopt::Long::Configure('bundling');
	GetOptions(
	       'v|version'     => \$opt_version,
	       'h|help'        => \$opt_help,
	       'd|debug=i'     => \$opt_debug,
	       't|to=s'	     => \$opt_to,
	       'Ntype=s'	     => \$opt_Ntype,
	       'D|date=s'      => \$opt_date,
	       'H|Hname=s'     => \$opt_Hname,
	       'Haddr=s'       => \$opt_Haddr,
	       'Hstate=s'      => \$opt_Hstate,
	       'Sname=s'       => \$opt_Sname,
	       'Sstate=s'      => \$opt_Sstate,
	       'Houtput=s'     => \$opt_Houtput,
	       'Soutput=s'     => \$opt_Soutput,
	); 

	if ($opt_version) {
		print $name." ".$version."\n";
	   exit(0);
	}
	if ($opt_help) {
	   print $usage;
	   exit(0);
	}
	if (!$opt_to) {
		print $usage;
		exit(3);
	} 
	if ( ($opt_debug) and ($opt_debug !~ /1|2/ ) ) {
		usage("Option Missing:\t-d|--debug\t(1|2)\n");
	}
}

sub get_msg {
	my $msg;
	if ( ($opt_Sname == '$SERVICEDESC$') or ($opt_Sname == "") ) {
		$msg = "$opt_Ntype\nHost: $opt_Hname\nState: $opt_Hstate\nAddress: $opt_Haddr\nDetail: $opt_Houtput\nDate/Time: $opt_date"; 
	} else { 
		$msg = "$opt_Ntype\nHost: $opt_Hname\nService: $opt_Sname\nState: $opt_Sstate\nDetail: $opt_Soutput\nDate/Time: $opt_date"; 
	}
	return($msg);
}

sub logger {
	my $opt = shift;
	my $msg = shift;
	my $log = "$path/$name.log";
	my $date = strftime("%d/%m/%Y - %H:%M:%S",localtime);
	if ($opt == 1) {
		print "$msg\n";
	}
	elsif ($opt == 2) {
		open(LOG, ">>$log");
		print LOG "$date => $msg\n";
		close(LOG);
	}
}

&main;
