|
Revision 7565, 1.0 kB
(checked in by tokuhirom, 5 years ago)
|
|
added Notify::Email
|
| Line | |
|---|
| 1 | package App::MadEye::Plugin::Notify::Email; |
|---|
| 2 | use strict; |
|---|
| 3 | use warnings; |
|---|
| 4 | use base qw/Class::Component::Plugin/; |
|---|
| 5 | use Params::Validate; |
|---|
| 6 | use MIME::Lite; |
|---|
| 7 | use DateTime; |
|---|
| 8 | |
|---|
| 9 | sub notify : Hook('notify') { |
|---|
| 10 | my ($self, $context, $args) = @_; |
|---|
| 11 | |
|---|
| 12 | my $conf = $self->{config}->{config}; |
|---|
| 13 | my $from_addr = $conf->{from_addr} or die "missing from_addr"; |
|---|
| 14 | my $to_addr = $conf->{to_addr} or die "missing to_addr"; |
|---|
| 15 | |
|---|
| 16 | while (my ($plugin, $results) = each %$args) { |
|---|
| 17 | $plugin =~ s/.+::Agent:://; |
|---|
| 18 | |
|---|
| 19 | my $mail = MIME::Lite->new( |
|---|
| 20 | 'To' => $from_addr, |
|---|
| 21 | 'From' => $to_addr, |
|---|
| 22 | 'Subject' => "$plugin alert !!!", |
|---|
| 23 | 'Data' => $self->_format( $plugin, $results ), |
|---|
| 24 | ); |
|---|
| 25 | # warn $mail->as_string; |
|---|
| 26 | $mail->send; |
|---|
| 27 | } |
|---|
| 28 | } |
|---|
| 29 | |
|---|
| 30 | sub _format { |
|---|
| 31 | my ($self, $plugin, $results) = @_; |
|---|
| 32 | |
|---|
| 33 | my $res = $plugin . "\n\n"; |
|---|
| 34 | for my $result (@$results) { |
|---|
| 35 | $res .= "- $result->{target}\n"; |
|---|
| 36 | $res .= "$result->{message}\n"; |
|---|
| 37 | $res .= "\n\n"; |
|---|
| 38 | } |
|---|
| 39 | $res; |
|---|
| 40 | } |
|---|
| 41 | |
|---|
| 42 | 1; |
|---|