I am looping through process on a linux box in perl. I want to display total cpu for a particular process, but I want to show total usage for every instance of the process. In example:
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
northriv 10228 0.0 0.2 23692 8084 ? S Sep18 0:00 /usr/local/apache2/bin/httpd -k start
northriv 10229 0.0 0.2 23692 8084 ? S Sep18 0:00 /usr/local/apache2/bin/httpd -k start
northriv 10186 0.0 0.2 23692 8084 ? S Sep18 0:00 /usr/local/apache2/bin/httpd -k start
northriv 10187 0.0 0.2 23692 8084 ? S Sep18 0:00 /usr/local/apache2/bin/httpd -k start
speaktra 25535 0.2 1.0 46788 33212 ? S Sep23 6:04 /usr/local/apache2/bin/httpd -k start
speaktra 25547 0.2 0.8 40320 26712 ? S Sep23 6:21 /usr/local/apache2/bin/httpd -k start
wvneuroc 1570 0.2 0.0 2136 1044 ? S 12:52 0:00 /usr/bin/qpopper -F -S
speaktra 25546 0.2 0.7 35680 22116 ? S Sep23 6:45 /usr/local/apache2/bin/httpd -k start
speaktra 1570 0.2 0.0 2136 1044 ? S 12:52 0:00 /usr/bin/qpopper -F -S
Something like this would then output by user and processs like this.
northriv
(0.0): /usr/local/apache2/bin/httpd
speacktra
(0.6): /usr/local/apache2/bin/httpd
(0.2): /usr/bin/qpopper -F -S
wvneuroc
(0.2): /usr/bin/qpopper -F -S
I know I need to use some type of hash but not strong there, here is the code I am using so far.
!/usr/bin/perl
use strict;
use warnings;
my @stats;
my $date=`date +"\%m-\%d-\%Y-\%r"`;
chomp $date;
my @process_table = `ps aux --sort=\%cpu|sed -e 's/\\s\\+/,/g'`;
for (@process_table)
{ chomp;
$_ =~ s/ / /g;
my ($user,$pid,$cpu,$mem,$cmd)=(split /,/,$_)[0,1,2,3,10];
next if $user eq 'USER';
if($cpu > 10)
{
push(@stats,"$user - WARNING(CPU:$cpu):\t$pid($cmd)\n");
}
if($cpu > 50)
{
push(@stats,"$user - CRITICAL(CPU:$cpu):\t$pid($cmd)\n");
}
}
print $_ for @stats;
You should use P9Y::ProcessTable module for this task.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With