b16f6a69b259c9d22ae51fb9f36ef06329e85e02
[debian/amanda] / application-src / script-email.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
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);
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    $self->sendmail("pre-dle-amcheck");
69 }
70
71 sub command_pre_host_amcheck {
72    my $self = shift;
73    $self->sendmail("pre-host-amcheck");
74 }
75
76 sub command_post_dle_amcheck {
77    my $self = shift;
78    $self->sendmail("post-dle-amcheck");
79 }
80
81 sub command_post_host_amcheck {
82    my $self = shift;
83    $self->sendmail("post-host-amcheck");
84 }
85
86 sub command_pre_dle_estimate {
87    my $self = shift;
88    $self->sendmail("pre-dle-estimate");
89 }
90
91 sub command_pre_host_estimate {
92    my $self = shift;
93    $self->sendmail("pre-host-estimate");
94 }
95
96 sub command_post_dle_estimate {
97    my $self = shift;
98    $self->sendmail("post-dle-estimate");
99 }
100
101 sub command_post_host_estimate {
102    my $self = shift;
103    $self->sendmail("post-host-estimate");
104 }
105
106 sub command_pre_dle_backup {
107    my $self = shift;
108    $self->sendmail("pre-dle-backup");
109 }
110
111 sub command_pre_host_backup {
112    my $self = shift;
113    $self->sendmail("pre-host-backup");
114 }
115
116 sub command_post_dle_backup {
117    my $self = shift;
118    $self->sendmail("post-dle-backup");
119 }
120
121 sub command_post_host_backup {
122    my $self = shift;
123    $self->sendmail("post-host-backup");
124 }
125
126 sub command_pre_recover {
127    my $self = shift;
128    $self->sendmail("pre-recover");
129 }
130
131 sub command_post_recover {
132    my $self = shift;
133    $self->sendmail("post-recover");
134 }
135
136 sub command_pre_level_recover {
137    my $self = shift;
138    $self->sendmail("pre-level-recover");
139 }
140
141 sub command_post_level_recover {
142    my $self = shift;
143    $self->sendmail("post-level-recover");
144 }
145
146 sub command_inter_level_recover {
147    my $self = shift;
148    $self->sendmail("inter-level-recover");
149 }
150
151 sub sendmail {
152    my $self = shift;
153    my($function) = @_;
154    my $dest;
155    if ($self->{mailto}) {
156       my $destcheck = join ',', @{$self->{mailto}};
157       $destcheck =~ /^([a-zA-Z,]*)$/;
158       $dest = $1;
159    } else {
160       $dest = "root";
161    }
162    my @args = ( "-s", "$self->{config} $function $self->{host} $self->{disk} $self->{device} " . join (" ", @{$self->{level}}), $dest );
163    my $args = join(" ", @args);
164    debug("cmd: $Amanda::Constants::MAILER $args\n");
165    my $mail;
166    open $mail, '|-', $Amanda::Constants::MAILER, @args;
167    print $mail "$self->{config} $function $self->{host} $self->{disk} $self->{device} ", join (" ", @{$self->{level}}), "\n";
168    close $mail;
169 }
170
171 package main;
172
173 sub usage {
174     print <<EOF;
175 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>.
176 EOF
177     exit(1);
178 }
179
180 my $opt_execute_where;
181 my $opt_config;
182 my $opt_host;
183 my $opt_disk;
184 my $opt_device;
185 my @opt_level;
186 my $opt_index;
187 my $opt_message;
188 my $opt_collection;
189 my $opt_record;
190 my @opt_mailto;
191
192 Getopt::Long::Configure(qw{bundling});
193 GetOptions(
194     'execute-where=s' => \$opt_execute_where,
195     'config=s'        => \$opt_config,
196     'host=s'          => \$opt_host,
197     'disk=s'          => \$opt_disk,
198     'device=s'        => \$opt_device,
199     'level=s'         => \@opt_level,
200     'index=s'         => \$opt_index,
201     'message=s'       => \$opt_message,
202     'collection=s'    => \$opt_collection,
203     'record=s'        => \$opt_record,
204     'mailto=s'        => \@opt_mailto
205 ) or usage();
206
207 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);
208
209 $script->do($ARGV[0]);
210