arrowHome arrow Performance Management arrow RRDtool Knowledge Base arrow Workshop : Follow-up of the CPU with RRDtool Wednesday, 07 January 2009  
Main Menu
Home
Contact Us
ozMonitor Teams
ozMonitor Terms of Use
Monitoring KB
ITIL
Fault Management
Performance Management
All-in-one Monitoring Tools
Management Skills
Time Management
Delegation
Managing People
Managing Conflict
Project Management
Prince2 Introduction
Prince2 Processes
Prince2 Components
Prince2 Techniques
Workshop : Follow-up of the CPU with RRDtool Print E-mail

The /proc/stat file contains the statistics of the core, and the system.

In the first line we have the following information that the system passed:

  • as a user mode,
  • as a user mode with a low priority (nice),
  • in system mode,
  • and the time of inactivity.
This article treats the follow-up of the CPU with RRDtool in Perl.

Initially, it will be necessary to put below script cpu.pl in the /opt/supervision directory.

Script Name : cpu.pl

Contents :

 

#!/usr/bin/perl
use RRDs;
use strict;

# global variables
my $plugin = "cpu";
use FindBin;
my $Directory = "${FindBin::Bin}";
my $rrd = "${Directory}/${plugin}-stats.rrd";

my $cpu =`cat /proc/stat | grep "cpu  "`;
$cpu=~ /cpu  (.*) (.*) (.*) (.*)/;
my $user = $1 ;
my $nice = $2;
my $system = $3;
my $idle = $4 ;
#print "\n $user $nice   $system   $idle \n";

my $rrdstep = 300;
my $interval = int($rrdstep / 60);
my $rows = 4000 / $interval;

my $day_steps = int(3600 * 24 / $rrdstep);
my $week_steps = $day_steps * 7;
my $month_steps = $week_steps * 5;
my $year_steps = $month_steps * 12;

if(not -f $rrd) {
    RRDs::create($rrd, '--step', $rrdstep,
            'DS:user:COUNTER:'.($rrdstep*2).':0:100',
            'DS:nice:COUNTER:'.($rrdstep*2).':0:100',
            'DS:system:COUNTER:'.($rrdstep*2).':0:100',
            'DS:idle:COUNTER:'.($rrdstep*2).':0:100',
            "RRA:AVERAGE:0.5:1:$rows", #day
            "RRA:AVERAGE:0.5:".int(30/$interval).":800", #week
            "RRA:AVERAGE:0.5:".int(120/$interval).":800", # month
            "RRA:AVERAGE:0.5:".int(1440/$interval).":800", #year
            "RRA:MAX:0.5:1:$rows", #day
            "RRA:MAX:0.5:".int(30/$interval).":800", #week
            "RRA:MAX:0.5:".int(120/$interval).":800", # month
            "RRA:MAX:0.5:".int(1440/$interval).":800", #year
        );
        my $e = RRDs::error();
        die "ERROR: Cannot create rrdfile: $e\n" if $e;
}

#print "\n RRDs::update $rrd, N:$user:$nice:$system:$idle \n";
RRDs::update $rrd, "N:$user:$nice:$system:$idle";
my $e = RRDs::error();
die "ERROR: Cannot Update rrdfile: $e\n" if $e;


my $host = `hostname`; chomp $host;
my $xpoints = 700;
my $ypoints = 180;

my @names = ('system','user','nice','idle');
my $maxlenght = 0;
foreach my $n (@names) {
    if ($maxlenght < length($n)) { $maxlenght = length($n); }
}
my @color = ("#FF0000","#0000FF","#FFFF00","#00FF00","#FF00FF","#00FFFF","#FFFFFF", "#800000","#008000","#808000","#000080","#800080","#008080","#808080");

my @options;
@options = (@options, "COMMENT:               Average       Max\\n");
@options = (@options, "--no-minor");
@options = (@options, "-l0 ");
@options = (@options, "-u100 ");
@options = (@options, "--height=150");
@options = (@options, "--width=450");

my $i =0;
foreach my $ds (@names) {
    @options = (@options, "DEF:${ds}=$rrd:${ds}:AVERAGE");
    my $name = $ds;
    while (length($name)<$maxlenght) { $name = "${name} "; }
    #@options = (@options, "LINE2:${ds}$color[$i]:${name}");
    if ($i == 0) {
     @options = (@options, "AREA:${ds}$color[$i]:${name}");
    }
    else {
     @options = (@options, "STACK:${ds}$color[$i]:${name}");
    }
    @options = (@options, "GPRINT:${ds}:AVERAGE:%8.0lf %s");
    @options = (@options, "GPRINT:${ds}:MAX:%8.0lf %s\\n");
    $i++;
}
@options = (@options, "COMMENT:\\n");
@options = (@options, "COMMENT:Generator \\: http\\://www.ozMonitor.Net\\n");

my $titre = "CPU Performances for $host ";
my $ylegend = "%";

my $width = 450; my $height = 150;
my $day = 3600*24;
my $week = $day*7;
my $month = $day*31;
my $year = $day*365;

&graph("day");
&graph("week");
&graph("month");

sub graph($) {
    my $range = shift;
    my $start;
    if ($range eq "week") {
        $start = $week;
        @options = (@options, "-xHOUR:3:HOUR:6:DAY:1:0:%d/%m");
    }
    elsif ($range eq "month") {
        $start = $month;
        @options = (@options, "-xHOUR:12:DAY:1:DAY:4:0:%d/%m");
    }
    elsif ($range eq "year") { $start = $year;}
    else {
        $start = $day;
        @options = (@options, "-xMINUTE:30:HOUR:1:HOUR:2:0:%Hh");
    }

    my $graph_file = "${Directory}/${plugin}-${range}.png";
    my ($avg,$xsize,$ysize) = RRDs::graph "$graph_file","--title", "$titre","--start",-${start},"-v",$ylegend,"-a","PNG",@options;
    if (my $ERROR = RRDs::error) { print "ERROR: $ERROR\n"; }
    #print qq(\n "$graph_file","--title", "$titre","--start",-$start,"-v",$ylegend,"-a","PNG",@options \n);
}

 

 Once, /opt/supervision/cpu.pl script set up, its rights are changed as follows:

  • chmod +x /opt/supervision/cpu.pl

 To activate this follow-up of the CPU, it would be necessary to add the following line in the crontab (with the command : crontab - E)

  •  */5 *** * /opt/supervision/cpu.pl >/dev/null 2>/dev/null
 

© 2009 ozMonitor Knowledge Base