2 # Copyright (c) 2008, 2010 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. Mathilda Ave., Suite 300
18 # Sunnyvale, CA 94086, USA, or: http://www.zmanda.com
20 use lib '@amperldir@';
25 use Amanda::Config qw( :init :getconf config_dir_relative );
26 use Amanda::Util qw( :constants );
28 use Amanda::Constants;
37 print "Usage: amcleanup [-k] [-v] [-p] conf\n";
41 Amanda::Util::setup_application("amcleanup", "server", $CONTEXT_CMDLINE);
43 my $config_overrides = new_config_overrides($#ARGV+1);
45 Getopt::Long::Configure(qw(bundling));
48 'p' => \$process_alive,
50 'help|usage' => \&usage,
51 'o=s' => sub { add_config_override_opt($config_overrides, $_[1]); },
54 my $config_name = shift @ARGV or usage;
56 if ($kill_enable && $process_alive) {
57 die "amcleanup: Can't use -k and -p simultaneously\n";
60 set_config_overrides($config_overrides);
61 config_init($CONFIG_INIT_EXPLICIT_NAME, $config_name);
62 my ($cfgerr_level, @cfgerr_errors) = config_errors();
63 if ($cfgerr_level >= $CFGERR_WARNINGS) {
64 config_print_errors();
65 if ($cfgerr_level >= $CFGERR_ERRORS) {
66 die("errors processing config file");
70 Amanda::Util::finish_setup($RUNNING_AS_DUMPUSER);
72 my $logdir=config_dir_relative(getconf($CNF_LOGDIR));
73 my $logfile = "$logdir/log";
74 my $amreport="$sbindir/amreport";
75 my $amtrmidx="$amlibexecdir/amtrmidx";
76 my $amcleanupdisk="$amlibexecdir/amcleanupdisk";
78 if ( ! -e "$CONFIG_DIR/$config_name" ) {
79 die "Configuration directory '$CONFIG_DIR/$config_name' doesn't exist\n";
81 if ( ! -d "$CONFIG_DIR/$config_name" ) {
82 die "Configuration directory '$CONFIG_DIR/$config_name' is not a directory\n";
85 my $Amanda_process = Amanda::Process->new($verbose);
86 $Amanda_process->load_ps_table();
89 $Amanda_process->scan_log($logfile);
90 } elsif (!$process_alive) {
91 $Amanda_process->set_master_process($config_name, "amdump", "amflush");
94 $Amanda_process->add_child();
96 my $nb_amanda_process = $Amanda_process->count_process();
97 #if amanda processes are running
98 if ($nb_amanda_process > 0) {
101 } elsif (!$kill_enable) {
102 print "amcleanup: ", $Amanda_process->{master_pname}, " Process is running at PID ", $Amanda_process->{master_pid}, " for $config_name configuration.\n";
103 print "amcleanup: Use -k option to stop all the process...\n";
104 print "Usage: amcleanup [-k] conf\n";
106 } else { #kill the processes
107 Amanda::Debug::debug("Killing amanda process");
108 $Amanda_process->kill_process("SIGTERM");
112 $pp = $Amanda_process->process_running();
121 $Amanda_process->kill_process("SIGKILL");
123 $pp = $Amanda_process->process_running();
125 print "amcleanup: ", $nb_amanda_process, " Amanda processes were found running.\n";
126 print "amcleanup: $pp processes failed to terminate.\n";
127 Amanda::Debug::debug("$nb_amanda_process Amanda processes were found running");
128 Amanda::Debug::debug("$pp processes failed to terminate");
133 my $check_code = shift;
142 Amanda::Debug::debug("failed to execute $pgm: $res");
143 print "failed to execute $pgm: $res\n";
144 } elsif ($err & 127) {
145 Amanda::Debug::debug(sprintf("$pgm died with signal %d, %s coredump",
146 ($err & 127), ($err & 128) ? 'with' : 'without'));
147 printf "$pgm died with signal %d, %s coredump\n",
148 ($err & 127), ($err & 128) ? 'with' : 'without';
149 } elsif ($check_code && $err > 0) {
150 Amanda::Debug::debug(sprintf("$pgm exited with value %d", $err >> 8));
151 printf "$pgm exited with value %d %d\n", $err >> 8, $err;
157 Amanda::Debug::debug("Processing log file");
158 run_system(0, $amreport, $config_name, "--from-amdump");
160 my $ts = Amanda::Logfile::get_current_log_timestamp();
161 Amanda::Logfile::log_rename($ts);
163 run_system(1, $amtrmidx, $config_name);
165 print "amcleanup: no unprocessed logfile to clean up.\n";
168 my $tapecycle = getconf($CNF_TAPECYCLE);
171 chdir "$CONFIG_DIR/$config_name";
172 foreach my $pname ("amdump", "amflush") {
173 my $errfile = "$logdir/$pname";
175 print "amcleanup: $errfile exists, renaming it.\n";
176 Amanda::Debug::debug("$errfile exists, renaming it");
178 # Keep debug log through the tapecycle plus a couple days
179 my $maxdays=$tapecycle + 2;
182 # First, find out the last existing errfile,
183 # to avoid ``infinite'' loops if tapecycle is infinite
184 while ($days < $maxdays && -f "$errfile.$days") {
188 # Now, renumber the existing log files
190 my $ndays = $days - 1;
191 rename("$errfile.$ndays", "$errfile.$days");
194 rename($errfile, "$errfile.1");
199 system $amcleanupdisk, "-v", $config_name;
201 system $amcleanupdisk, $config_name;
204 Amanda::Util::finish_application();