#!/usr/bin/perl -T

use strict;
use warnings qw(all);

$ENV{'PATH'} = '/sbin:/usr/sbin';

# Display SVM stats
# Author: Lanny Godsey, rewritten by Bruno Bonfils (asyd)
# <asyd@debian-fr.org>

# Print a blank line to be more readable
print "\n";

sub stats {

	my ($device, $used, $free, $counts, $size);

format STDOUT =
Device    Size    Used    Free    Counts
-----------------------------------------
@<<<<<<<< @<<<<<< @<<<<<< @<<<<<< @<<<<<<
$device,  $size,  $used,  $free,  $counts
.

	my %devices = ();
	my %devblksused = ();

	open FI, "metastat -p|";
	while (<FI>) {
		chomp;
		if (/^d\d+ -p (\w+) -o \d+ -b (\d+)/) {
			$devblksused{$1}+=$2;
			$devices{$1}++;
		}
	}
	close FI;

	foreach my $dev (keys %devices) {
		my %devblksize = ();
		my %devblks = ();

		if ( -e "/dev/rdsk/$dev" ) {
			open FI, "devinfo -p /dev/rdsk/$dev|";
			while (<FI>) {
				chomp;
				if (/$dev\t\d+\t\d+\t\d+\t(\d+)/) {
					$devblks{$dev}=$1;
				}
			}
			close FI;
			open FI, "devinfo -i /dev/rdsk/$dev|";
			while (<FI>) {
				chomp;
				if (/$dev\t\d+\t\d+\t\d+\t(\d+)/) {
					$devblksize{$dev}=$1;
				}
			}
			close FI;

			$device = $dev;
			$size = sprintf("%.2fG", $devblks{$dev} * $devblksize{$dev} / 1024 / 1024 / 1024);
			$used = sprintf("%.2fG", $devblksused{$dev} * $devblksize{$dev} / 1024 / 1024 / 1024);
			$free = sprintf("%.2fG", ($devblks{$dev} - $devblksused{$dev}) * $devblksize{$dev} / 1024 / 1024 / 1024);
			$counts = $devices{$dev};

			write(STDOUT);
		} else {
			print "Can't find block device /dev/rdsk/$dev\n";
		}
	}
}

stats();

# Print a final blankline
print "\n";
