- #!/usr/bin/perl
-
-
#This script is used to create cpu load graph
-
-
use strict;
-
use GD::Graph::lines;
-
-
my @tmp;
-
my @cpud = ();
-
my @timed = ();
-
my $time;
-
-
-
#Write the cpu load to the file
-
open (FH , "sar -u 60 10 > cpuload|") or die "Cannot write:$!";
-
close FH;
-
-
open (FILE, "cpuload") or die "Cannot open :$!";
-
-
while (<FILE>){
-
@tmp = split;
-
-
if ($tmp[8] =~ m/\d+/){
-
push @cpud, $tmp[8];
-
}
-
-
if ($tmp[0] =~ m/(\d{2}\:\d{2})\:\d{2}/){
-
push @timed, $1 ;
-
}
-
-
if ($tmp[1] =~ m/(.*M)/){
-
$time = $1;
-
}
-
-
}
-
-
#print STDOUT "@timed";
-
-
my $arrlen = @cpud;
-
my $i;
-
-
for $i (0..$arrlen -1){
-
$cpud[$i] = 100 - $cpud[$i];
-
}
-
-
close FILE;
-
-
#Create the cpu load graph
-
my @data = ( [@timed], [@cpud],);
-
-
my $my_graph = new GD::Graph::lines(1000,600);
-
-
$my_graph->set (
-
x_label => 'time'. "($time)",
-
y_label => 'CPU Usage(%)',
-
y_max_value => '100',
-
y_min_value => '0',
-
title => 'CPU Load',
-
transparent => 0,
-
);
-
-
-
my $gd = $my_graph->plot(\@data) or die $my_graph->error;
-
-
open OUT , ">cpu.png" or die "Cannot output:$!";
-
binmode OUT;
-
print OUT $gd->png();
-
close OUT;
备注:
此脚本在CentOS 5.5 x86_64实践过,
默认系统没有GD
::Graph
::lines这个,需要自己安装:"yum install perl-GD-Graph"
关于“sar"命令系统默认木有,需自己安装“yum install sysstat" 即可