Wednesday, April 16, 2008

Perl Script To Graph Iostat Output on Unix

The ravages of iostat depicted on graph

Note: Click the picture up above to check it out in "moderately-huge" size!

Hey there,

It's been a while since we had a nice picture on top of a post, so today we're putting out a Perl script to graph "iostat" output on Unix.

If you need to graph anything else out, feel free to check our previous posts on graphing disk, paging, memory and cpu statistics on Unix, and our follow-up to account for some "sar" differences in its paging, memory and cpu output on Linux.

I'm happy to notice (yes, it took me a long long time) that we've actually gotten a write-up on those graph scripts on's Perl Site and, in honor of the write up, I'm going to do two things (not including stopping the hyperlinks in this post once and for all ;) -

1. Explain the deprecated use of the GifGraph module. This is actually kind of sad. The only reason I used it is because it was already installed on the box I was working on, I had everything set up the way I liked and didn't want to mess up my little might-as-well-be-Linux installation I had on my ultra5 running Solaris 8 ;) No special reason. The new stuff is probably a lot easier to deal with and less of a headache :)

2. Write this last one using the Chart module, which hasn't been updated since January 24th, 2006 and which required me to compile all sorts of extra libraries and build and install all sorts of extra programs. And I wrote it on a newer computer and OS that I had to muck with to make it work.

After all the hassle, I'm happy to say that it actually works, but I still can't get the "gif" or "png" method's to function properly, so I'm creating a jpg (from libjpeg -- libgif and libpng just weren't good enough, I guess). I also promise (mostly to myself) to never write a script using a deprecated module on the latest version of Perl ever again... unless I have to for some inexplicable reason like... I don't know. Not a whole lot sounds that bad right now ;)

This script should work equally well on Linux or Unix, as long as you format your output to be in this order:

time rds/sec wrs/sec kbrds/sec kbwris/sec <--- The time, reads per second, writes per second, kb read per second and kb written per second. Of course you could just change the array values to suit the way your iostat output already is.

Anyhow. Enjoy, and hopefully you'll find this useful. If you haven't updated your Perl in a while (or enjoy an older version of Sun or Linux), this might be perfect just the way it is :)

Creative Commons License

This work is licensed under a
Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License


# - Takes no arguments. Won't even give you the satisfaction of getting upset
# 2008 - Mike Golvach -
# Creative Commons Attribution-Noncommercial-Share Alike 3.0 United States License

$source=`date +%m%d%y`;

use Chart::Bars;

open(hash, "<${basedir}/host_1.txt");
@hash = <hash>;

$line = "host_1 ReadWrite IO stats for $source";

$iostats = Chart::Bars->new (1024, 768);

%pickle = (
title => $line,
x_label => 'Time',
y_label => 'Rates',
x_ticks => 'vertical',
legend => 'left',
brush_size => 2,
pt_size => 12,
grey_background => 'true'

$iostats->set (%pickle);
@labels = ('ReadsPerSec', 'WritesPerSec', 'KBReadsPerSec', 'KBWritesPerSec');
$iostats->set (legend_labels => \@labels);

$w = 0;
$y = 0;
$z = 0;
$zz = 0;
$r = 0;

foreach $cornedbeef (@hash) {
@rhino = split(" ", $cornedbeef);
$spleen[$w] = "$rhino[0]";
$pepper[$y] = "$rhino[1]";
$hot[$z] = "$rhino[2]";
$sauce[$zz] = "$rhino[3]";
$nim[$r] = "$rhino[4]";

@data = (\@spleen, \@pepper, \@hot, \@sauce, \@nim);
$iostats->jpeg ( "host_1iorw${source}.jpg" , \@data );

, Mike