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