Home > Linux, nagois, otrs, tutorial > OTRS and Nagios integration – to be tested

OTRS and Nagios integration – to be tested


Contents

-Email
-1)Attachment (NagiosGenericAgent.pl): application/x-perl, 12 KiB
-2)Attachment (otrs_ticket_handler.pl): application/x-perl, 3939 bytes
Source: http://permalink.gmane.org/gmane.comp.otrs.devel/813
NB
No gurantee it works, to be tested on my lab sytem.

 

RE: Nagios and OTRS
Volker Maibaum <volker.maibaum <at> eberspaecher.com>
2005-01-13 15:41:37 GMT

Hi,

ok I have the install-howto and the scripts attached.

Don’t hesitate if there are any questions…

Volker

Am Donnerstag, den 13.01.2005, 14:57 +0000 schrieb Nigel Clarke:
> Yes please. Sounds like a great solution.
>
> —–Original Message—–
> From: dev-bounces <at> otrs.org [mailto:dev-bounces <at> otrs.org] On Behalf Of
> Volker Maibaum
> Sent: Thursday, 13 January 2005 2:53 PM
> To: dev <at> otrs.org
> Subject: [dev] Nagios and OTRS
>
>
> Hello,
>
> we are using nagios for system monitoring in our company. As we also use
> OTRS, I thought it would be a nice thing to make the tools work
> together. Two perl-scripts and a bit of configuration was all that was
> needed.
>
> The integration works as follows:
> When NAGIOS notices a host or service problem it starts a perl-script
> that sends an Email to OTRS. A Tickets is created by OTRS after
> recieving the message. When the problem still exists after some time,
> NAGIOS calls the script again. This time the script does not send an
> email to OTRS, but calls a modified copy of GenericAgent (over SSH) to
> add a note to the existing Ticket. When the problem has vanished NAGIOS
> calls the script again, which then closes the ticket the same way.
>
> I tried to make the scripts as independent of the OTRS-version as
> possible, but as I use a modified copy of the GenericAgent.pl there is
> problably the need to do some minor adjustments to this perl-script when
> using a different version of OTRS than 1.2.2.
>
> Is there a way to create dynamic jobs (created from commandline
> parameters) for GenericAgent? Then i could make it completely
> independend from the OTRS Version.
>
> Is anyone interested in the scripts (incl. installation-howto)?
>
> Is there a place on the otrs-page where people can make there code
> available to others? I saw that the wiki was closed…  
>
>

mit freundlichen Grüßen

Volker Maibaum

Tel: 0711-939-0972
Fax: 0711-939-160972
volker.maibaum <at> eberspaecher.com

Attachment (NagiosGenericAgent.pl): application/x-perl, 12 KiB
Attachment (otrs_ticket_handler.pl): application/x-perl, 3939 bytes
Dokumentation: Integrating OTRS with NAGIOS

The integration works as follows:
When NAGIOS notices a host or service problem it starts a perl-script that sends an Email to OTRS. A Tickets
is created by OTRS after recieving the message.
When the problem still exists after some time, NAGIOS calls the script again. This time the script does not
send an email to OTRS, but calls a modified copy of GenericAgent (over SSH) to add a note to the existing
Ticket. When the problem has vanished NAGIOS calls the script again, which then closes the ticket the same way.

I tried to make the scripts as independent of the OTRS-version as possible, but as I use a modified copy of the
GenericAgent.pl there is problably the need to do some minor adjustments to this perl-script when using a
different version of OTRS than 1.2.2.

Requirements:
OTRS 1.2.2 (small modification in NagiosGenericAgent.pl might be necessary for other versions)
NAGIOS 1.2 (should work with other Versions too. Nagios only needs to be able to call an external script when
notifying OTRS and some nagios-macros are needed)
MailTools from CPAN (on the Nagios Server, I used MailTools-1.65)

Installation Instructions:

A Configuration of the Nagios Server
————————————
1) copy the script otrs_ticket_handler.pl to (e.g.) /usr/lib/nagios/ and make this script executable
for the nagios-user
2) define commands that are used for notifications to OTRS (e.g. in /etc/nagios/misccommands.cfg)
# for service notifications
define command{
        command_name    notify-by-email_otrs
        command_line    /usr/lib/nagios/otrs_ticket_handler.pl -T s -H $HOSTNAME$ -S ‘$SERVICEDESC$’ -s
‘$SERVICESTATE$’ -N ‘$NOTIFICATIONTYPE$’ -n ‘$NOTIFICATIONNUMBER$’ -o ‘$OUTPUT$’ -C
‘$CONTACTALIAS$’ -a ‘$HOSTADDRESS$’
        }
# for host notifications
define command{
        command_name    host-notify-by-email_otrs
        command_line    /usr/lib/nagios/otrs_ticket_handler.pl -T h -H $HOSTNAME$ -S ‘__’ -s ‘$HOSTSTATE$’ -N
‘$NOTIFICATIONTYPE$’-n ‘$NOTIFICATIONNUMBER$’ -o ‘$OUTPUT$’ -C ‘$CONTACTALIAS$’ -a ‘$HOSTADDRESS$’
        }
3) Define a contact (or several contacts) for OTRS notifications (e.g. in /etc/nagios/contacts.cfg)
# alias must be the otrs-queue where the notifications for this contact should go to!!
# ‘OTRS’ contact definition
  define contact {
        contact_name                    OTRS
        alias                           my_nagios_queue
        service_notification_period     24×7
        host_notification_period        24×7
        service_notification_options    c,r,w,u
        host_notification_options       d,u,r
        service_notification_commands   notify-by-email_otrs
        host_notification_commands      host-notify-by-email_otrs
        email                           dummy <at> email.address
        }

4) Configure the otrs_ticket_handler.pl script (look at the comments in the script)

5) Allow the NAGIOS server to execute the NagiosGenericAgengt.pl script on the OTRS host:
– create a ssh key pair for the nagios user (e.g. with ssh-keygen)
– add the public key to the ~/.ssh/authorized_keys file of the otrs user on the OTRS host. (if it’s not the
create the directory and create the authorized_keys file)

B. Configuration of the OTRS server
———————————–
1) create OTRS-queues where the NAGIOS-tickets should appear. The queue names should correspond to the
contact aliases from NAGIOS (-> section A. 3) )

2) copy the NagiosGenericAgent.pl script to the OTRS host (e.g. to /opt/otrs/bin/). Make sure that it is
executable by the otrs user.

3) configure NagiosGenericAgent.pl (look at the comments!). The settings must correspond to the
TicketFreeText settings in Config.pm and otrs_ticket_handler.pl

4) configure OTRS so that it interprets the X-OTRS-Headers in emails that come from the NAGIOS host
e.g in ~otrs/.procmailrc:
# —
# Remove all X-OTRS Header (allow this only for trusted email)
# e. g. from *@example.com
# —
:0 fhw :
* !^From.*my_nagios_host.com
| grep -vi ‘^X-OTRS-‘

5) TicketFreeKey / TicketFreeText configuration in Config.pm
hostname, servicename and host-/service state are stored in these fields.
Use the same settings as in otrs_ticket_handler.pl and NagiosGenericAgent.pl   
    # host name
    $Self->{“TicketFreeKey2”} = {
        ‘Server’ => ‘Server’,
    };
    # service name
    $Self->{“TicketFreeKey3”} = {
        ‘Service’ => ‘Service’,
    };
    # host-/service state
    $Self->{“TicketFreeKey4”} = {
        ‘Service-Status’ => ‘Service-Status’,
        ‘Server-Status’ => ‘Server-Status’,
    };
_______________________________________________
OTRS mailing list: dev – Webpage: http://otrs.org/
Archive: http://lists.otrs.org/pipermail/dev
To unsubscribe: http://lists.otrs.org/cgi-bin/listinfo/dev

 

 

 

@@@@@@@@@@@@@
@@@@@@@ Here is the version of NagiosGenericAgent.pl used@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@
@@@@@@

1) NagiosGenericAgent.pl

@@@@@@@@@

 

#!/usr/bin/perl -w
# —
# Changed Version of Generic Agent that is used for closing
# Tickets and adding Notes to Tickets
# —

#
# USAGE: GaCloseNagiosTickets.pl -h HOST -S SERVICE -s STATUS -m MESSAGE -c CLOSE
#
# if close = 1 then Ticket will be closed, otherwise a note will be added
#

# use ../ as lib location
use File::Basename;
use FindBin qw($RealBin);
use lib dirname($RealBin);
use lib dirname($RealBin).”/Kernel/cpan-lib”;

use strict;

use vars qw($VERSION $Debug $Limit);

use Getopt::Std;
use Kernel::Config;
use Kernel::System::Log;
use Kernel::System::DB;
use Kernel::System::Ticket;
use Kernel::System::Queue;

# configuration

my @nagios_queues = [‘nagios_test’, ‘nagios_test::or’, ‘nagios_test::krz’, ‘nagios_test::ti’, ‘nagios_test::or4’];
# Definitions for TicketFreeKeys and TicketFreeText
# (use the same settings in NagiosGenericAgent.pl !!)
# definitions for host freefield
my $host_key            = “X-OTRS-TicketKey2”;          # TicketFreeKey
my $host_text           = “X-OTRS-TicketValue2”;        # TicketFreeText
my $host_name           = “Server”;                     # Value for TicketFreeKey
# definitions for service freefield
my $service_key         = “X-OTRS-TicketKey4”;          # TicketFreeKey
my $service_text        = “X-OTRS-TicketValue4”;        # TicketFreeText
my $service_name        = “Service”;                    # Value for TicketFreeKey
# definitions for state freefield
my $state_key           = “X-OTRS-TicketKey3”;          # TicketFreeKey
my $state_text          = “X-OTRS-TicketValue3”;        # TicketFreeText
my $host_state_name     = “Server-Status”;              # Value for TicketFreeKey (State of the Host)
my $service_state_name  = “Service-Status”;             # Value for TicketFreeKey (State of the Service)

# subjects
my $subject_close  = “NAGIOS: Problem ist behoben!”; # e.g. “Nagios: Problem solved”
my $subject_note = “NAGIOS: Problem besteht weiterhin!”; # e.g. “Nagios: Problem still exists”

# article type for notes
my $article_type  = ‘note-internal’; # note-internal|note-external|note-report

# from
my $from   = “NAGIOS”;

# end of configuration

my %Opts = ();
getopt(‘hSscaoNCT’, \%Opts);
my $HOST  = $Opts{‘h’};
my $SERVICE = $Opts{‘S’};
my $STATUS  = $Opts{‘s’};
my $CLOSE = $Opts{‘c’} || 0;
my $NT   = $Opts{‘N’} || “”;
my $AD   = $Opts{‘a’} || “”;
my $OUT  = $Opts{‘o’} || “”;
my $CONTACT  = $Opts{‘C’} || “”;
my $TYPE  = $Opts{‘T’};

if (!$HOST || !$SERVICE || !$STATUS || !$TYPE){
 print “\nAt least a HOST, SERVICE, TYPE and STATUS must be provided!\n”;
 exit(1);
}

# translate this if you like
my $MESSAGE =   “Art der Meldung: $NT\n”. # notificationtype
                “Service: $SERVICE\n”.  # service
                “Servername: $HOST ($AD)\n”. # host (address)
                “Status: $STATUS\n\n”.  # state
                “Ausgabe: $OUT”;  # output

my $statefield = $service_state_name;
if ($TYPE eq “h”){ $statefield = $host_state_name; }
my $ticketstate=””;
my $ticketsubject=””;
if ($CLOSE){
  $ticketstate = “closed successful”;
  $ticketsubject = $subject_close;
}else{
  $ticketstate = “open”;
  $ticketsubject = $subject_note;
}

#
# Create Job
#
my %Jobs = (
   ‘nagios_job’ => {
 # get all tickets with this properties
 Queue => \@nagios_queues,
 # Match Host
 TicketFreeText2 => $HOST,
 # Match Service
 TicketFreeText3 => $SERVICE,
 New => {
        Note => {
         From => $from,
         Subject => $ticketsubject,
         Body => $MESSAGE,
         ArticleType => $article_type,
        },
  # set new Service/Host-State
  TicketFreeKey4 => $statefield,
  TicketFreeText4 => $STATUS,
  # set new ticketstate
  State => $ticketstate,
 }
   }
);

#####
#
# THE FOLLOWING IS THE NON-CHANGED CODE OF GenericAgent.pl FROM OTRS V. 1.2.2
#
# If you like to use this script with other OTRS Versions maybe you have to
# replace the code
#
#####

# set generic agent uid
my $UserIDOfGenericAgent = 1;

# common objects
my %CommonObject = ();
$CommonObject{ConfigObject} = Kernel::Config->new();
$CommonObject{LogObject} = Kernel::System::Log->new(
    LogPrefix => ‘OTRS-GenericAgent’,
    %CommonObject,
);
$CommonObject{DBObject} = Kernel::System::DB->new(%CommonObject);
$CommonObject{TicketObject} = Kernel::System::Ticket->new(
    %CommonObject,
    Debug => $Debug,
);
$CommonObject{QueueObject} = Kernel::System::Queue->new(%CommonObject);

# —
# process all jobs
# —
foreach my $Job (sort keys %Jobs) {
    print “$Job:\n”;
    # —
    # get regular tickets
    # —
    my %Tickets = ();
    if (! $Jobs{$Job}->{Escalation}) {
        my %PartJobs = %{$Jobs{$Job}};
        if (!$PartJobs{Queue}) {
            print ” For all Queues: \n”;
            %Tickets = $CommonObject{TicketObject}->SearchTicket(
                %{$Jobs{$Job}},
                Limit => $Limit,
            );
        }
        elsif (ref($PartJobs{Queue}) eq ‘ARRAY’) {
            foreach (@{$PartJobs{Queue}}) {
                print ” For Queue: $_\n”;
                %Tickets = ($CommonObject{TicketObject}->SearchTicket(
                    %{$Jobs{$Job}},
                    Queues => [$_],
                    Limit => $Limit,
                ), %Tickets);
            }
        }
        else {
            %Tickets = $CommonObject{TicketObject}->SearchTicket(
                %{$Jobs{$Job}},
                Queues => [$PartJobs{Queue}],
                Limit => $Limit,
            );
        }
    }
    # —
    # escalation tickets
    # —
    else {
        if (! $Jobs{$Job}->{Queue}) {
            my @Tickets = $CommonObject{TicketObject}->GetOverTimeTickets();
            foreach (@Tickets) {
                $Tickets{$_} = $CommonObject{TicketObject}->GetTNOfId(ID => $_);
            }
        }
        else {
            my @Tickets = $CommonObject{TicketObject}->GetOverTimeTickets();
            foreach (@Tickets) {
                my %Ticket = $CommonObject{TicketObject}->GetTicket(TicketID => $_);
                if ($Ticket{Queue} eq $Jobs{$Job}->{Queue}) {
                    $Tickets{$_} = $Ticket{TicketNumber};
                }
            }
        }
    }
    # —
    # process each ticket
    # —
    foreach (sort keys %Tickets) {
        Run($Job, $_, $Tickets{$_});
    }
}
# —
# process each ticket
# —
sub Run {
    my $Job = shift;
    my $TicketID = shift;
    my $TicketNumber = shift;
    print “* $TicketNumber ($TicketID) \n”;
    # —
    # move ticket
    # —
    if ($Jobs{$Job}->{New}->{Queue}) {
        print ”  – Move Ticket to Queue ‘$Jobs{$Job}->{New}->{Queue}’\n”;
        $CommonObject{TicketObject}->MoveByTicketID(
            QueueID => $CommonObject{QueueObject}->QueueLookup(Queue=>$Jobs{$Job}->{New}->{Queue}, Cache => 1),
            UserID => $UserIDOfGenericAgent,
            TicketID => $TicketID,
        );
    }
    # —
    # add note if wanted
    # —
    if ($Jobs{$Job}->{New}->{Note}->{Body}) {
        print ”  – Add note\n”;
        $CommonObject{TicketObject}->CreateArticle(
            TicketID => $TicketID,
            ArticleType => $Jobs{$Job}->{New}->{Note}->{ArticleType} || ‘note-internal’,
            SenderType => ‘agent’,
            From => $Jobs{$Job}->{New}->{Note}->{From} || ‘GenericAgent’,
            Subject => $Jobs{$Job}->{New}->{Note}->{Subject} || ‘Note’,
            Body => $Jobs{$Job}->{New}->{Note}->{Body},
            UserID => $UserIDOfGenericAgent,
            HistoryType => ‘AddNote’,
            HistoryComment => ‘Note added.’,
        );
    }
    # —  
    # set new state
    # —
    if ($Jobs{$Job}->{New}->{State}) {
        print ”  – set state to ‘$Jobs{$Job}->{New}->{State}’\n”;
        $CommonObject{TicketObject}->SetState(
            TicketID => $TicketID,
            UserID => $UserIDOfGenericAgent,
            State => $Jobs{$Job}->{New}->{State},
        );
    }
    # —  
    # set customer id and customer user
    # —
    if ($Jobs{$Job}->{New}->{CustomerID} || $Jobs{$Job}->{New}->{CustomerUserLogin}) {
        if ($Jobs{$Job}->{New}->{CustomerID}) {
            print ”  – set customer id to ‘$Jobs{$Job}->{New}->{CustomerID}’\n”;
        }
        if ($Jobs{$Job}->{New}->{CustomerUserLogin}) {
            print ”  – set customer user id to ‘$Jobs{$Job}->{New}->{CustomerUserLogin}’\n”;
        }
        $CommonObject{TicketObject}->SetCustomerData(
            TicketID => $TicketID,
            No => $Jobs{$Job}->{New}->{CustomerID} || ”,
            User => $Jobs{$Job}->{New}->{CustomerUserLogin} || ”,
            UserID => $UserIDOfGenericAgent,
        );
    }
    # —  
    # set new priority
    # —
    if ($Jobs{$Job}->{New}->{Priority}) {
        print ”  – set priority to ‘$Jobs{$Job}->{New}->{Priority}’\n”;
        $CommonObject{TicketObject}->PrioritySet(
            TicketID => $TicketID,
            UserID => $UserIDOfGenericAgent,
            Priority => $Jobs{$Job}->{New}->{Priority},
        );
    }
    # —
    # set new owner
    # —
    if ($Jobs{$Job}->{New}->{Owner}) {
        print ”  – set owner to ‘$Jobs{$Job}->{New}->{Owner}’\n”;
        $CommonObject{TicketObject}->SetOwner(
            TicketID => $TicketID,
            UserID => $UserIDOfGenericAgent,
            NewUser => $Jobs{$Job}->{New}->{Owner},
        );
    }
    # —
    # set new lock
    # —
    if ($Jobs{$Job}->{New}->{Lock}) {
        print ”  – set lock to ‘$Jobs{$Job}->{New}->{Lock}’\n”;
        $CommonObject{TicketObject}->SetLock(
            TicketID => $TicketID,
            UserID => $UserIDOfGenericAgent,
            Lock => $Jobs{$Job}->{New}->{Lock},
        );
    }
    # —
    # set ticket free text options
    # —
    foreach (1..8) {
        if ($Jobs{$Job}->{New}->{“TicketFreeKey$_”} || $Jobs{$Job}->{New}->{“TicketFreeText$_”}) {
            my $Key = $Jobs{$Job}->{New}->{“TicketFreeKey$_”} || ”;
            my $Value = $Jobs{$Job}->{New}->{“TicketFreeText$_”} || ”;
            print ”  – set ticket free text to Key: ‘$Key’ Text: ‘$Value’\n”;
            $CommonObject{TicketObject}->SetTicketFreeText(
                TicketID => $TicketID,
                UserID => $UserIDOfGenericAgent,
                Key => $Key,
                Value => $Value,
                Counter => $_,
            );
        }
    }
    # —
    # run module
    # —
    if ($Jobs{$Job}->{New}->{Module}) {
        print ”  – use module ($Jobs{$Job}->{New}->{Module})\n”;
        $CommonObject{LogObject}->Log(
            Priority => ‘notice’,
            Message => “Use module ($Jobs{$Job}->{New}->{Module}) Ticket [$TicketNumber], TicketID [$TicketID].”,
        );
        if ($Debug) {
            $CommonObject{LogObject}->Log(
                Priority => ‘debug’,
                Message => “Try to load module: $Jobs{$Job}->{New}->{Module}!”,
            );
        }
        if (eval “require $Jobs{$Job}->{New}->{Module}”) {
            my $Object = $Jobs{$Job}->{New}->{Module}->new(
                %CommonObject,
                Debug => $Debug,
            );
            if ($Debug) {
                $CommonObject{LogObject}->Log(
                    Priority => ‘debug’,
                    Message => “Loaded module: $Jobs{$Job}->{New}->{Module}!”,
                );
                $CommonObject{LogObject}->Log(
                    Priority => ‘debug’,
                    Message => “Run module: $Jobs{$Job}->{New}->{Module}!”,
                );
            }
            $Object->Run(TicketID => $TicketID);
        }
        else {
            $CommonObject{LogObject}->Log(
                Priority => ‘error’,
                Message => “Can’t load module: $Jobs{$Job}->{New}->{Module}!”,
            );
        }
    }
    # —
    # cmd
    # —
    if ($Jobs{$Job}->{New}->{CMD}) {
        print ”  – call cmd ($Jobs{$Job}->{New}->{CMD}) for ticket_id $_\n”;
        $CommonObject{LogObject}->Log(
            Priority => ‘notice’,
            Message => “Execut ‘$Jobs{$Job}->{New}->{CMD} $TicketNumber $TicketID’.”,
        );
        system(“$Jobs{$Job}->{New}->{CMD} $TicketNumber $TicketID “);
    }
    # —
    # delete ticket
    # —
    if ($Jobs{$Job}->{New}->{Delete}) {
        print ”  – delete ticket_id $TicketID\n”;
        $CommonObject{LogObject}->Log(
            Priority => ‘notice’,
            Message => “Delete Ticket [$TicketNumber], TicketID [$TicketID].”,
        );
        $CommonObject{TicketObject}->DeleteTicket(
            UserID => $UserIDOfGenericAgent,
            TicketID => $TicketID,
        );
    }
}
# —

 

@
@@@@

@@@@@@

@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@

 

 

2)))))  Attachment (otrs_ticket_handler.pl):

 

@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@@@@@@
@@@@@@@@@@@@

 

 
#!/usr/bin/perl

use strict;
use Mail::Send;
use Getopt::Std;

# configuration

# Email of the OTRS-system
my $emailto   = “otrs\@my-company.xyz”;
# standard Queue (if no contact alias available from nagios)
my $std_queue   = “nagios_test”;
#
# Definitions for TicketFreeKeys and TicketFreeText
# (use the same settings in NagiosGenericAgent.pl !!)
# definitions for host freefield
my $host_key   = “X-OTRS-TicketKey2”;  # TicketFreeKey
my $host_text   = “X-OTRS-TicketValue2”; # TicketFreeText
my $host_name  = “Server”;   # Value for TicketFreeKey
# definitions for service freefield
my $service_key  = “X-OTRS-TicketKey4”;  # TicketFreeKey
my $service_text  = “X-OTRS-TicketValue4”; # TicketFreeText
my $service_name = “Service”;   # Value for TicketFreeKey
# definitions for state freefield
my $state_key   = “X-OTRS-TicketKey3”;  # TicketFreeKey
my $state_text   = “X-OTRS-TicketValue3”; # TicketFreeText
my $host_state_name = “Server-Status”;  # Value for TicketFreeKey (State of the Host)
my $service_state_name = “Service-Status”;  # Value for TicketFreeKey (State of the Service)
 
# script execution command for closing tickets and adding notes
#
# nagios user (e.g. daemon) needs to execute this command without
# having to type a password. Create an ssh key pair and place the
# public key in .ssh/authorized_key of the otrs user.
#
my $ga_script = “ssh otrs\@otrs-host /opt/otrs/bin/NagiosGenericAgent.pl”;

# end of configuration

my $MAIL = 1;
my $ADD_NOTE = 2;
my $CLOSE = 3;
my $action;

# Read Commandline Options
my %Opts = ();
getopt(‘HSsNdaonCT’, \%Opts);

if (!$Opts{“H”} || !$Opts{“S”} || !$Opts{“s”} || !$Opts{“T”} || !( $Opts{“T”} eq ‘s’ || $Opts{“T”} eq ‘h’ )){
 print “Usage: otrs_mail.pl -T [s,h] -H HOSTALIAS -S SERVICEDESCRIPTION -s STATE \n”.
  ”      [ -t DATETIME -n NOTIFICATIONNUMBER -a HOSTADRESS -o OUTPUT \n”.
  ”  -N NOTIFICATIONTYPE -C CONTACTALIAS]\n”;
 exit(1);
}

# get Service State and set action
if ($Opts{‘N’} eq “PROBLEM” && $Opts{‘n’} == 1){
 $action = $MAIL;     
}elsif ($Opts{‘N’} eq “RECOVERY”){
 $action = $CLOSE;
}elsif ($Opts{‘N’} eq “PROBLEM” || $Opts{‘N’} eq “ACKNOWLEDGEMENT”){
 $action = $ADD_NOTE;
}

# set variables for HOST-Notification
my $type = ” -T h”;
my $freefield =$host_state_name;
my $subject = “Nagios-alert: HOST $Opts{‘H’} IS DOWN!!!”;
if ($Opts{‘T’} eq “s”){
 # set variables for SERVICE-Notification
 $type = ” -T s”;
 $freefield = $service_state_name;
  $subject = “Nagios-alert: Service-Check ‘$Opts{‘S’}’ hat den Status ‘$Opts{‘s’}’ auf Server ‘$Opts{‘H’}'”;
}

my $message =   “Art der Meldung: $Opts{‘N’}\n”.
                “Service: $Opts{‘S’}\n”.
                “Servername: $Opts{‘H’}\n”.
                “Serveradresse: $Opts{‘a’}\n”.
                “Status: $Opts{‘s’}\n\n”.
                “Ausgabe: $Opts{‘o’}”;

my $sshcmd =  $ga_script . ” -h $Opts{‘H’} -S $Opts{‘S’} -s $Opts{‘s’} “.
       “-N $Opts{‘N’} -a \”‘$Opts{‘a’}’\” -C $Opts{‘C’} \”‘-o $Opts{‘o’}’\””;

# send email to OTRS = Create new Ticket
if ($action eq $MAIL){
 # OTRS-Email

 my $msg = new Mail::Send;
 $msg->to($emailto);
 $msg->subject($subject);

 # set headers so that message is put in the correct queue
 # and TicketFreeText fields are filled
 my $queue = $Opts{‘C’} || $std_queue;
 $msg->set(“X-OTRS-Queue”, $queue);
 $msg->set($host_key, “Host”);
 $msg->add($host_text,$Opts{“H”} );
 $msg->set($service_key, “Service”);
 $msg->add($service_text,$Opts{“S”} );
 $msg->set($state_key, $freefield);
 $msg->add($state_text, $Opts{“s”} );

 my $fh = $msg->open;               # some default mailer
 print $fh $message; 

 $fh->close;              # complete the message and send it
}elsif ($action eq $CLOSE){
        #
        # CLOSE TICKET ON OTRS SYSTEM
        #
        my $cmd = $sshcmd . ” -c 1″ . $type;
        system($cmd);

}elsif ($action eq $ADD_NOTE){
 #
 # ADD TICKET NOTE ON OTRS SYSTEM
 #
        my $cmd = $sshcmd . ” -c 0″ . $type ;
 system($cmd);
}

 

 

 

Categories: Linux, nagois, otrs, tutorial
  1. No comments yet.
  1. No trackbacks yet.

Leave a comment