linux查看perl进程,linux – Perl进程解析

我在perl的linux机器上循环进程.我想显示特定进程的总cpu,但我想显示进程的每个实例的总用量.例如:

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

这样的东西将由用户和这样的过程输出.

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

我知道我需要使用某种类型的哈希但不强大,这是我目前使用的代码.

!/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;

解决方法:

我在您的代码中添加了%users hash-of-hashes.另见:perldoc perldsc

use warnings;

use strict;

my @stats;

my $date=`date +"\%m-\%d-\%Y-\%r"`;

chomp $date;

my %users;

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';

$users{$user}{$cmd} += $cpu;

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;

for my $user (sort keys %users) {

print "$user\n";

print "($users{$user}{$_}): $_\n" for (sort keys %{ $users{$user} });

print "\n";

}

标签:linux,perl

来源: https://codeday.me/bug/20190517/1121398.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值