CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Trying to sort output

 



stumpd
Novice

Feb 23, 2013, 8:35 AM

Post #1 of 14 (1488 views)
Trying to sort output Can't Post

Hey gurus! I have a question on how to sort some output so I can return the correct value. I have tried several different things, including "greping" for different words and trying to sort the output in that fashion, but the problem is based on the names of the volumes in the tape library sometimes the numbers I need to check against are on a different line. It was suggest to me to remove all the carriage returns, then somehow compare the output against the threshold of seconds. Which in this case is 1001. Let me show you the output:

Code
Process Process Description  Status                                            

Number

-------- -------------------- -------------------------------------------------

69 Backup Storage Pool Primary Pool TAPE_BACKUP, Copy Pool TAPE_OFFSITE,

Files Backed Up: 36081, Bytes Backed Up:

27,770,282,615, Unreadable Files: 0, Unreadable

Bytes: 0. Current Physical File (bytes):

8,409,734,275 Waiting for access to input volume

300082 (3116 seconds). Current output volume:

300724.

70 Backup Storage Pool Primary Pool TAPE_BACKUP, Copy Pool TAPE_OFFSITE,

Files Backed Up: 39, Bytes Backed Up:

330,587,389,610, Unreadable Files: 0, Unreadable

Bytes: 0. Current Physical File (bytes):

10,636,058,581 Current input volume: 300082.

Current output volume: 300412.

71 Space Reclamation Offsite Volume(s) (storage pool TAPE_OFFSITE),

Moved Files: 8401, Moved Bytes: 851,107,244,

Unreadable Files: 0, Unreadable Bytes: 0.

Current Physical File (bytes): 4,356,813 Waiting

for access to input volume 300082 (1843

seconds). Current output volume: 300351.

This is the output from a command ran on the AIX command line to get the current processes running in our backup utility. I need to return a value if wait seconds are above 1001. However, since the spacing of these outputs and carriage returns is dynamic based on the name of process waiting what I wrote using grep only works in certain situations and even then I am not sure I am evaluating the string correctly. I use "ge" to do that, which as I understand it, evaluates the numerical value of the string. It is my hope some awesome guru here knows a much better and easier way of doing this, because I am stumped!!! Thank you.

Also I am going to add the code I currently use at the bottom here. Which does not work as it needs too. Thanks again!!!

Code
#!/usr/bin/perl 



my $rundsm = "dsmadmc -id=reports -pa=reports q proc | grep Waiting";

my @output = `$rundsm`;

my $string = "Waiting call (1001";



if (@output gt $string) {

print "Message: Issue Found, Alert Operators\n";

print "Statistic: 1\n";

}

else {

print "Message: No Issues Found\n";

print "Statistic: 0\n";

}

exit 0;



wickedxter
User

Feb 23, 2013, 10:04 AM

Post #2 of 14 (1480 views)
Re: [stumpd] Trying to sort output [In reply to] Can't Post

This can get the seconds found within the data


Code
 
use strict;
use warnings;

my $data = "69 Backup Storage Pool Primary Pool TAPE_BACKUP, Copy Pool TAPE_OFFSITE,

Files Backed Up: 36081, Bytes Backed Up:

27,770,282,615, Unreadable Files: 0, Unreadable

Bytes: 0. Current Physical File (bytes):

8,409,734,275 Waiting for access to input volume

300082 (3116 seconds). Current output volume:

300724.

70 Backup Storage Pool Primary Pool TAPE_BACKUP, Copy Pool TAPE_OFFSITE,

Files Backed Up: 39, Bytes Backed Up:

330,587,389,610, Unreadable Files: 0, Unreadable

Bytes: 0. Current Physical File (bytes):

10,636,058,581 Current input volume: 300082.

Current output volume: 300412.

71 Space Reclamation Offsite Volume(s) (storage pool TAPE_OFFSITE),

Moved Files: 8401, Moved Bytes: 851,107,244,

Unreadable Files: 0, Unreadable Bytes: 0.

Current Physical File (bytes): 4,356,813 Waiting

for access to input volume 300082 (1843

seconds). Current output volume: 300351.";


$data =~ s/\n|\r//g; #remove the \n or \r and make it all one line of data to search thru..

#save all matches then print
my @return2 = $data =~ /(\(\d+\s+seconds\))/g;
print "Found: $_\n\n" for @return2;


I this is what i think you were asking about was getting the seconds from the data.


(This post was edited by wickedxter on Feb 23, 2013, 10:04 AM)


Laurent_R
Veteran / Moderator

Feb 23, 2013, 10:07 AM

Post #3 of 14 (1478 views)
Re: [stumpd] Trying to sort output [In reply to] Can't Post


In Reply To

Code
if (@output gt $string) {



I am not sure of what you are trying to do here, but it is very unlikely to do what you want. You are comparing "ASCIIbetically" a string and an array, which doesn't make much sense to me, amthough I don't really know what tyhe content of the @output array is.

In addition, from what I can gather in your post, your unix command is probably not correct, because the grep will usually not filter the right line.

You don't give enough clear information on what you intend to do, but if you are looking for the number of seconds, it would seem from your sample that looking for an opening parenthesis followed by some digits can give you what you are looking for. So you could possibly read your input with something like this:


Code
my $process_nr; 
while (my $line = <$INPUT>) {
$process_nr = $1 if $line =~ /\s+(\d)+ Backup Storage/;
if ($line =~ /\((\d)+/) {
my $waiting_time = $1;
print "Process $process_nr took $waiting_time seconds to execute.\n" if ($waiting_time > 1001);
}
}

Code
 
EDIT: I had not seen Wickedxter's answer when I posted mine.


(This post was edited by Laurent_R on Feb 23, 2013, 10:08 AM)


FishMonger
Veteran / Moderator

Feb 23, 2013, 10:16 AM

Post #4 of 14 (1469 views)
Re: [Laurent_R] Trying to sort output [In reply to] Can't Post

FYI, cross posted at http://perlmonks.org/?node_id=1020321


(This post was edited by FishMonger on Feb 23, 2013, 10:16 AM)


stumpd
Novice

Feb 23, 2013, 10:17 AM

Post #5 of 14 (1467 views)
Re: [stumpd] Trying to sort output [In reply to] Can't Post

Sorry I was not clear. To answer both your questions, I am trying to run the command:

dsmadmc -id=reports -pa=reports q proc


On the AIX command line. Then get the number of seconds a waiting process is waiting from the output and compare it against 1001 seconds. If greater then 1001 seconds return value 1, if not return value 0.

Sorry my first post was unclear. I am pretty new to this! Thank you for any further input.


FishMonger
Veteran / Moderator

Feb 23, 2013, 10:33 AM

Post #6 of 14 (1458 views)
Re: [stumpd] Trying to sort output [In reply to] Can't Post

Do you need to track which process went over the threshold?


Laurent_R
Veteran / Moderator

Feb 23, 2013, 10:38 AM

Post #7 of 14 (1455 views)
Re: [stumpd] Trying to sort output [In reply to] Can't Post


In Reply To
Thank you for any further input.


For the time being, Wickedxter and myself both have provided possible solutions. It is us that are waiting for a feedback from you as to whether the proposed solutions fit your needs.


stumpd
Novice

Feb 23, 2013, 10:39 AM

Post #8 of 14 (1455 views)
Re: [stumpd] Trying to sort output [In reply to] Can't Post

@Fishmonger:

I do not need to track which process exceeded just the fact that ANY process did. Return value 1 if anything did exceed or return value 0 if they do not.

Thanks!!!!


stumpd
Novice

Feb 23, 2013, 10:57 AM

Post #9 of 14 (1452 views)
Re: [stumpd] Trying to sort output [In reply to] Can't Post

@Laurent_R:

Sorry, first time using any forums! I took suggestions from both you and wickedxter. I have modified my code to look like this based on your suggestions:


Code
use strict; 
use warnings;

my $rundsm = "dsmadmc -id=reports -pa=reports q proc";
$rundsm =~ s/\n|\r//g;
my $seconds = $rundsm =~ /(\(\d+\s+seconds\))/g;

if ($seconds >= 1001) {
print "Message: Issue Found, Alert Operations\n";
print "Statistic: 1\n";
}
else {
print "Message: No Issues Foun\n";
print "Statistic: 0\n";
}
exit 0;


This seems to work correctly, however, I will have to wait until our tape library has an actual wait state to see if it does in fact evaluate the way I think I understand it after you two helped me with examples! Soon as I get a wait state I will confirm on this thread!!!


wickedxter
User

Feb 23, 2013, 11:39 AM

Post #10 of 14 (1446 views)
Re: [stumpd] Trying to sort output [In reply to] Can't Post


Code
use strict;  
use warnings;

my $rundsm = "dsmadmc -id=reports -pa=reports q proc";
$rundsm =~ s/\n|\r//g;
my @seconds = $rundsm =~ /(\(\d+\s+seconds\))/g;

#useing a regex in scalar context($seconds = $rundsm) only returns the last match, useing the array returnes all

#also the regex is returning 1 or more digets followed by \s(space) followed by seconds
#ie: 1336 seconds, 1886 seconds

#loop thru the results
for my $each (@seconds){
$each =~ /(\d+)/; #capture only the seconds

if ($1 >= 1001) {
print "Message: Issue Found, Alert Operations\n";
print "Statistic: 1\n";
} else {
print "Message: No Issues Foun\n";
print "Statistic: 0\n";
}
}
exit 0;



FishMonger
Veteran / Moderator

Feb 23, 2013, 12:06 PM

Post #11 of 14 (1442 views)
Re: [stumpd] Trying to sort output [In reply to] Can't Post


Code
#!/usr/bin/perl 

use strict;
use warnings;
use List::Util qw(max);

my $rundsm = `dsmadmc -id=reports -pa=reports q proc`;
my @seconds = $rundsm =~ /\((\d+)\s+seconds\)/mg;

if (max(@seconds) >= 1001) {
print "Message: Issue Found, Alert Operations\n",
"Statistic: 1\n";
exit 1;
}
else {
print "Message: No Issues Foun\n",
"Statistic: 0\n";
exit 0;
}



(This post was edited by FishMonger on Feb 23, 2013, 12:11 PM)


Laurent_R
Veteran / Moderator

Feb 23, 2013, 1:59 PM

Post #12 of 14 (1433 views)
Re: [stumpd] Trying to sort output [In reply to] Can't Post

@stumpd: just in case you did not pick up what Wickedxter and Fishmonger are saying, I want to stress that your solution does not work correctly because you are checking only one of the times elapsed (the last one), not each of them. So if the duration you are checking is correct (below 1001 seconds), your program will return success ("No issues found"), whereas you might have a failure for some other case.

The solutions provided by Wickedxter and Fishmonger solve this problem.


stumpd
Novice

Feb 24, 2013, 8:25 AM

Post #13 of 14 (1413 views)
Re: [stumpd] Trying to sort output [In reply to] Can't Post

@wickedxter, Fishmonger & Laurent_R:

I have implemented the changes so that all the data will be sorted not just the last entry as explained. We still have not a high wait state in our environment but that usually happens on Monday afternoons. Hopefully I will get a real world test soon! I really do appreciate your help and also helping me to understand more of Perl than I previously did! You folks are awesome!

Very awesome experience for a first post in the Perl community! I would have given all three of you points for answers but I did not see a way to do that like in other forums.

Once I got a real world test I will update the thread.


stumpd
Novice

Feb 26, 2013, 9:09 AM

Post #14 of 14 (1383 views)
Re: [stumpd] Trying to sort output [In reply to] Can't Post

@wickedxter, Fishmonger & Laurent_R:

I just wanted to say "thank you" too all three of you for the help! We had several high wait states in the last two days and it works like a charm!!! I appreciate your help and helping me to understand more about perl! You guys rock.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives