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

Hi lnickt, I am getting the error here: E:\Temp\inyrohs>perl outage_nodes_updated.pl on Can't locate Path/Tiny.pm in @INC (@INC contains: E:/apps/Perl64/site/lib E:/apps/Perl64/lib .) at outage_nodes_updated.pl line 8. BEGIN failed--compilation aborted at outage_nodes_updated.pl line 8.
#!C:\Perl\bin\perl.exe use Win32; use IO::Handle; use File::Find; use strict; use warnings; use File::Copy qw(copy); use Path::Tiny qw/ path /; use feature qw/ say /; ###################################################################### +############## #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. # # # require "E:/Temp/inyrohs/omwNodeDetails.pm"; #expect values 'on|off' my ($SEC,$MIN,$HOUR, $DAY, $MON, $YEAR)= (localtime) [0..6]; my $year=$YEAR+1900; my $month=$MON+1; my $day=$DAY; my $date="$year\_$month\_$day"; my $LOG="E:/Temp/inyrohs/maintenanceMode_$date.log"; my $serverlist="E:/Temp/INYROHS/serverlist.txt"; open( MYFILE, "E:/Temp/INYROHS/serverlist.txt") or die "Can't open '$ +serverlist': $!"; my @outagenodes=path($MYFILE)->lines({chomp => 1}); #chomp(@outagenodes); #print "@outagenodes\n"; my $maintMode=$ARGV[0]; chomp($maintMode); open (LOG,">> $LOG") or die "Can't open $LOG file: $!\n"; printf LOG ("\nTime is %02d:%02d:%02d.\nStarting the maintenance mode +process to turn $maintMode outages.\n\n", $HOUR, $MIN, $SEC); #print "$date \n"; #print "starting the process \n"; #print "maintMode: $maintMode:\n"; while (<$MYFILE>){ s/\s+$//; s/^\s+//; next if /^$/; if($maintMode =~ m/on/) { foreach my $NODES (@outagenodes) { my @parts=split /\./, $NODES; my $hostname=$parts[0]; chomp($hostname); my $fqdn = getNodeAttributes($hostname,"PrimaryNodeName"); print "$hostname\n"; #printLog("Node: $NODES, processing...\n"); #print "entered loop \n"; my $cmd="ovownodeutil.cmd -outage_node -unplanned -disable_ +heartbeat -delete_msgs -node_name $fqdn -$maintMode "; print "$cmd\n"; `$cmd`; print "done with command\n"; } } elsif($maintMode=~ m/off/) { foreach my $NODES (@outagenodes) { my @parts=split /\./, $NODES; my $hostname=$parts[0]; chomp($hostname); my $fqdn = getNodeAttributes($hostname,"PrimaryNodeName"); print "$hostname\n"; printLog("Node: $NODES, processing...\n"); printLog("Bringing the server $NODES out of outage.\n"); my $cmd="ovownodeutil.cmd -outage_node -unplanned -disable_ +heartbeat -delete_msgs -node_name $fqdn -$maintMode"; my $cmdresopcmona="ovdeploy -cmd \"ovc -restart opcmona\" - +host $fqdn"; my $cmdresopcle="ovdeploy -cmd \"ovc -restart opcle\" -host + $fqdn"; print "$cmd\n"; `$cmd`; print "$cmdresopcmona\n"; `$cmdresopcmona`; print "$cmdresopcle\n"; `$cmdresopcle`; } } } my @append=(); path($file)->append(@append, {truncate =>1}); say "Done."; sub printLog { my ($logLine) = @_; chomp($logLine); $logLine=$logLine . "\n"; print "$logLine"; print LOG "$logLine"; }

Replies are listed 'Best First'.
Re^7: Clear the contents of the text file
by 1nickt (Canon) on Jul 22, 2015 at 02:49 UTC

    You were already given the answer. You get that error because you don't have the Path::Tiny module installed.

    Note that you do not need Path::Tiny, or any module, to read in the lines from a file, or to "clear the contents of the file." You can do it without additional modules, including by using Perl's built-int truncate function, as roboticus told you in the first response to your question.

    The way forward always starts with a minimal test.
Re^7: Clear the contents of the text file
by poj (Abbot) on Jul 22, 2015 at 09:01 UTC

    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.

    poj
      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