2 # Copyright (c) 2008-2012 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;
32 use Amanda::Debug qw( debug );
39 print "Usage: amcleanup [-k] [-v] [-p] [-r] conf\n";
43 Amanda::Util::setup_application("amcleanup", "server", $CONTEXT_CMDLINE);
45 my $config_overrides = new_config_overrides($#ARGV+1);
47 debug("Arguments: " . join(' ', @ARGV));
48 Getopt::Long::Configure(qw(bundling));
50 'version' => \&Amanda::Util::version_opt,
52 'p' => \$process_alive,
54 'r' => \$clean_holding,
55 'help|usage' => \&usage,
56 'o=s' => sub { add_config_override_opt($config_overrides, $_[1]); },
59 my $config_name = shift @ARGV or usage;
61 if ($kill_enable && $process_alive) {
62 die "amcleanup: Can't use -k and -p simultaneously\n";
65 set_config_overrides($config_overrides);
66 config_init($CONFIG_INIT_EXPLICIT_NAME, $config_name);
67 my ($cfgerr_level, @cfgerr_errors) = config_errors();
68 if ($cfgerr_level >= $CFGERR_WARNINGS) {
69 config_print_errors();
70 if ($cfgerr_level >= $CFGERR_ERRORS) {
71 die("errors processing config file");
75 Amanda::Util::finish_setup($RUNNING_AS_DUMPUSER);
77 my $logdir=config_dir_relative(getconf($CNF_LOGDIR));
78 my $logfile = "$logdir/log";
79 my $amreport="$sbindir/amreport";
80 my $amtrmidx="$amlibexecdir/amtrmidx";
81 my $amcleanupdisk="$sbindir/amcleanupdisk";
83 if ( ! -e "$CONFIG_DIR/$config_name" ) {
84 die "Configuration directory '$CONFIG_DIR/$config_name' doesn't exist\n";
86 if ( ! -d "$CONFIG_DIR/$config_name" ) {
87 die "Configuration directory '$CONFIG_DIR/$config_name' is not a directory\n";
90 my $Amanda_process = Amanda::Process->new($verbose);
91 $Amanda_process->load_ps_table();
94 $Amanda_process->scan_log($logfile);
95 } elsif (!$process_alive) {
96 $Amanda_process->set_master_process($config_name, "amdump", "amflush");
99 $Amanda_process->add_child();
101 my $nb_amanda_process = $Amanda_process->count_process();
102 #if amanda processes are running
103 if ($nb_amanda_process > 0) {
104 if ($process_alive) {
106 } elsif (!$kill_enable) {
107 print "amcleanup: ", $Amanda_process->{master_pname}, " Process is running at PID ", $Amanda_process->{master_pid}, " for $config_name configuration.\n";
108 print "amcleanup: Use -k option to stop all the process...\n";
109 print "Usage: amcleanup [-k] conf\n";
111 } else { #kill the processes
112 Amanda::Debug::debug("Killing amanda process");
113 $Amanda_process->kill_process("SIGTERM");
117 $pp = $Amanda_process->process_running();
126 $Amanda_process->kill_process("SIGKILL");
128 $pp = $Amanda_process->process_running();
130 print "amcleanup: ", $nb_amanda_process, " Amanda processes were found running.\n";
131 print "amcleanup: $pp processes failed to terminate.\n";
132 Amanda::Debug::debug("$nb_amanda_process Amanda processes were found running");
133 Amanda::Debug::debug("$pp processes failed to terminate");
138 my $check_code = shift;
147 Amanda::Debug::debug("failed to execute $pgm: $res");
148 print "failed to execute $pgm: $res\n";
149 } elsif ($err & 127) {
150 Amanda::Debug::debug(sprintf("$pgm died with signal %d, %s coredump",
151 ($err & 127), ($err & 128) ? 'with' : 'without'));
152 printf "$pgm died with signal %d, %s coredump\n",
153 ($err & 127), ($err & 128) ? 'with' : 'without';
154 } elsif ($check_code && $err > 0) {
155 Amanda::Debug::debug(sprintf("$pgm exited with value %d", $err >> 8));
156 printf "$pgm exited with value %d %d\n", $err >> 8, $err;
162 Amanda::Debug::debug("Processing log file");
163 run_system(0, $amreport, $config_name, "--from-amdump");
165 my $ts = Amanda::Logfile::get_current_log_timestamp();
166 Amanda::Logfile::log_rename($ts);
168 run_system(1, $amtrmidx, $config_name);
170 print "amcleanup: no unprocessed logfile to clean up.\n";
173 my $tapecycle = getconf($CNF_TAPECYCLE);
176 chdir "$CONFIG_DIR/$config_name";
177 foreach my $pname ("amdump", "amflush") {
178 my $errfile = "$logdir/$pname";
180 print "amcleanup: $errfile exists, renaming it.\n";
181 Amanda::Debug::debug("$errfile exists, renaming it");
183 # Keep debug log through the tapecycle plus a couple days
184 my $maxdays=$tapecycle + 2;
187 # First, find out the last existing errfile,
188 # to avoid ``infinite'' loops if tapecycle is infinite
189 while ($days < $maxdays && -f "$errfile.$days") {
193 # Now, renumber the existing log files
195 my $ndays = $days - 1;
196 rename("$errfile.$ndays", "$errfile.$days");
199 rename($errfile, "$errfile.1");
204 push @amcleanupdisk, $amcleanupdisk;
205 push @amcleanupdisk, "-v" if $verbose;
206 push @amcleanupdisk, "-r" if $clean_holding;
207 push @amcleanupdisk, $config_name;
208 system @amcleanupdisk;
210 Amanda::Util::finish_application();