#!/usr/bin/perl

use strict;

my @disks;
# Scan IDE disks

my $i;
for($i = ord "a"; $i<ord "z"; $i++) {
	my %disk;
	$disk{'Device'} = "/dev/hd".chr($i);
	$disk{'Type'} = "IDE";
	my $channel = int(($i - ord('a'))/2);
	my $pri_sec = ($i - ord('a'))%2;
	$disk{'Location'} = $channel."-".$pri_sec;
	
	my $dir = "/proc/ide/hd".chr($i);
	if( -e $dir ) {
		open FILE, "$dir/media";
		my $media = <FILE>;
		chomp $media;
		close FILE;
		
		if($media ne "disk") {
			next;
		}
		
		open FILE, "$dir/model";
		my $model = <FILE>;
		chomp $model;
		my ($vendor) = split(/ /, $model);
		$model =~ s/^$vendor +//;
		close FILE;
		
		$disk{'Vendor'} = $vendor;
		$disk{'Product'} = $model;
		
		my ($capacity) = `sfdisk -s $disk{'Device'}`;
		chomp $capacity;
		$disk{'Capacity'} = $capacity;
		
		$disks[@disks] = \%disk;
	}
}

# Scan SCSI disk
for($i = ord "a"; $i<ord "z"; $i++) {
	my %disk;
	$disk{'Device'} = "/dev/sd".chr($i);
	$disk{'Type'} = "SCSI";
	
	my ($host, $channel, $target, $lun) = split(/ +/, `scsiGetABTL $disk{'Device'}`);
	if(!defined $lun) {
		next;
	}
	chomp $lun;

	$disk{'Location'} = $host."-".$channel."-".$target."-".$lun;
	
	my $capacity = `sfdisk -s $disk{'Device'}`;
	chomp $capacity;
	$disk{'Capacity'} = $capacity;
	
	my $proc_line = 
		sprintf "Host: scsi%d Channel: %02d Id: %02d Lun: %02d", $host, $channel, $target, $lun;
	open PROCFILE, "/proc/scsi/scsi";
	my $line;
	while(defined($line=<PROCFILE>)) {
		if($line !~ $proc_line) {
			next;
		}
		$line = <PROCFILE>;
		my @words = split(/: /, $line);
		
		$words[1] =~ s/ +Model//;
		$disk{'Vendor'} = $words[1];
		
		$words[2] =~ s/ +Rev//;
		$disk{'Product'} = $words[2];
		
		last;
	}
	close PROCFILE;
	
	# Exclude iSCSI Disks
	if(! -e "/proc/scsi/iscsi/$host" ) {
		$disks[@disks] = \%disk;
	}
}



my $fmt = "%-8s %-8s %-30s %-4s %-8s %-10s\n";
printf STDOUT $fmt, "# Device", "Vendor", "Product", "Type", "Location", "Capacity(K)";

my $disk;
foreach $disk (@disks) {
	printf STDOUT $fmt, $disk->{'Device'}, $disk->{'Vendor'}, $disk->{'Product'}, 
		$disk->{'Type'}, $disk->{'Location'}, $disk->{'Capacity'};
}
exit 0;

