How to access content for an attachment in scrip?

Hi. I’m trying to customize the scrip shown at the bottom of this wiki page: http://requesttracker.wikia.com/wiki/AutoCloseOnNagiosRecoveryMessages

I know there is an extension that does merging, but I want to do some other things with the Nagios variables I have attached as a plain text file to the ticket that Nagios creates in RT.
The first few lines in the scrip attempt to get the content of an attachment and put it in a variable named $content. I suspected that this was not working, so I added a debug line to dump the contents of $contents and it is empty.

Here is the scrip code.
my $T_Obj = $self->TicketObj;
my $AttachObj = $self->TransactionObj->Attachments->First;
my $content = $AttachObj->Content;
$RT::Logger->debug("Contents: " . $content);

The log output shows:
[Wed Jul 6 19:47:49 2011] [debug]: Contents: ((eval 3934):4)

The attachment is attached to the ticket. I can see it in the web ui and can open and read it with no problems. Any idea what I’m doing wrong here?

Thanks

John

Using the rt console, I can see that there are actually 3 attachments, even though the I only sent one and the web ui only shows 1, so I’m sure the problem is that I’m selecting the wrong attachment.

Here’s what I see from the console.
./rt show 2075/attachments
19671: (Unnamed) (multipart/mixed / 0b),
19672: (Unnamed) (text/plain / 305b),
19673: env-vars.txt (application/octet-stream / 16.8k)

The attachment I would like will always be named env-vars.txt. How can I get the contents for the attachment name env-vars.txt?

Thanks

JohnFrom: John Alberts <john.alberts@exlibrisgroup.commailto:john.alberts@exlibrisgroup.com>
Date: Wed, 6 Jul 2011 20:06:34 +0000
To: “rt-users@lists.bestpractical.commailto:rt-users@lists.bestpractical.com” <rt-users@lists.bestpractical.commailto:rt-users@lists.bestpractical.com>
Subject: [rt-users] how to access content for an attachment in scrip?

Hi. I’m trying to customize the scrip shown at the bottom of this wiki page: http://requesttracker.wikia.com/wiki/AutoCloseOnNagiosRecoveryMessages

I know there is an extension that does merging, but I want to do some other things with the Nagios variables I have attached as a plain text file to the ticket that Nagios creates in RT.
The first few lines in the scrip attempt to get the content of an attachment and put it in a variable named $content. I suspected that this was not working, so I added a debug line to dump the contents of $contents and it is empty.

Here is the scrip code.
my $T_Obj = $self->TicketObj;
my $AttachObj = $self->TransactionObj->Attachments->First;
my $content = $AttachObj->Content;
$RT::Logger->debug("Contents: " . $content);

The log output shows:
[Wed Jul 6 19:47:49 2011] [debug]: Contents: ((eval 3934):4)

The attachment is attached to the ticket. I can see it in the web ui and can open and read it with no problems. Any idea what I’m doing wrong here?

Thanks

John

-------- 2011 Training: http://bestpractical.com/services/training.html

I’ve found several unanswered questions in the mail list archives with similar questions about looping through attachments.
I saw several times it suggested to loop through attachments using something like
while ( my $AttachObj = $self->TransactionObj->Attachments->Next ) {
# do something with $AttachObj
}

This code doesn’t work because ->Next doesn’t exist for the Attachment object. Is there any other way to do this? Sorry if this is incredibly simple to do. I’m not familiar with Perl and try to poke around and see if I can figure it out.

John Alberts
Cloud Optimization Engineer
Ex Libris (USA) Inc.
1350 E. Touhy Ave. Suite 200 East
Des Plaines, IL 60018
Phone: 1-219-979-6560

Follow Ex Libris on Twitter: @exlibrisgrouphttp://twitter.com/ExLibrisGroupFrom: John Alberts <john.alberts@exlibrisgroup.commailto:john.alberts@exlibrisgroup.com>
Date: Wed, 6 Jul 2011 22:01:47 +0000
To: “rt-users@lists.bestpractical.commailto:rt-users@lists.bestpractical.com” <rt-users@lists.bestpractical.commailto:rt-users@lists.bestpractical.com>
Subject: Re: [rt-users] how to access content for an attachment in scrip?

Using the rt console, I can see that there are actually 3 attachments, even though the I only sent one and the web ui only shows 1, so I’m sure the problem is that I’m selecting the wrong attachment.

Here’s what I see from the console.
./rt show 2075/attachments
19671: (Unnamed) (multipart/mixed / 0b),
19672: (Unnamed) (text/plain / 305b),
19673: env-vars.txt (application/octet-stream / 16.8k)

The attachment I would like will always be named env-vars.txt. How can I get the contents for the attachment name env-vars.txt?

Thanks

John

From: John Alberts <john.alberts@exlibrisgroup.commailto:john.alberts@exlibrisgroup.com>
Date: Wed, 6 Jul 2011 20:06:34 +0000
To: “rt-users@lists.bestpractical.commailto:rt-users@lists.bestpractical.com” <rt-users@lists.bestpractical.commailto:rt-users@lists.bestpractical.com>
Subject: [rt-users] how to access content for an attachment in scrip?

Hi. I’m trying to customize the scrip shown at the bottom of this wiki page: http://requesttracker.wikia.com/wiki/AutoCloseOnNagiosRecoveryMessages

I know there is an extension that does merging, but I want to do some other things with the Nagios variables I have attached as a plain text file to the ticket that Nagios creates in RT.
The first few lines in the scrip attempt to get the content of an attachment and put it in a variable named $content. I suspected that this was not working, so I added a debug line to dump the contents of $contents and it is empty.

Here is the scrip code.
my $T_Obj = $self->TicketObj;
my $AttachObj = $self->TransactionObj->Attachments->First;
my $content = $AttachObj->Content;
$RT::Logger->debug("Contents: " . $content);

The log output shows:
[Wed Jul 6 19:47:49 2011] [debug]: Contents: ((eval 3934):4)

The attachment is attached to the ticket. I can see it in the web ui and can open and read it with no problems. Any idea what I’m doing wrong here?

Thanks

John

-------- 2011 Training: http://bestpractical.com/services/training.html
-------- 2011 Training: http://bestpractical.com/services/training.html

I’ve found several unanswered questions in the mail list archives with
similar questions about looping through attachments.
I saw several times it suggested to loop through attachments using
something like
while ( my $AttachObj = $self->TransactionObj->Attachments->Next ) {

do something with $AttachObj

}

This code doesn’t work because ->Next doesn’t exist for the Attachment
object. Is there any other way to do this? Sorry if this is incredibly
simple to do. I’m not familiar with Perl and try to poke around and see
if I can figure it out.

Take a look at the code I put in my question "Faking 

{$Transaction->Content()}." I still do not have an answer to my own
question but it might be useful to you: see what I’ve done to locate all
attachments. The multipart/mixed part AFAIK is just the list of all the
other attachments or something like that. In other words, ignore it.
About env-vars.txt, is it a plain text file or, say, a Windows one (with
CRLF)? If that is the case, you might be able to select it (see the if
statements I did for ideas) and print it out.

Thanks for the tip. It led me in the right direction and I ended up with
this, which finally works.

my $T_Obj = $self->TicketObj;
my $a = RT::Attachments->new($self->TransactionObj->CurrentUser);
my $b = RT::Attachments->new($self->TransactionObj->CurrentUser);
my $AttachObj = $self->TransactionObj->Attachments;
while ( $a = $AttachObj->Next ) {
$b = $a;
next unless $a->Filename eq “env-vars.txt”;
}
my $content = $b->Content;

I don’t understand why I had to do this, but I had to assign $b = $a in
order to get access to the attachment content after I was outside of the
loop. If I try $a->Content, I always get an error. I assume it has to do
with the scope of $a, but my Perl skills are close to nil, and I couldn¹t
figure it out.

JohnOn 7/6/11 11:24 PM, “Mauricio Tavares” raubvogel@gmail.com wrote:

On 07/06/2011 08:52 PM, John Alberts wrote:

I’ve found several unanswered questions in the mail list archives with
similar questions about looping through attachments.
I saw several times it suggested to loop through attachments using
something like
while ( my $AttachObj = $self->TransactionObj->Attachments->Next ) {

do something with $AttachObj

}

This code doesn’t work because ->Next doesn’t exist for the Attachment
object. Is there any other way to do this? Sorry if this is incredibly
simple to do. I’m not familiar with Perl and try to poke around and see
if I can figure it out.

Take a look at the code I put in my question “Faking
{$Transaction->Content()}.” I still do not have an answer to my own
question but it might be useful to you: see what I’ve done to locate all
attachments. The multipart/mixed part AFAIK is just the list of all the
other attachments or something like that. In other words, ignore it.
About env-vars.txt, is it a plain text file or, say, a Windows one (with
CRLF)? If that is the case, you might be able to select it (see the if
statements I did for ideas) and print it out.


John Alberts

Cloud Optimization Engineer

Ex Libris (USA) Inc.
1350 E. Touhy Ave. Suite 200 East
Des Plaines, IL 60018
Phone: 1-219-979-6560

Follow Ex Libris on Twitter: @exlibrisgroup
http://twitter.com/ExLibrisGroup

From: John Alberts <john.alberts@exlibrisgroup.com
mailto:john.alberts@exlibrisgroup.com>
Date: Wed, 6 Jul 2011 22:01:47 +0000
To: “rt-users@lists.bestpractical.com
mailto:rt-users@lists.bestpractical.com
<rt-users@lists.bestpractical.com
mailto:rt-users@lists.bestpractical.com>
Subject: Re: [rt-users] how to access content for an attachment in
scrip?

Using the rt console, I can see that there are actually 3 attachments,
even though the I only sent one and the web ui only shows 1, so I’m sure
the problem is that I’m selecting the wrong attachment.

Here’s what I see from the console.
./rt show 2075/attachments
19671: (Unnamed) (multipart/mixed / 0b),
19672: (Unnamed) (text/plain / 305b),
19673: env-vars.txt (application/octet-stream / 16.8k)

The attachment I would like will always be named env-vars.txt. How can I
get the contents for the attachment name env-vars.txt?

Thanks

John

From: John Alberts <john.alberts@exlibrisgroup.com
mailto:john.alberts@exlibrisgroup.com>
Date: Wed, 6 Jul 2011 20:06:34 +0000
To: “rt-users@lists.bestpractical.com
mailto:rt-users@lists.bestpractical.com
<rt-users@lists.bestpractical.com
mailto:rt-users@lists.bestpractical.com>
Subject: [rt-users] how to access content for an attachment in scrip?

Hi. I’m trying to customize the scrip shown at the bottom of this wiki
page:
http://requesttracker.wikia.com/wiki/AutoCloseOnNagiosRecoveryMessages

I know there is an extension that does merging, but I want to do some
other things with the Nagios variables I have attached as a plain text
file to the ticket that Nagios creates in RT.
The first few lines in the scrip attempt to get the content of an
attachment and put it in a variable named $content. I suspected that
this was not working, so I added a debug line to dump the contents of
$contents and it is empty.

Here is the scrip code.
my $T_Obj = $self->TicketObj;
my $AttachObj = $self->TransactionObj->Attachments->First;
my $content = $AttachObj->Content;
$RT::Logger->debug("Contents: " . $content);

The log output shows:
[Wed Jul 6 19:47:49 2011] [debug]: Contents: ((eval 3934):4)

The attachment is attached to the ticket. I can see it in the web ui and
can open and read it with no problems. Any idea what I’m doing wrong
here?

Thanks

John

-------- 2011 Training: http://bestpractical.com/services/training.html
-------- 2011 Training: http://bestpractical.com/services/training.html


2011 Training: http://bestpractical.com/services/training.html


2011 Training: http://bestpractical.com/services/training.html

Thanks for the tip. It led me in the right direction and I ended up with
this, which finally works.

my $T_Obj = $self->TicketObj;
my $a = RT::Attachments->new($self->TransactionObj->CurrentUser);
my $b = RT::Attachments->new($self->TransactionObj->CurrentUser);
my $AttachObj = $self->TransactionObj->Attachments;
while ( $a = $AttachObj->Next ) {
$b = $a;
next unless $a->Filename eq “env-vars.txt”;
}
my $content = $b->Content;

The above will fail when env-vars.txt isn’t the last attachment RT
parsed. Try this (untested) much simpler version:

my $attachments = $self->TransactionObj->Attachments;
my $content;
while (my $attach = $attachments->Next) {
    if ($attach->Filename eq "env-vars.txt") {
        $content = $attach->Content;
        last; # we found it, no need to keep looking
    }
}
# use $content for whatever you'd like here

There are better, faster ways to do this than looping over the
attachments, but looping is the easiest to understand.

Thomas

Thank you. I’ll give this a try tomorrow. I figured there had to be a
simpler way to get what I wanted, but I am just fumbling around trying to
figure things out. It actually took me almost 5 hours of trial and error
to get what I have working.

JohnOn 7/7/11 7:25 PM, “Thomas Sibley” trs@bestpractical.com wrote:

On 07/07/2011 07:21 PM, John Alberts wrote:

Thanks for the tip. It led me in the right direction and I ended up
with
this, which finally works.

my $T_Obj = $self->TicketObj;
my $a = RT::Attachments->new($self->TransactionObj->CurrentUser);
my $b = RT::Attachments->new($self->TransactionObj->CurrentUser);
my $AttachObj = $self->TransactionObj->Attachments;
while ( $a = $AttachObj->Next ) {
$b = $a;
next unless $a->Filename eq “env-vars.txt”;
}
my $content = $b->Content;

The above will fail when env-vars.txt isn’t the last attachment RT
parsed. Try this (untested) much simpler version:

my $attachments = $self->TransactionObj->Attachments;
my $content;
while (my $attach = $attachments->Next) {
if ($attach->Filename eq “env-vars.txt”) {
$content = $attach->Content;
last; # we found it, no need to keep looking
}
}

use $content for whatever you’d like here

There are better, faster ways to do this than looping over the
attachments, but looping is the easiest to understand.

Thomas


2011 Training: http://bestpractical.com/services/training.html

Just checking John, if this worked. We have a problem with attachments as well - we are not able to search for non-text attachments. The search just won’t return anything that is non-text.

Barbara Jennings
Sandia National Laboratories - Albuquerque
Organization 6924 - NISAC Support
(505)845-8554
bjjenni@sandia.gov