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