File Coverage

blib/lib/WWW/CheckHTML.pm
Criterion Covered Total %
statement 39 71 54.9
branch 0 26 0.0
condition 0 8 0.0
subroutine 14 16 87.5
pod n/a
total 53 121 43.8


line stmt bran cond sub pod time code
1             package WWW::CheckHTML;
2 2     2   49470 use strict;
  2         5  
  2         80  
3 2     2   10 use warnings;
  2         3  
  2         49  
4 2     2   2137 use HTTP::Tiny;
  2         139196  
  2         88  
5 2     2   8196 use Method::Signatures;
  2         181925  
  2         17  
6 2     2   3565 use Time::Piece;
  2         31879  
  2         256  
7 2     2   1871 use YAML::XS qw/LoadFile/;
  2         6334  
  2         157  
8 2     2   17 use Carp qw/croak/;
  2         4  
  2         103  
9 2     2   1899 use Email::Sender::Simple qw(sendmail);
  2         413837  
  2         19  
10 2     2   780 use Email::Simple;
  2         4  
  2         51  
11 2     2   30 use Email::Simple::Creator;
  2         5  
  2         52  
12 2     2   7360 use Email::Sender::Transport::SMTP;
  2         32844  
  2         222  
13              
14             =head1 NAME
15              
16             WWW::CheckHTML - check remote website HTML and send email alert via SMTP if check fails.
17              
18             =head1 VERSION
19              
20             Version 0.05
21              
22             =cut
23              
24             BEGIN {
25 2     2   23 require Exporter;
26 2         4 our $VERSION = 0.05;
27 2         39 our @ISA = qw(Exporter);
28 2         87 our @EXPORT = qw(checkPage);
29             }
30              
31             =head1 SYNOPSIS
32              
33             L<WWW::CheckHTML> exports a subroutine called checkPage to check remote web pages are retrievable and that they contain a specific HTML pattern. It will send an email via an SMTP server with the error found if either the page is not retrievable or the HTML pattern match fails.
34              
35             use WWW::CheckHTML;
36            
37             checkPage('http://www.google.com', '<title>', 'sillymoos@cpan.org', '/home/sillymoose/sendmail.yaml');
38              
39             =head1 CONFIGURATION
40              
41             L<WWW::CheckHTML> requires a yaml configuration file. The configuration file should have the following key / pair values:
42              
43             =over
44              
45             =item *
46              
47             from_email - this is the sending email address from which alerts will be sent
48              
49             =item *
50              
51             host - this is the SMTP host address for the sending email (e.g. smtp.google.com)
52              
53             =item *
54              
55             username - this is the sending email account username
56              
57             =item *
58              
59             password - this is the sending email account password
60              
61             =item *
62              
63             timeout - the number of seconds to wait before terminating the HTTP request. This is the only optional parameter and defaults to 30 seconds if not provided.
64              
65             =back
66              
67             Example yaml configuration file
68              
69             ---
70             host: smtp.google.com
71             username: sillymoos
72             password: itsasecret
73             from_email: sillymoos@gmail.com
74             timeout: 20
75              
76             =head1 SUBROUTINES
77              
78             =head2 checkPage
79              
80             Requires a url, regex pattern, an email address and optionally a path to a yaml configuration file. If the yaml filepath is not provided the checkPage method will search for 'sendmail.yaml' in the current directory context. checkPage initiates an HTTP get request for the url and if successful, will try to match the HTML regex pattern against the retrieved HTML. If either check fails, it will send an alert email to the email address provided.
81              
82             =cut
83              
84             my $CONFIG;
85              
86 2 0   2   353785 func checkPage( $url, $htmlPattern, $emailAddress, $yamlConfigPath? = 'sendmail.yaml') {
  0 0   0      
  0 0          
  0 0          
  0 0          
  0            
  0            
  0            
  0            
87              
88             # read sendmail.yaml
89 0 0         $CONFIG =
90             -e $yamlConfigPath
91             ? LoadFile($yamlConfigPath)
92             : croak "Error no sendmail.yaml not found $!";
93              
94 0 0 0       unless ( $CONFIG->{username}
      0        
95             and $CONFIG->{password}
96             and $CONFIG->{host} )
97             {
98 0           croak "Missing mandatory values in sendmail.yaml $!";
99             }
100 0   0       my $timeout = $CONFIG->{timeout} || 30;
101 0           my $response = HTTP::Tiny->new(timeout => $timeout)->get($url);
102 0           my $t = localtime;
103 0           my $datetime = $t->strftime;
104 0 0         unless ( $response->{success} ) {
105 0           _sendEmail(
106             $emailAddress,
107             'CheckHTML error',
108             "Error retrieving $url at $datetime. HTTP response: $response->{reason}\n",
109             );
110 0           return 0;
111             }
112 0 0         unless ( $response->{content} =~ /$htmlPattern/ ) {
113 0           _sendEmail(
114             $emailAddress,
115             'CheckHTML error',
116             "Error $url retrieved but HTML pattern not found at $datetime\n",
117             );
118 0           return 0;
119             }
120 0           return 1;
121             }
122              
123 2 0   2   8119 func _sendEmail( $emailAddress, $subject, $body ) {
  0 0   0      
  0 0          
  0 0          
  0            
  0            
  0            
  0            
124 0           my $email = Email::Simple->create(
125             header => [
126             To => $emailAddress,
127             From => 'alerts.checkhtml@gmail.com',
128             Subject => $subject,
129             ],
130             body => $body,
131             );
132              
133 0           my $transport = Email::Sender::Transport::SMTP->new(
134             {
135             host => $CONFIG->{host},
136             ssl => 1,
137             sasl_username => $CONFIG->{username},
138             sasl_password => $CONFIG->{password},
139             }
140             );
141 0           sendmail( $email, { transport => $transport } );
142             }
143              
144             1;
145              
146             =head1 AUTHOR
147              
148             David Farrell, C<< <sillymoos at cpan.org> >>, L<perltricks.com|http://perltricks.com>
149              
150             =head1 BUGS
151              
152             Please report any bugs or feature requests to C<bug-www-checkhtml at rt.cpan.org>, or through
153             the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=WWW-CheckHTML>. I will be notified, and then you'll
154             automatically be notified of progress on your bug as I make changes.
155              
156             =head1 SUPPORT
157              
158             You can find documentation for this module with the perldoc command.
159              
160             perldoc WWW::CheckHTML
161              
162              
163             You can also look for information at:
164              
165             =over 4
166              
167             =item * RT: CPAN's request tracker (report bugs here)
168              
169             L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=WWW-CheckHTML>
170              
171             =item * AnnoCPAN: Annotated CPAN documentation
172              
173             L<http://annocpan.org/dist/WWW-CheckHTML>
174              
175             =item * CPAN Ratings
176              
177             L<http://cpanratings.perl.org/d/WWW-CheckHTML>
178              
179             =item * Search CPAN
180              
181             L<http://search.cpan.org/dist/WWW-CheckHTML/>
182              
183             =back
184              
185             =head1 LICENSE AND COPYRIGHT
186              
187             Copyright 2013 David Farrell.
188              
189             This program is free software; you can redistribute it and/or modify it
190             under the terms of the the Artistic License (2.0). You may obtain a
191             copy of the full license at:
192              
193             L<http://www.perlfoundation.org/artistic_license_2_0>
194              
195             Any use, modification, and distribution of the Standard or Modified
196             Versions is governed by this Artistic License. By using, modifying or
197             distributing the Package, you accept this license. Do not use, modify,
198             or distribute the Package, if you do not accept this license.
199              
200             If your Modified Version has been derived from a Modified Version made
201             by someone other than you, you are nevertheless required to ensure that
202             your Modified Version complies with the requirements of this license.
203              
204             This license does not grant you the right to use any trademark, service
205             mark, tradename, or logo of the Copyright Holder.
206              
207             This license includes the non-exclusive, worldwide, free-of-charge
208             patent license to make, have made, use, offer to sell, sell, import and
209             otherwise transfer the Package with respect to any patent claims
210             licensable by the Copyright Holder that are necessarily infringed by the
211             Package. If you institute patent litigation (including a cross-claim or
212             counterclaim) against any party alleging that the Package constitutes
213             direct or contributory patent infringement, then this Artistic License
214             to you shall terminate on the date that such litigation is filed.
215              
216             Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
217             AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
218             THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
219             PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
220             YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
221             CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
222             CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
223             EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
224              
225              
226             =cut
227              
228             1; # End of WWW::CheckHTML