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@';
24 use Amanda::Config qw( :init :getconf config_dir_relative );
25 use Amanda::Util qw( :constants );
27 use Amanda::Constants;
35 print "Usage: amcleanup [-k] [-v] [-p] conf\n";
39 Amanda::Util::setup_application("amcleanup", "server", $CONTEXT_CMDLINE);
41 my $config_overrides = new_config_overrides($#ARGV+1);
43 Getopt::Long::Configure(qw(bundling));
46 'p' => \$process_alive,
48 'help|usage' => \&usage,
49 'o=s' => sub { add_config_override_opt($config_overrides, $_[1]); },
52 my $config_name = shift @ARGV or usage;
54 if ($kill_enable && $process_alive) {
55 die "amcleanup: Can't use -k and -p simultaneously\n";
58 set_config_overrides($config_overrides);
59 config_init($CONFIG_INIT_EXPLICIT_NAME, $config_name);
60 my ($cfgerr_level, @cfgerr_errors) = config_errors();
61 if ($cfgerr_level >= $CFGERR_WARNINGS) {
62 config_print_errors();
63 if ($cfgerr_level >= $CFGERR_ERRORS) {
64 die("errors processing config file");
68 Amanda::Util::finish_setup($RUNNING_AS_DUMPUSER);
70 my $logdir=config_dir_relative(getconf($CNF_LOGDIR));
71 my $logfile = "$logdir/log";
72 my $amreport="$sbindir/amreport";
73 my $amlogroll="$amlibexecdir/amlogroll";
74 my $amtrmidx="$amlibexecdir/amtrmidx";
75 my $amcleanupdisk="$amlibexecdir/amcleanupdisk";
77 if ( ! -e "$CONFIG_DIR/$config_name" ) {
78 die "Configuration directory '$CONFIG_DIR/$config_name' doesn't exist\n";
80 if ( ! -d "$CONFIG_DIR/$config_name" ) {
81 die "Configuration directory '$CONFIG_DIR/$config_name' is not a directory\n";
84 my $Amanda_process = Amanda::Process->new($verbose);
85 $Amanda_process->load_ps_table();
88 $Amanda_process->scan_log($logfile);
89 } elsif (!$process_alive) {
90 $Amanda_process->set_master_process($config_name, "amdump", "amflush");
93 $Amanda_process->add_child();
95 my $nb_amanda_process = $Amanda_process->count_process();
96 #if amanda processes are running
97 if ($nb_amanda_process > 0) {
100 } elsif (!$kill_enable) {
101 print "amcleanup: ", $Amanda_process->{master_pname}, " Process is running at PID ", $Amanda_process->{master_pid}, " for $config_name configuration.\n";
102 print "amcleanup: Use -k option to stop all the process...\n";
103 print "Usage: amcleanup [-k] conf\n";
105 } else { #kill the processes
106 Amanda::Debug::debug("Killing amanda process");
107 $Amanda_process->kill_process("SIGTERM");
111 $pp = $Amanda_process->process_running();
120 $Amanda_process->kill_process("SIGKILL");
122 $pp = $Amanda_process->process_running();
124 print "amcleanup: ", $nb_amanda_process, " Amanda processes were found running.\n";
125 print "amcleanup: $pp processes failed to terminate.\n";
126 Amanda::Debug::debug("$nb_amanda_process Amanda processes were found running");
127 Amanda::Debug::debug("$pp processes failed to terminate");
132 my $check_code = shift;
141 Amanda::Debug::debug("failed to execute $pgm: $res");
142 print "failed to execute $pgm: $res\n";
143 } elsif ($err & 127) {
144 Amanda::Debug::debug(sprintf("$pgm died with signal %d, %s coredump",
145 ($err & 127), ($err & 128) ? 'with' : 'without'));
146 printf "$pgm died with signal %d, %s coredump\n",
147 ($err & 127), ($err & 128) ? 'with' : 'without';
148 } elsif ($check_code && $err > 0) {
149 Amanda::Debug::debug(sprintf("$pgm exited with value %d", $err >> 8));
150 printf "$pgm exited with value %d %d\n", $err >> 8, $err;
156 Amanda::Debug::debug("Processing log file");
157 run_system(0, $amreport, $config_name, "--from-amdump");
158 run_system(1, $amlogroll, $config_name);
159 run_system(1, $amtrmidx, $config_name);
161 print "amcleanup: no unprocessed logfile to clean up.\n";
164 my $tapecycle = getconf($CNF_TAPECYCLE);
167 chdir "$CONFIG_DIR/$config_name";
168 foreach my $pname ("amdump", "amflush") {
169 my $errfile = "$logdir/$pname";
171 print "amcleanup: $errfile exists, renaming it.\n";
172 Amanda::Debug::debug("$errfile exists, renaming it");
174 # Keep debug log through the tapecycle plus a couple days
175 my $maxdays=$tapecycle + 2;
178 # First, find out the last existing errfile,
179 # to avoid ``infinite'' loops if tapecycle is infinite
180 while ($days < $maxdays && -f "$errfile.$days") {
184 # Now, renumber the existing log files
186 my $ndays = $days - 1;
187 rename("$errfile.$ndays", "$errfile.$days");
190 rename($errfile, "$errfile.1");
195 system $amcleanupdisk, "-v", $config_name;
197 system $amcleanupdisk, $config_name;
200 Amanda::Util::finish_application();