« Monitoring CPU usage with VMSTAT | Main | VMSTAT part 3 »

VMSTAT part 2

I revised my perl program to capture the output of vmstat. Now VMSTAT is left running and the output is captured by my perl program and saved in MySQL.

Here's the code:

use DBI;
use strict;
my $user = 'some_user';
my $password = 'some_pw';
my $dsn = 'DBI:mysql:cpu_stat:localhost';
my $dbh = DBI->connect($dsn, $user, $password,
                      { RaiseError => 1, AutoCommit => 0 })
   or die "Couldn't connect to database: " . DBI->errstr;
my $insert_cpu =
            $dbh->prepare_cached("INSERT INTO vm_stat VALUES (CURRENT_TIMESTAMP,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");

while(<STDIN>) {
if( /(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)$/) {

        $insert_cpu->execute($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16)
                or die "Couldn't insert row into cpu_stat table: " . DBI->errstr;


Now I save stats by running a command like "vmstat 60 1440 | perl vmstat.pl & ". One nice thing about this method is I can change the reporting interval. "vmstat 60 1140" will capture stats every 60 seconds for 1440 repetitions or one day.

"2007-03-16 15:41:20","1","0","369712","29108","103676","363452","0","0","0","0","0","64","2","1","97","0"
"2007-03-16 15:42:20","0","0","369712","27308","103856","363612","0","0","0","0","0","238","11","2","86","1"
"2007-03-16 15:43:20","0","0","369712","24700","103940","363868","0","0","0","0","0","156","3","1","96","0"
"2007-03-16 15:44:20","0","0","369712","30220","104124","364296","0","0","0","0","0","174","1","1","96","2"

Tommorrow I might try a 10 second reporting period. But in truth I don't have much of a CPU load problem so I think I'll stop here. But I've got the program now so I can pull it out whenever it looks like things are getting too busy.

But I'm going to include a graph of my CPU usuage for one day. I think a graph will be the best way to present these stats.


As you can see the server is lightly loaded, except for a few spikes. I'm going to run this from time to time and for different days of the week just to get an idea of what is normal.

© 2016 Mike Silversides


This page contains a single entry from the blog posted on March 16, 2007 3:30 PM.

The previous post in this blog was Monitoring CPU usage with VMSTAT.

The next post in this blog is VMSTAT part 3.

Many more can be found on the main index page or by looking through the archives.