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