#!/usr/bin/perl
# scripit name	: decommision_media_server.pl
# function	: script to move media ownership from one media server to another when decommissioning a media server.
# Usage  	: decommision_media_server.pl {old_server_to_decommission} {server_to_take_ownership}
# Limitation	: The script does not check for server groups nor sets them. Works with NBU 6.5 only.
#
# Change log:
# Date     Init Review Test  Version level chritical Comments
#                                            script
#-------------------------------------------------------------------------------
# 2008.11.13 NMA N/A   N/A    6.5     1        NO       new script
#

#
# Use module warnings to make perl warn about code/variable messup's.
use warnings;

# Declare variables - alle variables declared wth "my" is local to the main program.
my $old_mediaserver=$ARGV[0];
my $new_mediaserver=$ARGV[1];
my $master_server=$ARGV[2];
my %lastwritehost;
my $barcode;
my $answer;

# Function query_emm. Read in the input from nbemmcmd. Data are put into hash where the barcode is the key and "last write host" is the value.
# Written as a function because we need to call the nbemmcdm -listmedia twice.
sub query_emm {
open(EMM_OUTPUT, "/usr/openv/netbackup/bin/admincmd/nbemmcmd -listmedia -allrecords | ") or die  "Can't open nbemmcmd : $!";
 while (<EMM_OUTPUT>) 
 {
 chomp;
 $barcode = $1 if /^Barcode:\s+(\S+)/;
 $lastwritehost{$barcode} = $1 if /^Last Write Host:\s+(\S+)/;
 }
close EMM_OUTPUT;
}

# Check number of arguments - we fail if argument isn't 2
$num_args = $#ARGV+1;
if ( $num_args != 3 ) {
 print "\n";
 print "Only 3 argument is expected - {oldserver} {newserver} {masterserver} \n";
 print "Number of Arguments: ", $num_args, "\n";
 exit 1;
}

# Check unrestricted media sharing is enabled,if not we FAIL. 
open(EMM_SETTINGS, "/usr/openv/netbackup/bin/admincmd/nbemmcmd -listsettings -machinename $master_server |") or die "Can't open nbemmcmd : $!";
while(<EMM_SETTINGS>)
{
 if ( /^UNRESTRICTED_SHARING="no"/ )
  {
   print "Media sharing not enabled, this script will not work \n";
   print "EMM says:  $_  \n";
   exit 1;
  }
}
close EMM_SETTINGS;

# Make sure the user know what's going on.
print "YOU ARE ABOUT TO MOVE ALL MEDIA OWNERSHIP FROM $old_mediaserver TO $new_mediaserver \n";
print "Are you sure of what you are doing ? \n"; 
print "type AGREE to continue:";
$answer = <STDIN>;
chomp($answer);
if (  $answer ne "AGREE" ) {
  print "\n";
  print "Stopping execution";
  exit 1;
 }

# Now we call the function to retrive the media information from the EMM database
query_emm();

# Main part of the program. For each media owned by old media server we call bpmedia to move ownership to new mediserver
print "\n";
print "Number of media in EMM found for processing analyze: " . keys( %lastwritehost ) . ".\n";
while ( my ($key, $value) = each(%lastwritehost) ) {
        if ( $value eq $old_mediaserver) {
         if ( system("/usr/openv/netbackup/bin/admincmd/bpmedia -movedb -m $key -oldserver $old_mediaserver -newsvr_group UNRESTRICTED_SHARING_GROUP -newserver $new_mediaserver") != 0) { print "Failed moving media $key \n"; 
         } else { print "Succefully moved media $key \n"; }
        }
       }

# Check if we missed something. Call the function query_emm to get a fresh copy of the media assigment.
query_emm();
while ( my ($key, $value) = each(%lastwritehost) ) {
       if ( $value eq $old_mediaserver) {
        print "\n";
        print "Media $key still belong to $old_mediaserver. Is $old_mediaserver still active ?. Please investigate and re-run the script";
       } 
 }

