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