2 # Copyright (c) 2005-2008 Zmanda Inc. All Rights Reserved.
4 # This program is free software; you can redistribute it and/or modify it
5 # under the terms of the GNU General Public License version 2 as published
6 # by the Free Software Foundation.
8 # This program is distributed in the hope that it will be useful, but
9 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
10 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 # You should have received a copy of the GNU General Public License along
14 # with this program; if not, write to the Free Software Foundation, Inc.,
15 # 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 # Contact information: Zmanda Inc., 465 S Mathlida Ave, Suite 300
18 # Sunnyvale, CA 94086, USA, or: http://www.zmanda.com
20 use lib '@amperldir@';
24 use Amanda::Config qw( :init :getconf config_dir_relative );
25 use Amanda::Util qw( :constants );
27 use Amanda::Constants;
35 if ( $Amanda::Constants::USE_VERSION_SUFFIXES =~ /^yes$/i ) {
36 $suf="-$Amanda::Constants::VERSION";
40 print "Usage: amcleanup [-k] [-v] [-p] conf\n";
44 Amanda::Util::setup_application("amcleanup", "server", $CONTEXT_SCRIPTUTIL);
46 my $config_overwrites = new_config_overwrites($#ARGV+1);
48 Getopt::Long::Configure(qw(bundling));
51 'p' => \$process_alive,
53 'help|usage' => \&usage,
54 'o=s' => sub { add_config_overwrite_opt($config_overwrites, $_[1]); },
57 my $config_name = shift @ARGV;
59 if ($kill_enable && $process_alive) {
60 die "amcleanup: Can't use -k and -p simultaneously\n";
63 config_init($CONFIG_INIT_EXPLICIT_NAME, $config_name);
64 apply_config_overwrites($config_overwrites);
65 my ($cfgerr_level, @cfgerr_errors) = config_errors();
66 if ($cfgerr_level >= $CFGERR_WARNINGS) {
67 config_print_errors();
68 if ($cfgerr_level >= $CFGERR_ERRORS) {
69 die("errors processing config file");
73 Amanda::Util::finish_setup($RUNNING_AS_DUMPUSER);
75 my $logdir=config_dir_relative(getconf($CNF_LOGDIR));
76 my $logfile = "$logdir/log";
77 my $amreport="$sbindir/amreport$suf";
78 my $amlogroll="$amlibexecdir/amlogroll$suf";
79 my $amtrmidx="$amlibexecdir/amtrmidx$suf";
80 my $amcleanupdisk="$amlibexecdir/amcleanupdisk$suf";
82 if ( ! -e "$CONFIG_DIR/$config_name" ) {
83 die "Configuration directory '$CONFIG_DIR/$config_name' doesn't exist\n";
85 if ( ! -d "$CONFIG_DIR/$config_name" ) {
86 die "Configuration directory '$CONFIG_DIR/$config_name' is not a directory\n";
89 my $Amanda_process = Amanda::Process->new($verbose);
90 $Amanda_process->load_ps_table();
93 $Amanda_process->scan_log($logfile);
94 } elsif (!$process_alive) {
95 #check amdump/amflush process
96 foreach my $pname ("amdump", "amflush") {
97 my $pid = `ps -ef|grep -w ${pname}|grep -w ${config_name}| grep -v grep | awk '{print \$2}'`;
100 $Amanda_process->set_master($pname, $pid);
105 $Amanda_process->add_child();
107 my $nb_amanda_process = $Amanda_process->count_process();
108 #if amanda processes are running
109 if ($nb_amanda_process > 0) {
110 if ($process_alive) {
112 } elsif (!$kill_enable) {
113 print "amcleanup: ", $Amanda_process->{master_pname}, " Process is running at PID ", $Amanda_process->{master_pid}, " for $config_name configuration.\n";
114 print "amcleanup: Use -k option to stop all the process...\n";
115 print "Usage: amcleanup [-k] conf\n";
117 } else { #kill the processes
118 $Amanda_process->kill_process("SIGTERM");
122 $pp = $Amanda_process->process_running();
131 $Amanda_process->kill_process("SIGKILL");
133 $pp = $Amanda_process->process_running();
135 print "amcleanup: ", $nb_amanda_process, " Amanda processes were found running.\n";
136 print "amcleanup: $pp processes failed to terminate.\n";
142 system $amreport, $config_name;
143 system $amlogroll, $config_name;
144 system $amtrmidx, $config_name;
146 print "amcleanup: no unprocessed logfile to clean up.\n";
149 my $tapecycle = getconf($CNF_TAPECYCLE);
152 chdir "$CONFIG_DIR/$config_name";
153 foreach my $pname ("amdump", "amflush") {
154 my $errfile = "$logdir/$pname";
156 print "amcleanup: $errfile exists, renaming it.\n";
158 # Keep debug log through the tapecycle plus a couple days
159 my $maxdays=$tapecycle + 2;
162 # First, find out the last existing errfile,
163 # to avoid ``infinite'' loops if tapecycle is infinite
164 while ($days < $maxdays && -f "$errfile.$days") {
168 # Now, renumber the existing log files
170 my $ndays = $days - 1;
171 rename("$errfile.$ndays", "$errfile.$days");
174 rename($errfile, "$errfile.1");
179 system $amcleanupdisk, "-v", $config_name;
181 system $amcleanupdisk, $config_name;