Veritas-bu

[Veritas-bu] netbackup available_media script

2004-11-07 23:34:15
Subject: [Veritas-bu] netbackup available_media script
From: dale AT daleking DOT org (dale AT daleking DOT org)
Date: Mon, 8 Nov 2004 15:34:15 +1100
--FCuugMFkClbJLl1L
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline

Hi All,

The available_media script in /usr/openv/netbackup/bin/goodies is a dog

for sites with a large number of tapes.
I've rewritten this script in perl for performance and was wondering if
anyone who uses the veritas version would like to test it.

On our system ~1400 carts the veritas version takes ~55 minutes.  This
version takes ~12 seconds and emulates the output, including the -summary
option.

Things to note:
        - added -size option to display data sizes in Ki/Mi/Gi
        - added -pool= to only show a specific pool name
        - added WARNING lines if multiple mediaids are found in the MM
          (remove or filter these lines if you depend on the output
          format)
        - my perl is weak - please submit improvements after which I'll
          make it generally available somewhere else
        - only tested with nbu 4.5 under AIX
        - don't forget to update the script with the location of perl
          on your system 

I hope someone finds this useful.

- Dale

--FCuugMFkClbJLl1L
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename=available_media

#!/usr/bin/perl
# available_media script based on Veritas' goodies script.
# Dale King 20041108
# Tested with NBU 4.5 FP3 under AIX.
#
# Usage: available_media [-summary] [-size] [-pool=poolname]
# New options are:
#       -size   to show size in Ki/Mi/Gi
#       -pool=  to show only media in specified pool name.
#

use integer;

$opt_s=0;
$opt_m=0;
$opt_p="";

foreach $i ( @ARGV ) {
        ($i, $j) = split(/\=/, $i);
        if( $i eq "-pool" ) {
                $opt_p=$j;
        } elsif( $i eq "-size" ) {
                $opt_s=1;
        } elsif( $i eq "-summary" ) {
                $opt_m=1;
        } else {
                print "Usage: $0 [-summary] [-size] [-pool=poolname]\n";
                exit;
        }
}

$VMQUERY="/usr/openv/volmgr/bin/vmquery";
$VMPOOL="/usr/openv/volmgr/bin/vmpool";
$BPMEDIALIST="/usr/openv/netbackup/bin/admincmd/bpmedialist";

$asterix=0;

#store the vmpool brief output skipping the first two lines
open(VMPOOL, "$VMPOOL -listall -b |")
  || die("Unable to open pipe to vmpool: $!\n");
        $junk = <VMPOOL>;
        $junk = <VMPOOL>;
while (<VMPOOL>) {
        ($poolname, $junk) = split;
        push(@poollist, $poolname);     #save the pool names
}
close(VMPOOL);

if( $opt_p ne "" ) {
        $vaild=0;
        foreach $i (@poollist) {
                if( $i eq $opt_p ) {
                        $valid=1;
                }
        }
        if( $valid ) {
                @poollist = $opt_p;
        } else {
                die("$opt_p is not a valid pool name.\n");
        }
} else {
        @poollist = sort( @poollist );
}

#store the bpmedialist output
open(MLIST, "$BPMEDIALIST -mlist -l -ev |")
  || die("Uname to open pipe to bpmedialist: $!\n");
while (<MLIST>) {
        ($mediaid, $junk) = split;
        if( exists $mediastatus{$mediaid} ) {
                print "WARNING: Multiple media records for mediaid $mediaid\n";
        }
        $mediastatus{$mediaid} = $_;
}

#print the header
print "media\tmedia\trobot\trobot\trobot\tside/\tret    size\tstatus\n";
print " ID\ttype\ttype\t  #\tslot\tface\tlevel\n";
print 
"----------------------------------------------------------------------------\n";

foreach $poolname (@poollist) {

        open(VMQUERY, "$VMQUERY -pn $poolname -bx |")
          || die("Unablee to open pipe to vmquery: $!\n");
        $junk = <VMQUERY>;
        $junk = <VMQUERY>;
        $junk = <VMQUERY>;

        my @medialist;
        while (<VMQUERY>) {
                ($vmediaid, $vmediatype, $vrobottype, $vrobotnum, $vrobotslot, 
$vside, $vvol, $vop, $vmnts, $vlmtdate, $vlmttime, $vasgndate, $vasgntime, 
$vpool) = split;
                $vmediatype=sprintf("%-8s",$vmediatype);

                if (($vasgndate eq "---") && ($vasgntime eq "---")) {
                        $bpkbytes="-";
                        $bpretlev="-";
                        $bpstatus="AVAILABLE";

                        push(@medialist, "$bpstatus#$vmediaid\t$vmediatype 
$vrobottype\t  $vrobotnum\t  $vrobotslot\t $vside\t $bpretlev     $bpkbytes\t");
                } else {
                #parse bpmedialist
                        ($bpmediaid, $bppartner, $bpver, $bpden, $bpalloc, 
$bplwrite, $bpexp, $bplrest, $bpkbytes, $bpnimages, $bpvimages, $bpretlev, 
$bpunused1, $bpnumrest, $bpstat, $bprest) = split(/\s+/, 
$mediastatus{$vmediaid});

                        $bpstatus="";
                        if( $bpmediaid ne "") {

                                $spaces="     ";        
                                $ismpx=0;
                        
                                if( $bpstat > 511 ) {
                                        $ismpx=1;
                                        $bpstat=$bpstat - 512;  
                                } if( $bpstat > 255 ) {
                                        # Ignore this bit.
                                        $bpstat=$bpstat - 256;
                                } if( $bpstat > 127 ) {
                                        $bpstat=$bpstat - 128;
                                        $bpstatus="IMPORTED";
                                } if( $bpstat > 63 ) {
                                        $bpstat=$bpstat - 64;
                                        # Add * on retention level to indicate
                                        # this media id has multiple retention
                                        # levels on it.  Reduce the number of
                                        # spaces to account for the *.
                                        $spaces="    ";
                                        $bpretlev=$bpretlev . "*";
                                        $asterix=1;
                                } if( $bpstat > 31 ) {
                                        # Ignore this bit.
                                        $bpstat=$bpstat - 32;
                                } if( $bpstat > 15 ) {
                                        # Ignore this bit.
                                        $bpstat=$bpstat - 16;
                                } if( $bpstat > 7 ) {
                                        $bpstat=$bpstat - 8;
                                        if( $bpstatus eq "" ) {
                                                $bpstatus="FULL";
                                        } else {
                                                $bpstatus=$bpstatus . "/FULL";
                                        }
                                } if( $bpstat > 3 ) {
                                         # Ignore this bit.
                                        $bpstat=$bpstat - 4;
                                } if( $bpstat > 1 ) {
                                        $bpstat=$bpstat - 2;
                                        if( $bpstatus eq "" ) {
                                                $bpstatus="SUSPENDED";
                                        } else {
                                                $bpstatus=$bpstatus . 
"/SUSPENDED";
                                        }
                                } if( $bpstat > 0 ) {
                                        if( $bpstatus eq "" ) {
                                                $bpstatus="FROZEN";
                                        } else {
                                                $bpstatus=$bpstatus . "/FROZEN";
                                        }
                                } 
                        
                                if( $bpstatus eq "" ) {
                                        if( $ismpx == 1 ) {
                                                $bpstatus=$bpstatus . 
"ACTIVE/MPX";
                                        } else {
                                                $bpstatus=$bpstatus . "ACTIVE";
                                        }
                                } elsif( $ismpx == 1 ) {
                                                $bpstatus=$bpstatus . "/MPX";
                                }

                                #get_med_type

                                if( $opt_s ) {
                                        if( $bpkbytes >= 10485760 ) {
                                                $bpkbytes=$bpkbytes/1048576;
                                                $multiplier="G";
                                        } elsif( $bpkbytes >= 10240 ) {
                                                $bpkbytes=$bpkbytes/1024;
                                                $multiplier="M";
                                        } else {
                                                $multiplier="K";
                                        }
                                }       
                                
                                push(@medialist, 
"$bpstatus#$vmediaid\t$vmediatype $vrobottype\t  $vrobotnum\t  $vrobotslot\t 
$vside\t $bpretlev$spaces$bpkbytes$multiplier\t");
                        } else {
                                $bpkbytes="-";
                                $bpretlev="-";
                                $bpstatus="DBBACKUP";

                                #get_med_type;
                
                                push(@medialist, 
"$bpstatus#$vmediaid\t$vmediatype $vrobottype\t  $vrobotnum\t  $vrobotslot\t 
$vside\t $bpretlev     $bpkbytes\t");
                        }
                }
        }
        close(VMQUERY);

        print "$poolname pool\n\n";

        if( @medialist ) {
                # Sort the entries by status
                @medialist = sort( @medialist );

                foreach $i (@medialist) {
                        ($status, $line) = split(/\#/, $i);
                        print "$line$status\n";
                }
                print "\n";

                if( $opt_m ) {
                        my %counts;
                        foreach $i (@medialist) {
                                ($status, $line) = split(/\#/, $i);
                                ($mediaid, $mediatype) = split(/\s+/, $line);
                                $line=$status . "#" . $mediatype;
                                        
                                $counts{$line}++;
                        }
                        $pn=substr("$poolname", 0, 10);
                        @sorted = sort keys %counts;
                        foreach $j ( @sorted ) {
                                ($status, $mediatype) = split(/\#/, $j);
                
                                $line=sprintf("%6d %-10s %-10s %s", 
$counts{$j}, $pn, $mediatype, $status);
                                push(@poolsummary, $line);
                        }
                }
                        
        }       
}

if( $asterix ) {
        print "\n* - More than 1 retention level present.\n\n";
}

if( $opt_m ) {
        print "Summary:\n\n";
        print "#Media Pool       Type       State\n";
        print "------ ---------- ---------- -----\n";

        foreach $i ( @poolsummary ) {
                print "$i\n";
        }
}

--FCuugMFkClbJLl1L--