Imported Upstream version 3.3.2
[debian/amanda] / application-src / script-email.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 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
25 package Amanda::Script::Script_email;
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, $mailto) = @_;
37     my $self = $class->SUPER::new($execute_where, $config);
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->{mailto}        = [ @{$mailto} ]; # Copy the array
50
51     return $self;
52 }
53
54 sub command_support {
55    my $self = shift;
56
57    print "CONFIG YES\n";
58    print "HOST YES\n";
59    print "DISK YES\n";
60    print "MESSAGE-LINE YES\n";
61    print "MESSAGE-XML NO\n";
62    print "EXECUTE-WHERE YES\n";
63 }
64
65 #define a execute_on_* function for every execute_on you want the script to do
66 #something
67 sub command_pre_dle_amcheck {
68    my $self = shift;
69
70    $self->sendmail("pre-dle-amcheck");
71 }
72
73 sub command_pre_host_amcheck {
74    my $self = shift;
75
76    $self->sendmail("pre-host-amcheck");
77 }
78
79 sub command_post_dle_amcheck {
80    my $self = shift;
81
82    $self->sendmail("post-dle-amcheck");
83 }
84
85 sub command_post_host_amcheck {
86    my $self = shift;
87
88    $self->sendmail("post-host-amcheck");
89 }
90
91 sub command_pre_dle_estimate {
92    my $self = shift;
93
94    $self->sendmail("pre-dle-estimate");
95 }
96
97 sub command_pre_host_estimate {
98    my $self = shift;
99
100    $self->sendmail("pre-host-estimate");
101 }
102
103 sub command_post_dle_estimate {
104    my $self = shift;
105
106    $self->sendmail("post-dle-estimate");
107 }
108
109 sub command_post_host_estimate {
110    my $self = shift;
111
112    $self->sendmail("post-host-estimate");
113 }
114
115 sub command_pre_dle_backup {
116    my $self = shift;
117
118    $self->sendmail("pre-dle-backup");
119 }
120
121 sub command_pre_host_backup {
122    my $self = shift;
123
124    $self->sendmail("pre-host-backup");
125 }
126
127 sub command_post_dle_backup {
128    my $self = shift;
129
130    $self->sendmail("post-dle-backup");
131 }
132
133 sub command_post_host_backup {
134    my $self = shift;
135
136    $self->sendmail("post-host-backup");
137 }
138
139 sub command_pre_recover {
140    my $self = shift;
141
142    $self->sendmail("pre-recover");
143 }
144
145 sub command_post_recover {
146    my $self = shift;
147
148    $self->sendmail("post-recover");
149 }
150
151 sub command_pre_level_recover {
152    my $self = shift;
153
154    $self->sendmail("pre-level-recover");
155 }
156
157 sub command_post_level_recover {
158    my $self = shift;
159
160    $self->sendmail("post-level-recover");
161 }
162
163 sub command_inter_level_recover {
164    my $self = shift;
165
166    $self->sendmail("inter-level-recover");
167 }
168
169 sub sendmail {
170    my $self = shift;
171    my($function) = @_;
172    my $dest;
173    if ($self->{mailto}) {
174       my $destcheck = join ',', @{$self->{mailto}};
175       $destcheck =~ /^([a-zA-Z,]*)$/;
176       $dest = $1;
177    } else {
178       $dest = "root";
179    }
180    my @args = ( "-s", "$self->{config} $function $self->{host} $self->{disk} $self->{device} " . join (" ", @{$self->{level}}), $dest );
181    my $args = join(" ", @args);
182    debug("cmd: $Amanda::Constants::MAILER $args\n");
183    my $mail;
184    open $mail, '|-', $Amanda::Constants::MAILER, @args;
185    print $mail "$self->{action} $self->{config} $function $self->{host} $self->{disk} $self->{device} ", join (" ", @{$self->{level}}), "\n";
186    close $mail;
187 }
188
189 package main;
190
191 sub usage {
192     print <<EOF;
193 Usage: script-email <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> --mailto=<email>.
194 EOF
195     exit(1);
196 }
197
198 my $opt_execute_where;
199 my $opt_config;
200 my $opt_host;
201 my $opt_disk;
202 my $opt_device;
203 my @opt_level;
204 my $opt_index;
205 my $opt_message;
206 my $opt_collection;
207 my $opt_record;
208 my @opt_mailto;
209
210 Getopt::Long::Configure(qw{bundling});
211 GetOptions(
212     'execute-where=s' => \$opt_execute_where,
213     'config=s'        => \$opt_config,
214     'host=s'          => \$opt_host,
215     'disk=s'          => \$opt_disk,
216     'device=s'        => \$opt_device,
217     'level=s'         => \@opt_level,
218     'index=s'         => \$opt_index,
219     'message=s'       => \$opt_message,
220     'collection=s'    => \$opt_collection,
221     'record=s'        => \$opt_record,
222     'mailto=s'        => \@opt_mailto
223 ) or usage();
224
225 my $script = Amanda::Script::Script_email->new($opt_execute_where, $opt_config, $opt_host, $opt_disk, $opt_device, \@opt_level, $opt_index, $opt_message, $opt_collection, $opt_record, \@opt_mailto);
226
227 $script->do($ARGV[0]);
228