Chinaunix首页 | 论坛 | 博客
  • 博客访问: 200339
  • 博文数量: 17
  • 博客积分: 2459
  • 博客等级: 大尉
  • 技术积分: 240
  • 用 户 组: 普通用户
  • 注册时间: 2009-12-02 21:32
文章分类

全部博文(17)

文章存档

2012年(6)

2011年(16)

2009年(1)

分类: Python/Ruby

2011-04-06 20:50:16

  1. #!/usr/bin/perl

  2. #This script is used to create cpu load graph

  3. use strict;
  4. use GD::Graph::lines;

  5. my @tmp;
  6. my @cpud = ();
  7. my @timed = ();
  8. my $time;


  9. #Write the cpu load to the file
  10. open (FH , "sar -u 60 10 > cpuload|") or die "Cannot write:$!";
  11. close FH;

  12. open (FILE, "cpuload") or die "Cannot open :$!";

  13. while (<FILE>){
  14.     @tmp = split;

  15.     if ($tmp[8] =~ m/\d+/){
  16.         push @cpud, $tmp[8];
  17.     }

  18.     if ($tmp[0] =~ m/(\d{2}\:\d{2})\:\d{2}/){
  19.         push @timed, $1 ;
  20.     }

  21.     if ($tmp[1] =~ m/(.*M)/){
  22.          $time = $1;
  23.     }

  24. }

  25. #print STDOUT "@timed";

  26. my $arrlen = @cpud;
  27. my $i;

  28. for $i (0..$arrlen -1){
  29.     $cpud[$i] = 100 - $cpud[$i];
  30. }

  31. close FILE;

  32. #Create the cpu load graph
  33. my @data = ( [@timed], [@cpud],);

  34. my $my_graph = new GD::Graph::lines(1000,600);

  35. $my_graph->set (
  36.     x_label => 'time'. "($time)",
  37.     y_label => 'CPU Usage(%)',
  38.     y_max_value => '100',
  39.     y_min_value => '0',
  40.     title => 'CPU Load',
  41.     transparent => 0,
  42. );


  43. my $gd = $my_graph->plot(\@data) or die $my_graph->error;

  44. open OUT , ">cpu.png" or die "Cannot output:$!";
  45. binmode OUT;
  46. print OUT $gd->png();
  47. close OUT;

备注:
此脚本在CentOS 5.5 x86_64实践过,
 默认系统没有GD::Graph::lines这个,需要自己安装:"yum install  perl-GD-Graph"
关于“sar"命令系统默认木有,需自己安装“yum install sysstat" 即可
阅读(2339) | 评论(0) | 转发(1) |
0

上一篇:Linux ACL 体验

下一篇:制作RPM安装包

给主人留下些什么吧!~~