求perl脚本,用于抓取通过top指令获得的CPU信息
答案:3 悬赏:30 手机版
解决时间 2021-03-05 09:30
- 提问者网友:凉末
- 2021-03-04 11:13
求perl脚本,用于抓取通过top指令获得的CPU信息
最佳答案
- 五星知识达人网友:由着我着迷
- 2021-03-04 11:35
#!perl
my @list =`top -n 1`;
foreach my $listfile (@list){
if($listfile =~ /Cpu/g){#cpu :Cpu(s): 0.1%us, 1.0%sy, 0.4%ni, 87.4%id, 10.6%wa, 0.0%hi, 0.5%si, 0.0%st -我使用top得到的目标行,则把Cpu作为了/Cpu/g里面的过滤条件
chomp $listfile;
my $cpuinfo = $listfile;#$cpuinfo 即是所要行
print "cpu :",$cpuinfo, "
";
}
}
my @list =`top -n 1`;
foreach my $listfile (@list){
if($listfile =~ /Cpu/g){#cpu :Cpu(s): 0.1%us, 1.0%sy, 0.4%ni, 87.4%id, 10.6%wa, 0.0%hi, 0.5%si, 0.0%st -我使用top得到的目标行,则把Cpu作为了/Cpu/g里面的过滤条件
chomp $listfile;
my $cpuinfo = $listfile;#$cpuinfo 即是所要行
print "cpu :",$cpuinfo, "
";
}
}
全部回答
- 1楼网友:西岸风
- 2021-03-04 14:48
#!/usr/bin/env perl
use strict;
my $content = qx|top -bn 1|;
my ($reqult) = ($result =~ /CPU-usage:?s*([^s,]+)/);
printf("%s ", $result);
use strict;
my $content = qx|top -bn 1|;
my ($reqult) = ($result =~ /CPU-usage:?s*([^s,]+)/);
printf("%s ", $result);
- 2楼网友:煞尾
- 2021-03-04 13:13
my @data = `top`;
my $targetLine = undef;
foreach my $line ( @data ) {
$targetLine = $line if ( $line =~ /^cpu/i ) ;
last if $targetLine;
}
@parts = split /s+/, $targetLine;
$usage = $parts[1];
print "Usage: $usage$/";
$usage =~ s/%us$//i;
print "Usage: $usage";
my $targetLine = undef;
foreach my $line ( @data ) {
$targetLine = $line if ( $line =~ /^cpu/i ) ;
last if $targetLine;
}
@parts = split /s+/, $targetLine;
$usage = $parts[1];
print "Usage: $usage$/";
$usage =~ s/%us$//i;
print "Usage: $usage";
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯