284e68f99aa7abd7e8383ddd709c7244a015656a
[debian/amanda] / application-src / amlog-script.pl
1 #!@PERL@
2 # Copyright (c) 2008, 2009, 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 use Getopt::Long;
24 use File::Basename;
25
26 package Amanda::Script::amlog_script;
27 use base qw(Amanda::Script);
28 use Amanda::Config qw( :getconf :init );
29 use Amanda::Debug qw( :logging );
30 use Amanda::Util qw( :constants );
31 use Amanda::Paths;
32 use Amanda::Constants;
33
34
35 sub new {
36     my $class = shift;
37     my ($execute_where, $config, $host, $disk, $device, $level, $index, $message, $collection, $record, $logfile, $text) = @_;
38     my $self = $class->SUPER::new($execute_where, $config);
39
40     $self->{execute_where} = $execute_where;
41     $self->{config}        = $config;
42     $self->{host}          = $host;
43     $self->{disk}          = $disk;
44     $self->{device}        = $device;
45     $self->{level}         = [ @{$level} ]; # Copy the array
46     $self->{index}         = $index;
47     $self->{message}       = $message;
48     $self->{collection}    = $collection;
49     $self->{record}        = $record;
50     $self->{logfile}       = $logfile;
51     $self->{text}          = $text;
52
53     return $self;
54 }
55
56 sub setup() {
57     my $self = shift;
58
59     if (!defined $self->{logfile}) {
60         $self->print_to_server_and_die("property LOGFILE not set",
61                                        $Amanda::Script_App::ERROR);
62     }
63
64     my $dirname = File::Basename::dirname($self->{logfile});
65     if (! -e $dirname) {
66         $self->print_to_server_and_die("Directory '$dirname' doesn't exist",
67                                        $Amanda::Script_App::ERROR);
68     }
69     if (! -d $dirname) {
70         $self->print_to_server_and_die(
71                                 "Directory '$dirname' is not a directory",i
72                                 $Amanda::Script_App::ERROR);
73     }
74 }
75
76 sub command_support {
77    my $self = shift;
78
79    print "CONFIG YES\n";
80    print "HOST YES\n";
81    print "DISK YES\n";
82    print "MESSAGE-LINE YES\n";
83    print "MESSAGE-XML NO\n";
84    print "EXECUTE-WHERE YES\n";
85 }
86
87 #define a execute_on_* function for every execute_on you want the script to do
88 #something
89 sub command_pre_amcheck {
90    my $self = shift;
91
92    $self->setup();
93    $self->log_data("pre-amcheck");
94 }
95
96 sub command_pre_dle_amcheck {
97    my $self = shift;
98
99    $self->setup();
100    $self->log_data("pre-dle-amcheck");
101 }
102
103 sub command_pre_host_amcheck {
104    my $self = shift;
105
106    $self->setup();
107    $self->log_data("pre-host-amcheck");
108 }
109
110 sub command_post_amcheck {
111    my $self = shift;
112
113    $self->setup();
114    $self->log_data("post-amcheck");
115 }
116
117 sub command_post_dle_amcheck {
118    my $self = shift;
119
120    $self->setup();
121    $self->log_data("post-dle-amcheck");
122 }
123
124 sub command_post_host_amcheck {
125    my $self = shift;
126
127    $self->setup();
128    $self->log_data("post-host-amcheck");
129 }
130
131 sub command_pre_estimate {
132    my $self = shift;
133
134    $self->setup();
135    $self->log_data("pre-estimate");
136 }
137
138 sub command_pre_dle_estimate {
139    my $self = shift;
140
141    $self->setup();
142    $self->log_data("pre-dle-estimate");
143 }
144
145 sub command_pre_host_estimate {
146    my $self = shift;
147
148    $self->setup();
149    $self->log_data("pre-host-estimate");
150 }
151
152 sub command_post_estimate {
153    my $self = shift;
154
155    $self->setup();
156    $self->log_data("post-estimate");
157 }
158
159 sub command_post_dle_estimate {
160    my $self = shift;
161
162    $self->setup();
163    $self->log_data("post-dle-estimate");
164 }
165
166 sub command_post_host_estimate {
167    my $self = shift;
168
169    $self->setup();
170    $self->log_data("post-host-estimate");
171 }
172
173 sub command_pre_backup {
174    my $self = shift;
175
176    $self->setup();
177    $self->log_data("pre-backup");
178 }
179
180 sub command_pre_dle_backup {
181    my $self = shift;
182
183    $self->setup();
184    $self->log_data("pre-dle-backup");
185 }
186
187 sub command_pre_host_backup {
188    my $self = shift;
189
190    $self->setup();
191    $self->log_data("pre-host-backup");
192 }
193
194 sub command_post_backup {
195    my $self = shift;
196
197    $self->setup();
198    $self->log_data("post-backup");
199 }
200
201 sub command_post_dle_backup {
202    my $self = shift;
203
204    $self->setup();
205    $self->log_data("post-dle-backup");
206 }
207
208 sub command_post_host_backup {
209    my $self = shift;
210
211    $self->setup();
212    $self->log_data("post-host-backup");
213 }
214
215 sub command_pre_recover {
216    my $self = shift;
217
218    $self->setup();
219    $self->log_data("pre-recover");
220 }
221
222 sub command_post_recover {
223    my $self = shift;
224
225    $self->setup();
226    $self->log_data("post-recover");
227 }
228
229 sub command_pre_level_recover {
230    my $self = shift;
231
232    $self->setup();
233    $self->log_data("pre-level-recover");
234 }
235
236 sub command_post_level_recover {
237    my $self = shift;
238
239    $self->setup();
240    $self->log_data("post-level-recover");
241 }
242
243 sub command_inter_level_recover {
244    my $self = shift;
245
246    $self->setup();
247    $self->log_data("inter-level-recover");
248 }
249
250 sub log_data {
251    my $self = shift;
252    my($function) = shift;
253    my $log;
254
255    my $text = $self->{'text'} || "";
256    open($log, ">>$self->{logfile}") ||
257         $self->print_to_server_and_die(
258                         "Can't open logfile '$self->{logfile}' for append: $!",
259                         $Amanda::Script_App::ERROR);
260    print $log "$self->{action} $self->{config} $function $self->{execute_where} $self->{host} $self->{disk} $self->{device} ", join (" ", @{$self->{level}}), " $text\n";
261    close $log;
262 }
263
264 package main;
265
266 sub usage {
267     print <<EOF;
268 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>.
269 EOF
270     exit(1);
271 }
272
273 my $opt_execute_where;
274 my $opt_config;
275 my $opt_host;
276 my $opt_disk;
277 my $opt_device;
278 my @opt_level;
279 my $opt_index;
280 my $opt_message;
281 my $opt_collection;
282 my $opt_record;
283 my $opt_logfile;
284 my $opt_text;
285
286 Getopt::Long::Configure(qw{bundling});
287 GetOptions(
288     'execute-where=s' => \$opt_execute_where,
289     'config=s'        => \$opt_config,
290     'host=s'          => \$opt_host,
291     'disk=s'          => \$opt_disk,
292     'device=s'        => \$opt_device,
293     'level=s'         => \@opt_level,
294     'index=s'         => \$opt_index,
295     'message=s'       => \$opt_message,
296     'collection=s'    => \$opt_collection,
297     'record=s'        => \$opt_record,
298     'logfile=s'       => \$opt_logfile,
299     'text=s'          => \$opt_text
300 ) or usage();
301
302 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);
303
304 $script->do($ARGV[0]);
305