I’ve been scripting somewhat with sar on Solaris 10. The major problem is that for some reason there is no flag to output machine readable output (ie. make it easy to import into spreadsheets or other script). The most imporant part was adding a time field to each and every block device to make it much easier to create disk statistics.
So I threw one together sarpipe.pl:
#!/usr/bin/perl -w use strict; #default field delimiter/separator my $delim = "|"; while($_ = shift @ARGV) { if($_ =~ m/--delim/) { #change the default field delimter $delim = shift(@ARGV); } else { die "Usage: sar [-A...] | $0 [--delim seperator]n"; } } #preset so we don't get any concat empty val errors my $latesttime = ""; #loop through the sar output while(<>) { chomp; #catch time field of output, remove from line if($_ =~ s/^(dd[:]dd[:]dd|Average)//) { $latesttime = $1 . $delim; } #remove leading and tailing whitespace $_ =~ s/(^s+|s+$)//; #replace spaces with field delimiter $_ =~ s/s+/$delim/g; #if the line contains any content, print time field and line print $latesttime . $_ if($_ =~ m/^.+$/); print "n"; }
In use:
user@example$ ./sarpipe.pl -h Usage: sar [-A...] | ./sarpipe.pl [--delim seperator] user@example$ sar -d | ./sarpipe.pl | more SunOS|bcaeao|5.10|Generic_144488-10|sun4u|08/16/2011 00:00:00|device|%busy|avque|r+w/s|blks/s|avwait|avserv 00:10:01|md110|0|0.0|0|1|0.0|29.9 00:10:01|md111|0|0.0|0|0|0.0|0.0 00:10:01|md115|1|0.0|2|30|0.0|15.3 00:10:01|md116|0|0.0|0|0|0.0|12.8 00:10:01|md120|0|0.0|0|1|0.0|27.4 00:10:01|md121|0|0.0|0|0|0.0|0.0 00:10:01|md125|1|0.0|2|30|0.0|13.4 00:10:01|md126|0|0.0|0|0|0.0|13.0 00:10:01|md130|0|0.0|0|1|0.0|0.0 ... Average|ssd35,c|0|0.0|0|0|0.0|0.0 Average|ssd35,g|1|0.0|2|179|0.0|10.1 Average|ssd36|0|0.0|0|0|0.0|0.0 Average|ssd36,a|0|0.0|0|0|0.0|0.0 Average|ssd36,b|0|0.0|0|0|0.0|0.0 Average|ssd36,c|0|0.0|0|0|0.0|0.0 Average|ssd36,f|0|0.0|0|0|0.0|0.0 Average|ssd36,g|0|0.0|0|0|0.0|0.0 Average|ssd36,h|0|0.0|0|0|0.0|0.0 Average|ssd38|0|0.0|1|5|0.0|2.2 Average|ssd38,c|0|0.0|0|0|0.0|0.0 Average|ssd38,g|0|0.0|1|5|0.0|2.2 user@example$
As you can see, it just formats the sar output to be easily used. It does not remove empty lines or remove annoying psudo block devices (ssd36,h). The default delimiter is a pipe because a comma would interfere with device names with a comma. If you change the delimiter via command line (–delim) be sure to be aware of shell escapes (eg –delim ! not –delim !).