in reply to Re^6: Clear the contents of the text file
in thread Clear the contents of the text file

Your indentation makes the code difficult to read but it looks like you are iterating through @outagenodes for each line in the serverlist. I don't think that is what you want. Here's a cleaned up version of your code with some added error checks and logging. Uncomment the commands when you are finished testing.

#!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. # my $path = 'E:/Temp/INYROHS/'; 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 (<SRV>){ # skip blank lines s/\s+$//; s/^\s+//; next if /^$/; # validate records if (/.+/){ # change to suit 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'); # catch errors #unless ($fqdn =~/pattern/){ #printLog ("ERROR : Invalid fqdn '$fqdn' from '$hostname'"); #next; #} # command common to on/off my $cmd = "ovownodeutil.cmd -outage_node -unplanned -disable_heartbe +at -delete_msgs -node_name $fqdn -$maintMode "; printLog("Node $hostname >>> $maintMode"); # on command or off commands if ( $maintMode eq 'on' ){ printLog($cmd); #`$cmd`; } elsif( $maintMode eq 'off' ){ printLog("Bringing the server $NODE out of outage."); my $cmdresopcmona="ovdeploy -cmd \"ovc -restart opcmona\" -host $f +qdn"; my $cmdresopcle ="ovdeploy -cmd \"ovc -restart opcle\" -host $fqd +n"; printLog($cmd); #`$cmd`; printLog($cmdresopcmona); #`$cmdresopcmona`; printLog($cmdresopcle); #`$cmdresopcle`; } printLog("done with command\n"); } # 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"; }
poj

Replies are listed 'Best First'.
Re^8: Clear the contents of the text file
by shroh (Acolyte) on Jul 22, 2015 at 18:25 UTC
    Hi Poj, Thanks a lot for the refined code, It looks prettier. Just need to know the purpose of the below part of the code. But your code is working very well.
    my @nodes = (); while (<SRV>){ # skip blank lines s/\s+$//; s/^\s+//; next if /^$/; # validate records if (/.+/){ # change to suit push @nodes,$_; } else { printLog("ERROR Invalid record '$_'"); } }

      Do you mean the validate records ?

      Since you said in an earlier post This file is updated manually I thought you might like to do some validation on the values and assumed that server names would have some structure that you could check i.e. only [A-Za-z0-9_] characters perhaps ?

      I used /.+/ as a placeholder for your own regex.

      poj