Debian Codegraph @

application-src/amlog-script.pl

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