#!C:\Perl\bin\perl.exe use strict; use warnings; use Data::Dump 'pp'; #use diagnostics; # 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) main(); sub main { my $result = {}; # holds result of commands my %count=( 'error' => 0, 'OK' => 0, ); my $path = 'E:/scripts/OutageNodes/'; require $path.'omwNodeDetails.pm'; open_log($path.'maintenanceMode_'); my $mode = get_mode($ARGV[0]); if ($mode eq 'error'){ print_log("ERROR Invalid Mode '$ARGV[0]'"); ++$count{'error'}; } else { my $nodelist = get_node($path.'serverlist.txt'); if (@$nodelist == 0){ print_log("ERROR No nodes found in serverlist.txt"); ++$count{'error'}; } else { if ( $mode eq 'enable'){ $result = enable_unplanned_outage($nodelist); } if ($mode eq 'disable'){ $result = disable_unplanned_outage($nodelist); } # count errors from results process_error(\%count,$result); # clear serverlist if ($count{'error'} == 0){ print_log("Truncating serverlist"); open SRV,'>',$path.'serverlist.txt' or die "Can't open SRV '$path.serverlist': $!"; close SRV; } } } close_log(); print "Completed Errors = $count{'error'} OK = $count{'OK'}\n"; # Data::Dump pretty print pp $result; } # expect values 'enable|disable' sub get_mode { my $maintMode = lc shift; if ($maintMode ne 'enable' && $maintMode ne 'disable'){ $maintMode = "error"; } return $maintMode; } sub get_node { my ($infile) = @_; my @nodelist = (); ##checks if the file exists and pulls the info out if (-e $infile){ open INFILE, '<', $infile or die "Could not open $infile : $!"; print_log("Scanning $infile"); while (my $node = ){ chomp($node); my ($hostname) = split /\./, $node; my $fqdn = getNodeAttributes($hostname,'PrimaryNodeName'); if (length($fqdn) < 1) { print_log("No value returned from WMI, node ($node) doesn't exists in OMW."); } else { print_log("$node => $hostname => $fqdn"); push @nodelist,$fqdn; } } close INFILE; } else { print_log("ERROR Cannot open $infile"); } return \@nodelist; } sub enable_unplanned_outage { my ($nodelist) = @_; my %result = (); foreach my $node (@$nodelist) { print_log ("===================\n Putting the server $node into outage."); my $cmd = "ovownodeutil.cmd -outage_node -unplanned -disable_heartbeat -delete_msgs -node_name $node -on "; print_log($cmd); my $output = `$cmd`; print_log($output); $result{$node}{'ovownodeutil'} = $output; } return \%result; } # When the servers is brought out of maintenance # the agent has to be recycled to reset all the monitors. sub disable_unplanned_outage { my ($nodelist) = @_; my %result=(); foreach my $node (@$nodelist) { print_log("Node $node >>> off"); my $cmd = "ovownodeutil.cmd -outage_node -unplanned -disable_heartbeat -delete_msgs -node_name $node -off "; print_log($cmd); my $output = `$cmd`; print_log($output); $result{$node}{'ovownodeutil'} = $output; my $cmdresopcmona = "ovdeploy -cmd \"ovc -restart opcmona\" -host $node"; print_log($cmdresopcmona); $output = `$cmdresopcmona`; print_log($output); $result{$node}{'ovdeploy'} = $output; } return \%result; } # check results for error sub process_error { my ($count,$result) = @_; for my $node (sort keys %$result){ for my $cmd (sort keys %{$result->{$node}}){ my $text = $result->{$node}{$cmd}; if ( $text =~ /error/i){ ++$count->{'error'}; } else { ++$count->{'OK'}; } } } } sub print_log { my ($logLine) = @_; my $now = sprintf "%02d:%02d:%02d",(localtime)[2,1,0]; print LOG "$now $logLine\n"; } sub open_log { my ($filename) = @_; my ($day,$month,$year) = (localtime) [3..5]; my $date = sprintf "%04d%02d%02d",$year + 1900 , $month + 1 , $day ; my $logfile = $filename.$date.'.log'; open LOG,'>>',$logfile or die "Can't open LOG '$logfile': $!\n"; print LOG "\n--- START ---\n"; } sub close_log { print LOG "=== END ===\n"; close LOG; }