Imported Upstream version 3.1.0
[debian/amanda] / server-src / amcleanup.pl
1 #!@PERL@
2 # Copyright (c) 2008, 2010 Zmanda, Inc.  All Rights Reserved.
3
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.
7
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
11 # for more details.
12
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
16
17 # Contact information: Zmanda Inc., 465 S. Mathilda Ave., Suite 300
18 # Sunnyvale, CA 94086, USA, or: http://www.zmanda.com
19
20 use lib '@amperldir@';
21 use strict;
22
23 use Getopt::Long;
24 use Amanda::Config qw( :init :getconf config_dir_relative );
25 use Amanda::Util qw( :constants );
26 use Amanda::Paths;
27 use Amanda::Constants;
28 use Amanda::Process;
29
30 my $kill_enable=0;
31 my $process_alive=0;
32 my $verbose=0;
33
34 sub usage() {
35     print "Usage: amcleanup [-k] [-v] [-p] conf\n";
36     exit 1;
37 }
38
39 Amanda::Util::setup_application("amcleanup", "server", $CONTEXT_CMDLINE);
40
41 my $config_overrides = new_config_overrides($#ARGV+1);
42
43 Getopt::Long::Configure(qw(bundling));
44 GetOptions(
45     'k' => \$kill_enable,
46     'p' => \$process_alive,
47     'v' => \$verbose,
48     'help|usage' => \&usage,
49     'o=s' => sub { add_config_override_opt($config_overrides, $_[1]); },
50 ) or usage();
51
52 my $config_name = shift @ARGV or usage;
53
54 if ($kill_enable && $process_alive) {
55     die "amcleanup: Can't use -k and -p simultaneously\n";
56 }
57
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");
65     }
66 }
67
68 Amanda::Util::finish_setup($RUNNING_AS_DUMPUSER);
69
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";
76
77 if ( ! -e "$CONFIG_DIR/$config_name" ) {
78     die "Configuration directory '$CONFIG_DIR/$config_name' doesn't exist\n";
79 }
80 if ( ! -d "$CONFIG_DIR/$config_name" ) {
81     die "Configuration directory '$CONFIG_DIR/$config_name' is not a directory\n";
82 }
83
84 my $Amanda_process = Amanda::Process->new($verbose);
85 $Amanda_process->load_ps_table();
86
87 if (-f "$logfile") {
88     $Amanda_process->scan_log($logfile);
89 } elsif (!$process_alive) {
90     $Amanda_process->set_master_process($config_name, "amdump", "amflush");
91 }
92
93 $Amanda_process->add_child();
94
95 my $nb_amanda_process = $Amanda_process->count_process();
96 #if amanda processes are running
97 if ($nb_amanda_process > 0) {
98     if ($process_alive) {
99         exit 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";
104         exit 0;
105     } else { #kill the processes
106         Amanda::Debug::debug("Killing amanda process");
107         $Amanda_process->kill_process("SIGTERM");
108         my $count = 5;
109         my $pp;
110         while ($count > 0) {
111            $pp = $Amanda_process->process_running();
112            if ($pp > 0) {
113                 $count--;
114                 sleep 1;
115            } else {
116                 $count = 0;
117            }
118         }
119         if ($pp > 0) {
120             $Amanda_process->kill_process("SIGKILL");
121             sleep 2;
122             $pp = $Amanda_process->process_running();
123         }
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");
128     }
129 }
130
131 sub run_system {
132     my $check_code = shift;
133     my @cmd = @_;
134     my $pgm = $cmd[0];
135
136     system @cmd;
137     my $err = $?;
138     my $res = $!;
139
140     if ($err == -1) {
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;
151     }
152 }
153
154 # rotate log
155 if (-f $logfile) {
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);
160 } else {
161     print "amcleanup: no unprocessed logfile to clean up.\n";
162 }
163
164 my $tapecycle = getconf($CNF_TAPECYCLE);
165
166 # cleanup logfiles
167 chdir "$CONFIG_DIR/$config_name";
168 foreach my $pname ("amdump", "amflush") {
169     my $errfile = "$logdir/$pname";
170     if (-f $errfile) {
171         print "amcleanup: $errfile exists, renaming it.\n";
172         Amanda::Debug::debug("$errfile exists, renaming it");
173
174         # Keep debug log through the tapecycle plus a couple days
175         my $maxdays=$tapecycle + 2;
176
177         my $days=1;
178         # First, find out the last existing errfile,
179         # to avoid ``infinite'' loops if tapecycle is infinite
180         while ($days < $maxdays  && -f "$errfile.$days") {
181             $days++;
182         }
183
184         # Now, renumber the existing log files
185         while ($days >= 2) {
186             my $ndays = $days - 1;
187             rename("$errfile.$ndays", "$errfile.$days");
188             $days=$ndays;
189         }
190         rename($errfile, "$errfile.1");
191     }
192 }
193
194 if ($verbose) {
195     system $amcleanupdisk, "-v", $config_name;
196 } else {
197     system $amcleanupdisk, $config_name;
198 }
199
200 Amanda::Util::finish_application();