#!/usr/bin/perl
#
# AUTHOR:
#    Copyright (C) 2007 Priyantha P Kumara
#
#    This script is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    Should you need a copy of the GNU General Public License
#    write to the Free Software Foundation, Inc., 51 Franklin St, 
#    Fifth Floor, Boston, MA  02110-1301  USA
#    Or visit http://www.gnu.org/licenses/gpl.html
#----------------------------------------------------------------------------------------

use Getopt::Std;
use Mail::Mailer;
$script    = "creat_ticket.pl";
$script_version = "1.1.1";
$host = "";
$ackcomment = "";
$stateid = "";
$ackauthor = "Creat_Ticket";
$hostoutput = "";
$notificationtype = "";
$servicedesc = "";
$contactemail = "";

# You may want to customized the '$from' e-mail address below
$from = "nagios\@wightman.ca";

$cmdfile = "/usr/local/nagios/var/rw/external.cmd";
$logfile = "/tmp/test.txt";
$debug = 0;
$external = 1;
# Do we have enough information?
if (@ARGV < 1) {
     print "Too few arguments\n";
     usage();
}

getopts("hS:");
#print "$opt_h\n\n";
if ($opt_h){
    usage();
    exit(0);
}
elsif ($opt_S){
   ($host,$ackcomment,$ackauthor,$output,$stateid,$notificationtype,$servicedesc,$contactemail) = split /\^/, $opt_S;
    # print "Input String $opt_S\n";
}
else {
    print "No Input String specified\n";
    usage();
}

if ($debug){
   open (file, ">>".$logfile);
   print file "Host:$host, AckComment:$ackcomment, AckAuthor:$ackauthor, OutPut:$output, StateID:$stateid, NotificationType:$notificationtype, ServiceDesc:$servicedesc, E-Mail:$contactemail\n";
   close (file);
}
@state = ("OK", "Warning", "Critical", "Unknown");

if ($notificationtype eq "ACKNOWLEDGEMENT"){
  $type = 'sendmail';
  $mailprog = Mail::Mailer->new($type);
  # mail headers to use in the message
  %headers = (
      'To' => "$contactemail",
      'From' => "$from",
      'Subject' => "Event Notification - $host/$servicedesc: $state[$stateid]",
  );
  $mailprog->open(\%headers);
  print $mailprog "Host: $host\n";
  print $mailprog "Service: $servicedesc\n";
  print $mailprog "Service State: $state[$stateid]\n";
  print $mailprog "Event: $output\n";
  print $mailprog "Comment: $ackcomment\n";
  print $mailprog "Submit by: $ackauthor\n";
 $mailprog->close;
 if ($external){
  $now = (time);
  open (cmdf, ">>".$cmdfile);
  print cmdf "[$now] ADD_HOST_COMMENT;$host;1;$ackauthor;<a href=\"https://black.wightman.ca/rt/\" TARGET='_blank'>A Ticket is opened by $ackauthor<\/a>\n";
  close (cmdf);
}

}

#else { 
#  print "ignoring.........\n";
#}

#if ($external){
#  $now = (time);
#  open (cmdf, ">>".$cmdfile);
#  print cmdf "[$now] ADD_HOST_COMMENT;$host;1;$ackauthor;<a href=\"https://black.wightman.ca/rt/\" TARGET='_blank'>A Ticket is opened by $ackauthor<\/a>\n";
#  close (cmdf);
#}
####################################################################
# help and usage information                                       #
####################################################################

sub usage {
    print << "USAGE";
--------------------------------------------------------------------
$script v$script_version

When an event is acknowledge in Nagios, this script will open a ticket in RT

Usage: $script -S "<hostname>^<ackcomment>^<ackauthor>^<output>^<stateid>^<notificationtype>^<servicedesc>^<contactemail>"

Options: -h       Shows this help
         -S       The sting that should contain the followings combined with "^" character.
                     Hostname (\$HOSTNAME\$) 
                     AckComment (\$HOSTACKCOMMENT\$ or \$SERVICEACKCOMMENT\$)
                     AckAuthor (\$HOSTACKAUTHOR\$ or \$SERVICEACKAUTHOR\$)
                     OutPut (\$HOSTOUTPUT\$ or \$SERVICEOUTPUT\$)
                     Notificationtype (\$NOTIFICATIONTYPE\$)
                     ServiceDescription (\$SERVICEDESC\$)
                     ContactE-Mail (\$CONTACTEMAIL\$)

--------------------------------------------------------------------
Copyright 2007 Priyantha P Kumara

    This script is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
------------------------------------------------------------------------
USAGE
     exit 1;
}

