A perl script to present summarized iostat output

Jan 15, '04 09:26:00AM

Contributed by: MartySells

iostat provides a means to monitor disk throughput, but the output is rather gross. The below perl script cleans it up. Call it with an interval in seconds. Oddly, the disk numbers don't seem to corespond to what df reports.


#!/usr/bin/perl

use strict;
use POSIX qw(strftime);

my $donedisks=0;
my $increment=shift;
my $fieldRE = '\s+(\d+\.\d+)\s+(\d)+\s+(\d+\.\d+)';

open(FILE,"iostat -n 99 -d -w $increment |") || die "Can't open iostat - $!";
while (<FILE>) {
 chop;
 next if (/KB/);   # ignore header
 if (/disk/) {     # grab disk names
   next if $donedisks++;
   s/^\s+|\s+$//g;
   s/\s+/,/g;
   my $now_string = strftime("%F,%T",localtime(time));
   my @disks = split(/,/); print "#$now_string,$_\n";
   next;
 }

 if (/$fieldRE/) {  # good info!
  my $result='';
  while (/^\s*$fieldRE/) {
    my ($kbt,$tps,$mbs) = ($1,$2,$3);
    s/^\s*$fieldRE//;
    $result .= "$mbs,";
  }
  $result =~ s/,$//;
  my $now_string = strftime("%F,%T",localtime(time));
  print "$now_string,$result\n";
  }
}
close FILE;
[robg adds: At least on 10.3, the iostat output looks fine on my machine, but the above script will give you a nicely condensed version, letting you see many more events on one Terminal screen...]

Comments (0)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20031227060508967