File Coverage

blib/lib/Paranoid/Log/Email.pm
Criterion Covered Total %
statement 48 59 81.3
branch 7 16 43.7
condition n/a
subroutine 12 12 100.0
pod 4 4 100.0
total 71 91 78.0


line stmt bran cond sub pod time code
1             # Paranoid::Log::Email -- Log Facility Email for paranoid programs
2             #
3             # (c) 2005 - 2015, Arthur Corliss
4             #
5             # $Id: lib/Paranoid/Log/Email.pm, 2.00 2016/05/13 19:49:51 acorliss Exp $
6             #
7             # This software is licensed under the same terms as Perl, itself.
8             # Please see http://dev.perl.org/licenses/ for more information.
9             #
10             #####################################################################
11              
12             #####################################################################
13             #
14             # Environment definitions
15             #
16             #####################################################################
17              
18             package Paranoid::Log::Email;
19              
20 1     1   96971 use strict;
  1         2  
  1         33  
21 1     1   6 use warnings;
  1         2  
  1         44  
22 1     1   6 use vars qw($VERSION);
  1         6  
  1         56  
23 1     1   7 use Paranoid;
  1         1  
  1         64  
24 1     1   6 use Paranoid::Debug qw(:all);
  1         2  
  1         206  
25 1     1   778 use Net::SMTP;
  1         51604  
  1         44  
26 1     1   420 use Net::Domain qw(hostfqdn);
  1         1397  
  1         76  
27              
28             ($VERSION) = ( q$Revision: 2.00 $ =~ /(\d+(?:\.\d+)+)/sm );
29              
30 1     1   5 use constant SMTP_DELAY => 30;
  1         1  
  1         389  
31              
32             my @tlevels = qw(debug informational notice warning critical alert emergency);
33              
34             #####################################################################
35             #
36             # Module code follows
37             #
38             #####################################################################
39              
40             sub init () {
41              
42             # Purpose: Exists purely for compliance.
43             # Returns: Boolean
44             # Usage: init();
45              
46 1     1 1 208 return 1;
47             }
48              
49             sub addLogger {
50              
51             # Purpose: Exists purely for compliance.
52             # Returns: Boolean
53             # Usage: startLogger();
54              
55 4     4 1 1687 my %record = @_;
56 4         6 my $rv = 1;
57              
58             # Validate required options
59 4 50       11 unless ( exists $record{options}{mailhost} ) {
60 0         0 Paranoid::ERROR = pdebug( 'failed to declare a mailhost', PDLEVEL1 );
61 0         0 $rv = 0;
62             }
63 4 50       7 unless ( exists $record{options}{recipient} ) {
64 0         0 Paranoid::ERROR = pdebug( 'failed to declare a recipient', PDLEVEL1 );
65 0         0 $rv = 0;
66             }
67              
68 4         9 return $rv;
69             }
70              
71             sub delLogger {
72              
73             # Purpose: Exists purely for compliance.
74             # Returns: Boolean
75             # Usage: stopLogger();
76              
77 4     4 1 1069 return 1;
78             }
79              
80             sub logMsg {
81              
82             # Purpose: Mails the passed message to the named recipient
83             # Returns: True (1) if successful, False (0) if not
84             # Usage: log($msgtime, $severity, $message, $name, $facility, $level,
85             # $scope);
86             # Usage: log($msgtime, $severity, $message, $name, $facility, $level,
87             # $scope, $mailhost);
88             # Usage: log($msgtime, $severity, $message, $name, $facility, $level,
89             # $scope, $mailhost, $recipient);
90             # Usage: log($msgtime, $severity, $message, $name, $facility, $level,
91             # $scope, $mailhost, $recipient, $sender);
92             # Usage: log($msgtime, $severity, $message, $name, $facility, $level,
93             # $scope, $mailhost, $recipient, $sender, $subject);
94              
95 4     4 1 1848 my %record = @_;
96 4         5 my $rv = 0;
97 4         4 my ( $sender, $subject );
98 0         0 my ( $smtp, $hostname, $data );
99              
100 4         10 pdebug( 'entering w/%s', PDLEVEL1, %record );
101 4         158 pIn();
102              
103 4 50       24 if ( defined $record{message} ) {
104              
105             # Get the system hostname
106 4         18 $hostname = hostfqdn();
107              
108             # Make sure something is set for the sender
109             $sender =
110             exists $record{options}{sender}
111             ? $record{options}{sender}
112 4 100       422 : "$ENV{USER}\@$hostname";
113              
114             # Make sure something is set for the subject
115             $subject =
116             exists $record{options}{subject}
117             ? $record{options}{subject}
118 4 50       17 : "ALERT from $ENV{USER}\@$hostname";
119              
120             # Compose the data block
121 4         40 $data = << "__EOF__";
122             To: $record{options}{recipient}
123             From: $sender
124             Subject: $subject
125              
126             This alert was sent out from $hostname by $ENV{USER} because of a log event which met the $tlevels[$record{severity}] level. The message of this event is as follows:
127              
128             $record{message}
129              
130             __EOF__
131              
132             pdebug(
133             'sending to %s via %s',
134             PDLEVEL2,
135             $record{options}{recipient},
136 4         25 $record{options}{mailhost} );
137              
138             # Try to open an SMTP connection
139 4 50       124 if ($smtp = Net::SMTP->new(
140             $record{options}{mailhost},
141             Timeout => SMTP_DELAY
142             )
143             ) {
144              
145             # Start the transaction
146 0 0       0 if ( $smtp->mail($sender) ) {
147              
148             # Send to recipient
149             Paranoid::ERROR = pdebug( 'server rejected recipient: %s',
150             PDLEVEL1, $record{options}{recipient} )
151 0 0       0 unless $smtp->to( $record{options}{recipient} );
152              
153             # Send the message
154 0         0 $rv = $smtp->data($data);
155              
156             # Log the error
157             } else {
158 0         0 Paranoid::ERROR =
159             pdebug( 'server rejected sender: %s', PDLEVEL1, $sender );
160 0         0 $rv = 0;
161             }
162              
163             # Close the connection
164 0         0 $smtp->quit;
165              
166             } else {
167              
168             # Failed to connect to the server!
169             Paranoid::ERROR = pdebug( 'couldn\'t connect to server : %s',
170 4         36828 PDLEVEL1, $record{options}{mailhost} );
171 4         142 $rv = 0;
172             }
173             }
174              
175 4         10 pOut();
176 4         20 pdebug( 'leaving w/rv: %s', PDLEVEL1, $rv );
177              
178 4         95 return $rv;
179             }
180              
181             1;
182              
183             __END__