#!C:\Perl\bin\perl.exe use strict; use warnings; # # This script is created to put the servers # in unplanned outage as part of the tasks that # are received to stop the monitoring on the servers # due to some maintenance activity on the servers. # Author : ROHIT SHARMA (INYROHS) my $path = 'E:/scripts/OutageNodes/'; require $path.'omwNodeDetails.pm'; # expect values 'on|off' my $maintMode = lc $ARGV[0] || ''; chomp($maintMode); unless ($maintMode =~ /^(on|off)$/){ die "Invalid mode $maintMode\n"; } # date/time my ($SEC,$MIN,$HOUR, $DAY, $MON, $YEAR)= (localtime) [0..6]; my $date = sprintf "%04d_%02d_%02d",$YEAR+1900,$MON+1,$DAY; my $time = sprintf "%02d:%02d:%02d",$HOUR,$MIN,$SEC; # log file my $logfile = $path . "maintenanceMode_$date.log"; open (LOG,'>>',$logfile) or die "Can't open LOG '$logfile': $!\n"; printLog ("\n=================== Date is $date Time is $time Starting the maintenance mode process to turn $maintMode outages."); # server file my $serverlist = $path.'serverlist.txt'; open( SRV,'<',$serverlist) or die "Can't open SVR '$serverlist': $!"; my @nodes = (); while (){ #This code is to remove any white spaces from the server list. # skip blank lines s/\s+$//; s/^\s+//; next if /^$/; # Validate records, it will make sure no unwanted characters are entered in server list. #if (/[A-Za-z0-9_]/){ push @nodes,$_; #} else { #printLog("ERROR Invalid record '$_'"); #} } # check serverlist had entries if (@nodes == 0){ printLog("ERROR No nodes found in $serverlist"); exit; } # process each node for my $NODE (@nodes){ my ($hostname) = split /\./, $NODE; my $fqdn = getNodeAttributes($hostname,'PrimaryNodeName'); if(length($fqdn) < 1) { printLog("No value returned from WMI, node ($NODE) doesn't exits in OMW."); next; } # command common to on/off my $cmd = "ovownodeutil.cmd -outage_node -unplanned -disable_heartbeat -delete_msgs -node_name $fqdn -$maintMode "; printLog("Node $hostname >>> $maintMode"); my $output=`$cmd`; # on command or off commands if ( $maintMode eq 'on' ){ printLog($cmd); `$cmd`; printLog($output); } elsif( $maintMode eq 'off' ){ printLog("Bringing the server $NODE out of outage."); ########################################################################### # If a node is leaving maintenance mode, the agent has to be recycled. # This will "reset" the monitors to re-alert if they are in an error state # my $cmdresopcmona="ovdeploy -cmd \"ovc -restart opcmona\" -host $fqdn"; my $cmdresopcle ="ovdeploy -cmd \"ovc -restart opcle\" -host $fqdn"; printLog($cmd); `$cmd`; printLog($output); # printLog($cmdresopcmona); # `$cmdresopcmona`; # printLog($cmdresopcle); # `$cmdresopcle`; } } # clear serverlist printLog ("Truncating $serverlist"); open( SRV,'>',$serverlist) or die "Can't open SRV '$serverlist': $!"; close SRV; # end close LOG; # screen and log sub printLog { my ($logLine) = @_; my $now = sprintf "%02d:%02d:%02d",(localtime)[2,1,0]; print "$logLine\n"; print LOG "$now $logLine\n"; }