3bfdfe63888cc23f837001dfec909acacfed059c
[debian/amanda] / server-src / amcleanup.pl
1 #!@PERL@
2 # Copyright (c) 2005-2008 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 Mathlida 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 my $suf = '';
35 if ( $Amanda::Constants::USE_VERSION_SUFFIXES =~ /^yes$/i ) {
36         $suf="-$Amanda::Constants::VERSION";
37 }
38
39 sub usage() {
40     print "Usage: amcleanup [-k] [-v] [-p] conf\n";
41     exit 1;
42 }
43
44 Amanda::Util::setup_application("amcleanup", "server", $CONTEXT_SCRIPTUTIL);
45
46 my $config_overwrites = new_config_overwrites($#ARGV+1);
47
48 Getopt::Long::Configure(qw(bundling));
49 GetOptions(
50     'k' => \$kill_enable,
51     'p' => \$process_alive,
52     'v' => \$verbose,
53     'help|usage' => \&usage,
54     'o=s' => sub { add_config_overwrite_opt($config_overwrites, $_[1]); },
55 ) or usage();
56
57 my $config_name = shift @ARGV;
58
59 if ($kill_enable && $process_alive) {
60     die "amcleanup: Can't use -k and -p simultaneously\n";
61 }
62
63 config_init($CONFIG_INIT_EXPLICIT_NAME, $config_name);
64 apply_config_overwrites($config_overwrites);
65 my ($cfgerr_level, @cfgerr_errors) = config_errors();
66 if ($cfgerr_level >= $CFGERR_WARNINGS) {
67     config_print_errors();
68     if ($cfgerr_level >= $CFGERR_ERRORS) {
69         die("errors processing config file");
70     }
71 }
72
73 Amanda::Util::finish_setup($RUNNING_AS_DUMPUSER);
74
75 my $logdir=config_dir_relative(getconf($CNF_LOGDIR));
76 my $logfile = "$logdir/log";
77 my $amreport="$sbindir/amreport$suf";
78 my $amlogroll="$amlibexecdir/amlogroll$suf";
79 my $amtrmidx="$amlibexecdir/amtrmidx$suf";
80 my $amcleanupdisk="$amlibexecdir/amcleanupdisk$suf";
81
82 if ( ! -e "$CONFIG_DIR/$config_name" ) {
83     die "Configuration directory '$CONFIG_DIR/$config_name' doesn't exist\n";
84 }
85 if ( ! -d "$CONFIG_DIR/$config_name" ) {
86     die "Configuration directory '$CONFIG_DIR/$config_name' is not a directory\n";
87 }
88
89 my $Amanda_process = Amanda::Process->new($verbose);
90 $Amanda_process->load_ps_table();
91
92 if (-f "$logfile") {
93     $Amanda_process->scan_log($logfile);
94 } elsif (!$process_alive) {
95     #check amdump/amflush process
96     foreach my $pname ("amdump", "amflush") {
97         my $pid = `ps -ef|grep -w ${pname}|grep -w ${config_name}| grep -v grep | awk '{print \$2}'`;
98         chomp $pid;
99         if ($pid ne "") {
100             $Amanda_process->set_master($pname, $pid);
101         }
102     }
103 }
104
105 $Amanda_process->add_child();
106
107 my $nb_amanda_process = $Amanda_process->count_process();
108 #if amanda processes are running
109 if ($nb_amanda_process > 0) {
110     if ($process_alive) {
111         exit 0;
112     } elsif (!$kill_enable) {
113         print "amcleanup: ", $Amanda_process->{master_pname}, " Process is running at PID ", $Amanda_process->{master_pid}, " for $config_name configuration.\n";
114         print "amcleanup: Use -k option to stop all the process...\n";
115         print "Usage: amcleanup [-k] conf\n";
116         exit 0;
117     } else { #kill the processes
118         $Amanda_process->kill_process("SIGTERM");
119         my $count = 5;
120         my $pp;
121         while ($count > 0) {
122            $pp = $Amanda_process->process_running();
123            if ($pp > 0) {
124                 $count--;
125                 sleep 1;
126            } else {
127                 $count = 0;
128            }
129         }
130         if ($pp > 0) {
131             $Amanda_process->kill_process("SIGKILL");
132             sleep 2;
133             $pp = $Amanda_process->process_running();
134         }
135         print "amcleanup: ", $nb_amanda_process, " Amanda processes were found running.\n";
136         print "amcleanup: $pp processes failed to terminate.\n";
137     }
138 }
139
140 # rotate log
141 if (-f $logfile) {
142     system $amreport, $config_name;
143     system $amlogroll, $config_name;
144     system $amtrmidx, $config_name;
145 } else {
146     print "amcleanup: no unprocessed logfile to clean up.\n";
147 }
148
149 my $tapecycle = getconf($CNF_TAPECYCLE);
150
151 # cleanup logfiles
152 chdir "$CONFIG_DIR/$config_name";
153 foreach my $pname ("amdump", "amflush") {
154     my $errfile = "$logdir/$pname";
155     if (-f $errfile) {
156         print "amcleanup: $errfile exists, renaming it.\n";
157
158         # Keep debug log through the tapecycle plus a couple days
159         my $maxdays=$tapecycle + 2;
160
161         my $days=1;
162         # First, find out the last existing errfile,
163         # to avoid ``infinite'' loops if tapecycle is infinite
164         while ($days < $maxdays  && -f "$errfile.$days") {
165             $days++;
166         }
167
168         # Now, renumber the existing log files
169         while ($days >= 2) {
170             my $ndays = $days - 1;
171             rename("$errfile.$ndays", "$errfile.$days");
172             $days=$ndays;
173         }
174         rename($errfile, "$errfile.1");
175     }
176 }
177
178 if ($verbose) {
179     system $amcleanupdisk, "-v", $config_name;
180 } else {
181     system $amcleanupdisk, $config_name;
182 }