Chinaunix首页 | 论坛 | 博客
  • 博客访问: 863181
  • 博文数量: 581
  • 博客积分: 7803
  • 博客等级: 少将
  • 技术积分: 3653
  • 用 户 组: 普通用户
  • 注册时间: 2007-04-27 08:21
文章分类

全部博文(581)

文章存档

2013年(7)

2012年(414)

2011年(159)

2009年(1)

分类:

2012-05-21 12:41:05

原文地址:使用perl 绘制CPU负载图 作者:jiannma

  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" 即可
阅读(311) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~