#!/usr/bin/perl -w
###
# March 2003
# Script to sort tapes in Jukebox. Not useful other than
# as an excercise in scripting
##
$starttime = (time); # Used to measure performance
# open (IN, "d:/perl/bin/test/rdtag3.txt") or die "Failed to open rdta
+g.txt Error: $!\n";
# @array=<IN>;
print "Get sjirdtag info from Scalar\n";
if (@array=`C:/PROGRA~1/nsr/bin/sjirdtag scsidev\@6.6.0`) { print "Got
+ sjirdtag info\n" }
else { die "Failed to get sjirdtag info from server.\nError $!\n" }
my $flag = '';
foreach $line (@array){
if ($line =~ /DATA TRANSPORT/) { $flag = 'drives' }
if ($line =~ /STORAGE/) { $flag = 'storage' }
if ($line =~ /MEDIA TRANSPORT/) { $flag = 'transport' }
if ($line =~ /IMPORT\/EXPORT/) { $flag = 'mailbox'}
# Contents of drives
if ($flag eq 'drives') { #print "Flag is currently - $flag\n" ;
if ($line =~ /^\s*Elem\[(\d{3})\]/) { $lastSlot = $1 ; $drive[
+$lastSlot] = ''}
if ($line =~ /VolumeTag=<(\w{6})\s*>\s*$/) { $drive[$lastSlot]
+ = "$1" ; $noDrives = $lastSlot}
}
# Contents of slots
if ($flag eq 'storage') { #print "Flag is currently - $flag\n" ;#}
if ($line =~ /^\s*Elem\[(\d{3})\]/) { $lastSlot = $1 ; $slot[$
+lastSlot] = ''}
if ($line =~ /VolumeTag=<(\w{6})\s*>\s*$/) { $slot[$lastSlot]
+= "$1" ; push @volumes, $1 ; $noSlots = $lastSlot} #%locations = ("$1
+" => "$lastSlot") ;
}
# Contents of robot arm
if ($flag eq 'transport') { #print "Flag is currently - $flag\n" ;
+#}
if ($line =~ /VolumeTag=<(\w{6})\s*>\s*$/) {
$robotArm = "$1" ;
print "Robot arm contains a volume: $robotArm. Suggest jukebo
+x be reset before script run again.\n"
}
}
# Contents of mailbox
if ($flag eq 'mailbox') { #print "Flag is currently - $flag\n" ;#}
if ($line =~ /^\s*Elem\[(\d{3})\]/) { $lastSlot = $1 ; $mailbo
+x[$lastSlot] = ''}
if ($line =~ /VolumeTag=<(\w{6})\s*>\s*$/) { $mailbox[$lastSlo
+t] = "$1" ; $noMailbox = $lastSlot }
}
}
print "Autochanger has $noDrives drives, $noSlots slots, $noMailbox ma
+ilbox slots\n";
$i=1;
#foreach (@drive) { if ($slot[$i]) {print "Drive $i contains $drive[$i
+]\n" }; $i++ }
print "\n";
$i=1;
#foreach (@slot) { if ($slot[$i]) {print "Slot $i contains $slot[$i]\n
+" }; $i++ }
print "\n";
$i=1;
#foreach (@mailbox) { if ($mailbox[$i]) {print "Mailbox slot $i contai
+ns $mailbox[$i]\n" }; $i++ }
print "\n";
print "Volumes\n";
$i=1;
#foreach (@volumes) { if ($volumes[$i]) {print "$volumes[$i]\n" }; $i+
++ }
print "\n";
###
# Sorting volumes into alphabetical order
##
@volumesSorted = sort { $a cmp $b ; } @volumes ;
#$i=0;
#foreach (@volumesSorted) { if ($volumesSorted[$i]) {print "$volumesSo
+rted[$i]\n" }; $i++ }
#print "\n";
print "\@volumes@volumes\n\n\@volumesSorted@volumesSorted\n";
###
# Remove cleaning tapes and slots
##
#print "Array before\n@volumesSorted\n\n"; # To show array content
+s before. 4DB
&removeCleaning();
sub removeCleaning {
$i=0;
foreach (@volumesSorted){
if ( /CLN\d{3}/) { splice (@volumesSorted,($i),1) ; &remov
+eCleaning()}
$i++;
}
}
#print "Array after - Volumes sorted minus cleaning volumes (hopef
+ully)\n@volumesSorted\n\n"; # to show array contents after. 4DB
###
# Move the tapes!
##
#print "\nSlots array \n@slot\n\n";
$i=1;
foreach (@slot) {
if ($slot[$i]) {
# print "Slot $i contains $slot[$i]\n" ; # To help see what is
+going on
$Vol2Slot{$slot[$i]} = "$i" ; # Create a hash that when given
+a Volume returns a (it's current) slot
$allSlots{$i} = $slot[$i]; # Create a hash that knows whats in
+ each slot.
}
$i++
}
sub printV2S {
print "\nvolume\tslot\t Contents of vol2Slots\n";
foreach ( keys %Vol2Slot ) { print "$_ $Vol2Slot{$_}\n" }
}
&printAS();
sub printAS {
print "\nslot\tVolumes\t Contents of allSlots\n";
foreach ( keys %allSlots ) { print "$_ $allSlots{$_}\n" }
}
print "\n\nVolumesSorted\n@volumesSorted\n";
$i=0;
$j=1;
foreach (@volumesSorted) {
if ($volumesSorted[$i]) {
print "$volumesSorted[$i]\n";
print "Slot $j needs to contain $volumesSorted[$i]\n" ; # To h
+elp see what is going on
# $Vol2SlotReq{$volumesSorted[$i]} = "$i" ; # Create a hash tha
+t when given a Volume returns the slot we want it to end up in
$Vol2SlotReq{$volumesSorted[$i]} = "$j" ; # Create a hash that
+ when given a Volume returns the slot we want it to end up in
}
$i++ ; $j++ ;
}
&printV2SR();
sub printV2SR {
print "\ntape\tslot\t Contents Vol2SlotReq\n";
foreach ( keys %Vol2SlotReq ) { print "$_ $Vol2SlotReq{$_}\n" }
}
$i=1;
$j = 1;
$noLoops = @volumesSorted ;
&moveTapes() ;
sub moveTapes {
print "\n";
# print "\nIn loop\t\$i is $i\t \$allSlots\{\$i\} is $allSlots{$i}\
+n"; # 4DB removed because no error handling
# unless ($allSlots{$i}) {
if ($allSlots{$i}) { print "Slot $i contains a tape ($allSlots{$i}
+) already. Going to next slot (?)...\n"; }
else {
# print "Slot $i is empty. Will attempt to move tape into slot
+ $i...\n";
if (%bySlot = reverse %Vol2SlotReq) { print "SUCCESSFULLY reve
+rsed \%Vol2SlotReq to create \%bySlot \n" }
else { print 'FAILED to reverse %Vol2SlotReq to create %by
+Slot\n'}
if ($Vol2Slot{$bySlot{$i}}) { print "$Vol2Slot{$bySlot{$i}}\n"
+ } # Checking that there are still slots to be filled
elsif ( $j < $noLoops ) { print "t\$j is $j\n" ; $j++ ; $i=1 ;
+ &moveTapes() }
else { &END() }
# elsif ( $doneThisBefore == $i ) { &END() } # Not sure this li
+ne is right
# else { $i = 1 ; $doneThisBefore = $i ; print "$doneThisBef
+ore\t$i\n" ; &moveTapes() } # If there isn't then set $i to 1 and sta
+rt again.
# print "Issueing command c:/progra~1/nsr/bin/sjimm scsidev\@6.
+6.0 slot $Vol2Slot{$bySlot{$i}} ($bySlot{$i}) ($bySlot{$Vol2Slot{$byS
+lot{$i}}}) slot $i\n" ;
print "c:/progra~1/nsr/bin/sjimm scsidev\@6.6.0 slot $Vol2Slot
+{$bySlot{$i}} slot $i\n";
if ( $cmd = `c:/progra~1/nsr/bin/sjimm scsidev\@6.6.0 slot $Vo
+l2Slot{$bySlot{$i}} slot $i` ) {
print "SUCCESSFULLY issued move command to system\n";
}
else { die "Failed to issue move command to system.\n\$cmd
+ returned...\n$cmd\nCant continue... $!\n" }
# $moveResult = `c:/progra~1/nsr/bin/sjimm scsidev@6.6.0 slot $
+Vol2Slot{$bySlot{$i}} slot $i` ;
#Assuming this worked need to update locations etc
if ( $allSlots{$i} = $bySlot{$i} ) {print "SUCCESSFULLY update
+d allslots so that it knows what has gone into a slot\n" }
else { print "FAILED to update allslots so that it knows
+what has gone into a slot\n" }
print "$allSlots{$Vol2Slot{$bySlot{$i}}}\n" ;
if ( delete $allSlots{$Vol2Slot{$bySlot{$i}}} ) { print "SUCC
+ESSFULLY updated \%allSlots so that it knows whats gone from a slot\n
+" }
else { print "FAILED to update allslots so that it knew wh
+at has gone from a slot\n" }
# &printAS();
# &printV2S();
# &pause();
&moveTapes();
}
$i++;
&moveTapes();
}
sub END {
print "\nThe tapes in the autochanger should have been tidied\nYou sho
+uld reset and inventory now.\n";
}
sub pause {
$var = <STDIN>; chomp $var ; # Just to cause program to pause till
+ a key is hit
}
$endtime = (time);
$runtime = $endtime - $starttime;
print "\nResults took $runtime second(s) to generate.";
exit 0;
|