Debian Codegraph @

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