changeset 11:b19df18aa19a

updated command line option parsing and logging
author casties
date Wed, 23 Mar 2005 13:12:08 +0100
parents 4417be0e2f07
children 620aad237f57
files archivecheck.pl archiver.pl unarchiver.pl
diffstat 3 files changed, 59 insertions(+), 45 deletions(-) [+]
line wrap: on
line diff
--- a/archivecheck.pl	Fri Jan 21 15:33:26 2005 +0100
+++ b/archivecheck.pl	Wed Mar 23 13:12:08 2005 +0100
@@ -16,7 +16,7 @@
 #
 
 # program version
-my $version = "0.4 (30.11.2004)";
+my $version = "0.4.1 (15.2.2005)";
 
 # read command line parameters
 my $args = parseargs;
@@ -322,8 +322,8 @@
 		logger("ERROR", "archived $ft ($atime) is older than file on disk ($ftime)!");
 		$errcnt++;
 	    } else {
-		logger("ERROR", "archived file $ft still on disk");
-		$errcnt++;
+		logger("WARNING", "archived file $ft still on disk");
+		$warncnt++;
 	    }
 	} else {
 	    logger("ERROR", "file on disk '$ft' is not in archive!");
@@ -399,6 +399,7 @@
 logger("INFO", "$errcnt errors");
 if ($errcnt == 0) {
     logger("DONE", "" . (scalar keys %archived_files) . " archived files OK");
+    exit 0;
 } else {
     logger("ABORT", "there were $errcnt errors!!");
     exit 1;
--- a/archiver.pl	Fri Jan 21 15:33:26 2005 +0100
+++ b/archiver.pl	Wed Mar 23 13:12:08 2005 +0100
@@ -16,7 +16,16 @@
 #
 
 # program version
-my $version = "0.5.2 (12.7.2004)";
+my $version = "0.6 (ROC 23.3.2005)";
+
+# short help
+my $shorthelp = "MPIWG archiver $version\nuse:\n  archiver [options] docpath\noptions:\n  -premigrate  don't delete archived files\n";
+
+# read command line parameters
+my $args = MPIWGStor::parseargs;
+
+# debug level
+my $debug = (exists $$args{'debug'}) ? $$args{'debug'} : 0;
 
 # rewrite XML file (necessary for archive date!)
 my $fix_xml = 1;
@@ -41,18 +50,18 @@
 #
 my $archprog = "/opt/tivoli/tsm/client/ba/bin/dsmc";
 if (! -x $archprog) {
-    print "ABORT: TSM client program '$archprog' missing!!\n";
+    logger('ABORT', "TSM client program '$archprog' missing!!\n");
     exit 1;
 }
 my $checkprog = "/usr/local/mpiwg/archive/metacheck";
 if (! -x $checkprog) {
-    print "ABORT: meta data checking program '$checkprog' missing!!\n";
+    logger('ABORT', "meta data checking program '$checkprog' missing!!\n");
     exit 1;
 }
 # log file for archiver
 my $log_file = "/var/log/mpiwg-archiver.log";
 if (! open LOG, ">>$log_file") {
-    print "ABORT: unable to write log file '$log_file'!!\n";
+    logger('ABORT', "unable to write log file '$log_file'!!\n");
     exit 1;
 }
 
@@ -60,25 +69,29 @@
 # check parameters that were passed to the program
 #
 if ($#ARGV < 0) {
-    print "ABORT: no document directory given!\n";
+    print $shorthelp;
     exit 1;
 }
-my $docdir = $ARGV[0];
+my $docdir = $$args{'path'};
 # strip double slashes
 $docdir =~ s/\/\//\//;
 # strip trailing slashes
 $docdir =~ s/\/+$//;
 if (! -d $docdir) {
-    print "ABORT: document directory \'$docdir\' doesn't exist!\n";
+    logger('ABORT', "document directory \'$docdir\' doesn't exist!\n");
     exit 1;
 }
-if (($#ARGV > 0)&&($ARGV[1] eq "-premigrate")) {
-    $delete_data_files = 0;
+# don't delete archived files with "-premigrate"
+if (exists $$args{'premigrate'}) {
+    $delete_data_files = not $$args{'premigrate'};
+}
+if ($delete_data_files) {
+    logger('INFO', "going to remove successfully archived files from disk");
 }
 
 my $metafile = "$docdir/index.meta";
 if (! -f $metafile) {
-    print "ABORT: metadata index file \'$metafile\' doesn't exist!\n";
+    logger('ABORT', "metadata index file \'$metafile\' doesn't exist!\n");
     exit 1;
 }
 
@@ -109,7 +122,7 @@
     # get archive-path
     $archname = MPIWGStor::sstrip($rootnode->findvalue('child::archive-path'));
     if (! $archname) {
-	print "ABORT: archive-name element missing!!\n";
+	logger('ABORT', "archive-name element missing!!\n");
 	exit 1;
     }
 
@@ -120,7 +133,7 @@
     foreach my $fn (@filenodes) {
 	my $name = MPIWGStor::sstrip($fn->findvalue('child::name'));
 	my $path = MPIWGStor::sstrip($fn->findvalue('child::path'));
-	print "FILE: ($path)$name\n";
+	logger('DEBUG', "FILE ($path)$name\n");
 	my $f = ($path) ? "$path/$name" : "$name";
 	$files{$f} = $name;
     }
@@ -130,7 +143,7 @@
     #
     my $stordatenode = ($rootnode->find('child::archive-storage-date'))->get_node(1);
     if ($stordatenode) {
-	print "WARNING: archive storage date exists! Resource already archived?\n";
+	logger('WARNING', "archive storage date exists! Resource already archived?\n");
 	$warncnt++;
 	# delete old date
 	$stordatenode->removeChildNodes;
@@ -173,7 +186,7 @@
 	    }
 	}
     } else {
-	print "ABORT: unable to start archive command '$archcmd'!!\n";
+	logger('ABORT', "unable to start archive command '$archcmd'!!\n");
 	exit 1;
     }
 
@@ -196,10 +209,10 @@
 	my $fp = "$docdir/$ft";
 	#print "  fp: $fp\n";
 	if ($$archived{$fp}) {
-	    print "DEBUG: $ft archived OK\n";
+	    logger('DEBUG', "$ft archived OK\n");
 	    $$archived{$fp} = "OK";
 	} else {
-	    print "ERROR: file '$ft' missing from archive!\n";
+	    logger('ERROR', "file '$ft' missing from archive!\n");
 	    $errcnt++;
 	}
     }
@@ -208,19 +221,19 @@
 	if ($$archived{$fa} ne "OK") {
 	    my ($fn, $fp) = MPIWGStor::split_file_path($fa);
 	    if ($MPIWGStor::index_files{$fn}) {
-		print "DEBUG: $fa ignored\n";
+		logger('DEBUG', "$fa ignored\n");
 		$na--;
 	    } else {
-		print "WARNING: $fa archived but not in list!\n";
+		logger('WARNING', "$fa archived but not in list!\n");
 		$warncnt++;
 	    }
 	}
     }
 
     if ($nt > $na) {
-	print "WARNING: less files were archived ($na vs. $nt)!\n";
+	logger('WARNING', "less files were archived ($na vs. $nt)!\n");
     } elsif ($na > $nt) {
-	print "WARNING: more files were archived ($na vs. $nt)!\n";
+	logger('WARNING', "more files were archived ($na vs. $nt)!\n");
     }
 
 }
@@ -248,9 +261,9 @@
 	next unless (-f $f);
 	# delete files
 	if (unlink $f) {
-	    print "INFO: remove $f ($fn)\n";
+	    logger('INFO', "remove $f ($fn)\n");
 	} else {
-	    print "ERROR: unable to delete $f!\n";
+	    logger('ERROR', "unable to delete $f!\n");
 	    $errcnt++;
 	}
     }
@@ -262,7 +275,7 @@
 	# dont't remove document dir (shouldn't be empty anyway)
 	next if ($d eq $docdir);
 	if (-d $d) {
-	    print "INFO: remove dir $d\n";
+	    logger('INFO', "remove dir $d\n");
 	    rmdir $d;
 	}
     }
@@ -279,7 +292,7 @@
     my ($files, $dir) = @_;
 
     if (! opendir DIR, $dir) {
-	print "ERROR: unable to read directory $dir!\n";
+	logger('ERROR', "unable to read directory $dir!\n");
 	$errcnt++;
 	return;
     }
@@ -293,28 +306,28 @@
 	    if (-f "$dir/$f") {
 		# $f is a file
 		if (unlink "$dir/$f") {
-		    print "INFO: removed $f\n";
+		    logger('INFO', "removed $f\n");
 		} else {
-		    print "ERROR: unable to delete $f!\n";
+		    logger('ERROR', "unable to delete $f!\n");
 		    $errcnt++;
 		}
 	    } elsif (-d _) {
 		# $f is a directory (unlink won't work)
 		if ((system 'rm', '-r', "$dir/$f") == 0) {
-		    print "INFO: removed directory $f\n";
+		    logger('INFO', "removed directory $f\n");
 		} else {
-		    print "ERROR: unable to delete directory $f!\n";
+		    logger('ERROR', "unable to delete directory $f!\n");
 		    $errcnt++;
 		}
 	    } else {
-		print "ERROR: funny object $dir/$f!\n";
+		logger('ERROR', "funny object $dir/$f!\n");
 		$errcnt++;
 	    }
 	} else {
 	    # $f is not in the list
 	    if (-d "$dir/$f") {
 		# recurse into directories
-		print "DEBUG: enter $dir/$f\n";
+		logger('DEBUG', "enter $dir/$f\n");
 		delete_all_files($files, "$dir/$f");
 	    }
 	}
@@ -326,7 +339,7 @@
 # main
 #
 
-print "START: archiver $version at $archdate\n";
+logger('START', "archiver $version at $archdate\n");
 
 # make shure the right user is running this program
 my $user = getlogin;
@@ -337,9 +350,9 @@
 
 # use metacheck first
 if (system("$checkprog $docdir >/dev/null") == 0) {
-    print "INFO: resource '$docdir' check OK\n";
+    logger('INFO', "resource '$docdir' check OK\n");
 } else {
-    print "ABORT: resource '$docdir' check failed!!\n";
+    logger('ABORT', "resource '$docdir' check failed!!\n");
     exit 1;
 }
 
@@ -354,10 +367,10 @@
 # check for .archived file
 if (-f "$docdir/.archived") {
     if (unlink "$docdir/.archived") {
-	print "WARNING: existing .archived file has been removed! Resource already archived?\n";
+	logger('WARNING', "existing .archived file has been removed! Resource already archived?\n");
 	$warncnt++;
     } else {
-	print "ERROR: unable to remove existing .archived file!\n";
+	logger('ERROR', "unable to remove existing .archived file!\n");
 	$errcnt++;
     }
 }
@@ -369,7 +382,7 @@
 
 # write new index.meta
 if ($errcnt > 0) {
-    print "ABORT: there were errors!\n";
+    logger('ABORT', "there were errors!\n");
     exit 1;
 } else {
     if ($fix_xml) {
@@ -381,7 +394,7 @@
 my $archived_files = run_archive();
 my $num_archfiles = scalar keys %$archived_files;
 
-print "INFO: $num_archfiles files archived\n";
+logger('INFO', "$num_archfiles files archived\n");
 
 # check list of archived files
 check_files($files_to_archive, $archived_files);
@@ -399,11 +412,11 @@
     }
 }
 
-print "INFO: $warncnt warnings\n";
-print "INFO: $errcnt errors\n";
+logger('INFO', "$warncnt warnings\n");
+logger('INFO', "$errcnt errors\n");
 if ($errcnt > 0) {
-    print "ABORT: there were errors! ($num_archfiles files archived) at ", stime(time), "\n";
+    logger('ABORT', "there were errors! ($num_archfiles files archived) at " . stime(time));
     exit 1;
 } else {
-    print "DONE: $num_archfiles files archived at ", stime(time), "\n";
+    logger('DONE', "$num_archfiles files archived at " . stime(time));
 }
--- a/unarchiver.pl	Fri Jan 21 15:33:26 2005 +0100
+++ b/unarchiver.pl	Wed Mar 23 13:12:08 2005 +0100
@@ -18,7 +18,7 @@
 #
 
 # program version
-my $version = "0.3 (20.1.2004)";
+my $version = "0.3 (20.1.2005)";
 
 # read command line parameters
 my $args = parseargs;