A tutorial on Config/GenericAgent.pm

Dont create your support topics here! No new topics with questions allowed!

Moderator: crythias

User avatar
crythias
Moderator
Posts: 9920
Joined: 04 May 2010, 18:38
OTRS Version?: 4.0.x
Location: SouthWest Florida, USA
Contact:

A tutorial on Config/GenericAgent.pm

Postby crythias » 10 Jun 2011, 15:37

Sometimes, you'll want to do something outside of the web interface to a bunch of (or specific groups of) tickets via Generic Agent, but you find the web interface, which updates the database (-c db) side of Generic Agent to be not as flexible as you'd need.

For simplicity sake, let's consider taking a Ticket Event and make it happen via Generic Agent.

Why would you do that?

Well, let's say you have made a new Ticket Event, but you want to apply its affects retroactively to tickets already in your database.

More or less, you'll copy the ticket event and rename it inside Kernel/System/GenericAgent
Make sure you update the package name, in the code, too.

Sometimes GenericAgent doesn't have all the Objects you need, so you'll need to

Code: Select all

use Kernel::System::State

and after you've

Code: Select all

    # get needed objects
    for (
        qw( ConfigObject DBObject LogObject MainObject TicketObject TimeObject EncodeObject)
        )

you'll want to also create the Object references that GenericAgent doesn't support out of the box:

Code: Select all

    $Self->{StateObject} = Kernel::System::State->new( %{$Self} );


What does GenericAgent support? The list above, and the API says:

Code: Select all

create an object

    use Kernel::Config;
    use Kernel::System::Encode;
    use Kernel::System::Log;
    use Kernel::System::Main;
    use Kernel::System::DB;
    use Kernel::System::Time;
    use Kernel::System::Queue;
    use Kernel::System::Ticket;
    use Kernel::System::GenericAgent;


So State is definitely one that you'd need to add, if you use it.

If you have code in the Event that says, "Do _this_ if the Event is TicketUpdate, and do _this_ if the Event is TicketCreate", you may decide to change that logic to reflect the state of the ticket as it exists at run of the generic agent.

When you get down to it, the sub new and sub Run are otherwise the same. :) Of course, that's what an API is all about.

Then, you'll need to add an entry into Kernel/Config/GenericAgent.pm or, optionally, your own config file:

This is rather straight forward, and you'll add an entry like this before the }; end of config options :

Code: Select all

    # --
    # [name of job] -> A job to run on a bunch of tickets.
    # --
    'TicketEventAsGenericAgent' => {
        #closed tickets are unlocked.
        #you always need a filter. Locks => ['unlock', 'lock'] is/should be all tickets.
        Locks => ['unlock'],
        New => {
            Module => 'Kernel::System::GenericAgent::TicketEventAsGenericAgent',
        },
    },


For your own config simply copy GenericAgent.pm to GenericAgentYourInfoHere.pm and add the entry. Don't forget to change the package line in the new file.


Since this is not in the db, it will need to either be run from the command line or via cron.
By default, otrs/var/cron/generic_agent.dist is in, but needs to have .dist removed if you want to run the job via cron. If the entr[y|ies] [is|are] in GenericAgent.pm, just run otrs/bin/otrs.GenericAgent.pl to run it manually, but if you need a specific config file, use the

Code: Select all

-c "Kernel::Config::GenericAgentYourInfoHere"
(see the otrs/var/cron/generic_agent.dist)

Note that there's also a generic_agent-database.dist, which when the .dist is removed will run GenericAgent every 10 minutes against the valid GenericAgent entries in the database. This appears to supersede the per-entry timing stored in the db.

Note: to be clear, when otrs.GenericAgent.pl runs, all of the entries in the config file it references will be run. If you want to separate times for a specific entry, you will want to separate the config for that entry.

Update: If you're going to apply an API to closed tickets, note that the ticket_history will be adjusted with the last closed state and timestamped with the action that was applied. This will hurt your reporting because closed times affected by API calls that leave history will now be set to the timestamp of when Generic Agent was run.
example:
Ticket was closed on 1/1/2010.
You run a ticket set action that affects closed tickets (Here, retroactively on closed tickets).
Ticket now is "closed" at time of run of Generic Agent.

To fix that may require removal of that/those ticket history action[s] via SQL or other means.

Questions directly related to this tutorial (especially, I don't understand ...) will be answered here. Questions for *your* GenericAgent should be made in the forums.
OTRS 4.0.x (private/testing/public) on Linux with MySQL database. Also on github.
Please edit your signature to include your OTRS version, Operating System, and database type.
Click Subscribe Topic below to get notifications. Consider amending your topic title to include [SOLVED] if it is so.
Need help? Before you ask

OTRSRDNewbie
OTRS expert
Posts: 69
Joined: 29 Apr 2016, 10:23
OTRS Version?: 3.2, 5
Real Name: Dennis Cua
Company: N/A

Re: A tutorial on Config/GenericAgent.pm

Postby OTRSRDNewbie » 24 Jun 2016, 09:19

The web interface GenericAgent has a event driven execution and schedule driven execution. How can I replicate this if I'm going to create a GenericAgent on Config/GenericAgent.pm?

I am hoping this will resolve the issue of caching. There is this issue now that the dynamic field which holds the resolve date empty when a user performs a ticket search and outputs it as csv.

I checked it and it seems that there is a delay when the change reflects. I think this is due to the caching. I created test tickets and resolve them on database contains the resolve date. But when I search the ticket and output it to csv the resolve date is empty. But I notice after waiting for 5 minutes or 10 minutes the resolve date finally shows up.

Before the resolve date is saved by a GenericAgent by executing a custom module. The custom module directly saves the resolve date on the database.

does a custom module executed by a GenericAgent does not get its changes reflected at once? I'm new to otrs so please pardon me.

How can I make an event driven execution similar or schedule driven execution similar to the web interface?

User avatar
crythias
Moderator
Posts: 9920
Joined: 04 May 2010, 18:38
OTRS Version?: 4.0.x
Location: SouthWest Florida, USA
Contact:

Re: A tutorial on Config/GenericAgent.pm

Postby crythias » 24 Jun 2016, 15:56

OTRSRDNewbie wrote:How can I make an event driven execution similar or schedule driven execution similar to the web interface?

viewtopic.php?t=10090
OTRS 4.0.x (private/testing/public) on Linux with MySQL database. Also on github.
Please edit your signature to include your OTRS version, Operating System, and database type.
Click Subscribe Topic below to get notifications. Consider amending your topic title to include [SOLVED] if it is so.
Need help? Before you ask

OTRSRDNewbie
OTRS expert
Posts: 69
Joined: 29 Apr 2016, 10:23
OTRS Version?: 3.2, 5
Real Name: Dennis Cua
Company: N/A

Re: A tutorial on Config/GenericAgent.pm

Postby OTRSRDNewbie » 26 Jun 2016, 20:01

I have tried your tutorial not sure where I made a mistake but the time I keep getting is January 1, 1970. Should I be entering something in SystemTime() for it to be this current date?

Code: Select all

    use Kernel::Config;
    use Kernel::System::Encode;
    use Kernel::System::Log;
    use Kernel::System::Time;

    my $ConfigObject = Kernel::Config->new();
    my $EncodeObject = Kernel::System::Encode->new(
        ConfigObject => $ConfigObject,
    );
    my $LogObject = Kernel::System::Log->new(
        ConfigObject => $ConfigObject,
        EncodeObject => $EncodeObject,
    );
    my $TimeObject = Kernel::System::Time->new(
        ConfigObject => $ConfigObject,
        LogObject    => $LogObject,
    );
   
    my $SystemTime = $TimeObject->SystemTime();
    my $TimeStamp = $TimeObject->SystemTime2TimeStamp(
        SystemTime => $SystemTime
    );
   
    # --
    # [name of job] -> A job to run on a bunch of tickets.
    # --
    'SaveResolveDate' => {
        #closed tickets are unlocked.
        #you always need a filter. Locks => ['unlock', 'lock'] is/should be all tickets.
        State => ['Resolved'],
        New => {
            DynamicField_ResolvedTime => localtime($TimeStamp)->strftime('%F %T');
        },
    },
   

OTRSRDNewbie
OTRS expert
Posts: 69
Joined: 29 Apr 2016, 10:23
OTRS Version?: 3.2, 5
Real Name: Dennis Cua
Company: N/A

Re: A tutorial on Config/GenericAgent.pm

Postby OTRSRDNewbie » 27 Jun 2016, 09:02

Can I do something like the one below in a Generic Agent Front End Tool - Ticket Action? I know this doesn't work. But may something of the sort can be done?

Image

reneeb
OTRS guru
Posts: 4350
Joined: 13 Mar 2011, 09:54
OTRS Version?: 3.3.x
Company: Perl-Services.de
Contact:

Re: A tutorial on Config/GenericAgent.pm

Postby reneeb » 28 Jun 2016, 08:27

No, for that you need a custom module...
Perl / OTRS development: http://perl-services.de
Free OTRS add ons from the community: http://opar.perl-services.de
Commercial add ons: http://feature-addons.de

OTRSRDNewbie
OTRS expert
Posts: 69
Joined: 29 Apr 2016, 10:23
OTRS Version?: 3.2, 5
Real Name: Dennis Cua
Company: N/A

Re: A tutorial on Config/GenericAgent.pm

Postby OTRSRDNewbie » 30 Jun 2016, 13:30

Could anyone point me on the direction I should be researching or what I should be looking for? I'm trying to create a GenericAgent that would close a ticket after it is resolve in x working days.

Currently I am able to close resolved tickets after x days. I know there is a perl module for business days. But there are country specific or state specific holidays, fixed holidays, one time President/Prime Minister/... declared holiday, holidays that have no fixed date like holy week, shiftings.

I know OTRS has those features for escallations computations. Are there otrs libraries/functions to make use of those?

We have custom defined states but reuse the state type so I'm not certain I can use the escallation of otrs or itsm.

User avatar
crythias
Moderator
Posts: 9920
Joined: 04 May 2010, 18:38
OTRS Version?: 4.0.x
Location: SouthWest Florida, USA
Contact:

Re: A tutorial on Config/GenericAgent.pm

Postby crythias » 01 Jul 2016, 18:44

Please don't use this moderated forum (HowTos) for discussion on implementation. Kindly use another (Help?) forum on this site.
OTRS 4.0.x (private/testing/public) on Linux with MySQL database. Also on github.
Please edit your signature to include your OTRS version, Operating System, and database type.
Click Subscribe Topic below to get notifications. Consider amending your topic title to include [SOLVED] if it is so.
Need help? Before you ask


Return to “Howtos”

Who is online

Users browsing this forum: No registered users and 1 guest