Old output from regex.

S

sunckell

Hello everyone,

I am getting some strange output from a regex. Wondering if anyone
can see what I am doing wrong. I am on a Solaris 8 server, running
perl 5.8

sub process_measurements{
my $cmd = "/usr/ucb/ps -auwwx";
my @trbl_pids;

open PS, "$cmd |" or die "Cannot open $cmd:$!\n";
while(<PS>){
next if /^USER/;

/^
(\w+) # user of process -- 1
\s+
(\d+) # pid of process -- 2
\s+
(\w+\.\w) # cpu percentage of process -- 3
\s+
(\w+\.\w) # memory percentage -- 4
\s+
(\w+) # virtual memory size -- 5
\s+
(\w+) # resident size of process -- 6
\s+
(\?|\w+\/\w+) # associated tty -- 7
\s+
(\w) # state of process -- 8
\s+
(.*) # start, time, and command -- 9

$/x or do
{
# this occurs when %MEM SZ ans RSS are wider than
# the column width and runs together. Which means
# they are higher usage processes. Will account
# for these separately.
warn "WARNING: line not in processable format:
$_\n";
my $user = $1;
my $pid = $2;
my $cpu = $3;
print "$pid $cpu $user\n";
push(@trbl_pids, $pid);
next;
}
}
close (PS);
print "TROUBLE: @trbl_pids\n";
}


Basically all I am doing is reading a ps output. When I print $user
and $pid I only get the first letter\digit of the field.

For example if root owns a process, I only get the letter r when I
print $user, where I should get root.


Anyone see my mistake, or am I over looking something in reading the
output?



Thanks,
sunckell
 
P

Paul Lalli

sunckell said:
I am getting some strange output from a regex. Wondering if anyone
can see what I am doing wrong. I am on a Solaris 8 server, running
perl 5.8

sub process_measurements{
my $cmd = "/usr/ucb/ps -auwwx";
my @trbl_pids;

open PS, "$cmd |" or die "Cannot open $cmd:$!\n";
while(<PS>){
next if /^USER/;

/^
(\w+) # user of process -- 1
\s+
(\d+) # pid of process -- 2
\s+
(\w+\.\w) # cpu percentage of process -- 3
\s+
(\w+\.\w) # memory percentage -- 4
\s+
(\w+) # virtual memory size -- 5
\s+
(\w+) # resident size of process -- 6
\s+
(\?|\w+\/\w+) # associated tty -- 7
\s+
(\w) # state of process -- 8
\s+
(.*) # start, time, and command -- 9

$/x or do

"or" means that the following block will be executed if the above
pattern did NOT match.
{
# this occurs when %MEM SZ ans RSS are wider than
# the column width and runs together. Which means
# they are higher usage processes. Will account
# for these separately.
warn "WARNING: line not in processable format:
$_\n";
my $user = $1;
my $pid = $2;
my $cpu = $3;

If the above pattern didn't match (which is the only way to be in this
block), $1, $2, and $3 were not set. The values of these variables
whatever they were after the last *successful* pattern match.
print "$pid $cpu $user\n";
push(@trbl_pids, $pid);
next;
}
}
close (PS);
print "TROUBLE: @trbl_pids\n";
}


Basically all I am doing is reading a ps output.

Have you considered using an already-built solution, like
Proc::processTable ?
When I print $user
and $pid I only get the first letter\digit of the field.

For example if root owns a process, I only get the letter r when I
print $user, where I should get root.


Anyone see my mistake, or am I over looking something in reading the
output?

You have a logic error. You can't say "if this pattern doesn't match,
print out the values of the successfully matched pattern".

Paul Lalli
 
S

sunckell

Thanks Paul,

Redefining the regex in the failed loop did it. As for
Proc::processTable, not every server on our network has perl 5.6.0 and
above. I am trying to write it so I can drop it on any server if need
be.


thanks again
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top