#!/usr/bin/perl
#
# Lux_Parse.PL -  Sun A5x00 Information Gathering Tool
#   	Version 1.0: Oct 9th, 2002 
# (c) Ben Rockwood - benr@cuddletech.com
# cuddletech - use unix or die(c). 

#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA


$ARGS = @ARGV;

if ($ARGS < 1) {
        print("Usage: lux_parse.pl array\n");
        exit(1);
}

$photon = $ARGV[0];
print("Photon is: $photon\n");
`luxadm display $photon \> /tmp/luxadm.$photon.out`;

open(LUX, "/tmp/luxadm.$photon.out");
$i = 0;

foreach(<LUX>){
        chomp($_);
        #print("$_\n");
        if ( $_ =~ m/^\d/ ) {
                #print("$_\n");
                @array[$i++] = $_;              
        }

        if( $_ =~ m/^FW/ ) {
                $name_line = $_;
        }
}
close(LUX);

$num_drives = @array;
if( $num_drives == 11 ) {
         $array_type = "A5200";
        } elsif ( $num_drives == 7 ) {
         $array_type = "A5100";
        } else {
         print("Array has $num_drives num of elements\n");
}

@crap_array = split(/:/, $name_line);
$array_name = $crap_array[4];

print("Drive Listing for $array_type named $array_name:\n");

`vxdisk list  > /tmp/vxdisk.tmp.out`;
`ls -l /dev/dsk > /tmp/devdsk.tmp.out`;

#Front Disks:
$i = 0;
foreach(@array){
        my @input = split(/\s+/, $_);
        my $long_wwn = $input[3];
        my $status1 = $input[1];
        my $status2 = $input[2];
        unless ( $long_wwn =~ m/\d+/ ) {
                $long_wwn = "error";
                $status1 = "error";
                $status2 = "error";
        }
        
        print("$array_name,f$i WWN: $long_wwn -> Status: $status1 $status2\n");

        if ( $long_wwn eq "error" ) {
                print(" ------ ERROR --------\n");
		$i++;
                next;
        }
        my $short_wwn = $long_wwn;
        $short_wwn =~ s/\d{6}//;
        foreach(`cat /tmp/devdsk.tmp.out | grep $short_wwn,0:c`){

                my @dev_out = split(/\s+/, $_);
                $scsi_id = $dev_out[8];
                #$scsi_id =~ s/s\d+//;
                $ctrl_path = $dev_out[10];
                $ctrl_path =~ s/^.*\/.*?\@(.*?),.*$/$1/;
                #print("controler path is: $ctrl_path\n");
                my $instpath = `grep $ctrl_path /etc/path_to_inst`;
                (my $devpath, my $inst, my $drv) = split(/\s+/, $instpath);
                $drv =~ s/\"//g;
                print("\t\tSCSI id: $scsi_id - $drv$inst");
                my @vx_out = split(/\s+/, `grep $scsi_id /tmp/vxdisk.tmp.out `);
                print("\tVx Disk/DG: $vx_out[2]/$vx_out[3]\n");
        }


        $i++;
}
print("\n");

# Rear Disks
$i = 0;
foreach(@array){
        my @input = split(/\s+/, $_);
        my $long_wwn = $input[6];
        my $status1 = $input[4];
        my $status2 = $input[5];
        unless ( $long_wwn =~ m/\d+/ ) {
                $long_wwn = "error";
                $status1 = "error";
                $status2 = "error";
        }


        print("$array_name,r$i WWN: $long_wwn -> Status: $status1 $status2\n");

        if ( $long_wwn eq "error" ) {
                print(" ------ ERROR --------\n");
		$i++;
                next;
        }

        my $short_wwn = $long_wwn;
        $short_wwn =~ s/\d{6}//;
        foreach(`cat /tmp/devdsk.tmp.out | grep $short_wwn,0:c`){

                my @dev_out = split(/\s+/, $_);                
                $scsi_id = $dev_out[8];           
                #$scsi_id =~ s/s\d+//;
                $ctrl_path = $dev_out[10];
                $ctrl_path =~ s/^.*\/.*?\@(.*?),.*$/$1/;        
                #print("controler path is: $ctrl_path\n");
                my $instpath = `grep $ctrl_path /etc/path_to_inst`;
                (my $devpath, my $inst, my $drv) = split(/\s+/, $instpath);
                $drv =~ s/\"//g;
                print("\t\tSCSI id: $scsi_id - $drv$inst"); 
                my @vx_out = split(/\s+/, `grep $scsi_id /tmp/vxdisk.tmp.out `);
                print("\tVx Disk/DG: $vx_out[2]/$vx_out[3]\n");
        }

        $i++;
}

print("Done... Cleaning up\n");
`rm /tmp/vxdisk.tmp.out`;
`rm /tmp/luxadm.$photon.out`;
`rm /tmp/devdsk.tmp.out`;

## Reference Notes:
# ls -l /dev/dsk/ | grep 2037935ec6,0:c
#lrwxrwxrwx   1 root     other         74 Jun  4 15:18 c2t1d0s2 -> ../../devices/sbus@2,0/SUNW,socal@d,10000/sf@0,0/ssd@w2100002037935ec6,0:c
#lrwxrwxrwx   1 root     other         74 Jun  4 15:18 c4t1d0s2 -> ../../devices/sbus@a,0/SUNW,socal@d,10000/sf@0,0/ssd@w2200002037935ec6,0:c
# 
# grep 2037935ec6 /etc/path_to_inst
#"/sbus@2,0/SUNW,socal@d,10000/sf@0,0/ssd@w2100002037935ec6,0" 563 "ssd"
#"/sbus@a,0/SUNW,socal@d,10000/sf@0,0/ssd@w2200002037935ec6,0" 564 "ssd"
# 
