Modify NewTicket.pm to run bash script

English! place to talk about development, programming and coding
Post Reply
sfault
Znuny newbie
Posts: 8
Joined: 21 Jun 2012, 11:57
Znuny Version: 3.0.9
Real Name: Ara

Modify NewTicket.pm to run bash script

Post by sfault »

Hi. I'm new to OTRS and I'm trying to modify NewTicket.pm to run a bash script whenever a new ticket is created. It's not working, though. I'm using Perl's exec command and the bash script that I'm trying to run is inside the same folder as NewTicket.pm (PostMaster). I've checked the bash script's permissions and owner and I'm pretty sure I did the right thing with it. Anyway, am I right to edit the NewTicket.pm file in the first place? Or am I supposed to be editing something else entirely? Thanks in advance! :)
jojo
Znuny guru
Posts: 15020
Joined: 26 Jan 2007, 14:50
Znuny Version: Git Master
Contact:

Re: Modify NewTicket.pm to run bash script

Post by jojo »

The only NewTicket.pm is a Postmastermodule so it will be triggered on new inbound emails
"Production": OTRS™ 8, OTRS™ 7, STORM powered by OTRS
"Testing": ((OTRS Community Edition)) and git Master

Never change Defaults.pm! :: Blog
Professional Services:: http://www.otrs.com :: enjoy@otrs.com
sfault
Znuny newbie
Posts: 8
Joined: 21 Jun 2012, 11:57
Znuny Version: 3.0.9
Real Name: Ara

Re: Modify NewTicket.pm to run bash script

Post by sfault »

Ohhhh. Thanks for that! So that's why whatever code I inserted in it never seemed to run. So what file should I be editing? Thanks again in advance!
jojo
Znuny guru
Posts: 15020
Joined: 26 Jan 2007, 14:50
Znuny Version: Git Master
Contact:

Re: Modify NewTicket.pm to run bash script

Post by jojo »

The best thin is that you create an event module an register it. So on updates it won't be overwritten.

Have a look on the files in Kernel/System/Ticket/Event/ (especially on Test.pm)

Please also read the devel manual (http://docs.otrs.org)
"Production": OTRS™ 8, OTRS™ 7, STORM powered by OTRS
"Testing": ((OTRS Community Edition)) and git Master

Never change Defaults.pm! :: Blog
Professional Services:: http://www.otrs.com :: enjoy@otrs.com
sfault
Znuny newbie
Posts: 8
Joined: 21 Jun 2012, 11:57
Znuny Version: 3.0.9
Real Name: Ara

Re: Modify NewTicket.pm to run bash script

Post by sfault »

Hi. I tried making my own module and used Test.pm and crythias's tutorial (viewtopic.php?f=60&t=10090) as references. However, I still can't make it work. My trigger is the TicketCreate Event. This is my XML file (/Kernel/Config/Files/MyTest.xml):

Code: Select all

<?xml version="1.0" encoding="utf-8" ?>
<otrs_config version="1.0" init="Changes">
    <ConfigItem Name="Ticket::EventModulePost###MyTest" Required="0" Valid="1">
        <Description Lang="en"></Description>
        <Description Lang="de"></Description>
        <Group>MyTest</Group>
        <SubGroup>EventModule</SubGroup>
        <Setting>
            <Hash>
                <Item Key="Module">Kernel::System::Ticket::Event::MyTest</Item>
                <Item Key="Event">(TicketCreate)</Item>
            </Hash>
        </Setting>
    </ConfigItem>
</otrs_config>
And this is MyTest.pm: (/Kernel/System/Ticket/Event/MyTest.pm)

Code: Select all


# --
# Kernel/System/Ticket/Event/EventModulePostTemplate.pm - event module
# Copyright (C) 2001-2010 xxx, http://otrs.org/
# --
# $Id: EventModulePostTemplate.pm,v 1.1 2010/05/10 17:56:20 sb Exp $
# --
# This software comes with ABSOLUTELY NO WARRANTY. For details, see
# the enclosed file COPYING for license information (AGPL). If you
# did not receive this file, see http://www.gnu.org/licenses/agpl.txt.
# --

package Kernel::System::Ticket::Event::MyTest;

use strict;
use warnings;

use vars qw($VERSION);
$VERSION = qw($Revision: 1.1 $) [1];

sub new {
    my ( $Type, %Param ) = @_;

    # allocate new hash for object
    my $Self = {};
    bless( $Self, $Type );

    # get needed objects
    for (
        qw(
        ConfigObject CustomerGroupObject CustomerUserObject DBObject EncodeObject GroupObject
        HTMLUtilsObject LinkObject LockObject LogObject LoopProtectionObject MainObject
        PriorityObject QueueObject SendmailObject ServiceObject SLAObject StateObject
        TicketObject TimeObject TypeObject UserObject ValidObject
        )
        )
    {
        $Self->{$_} = $Param{$_} || die "Got no $_!";
    }

    return $Self;
}

sub Run {
    my ( $Self, %Param ) = @_;

    # check needed stuff
    for (qw(TicketID Event Config)) {
        if ( !$Param{$_} ) {
            $Self->{LogObject}->Log( Priority => 'error', Message => "Need $_!" );
            return;
        }
    }

    return 1 if $Param{Event} ne 'TicketCreate';

    my %Ticket = $Self->{TicketObject}->TicketGet(
        TicketID => $Param{Data}->{TicketID}
        UserID   => 1,
    );
    return 1 if !%Ticket;
    return 1 if $Ticket{State} eq 'Open';

    # do some stuff
#    $Self->{TicketObject}->HistoryAdd(
#        TicketID     => $Param{TicketID},
#        CreateUserID => 1,
#        HistoryType  => 'Misc',
#        Name         => 'New ticket with state other than Open!',
#    );


	exec("bash hello.sh $Param{Data}->{TicketID}");
    return 1;
}

1;
And this is my hello.bash (/Kernel/System/Ticket/Event/hello.sh)

Code: Select all

#!/bin/bash

echo Hello World!
echo $1 > test.txt
echo $2 >> test.txt
date >> test.txt
exit 0

Is there any way to find what kind of errors I am encountering? Maybe a log file or something? Or maybe you can see what is/are wrong with my code/s? Thanks a lot!


EDIT: I tried modifying ForceUnlock.pm instead and got it working. However, I still can't call a shell script inside the .pm file. I've tried these two:

Code: Select all

#exec("bash hello.sh arg1 arg2");
and

Code: Select all

system("bash", "hello.sh", "arg1", "arg2");
Both are working when called from within an ordinary .pm file (and .pl) that I use when testing (command line). Any idea(s) why it's not working in OTRS? Thank you very much!
jojo
Znuny guru
Posts: 15020
Joined: 26 Jan 2007, 14:50
Znuny Version: Git Master
Contact:

Re: Modify NewTicket.pm to run bash script

Post by jojo »

The log is written to the apache error log and to the syslog
"Production": OTRS™ 8, OTRS™ 7, STORM powered by OTRS
"Testing": ((OTRS Community Edition)) and git Master

Never change Defaults.pm! :: Blog
Professional Services:: http://www.otrs.com :: enjoy@otrs.com
sfault
Znuny newbie
Posts: 8
Joined: 21 Jun 2012, 11:57
Znuny Version: 3.0.9
Real Name: Ara

Re: Modify NewTicket.pm to run bash script

Post by sfault »

Found it. Thanks for that. I did not find any errors related to what I was doing (especially in the apache logs). And I still can't run my bash script from inside the Perl modules. :(
Post Reply