Imported Upstream version 3.3.3
[debian/amanda] / application-src / amlog-script.pl
1 #!@PERL@
2 # Copyright (c) 2008-2012 Zmanda, Inc.  All Rights Reserved.
3 #
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful, but
10 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 # or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 # for more details.
13 #
14 # You should have received a copy of the GNU General Public License along
15 # with this program; if not, write to the Free Software Foundation, Inc.,
16 # 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
17 #
18 # Contact information: Zmanda Inc., 465 S. Mathilda Ave., Suite 300
19 # Sunnyvale, CA 94086, USA, or: http://www.zmanda.com
20
21 use lib '@amperldir@';
22 use strict;
23 use warnings;
24 use Getopt::Long;
25 use File::Basename;
26
27 package Amanda::Script::amlog_script;
28 use base qw(Amanda::Script);
29 use Amanda::Config qw( :getconf :init );
30 use Amanda::Debug qw( :logging );
31 use Amanda::Util qw( :constants );
32 use Amanda::Paths;
33 use Amanda::Constants;
34
35
36 sub new {
37     my $class = shift;
38     my ($execute_where, $config, $host, $disk, $device, $level, $index, $message, $collection, $record, $logfile, $text) = @_;
39     my $self = $class->SUPER::new($execute_where, $config);
40
41     $self->{execute_where} = $execute_where;
42     $self->{config}        = $config;
43     $self->{host}          = $host;
44     $self->{disk}          = $disk;
45     $self->{device}        = $device;
46     $self->{level}         = [ @{$level} ]; # Copy the array
47     $self->{index}         = $index;
48     $self->{message}       = $message;
49     $self->{collection}    = $collection;
50     $self->{record}        = $record;
51     $self->{logfile}       = $logfile;
52     $self->{text}          = $text;
53
54     return $self;
55 }
56
57 sub setup() {
58     my $self = shift;
59
60     if (!defined $self->{logfile}) {
61         $self->print_to_server_and_die("property LOGFILE not set",
62                                        $Amanda::Script_App::ERROR);
63     }
64
65     my $dirname = File::Basename::dirname($self->{logfile});
66     if (! -e $dirname) {
67         $self->print_to_server_and_die("Directory '$dirname' doesn't exist",
68                                        $Amanda::Script_App::ERROR);
69     }
70     if (! -d $dirname) {
71         $self->print_to_server_and_die(
72                                 "Directory '$dirname' is not a directory",i
73                                 $Amanda::Script_App::ERROR);
74     }
75 }
76
77 sub command_support {
78    my $self = shift;
79
80    print "CONFIG YES\n";
81    print "HOST YES\n";
82    print "DISK YES\n";
83    print "MESSAGE-LINE YES\n";
84    print "MESSAGE-XML NO\n";
85    print "EXECUTE-WHERE YES\n";
86 }
87
88 #define a execute_on_* function for every execute_on you want the script to do
89 #something
90 sub command_pre_amcheck {
91    my $self = shift;
92
93    $self->setup();
94    $self->log_data("pre-amcheck");
95 }
96
97 sub command_pre_dle_amcheck {
98    my $self = shift;
99
100    $self->setup();
101    $self->log_data("pre-dle-amcheck");
102 }
103
104 sub command_pre_host_amcheck {
105    my $self = shift;
106
107    $self->setup();
108    $self->log_data("pre-host-amcheck");
109 }
110
111 sub command_post_amcheck {
112    my $self = shift;
113
114    $self->setup();
115    $self->log_data("post-amcheck");
116 }
117
118 sub command_post_dle_amcheck {
119    my $self = shift;
120
121    $self->setup();
122    $self->log_data("post-dle-amcheck");
123 }
124
125 sub command_post_host_amcheck {
126    my $self = shift;
127
128    $self->setup();
129    $self->log_data("post-host-amcheck");
130 }
131
132 sub command_pre_estimate {
133    my $self = shift;
134
135    $self->setup();
136    $self->log_data("pre-estimate");
137 }
138
139 sub command_pre_dle_estimate {
140    my $self = shift;
141
142    $self->setup();
143    $self->log_data("pre-dle-estimate");
144 }
145
146 sub command_pre_host_estimate {
147    my $self = shift;
148
149    $self->setup();
150    $self->log_data("pre-host-estimate");
151 }
152
153 sub command_post_estimate {
154    my $self = shift;
155
156    $self->setup();
157    $self->log_data("post-estimate");
158 }
159
160 sub command_post_dle_estimate {
161    my $self = shift;
162
163    $self->setup();
164    $self->log_data("post-dle-estimate");
165 }
166
167 sub command_post_host_estimate {
168    my $self = shift;
169
170    $self->setup();
171    $self->log_data("post-host-estimate");
172 }
173
174 sub command_pre_backup {
175    my $self = shift;
176
177    $self->setup();
178    $self->log_data("pre-backup");
179 }
180
181 sub command_pre_dle_backup {
182    my $self = shift;
183
184    $self->setup();
185    $self->log_data("pre-dle-backup");
186 }
187
188 sub command_pre_host_backup {
189    my $self = shift;
190
191    $self->setup();
192    $self->log_data("pre-host-backup");
193 }
194
195 sub command_post_backup {
196    my $self = shift;
197
198    $self->setup();
199    $self->log_data("post-backup");
200 }
201
202 sub command_post_dle_backup {
203    my $self = shift;
204
205    $self->setup();
206    $self->log_data("post-dle-backup");
207 }
208
209 sub command_post_host_backup {
210    my $self = shift;
211
212    $self->setup();
213    $self->log_data("post-host-backup");
214 }
215
216 sub command_pre_recover {
217    my $self = shift;
218
219    $self->setup();
220    $self->log_data("pre-recover");
221 }
222
223 sub command_post_recover {
224    my $self = shift;
225
226    $self->setup();
227    $self->log_data("post-recover");
228 }
229
230 sub command_pre_level_recover {
231    my $self = shift;
232
233    $self->setup();
234    $self->log_data("pre-level-recover");
235 }
236
237 sub command_post_level_recover {
238    my $self = shift;
239
240    $self->setup();
241    $self->log_data("post-level-recover");
242 }
243
244 sub command_inter_level_recover {
245    my $self = shift;
246
247    $self->setup();
248    $self->log_data("inter-level-recover");
249 }
250
251 sub log_data {
252    my $self = shift;
253    my($function) = shift;
254    my $log;
255
256    my $text = $self->{'text'} || "";
257    open($log, ">>$self->{logfile}") ||
258         $self->print_to_server_and_die(
259                         "Can't open logfile '$self->{logfile}' for append: $!",
260                         $Amanda::Script_App::ERROR);
261    print $log "$self->{action} $self->{config} $function $self->{execute_where} $self->{host} $self->{disk} $self->{device} ", join (" ", @{$self->{level}}), " $text\n";
262    close $log;
263 }
264
265 package main;
266
267 sub usage {
268     print <<EOF;
269 Usage: amlog-script <command> --execute-where=<client|server> --config=<config> --host=<host> --disk=<disk> --device=<device> --level=<level> --index=<yes|no> --message=<text> --collection=<no> --record=<yes|no> --logfile=<filename>.
270 EOF
271     exit(1);
272 }
273
274 my $opt_execute_where;
275 my $opt_config;
276 my $opt_host;
277 my $opt_disk;
278 my $opt_device;
279 my @opt_level;
280 my $opt_index;
281 my $opt_message;
282 my $opt_collection;
283 my $opt_record;
284 my $opt_logfile;
285 my $opt_text;
286
287 Getopt::Long::Configure(qw{bundling});
288 GetOptions(
289     'execute-where=s' => \$opt_execute_where,
290     'config=s'        => \$opt_config,
291     'host=s'          => \$opt_host,
292     'disk=s'          => \$opt_disk,
293     'device=s'        => \$opt_device,
294     'level=s'         => \@opt_level,
295     'index=s'         => \$opt_index,
296     'message=s'       => \$opt_message,
297     'collection=s'    => \$opt_collection,
298     'record=s'        => \$opt_record,
299     'logfile=s'       => \$opt_logfile,
300     'text=s'          => \$opt_text
301 ) or usage();
302
303 my $script = Amanda::Script::amlog_script->new($opt_execute_where, $opt_config, $opt_host, $opt_disk, $opt_device, \@opt_level, $opt_index, $opt_message, $opt_collection, $opt_record, $opt_logfile, $opt_text);
304
305 $script->do($ARGV[0]);
306