Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Re: Re: Problem with subroutine

by coec (Chaplain)
on Dec 09, 2001 at 19:44 UTC ( [id://130528]=note: print w/replies, xml ) Need Help??


in reply to Re: Re: Problem with subroutine
in thread Problem with subroutine

OK, OK, here's the entire thing. Please, be gentle, I'm a sys admin, not a programmer...
#!/usr/bin/perl ###################################################################### # # Script name: assemble_erorrs.pl # # Author: Colin Coe # # Purpose: Assembles store error messages before emailling them # to the operators and systems group. # # History: <snipped> # # Conventions: Variable names are in uppercase (ie $HOME) # Function names are in proper case (ie &File_Length) # ###################################################################### ###################################################################### ## 'use' and 'require' section require "ctime.pl"; ###################################################################### ###################################################################### ## Define static variables # Misc vaiables $HOME = $ENV{"HOME"}; $DATE = &ctime(time); $MAIL_LIST = "me\@employer"; # File and directory name variables $STORE_LIST = "/store_list.full_listing"; $STORE_LIST_TMP = "/tmp/stores.$$"; $STORE_LIST_NEW = "/tmp/stores_new.$$"; $STORE_LIST_TMP_NEW = "/tmp/stores_tmp_new.$$"; $DELIVERY_DIR = $HOME . "/delivery"; $NO_CORRUPT_DIR = $HOME . "/no_corrupt_stores"; $OUTPUT_FILE = $HOME . "/store_errors"; $OUTPUT_FILE_CORRUPT = $HOME . "/store_corrupt"; $PING_OUTPUT = "/tmp/ping_check.out"; $ERR_LIST = "/tmp/err_list.$$"; $COR_LIST = "/tmp/cor_list.$$"; $NO_ERR_LIST = "/tmp/no_err_list.$$"; $NO_COR_LIST = "/tmp/no_cor_list.$$"; $TMP_LIST = "/tmp/tmp_file.$$"; $REPORTED_ERROR = "/tmp/err_report.$$"; $REPORTED_CORRUPT = "/tmp/cor_report.$$"; # File masks $ERROR_MASK = $DELIVERY_DIR . "error*"; $NO_ERROR_MASK = $DELIVERY_DIR . "no_error*"; $CORRUPT_MASK = $NO_CORRUPT_DIR . "corrupt*"; $NO_CORRUPT_MASK = $NO_CORRUPT_DIR . "no_corrupt*"; # Counter variables $NUM_NO_ERRORS = &Num_Files($NO_ERROR_MASK); $NUM_ERRORS = &Num_Files($ERROR_MASK); $NUM_NO_CORRUPT = &Num_Files($NO_CORRUPT_MASK); $NUM_CORRUPT = &Num_Files($CORRUPT_MASK); $NUM_STORES = &File_Length($STORE_LIST); $NUM_ERR_REPORTED = ($NUM_ERRORS + $NUM_NO_ERRORS) - $NUM_STORES; $NUM_COR_REPORTED = ($NUM_CORRUPT + $NUM_NO_CORRUPT) - $NUM_STORES; ###################################################################### ## Main program begins here # Do HA check and stop execution if not on bbsmce warn "About to do HA_Check\n"; &HA_Check || exit; #if (&HA_Check) || die "Exiting because running on wrong node\n"; open(OUTFILE, "> $OUTPUT_FILE") || die "Could not open $OUTPUT_FILE fo +r writing: $!\n"; &Outfile_Header; &Errors; &Corrupt; close(OUTFILE); # Email result &Clean_Up; exit; # End of main program ###################################################################### ## Functions and sub routines # Function to return the number of lines in a file # Returns the number of lines in the file sub File_Length { local($FILE) = chomp($_); $COUNT = 0; open(INFILE, "< $FILE") || die "Can't open $FILE for reading: +$!\n"; while (<INFILE>) { $COUNT += 1; } close(INFILE); $COUNT; } # Sub routine to print header to output file sub Outfile_Header { print OUTFILE "<snip> listing for " . $DATE; } # Sub routine to print error section header to output file sub Error_Section_Header { print OUTFILE "\nError Section\n"; } # Sub routine to print corruption section header to output file sub Corruption_Section_Header { print OUTFILE "\nCorruption Section\n"; } # Function to return the number of files matching the file mask, also # populates $ERR_LIST # Returns: -1 on error # 0 no error files found # >0 the number of cfiles found sub Num_Files { local($MASK, $LIST) = chomp(local($_)); local($NUM_FILES); open(OUTFILE, "ls -la $MASK > $TMP_FILE"); if ($? = 0) { &File_Length($TMP_FILE); while (<OUTFILE>) { $STRING = local($_); ($JUNK, $STORE) = split('.'); open(OUTFILE, ">> $LIST"); print OUTFILE $STORE . "\n"; close(OUTFILE); } close(OUTFILE); } else { $NUM_FILES=0; } $NUM_FILES; } # Function to test which node the script is running on # Returns: 0 if P01 # 1 if P02, P03 or P04 sub HA_Check { if ("`netstat -i | grep -i P01`" eq "P01") { return(0); } else { return(1); } close(INFILE); } sub Errors { &Error_Section_Header; &Num_Files($ERROR_MASK, $ERR_LIST); &Num_Files($NO_ERROR_MASK, $NO_ERR_LIST); print OUTFILE "There are a total of " . $NUM_STORES . " stores +\n"; print OUTFILE "\tThere were " . $NUM_ERR_REPORTED . " stores t +hat reported in.\n"; print OUTFILE "\tThere were " . $NUM_STORES - $NUM_ERR_REPORTE +D . " that did not report in.\n"; open(ERRFILE, "`cat $ERR_LIST $NO_ERR_LIST $STORE_LIST | sort +| uniq -u` > $REPORTED_ERROR"); close(ERRFILE); open(INFILE, $REPORTED_ERROR); while (<INFILE>) { print OUTFILE "\t\t" . chomp($_) . "\n"; } close(INFILE); print OUTFILE "\tThere were " . $NUM_ERR . " that reported err +ors.\n"; open(ERRFILE, "> $ERR_LIST"); while (<ERRFILE>) { print OUTFILE "\t\t" . chomp($_) . "\n"; } close(ERRFILE); } sub Corrupt { &Corrupt_Section_Header; &Num_Files($CORRUPT_MASK, $COR_LIST); &Num_Files($NO_CORRUPT_MASK, $NO_COR_LIST); print OUTFILE "There are a total of " . $NUM_STORES . " stores +\n"; print OUTFILE "\tThere were " . $NUM_COR_REPORTED . " stores t +hat reported in.\n"; print OUTFILE "\tThere were " . $NUM_STORES - $NUM_COR_REPORTE +D . " that did not report in.\n"; open(CORFILE, "`cat $COR_LIST $NO_COR_LIST $STORE_LIST | sort +| uniq -u` > $REPORTED_CORRUPT"); close(CORFILE); open(INFILE, $REPORTED_CORRUPT); while (<INFILE>) { print OUTFILE "\t\t" . chomp($_) . "\n"; } close(INFILE); print OUTFILE "\tThere were " . $NUM_COR . " that reported err +ors.\n"; open(CORFILE, "> $COR_LIST"); while (<CORFILE>) { print OUTFILE "\t\t" . chomp($_) . "\n"; } close(CORFILE); } # Sub routine to remove temporary files sub Clean_Up { unlink($STORE_LIST_TMP); unlink($STORE_LIST_NEW); unlink($STORE_LIST_TMP_NEW); unlink($OUTPUT_FILE); unlink($OUTPUT_FILE_CORRUPT); unlink($ERR_LIST); unlink($COR_LIST); unlink($NO_ERR_LIST); unlink($NO_COR_LIST); unlink($TMP_LIST); unlink($REPORTED_ERROR); unlink($REPORTED_CORRUPT); }

Replies are listed 'Best First'.
Re: Re: Re: Re: Problem with subroutine
by wog (Curate) on Dec 09, 2001 at 21:59 UTC
    A rewrite (yes, I've changed the coding style slightly...): (Be forewarned that this is very much untested.)

    #!/usr/bin/perl -w use strict; my $home = $ENV{HOME} || (getpwuid($<))[7]; my $mail_to = 'me@employer'; # XXX when is this used? my $outfile = "$home/store_errors"; my $delivery_dir = "$home/delivery"; my $no_curropt_dir = "$home/no_curropt_stores"; my $error_pat = "$delivery_dir/error*"; my $no_error_pat = "$delivery_dir/no_error*"; my $corrupt_pat = "$no_curropt_dir/corrupt*"; my $no_corrupt_pat = "$no_curropt_dir/no_corrupt*"; my $store_list = "/store_list.full_listing"; my $date = scalar localtime; exit unless `netstat -i` =~ /^P01$/mi; sub store_name_map { map { (split /\./, $_, 2)[1] } @_ } sub uniques { my %seen; $seen{$_}++ for @_; return sort grep { $seen{$_} == 1 } keys %seen; } open STORES, "<$store_list" or die "opening $store_list for reading: $ +!\n"; my @stores = <STORES>; close STORES; my @errors = store_name_map(glob($error_pat)); my @no_errors = store_name_map(glob($no_error_pat)); my @error_union= uniques(@stores,@errors,@no_errors) my @corrupt = store_name_map(glob($curropt_pat)); my @no_curropt= store_name_map(glob($no_curropt_pat)); my @curropt_union= unqiues(@stores,@corrupt,@no_curropt); my $store_count = scalar(@stores); my $error_count = @errors + @no_errors; # - $store_count; my $curropt_count = @curropt + @no_curropt; # - $store_count; # XXX what were the - $NUM_STORES for? my $no_error_count = $store_count - $error_count; my $no_curropt_count = $store_count - $curropt_count; open OUTFILE, ">$outfile" or die "opening $outfile (for writing): $!\n +"; local $" = "\n\t\t"; # proper array output format print OUTFILE <<"END"; <snip> listing for $date Error Section There were a total of $store_count stores. \tThere were $error_count stores that reported in. \tThere were $no_error_count that did not report in. \t\t@error_union \tThere were $error_count that reported errors. \t\t@errors Curroption Section There were a total of $store_count stores. \tThere were $curropt_count stores that reported in. \tThere were $no_curropt_count stores that did not report in. \t\t@corrupt_union \tThere were $curropt_count that reported errors. \t\t@corrupt END

    I guessed in a few places what you wanted to do.

    This is probably a lot more wasteful then yours would be if it worked, due to not using temperorary files; that could probably be remidied with a bit of work. I hope that this helps you learn how to code better...

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://130528]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (9)
As of 2024-04-19 07:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found