免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 5580 | 回复: 15
打印 上一主题 下一主题

请教高手能不能给我解释一下这个脚本 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2013-10-09 15:22 |只看该作者 |倒序浏览
本帖最后由 Iinvincible 于 2013-10-09 15:26 编辑

root@root$ vmstat 2 3
procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu------
r  b   swpd   free   buff  cache   si   so    bi    bo   in   cs us sy id wa st
2  0 213728  87600 1183808 849440    0    0    86    85    2    0  3  3 92  2  0
0  0 213728  88864 1183808 849460    0    0     0     0 1022  372  5  3 93  0  0
0  0 213728  88740 1183808 849472    0    0     0     2 1014  285  0  0 100  0  0



use strict;
use RRD::Simple;

my $cmd = "/usr/bin/vmstat 2 3";
my $rrdfile = "vmstat-cpu.rrd";
my $rrd = RRD::Simple->new( file => $rrdfile );

my @keys = ();
my %update = ();
open(PH,"-|",$cmd) or die qq{Unable to open file handle PH for command "$cmd": $!};
while (local $_ = <PH>) {
     next if /---/;
     s/^\s+|\s+$//g;
     if (/\d+/ && @keys) {
         @update{@keys} = split(/\s+/,$_);
     } else { @keys = split(/\s+/,$_); }
}

close(PH) or die qq{Unable to close file handle PH for command "$cmd": $!};

my @cpukeys = splice(@keys,-4,4);
my %labels = (wa => "IO wait", id => "Idle", sy => "System", us => "User");

$rrd->create(map { ($_ => "GAUGE") } @cpukeys) unless -f $rrdfile;
$rrd->update(map { ($_ => $update{$_}) } @cpukeys);

我知道这个脚本大概就是去vmstat cpu部分的数据然后画图,但是看不懂加粗的部分,尤其是红色的部分。请高手帮我讲解一下。谢谢啦!

论坛徽章:
0
2 [报告]
发表于 2013-10-09 16:55 |只看该作者
@update{@keys}   ??

论坛徽章:
0
3 [报告]
发表于 2013-10-09 17:26 |只看该作者
回复 2# w123456_cu


    是的,   
if (/\d+/ && @keys) {
         @update{@keys} = split(/\s+/,$_);
     } else { @keys = split(/\s+/,$_); }

论坛徽章:
6
丑牛
日期:2014-03-21 15:42:04子鼠
日期:2014-04-12 11:50:17处女座
日期:2014-09-01 09:25:1115-16赛季CBA联赛之吉林
日期:2015-12-22 14:01:5215-16赛季CBA联赛之广东
日期:2016-03-08 18:49:422016科比退役纪念章
日期:2016-07-06 12:19:55
4 [报告]
发表于 2013-10-09 21:49 |只看该作者
本帖最后由 stanley_tam 于 2013-10-09 21:49 编辑
  1. @update{@keys}是hash切片,跟数组切片类似,如@array[0 .. $index]

  2. 建议你用Data::Dumper模块把数据打印出来,就一目了然了,如

  3. use strict;
  4. # use RRD::Simple;

  5. use Data::Dumper;


  6. my $cmd = "/usr/bin/vmstat 2 3";
  7. # my $rrdfile = "vmstat-cpu.rrd";
  8. # my $rrd = RRD::Simple->new( file => $rrdfile );

  9. my @keys = ();
  10. my %update = ();
  11. open(PH,"-|",$cmd) or die qq{Unable to open file handle PH for command "$cmd": $!};
  12. while (local $_ = <PH>) {
  13.      next if /---/;
  14.      s/^\s+|\s+$//g;
  15.      if (/\d+/ && @keys) {
  16.          @update{@keys} = split(/\s+/,$_);
  17.      } else { @keys = split(/\s+/,$_); }
  18. }
  19. close(PH) or die qq{Unable to close file handle PH for command "$cmd": $!};

  20. my @cpukeys = splice(@keys,-4,4);

  21. print Dumper \@keys;
  22. print Dumper \%update;
  23. print Dumper \@cpukeys;

  24. __END__

  25. my %labels = (wa => "IO wait", id => "Idle", sy => "System", us => "User");

  26. $rrd->create(map { ($_ => "GAUGE") } @cpukeys) unless -f $rrdfile;
  27. $rrd->update(map { ($_ => $update{$_}) } @cpukeys);
复制代码
回复 1# Iinvincible


   

论坛徽章:
8
技术图书徽章
日期:2013-09-30 08:51:28技术图书徽章
日期:2013-12-11 09:26:39白羊座
日期:2013-12-27 15:27:13金牛座
日期:2014-01-06 09:13:05天蝎座
日期:2014-01-21 14:23:28酉鸡
日期:2014-05-09 16:51:12卯兔
日期:2014-08-11 16:49:1515-16赛季CBA联赛之八一
日期:2017-08-14 23:24:57
5 [报告]
发表于 2013-10-09 22:25 |只看该作者
这个就是哈希切片的用法回复 3# Iinvincible


   

论坛徽章:
0
6 [报告]
发表于 2013-10-10 10:09 |只看该作者
For example, consider the bowling scores set individually:
  1. $score{"fred"} = 205;
  2. $score{"barney"} = 195;
  3. $score{"dino"} = 30;
复制代码
This seems rather redundant, and in fact can be shortened to:
  1. ($score{"fred"},$score{"barney"},$score{"dino"}) =
  2.     (205,195,30);
复制代码
But even these seems redundant. Let's use a hash slice :
  1. @score{"fred","barney","dino"} = (205,195,30);
复制代码
There. Much shorter. We can use a hash slice with variable interpolation as well:
  1. @players = qw(fred barney dino);
  2. print "scores are: @score{@players}\n";
复制代码

论坛徽章:
0
7 [报告]
发表于 2013-10-10 16:23 |只看该作者
回复 6# w123456_cu

多谢!通过列子看起来容易些了。

但是这段脚本:
@players = qw(fred barney dino);
print "scores are: @score{@players}\n";

运行后打印结果为:

score are:
没打印出来啊


   

论坛徽章:
0
8 [报告]
发表于 2013-10-10 16:30 |只看该作者
本帖最后由 Iinvincible 于 2013-10-10 16:32 编辑

回复 4# stanley_tam


    多谢啊!明白点了,又学了一招!

但我不明白 if (/\d+/ && @keys) 这是什么意思。d+代表匹配数字,@key是什么意思呢?

论坛徽章:
5
丑牛
日期:2014-01-21 08:26:26卯兔
日期:2014-03-11 06:37:43天秤座
日期:2014-03-25 08:52:52寅虎
日期:2014-04-19 11:39:48午马
日期:2014-08-06 03:56:58
9 [报告]
发表于 2013-10-10 16:35 |只看该作者
强!4,6 楼 很赞啊

论坛徽章:
5
丑牛
日期:2014-01-21 08:26:26卯兔
日期:2014-03-11 06:37:43天秤座
日期:2014-03-25 08:52:52寅虎
日期:2014-04-19 11:39:48午马
日期:2014-08-06 03:56:58
10 [报告]
发表于 2013-10-10 16:50 |只看该作者
请高手帮我讲解一下。
  1. while (local $_ = <PH>) {
复制代码
  1. while (<PH>) {
复制代码
谢谢啦!@stanley_tam@w123456_cu
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP