File Coverage

blib/lib/URL/Check.pm
Criterion Covered Total %
statement 90 98 91.8
branch 29 36 80.5
condition 5 8 62.5
subroutine 14 14 100.0
pod 4 9 44.4
total 142 165 86.0


line stmt bran cond sub pod time code
1             package URL::Check;
2              
3             $URL::Check::VERSION = '0.09';
4             $URL::Check::AUTHORITY = 'cpan:ALEXMASS';
5              
6             =head1 NAME
7              
8             URL::Check - Check a list of URL and react (emails etc.) in case of failures.
9              
10             =head1 VERSION
11              
12             Version 0.09
13              
14             =cut
15              
16 4     4   106807 use 5.006;
  4         19  
17 4     4   34 use strict; use warnings;
  4     4   8  
  4         137  
  4         24  
  4         19  
  4         159  
18              
19 4     4   3720 use LWP::Simple qw/get/;
  4         890804  
  4         32  
20 4     4   4508 use Time::HiRes qw /gettimeofday/;
  4         6462  
  4         21  
21              
22             =head1 DESCRIPTION
23              
24             This module is not aimed at being used directly but via the script C.
25              
26             url-check --config=my-config.txt
27              
28             If no C<--config argument> is set, the value is taken from environment variable C.
29              
30             More examples can be found in the C directory but consist
31             in default parameter (mailto etc., then each url to be tested can be followed
32             by dedicated test (time.delay, xpath etc.)
33              
34             More info can be found L
35              
36             =head1 METHODS
37              
38             =head2 readConfig([configFile])
39              
40             Read the config file. Default file name is taken from C<$URL_CHECK_FILE>
41              
42             =cut
43              
44             our %config;
45             our @report;
46              
47             sub readConfig {
48             my $configFile = shift || $ENV{URL_CHECK_CONFIG}
49 6   50 6 1 6564 || die "ERROR: No config file is passed or env URL_CHECK_CONFIG is set.\n";
50              
51 5         11 my $FD;
52 5 100       269 open ($FD, "<$configFile")
53             || die "ERROR: Cannot open config file [$configFile]: $!\n";
54              
55 4         19 p_clearConfig();
56              
57             # $currenturl will contain at first the default map for handling error,
58             # then, when a url is encountered, it will contain the url's error handlers
59 4         9 my $currentUrl;
60 4         92 while (my $line = <$FD>) {
61 39         90 $line=~s/^#.*//; # remove comments
62 39         215 $line=~s/\s*$//; # end of line spaces
63 39 100       171 next unless $line =~/\S/; # skip emplty lines
64              
65 23 100       86 if ($line=~/^onerror/i) {
66 4   33     39 p_addOnErrorLine($line, $currentUrl || $config{default});
67 4         19 next;
68             }
69              
70 19 100       64 if ($line=~/^check/i) {
71 5         11 p_addCheckLine($line, $currentUrl);
72 5         21 next;
73             }
74              
75 14 50       69 if ($line=~/^(ftp|http|file):\/\//i) {
76 14         35 $currentUrl = p_addUrl($line);
77 14         68 next;
78             }
79              
80 0         0 die "ERROR: Cannot parse line: $line\n";
81             }
82              
83 4         57 close($FD);
84             }
85              
86             =head2 run()
87              
88             Run the configured tests, and store the result into the local @report
89              
90             =cut
91              
92             sub run {
93              
94 3     3 1 1222 undef @report;
95 3         7 foreach my $urlConfig (@{$config{urls}}) {
  3         12  
96 10         40 push @report, p_runOneUrl($urlConfig);
97             }
98             }
99              
100             =head2 submitReport(%report)
101              
102             Print on console or send by mail the error output
103              
104             =cut
105              
106             sub submitReport {
107 2     2 1 5938 my %report = @_;
108              
109 2 50       19 if ($config{default}{onError}{console}) {
110 2         61 print "ERROR REPORT: $report{subject}\n$report{contents}\n";
111             }
112              
113 2 50       22 if ($config{default}{onError}{mailto}) {
114 0         0 require Mail::Sendmail;
115 0         0 my $hostname = `hostname`;
116 0         0 chomp $hostname;
117             my %mail = (
118 0         0 To => join(',', @{$config{default}{onError}{mailto}}),
  0         0  
119             Subject => "[url-check] $report{subject}",
120             From => "url.check\@$hostname",
121             Message => "$report{contents}\n",
122             );
123              
124 0         0 warn "sending error report by mail: $report{subject}\n";
125 0 0       0 Mail::Sendmail::sendmail(%mail) or die $Mail::Sendmail::error
126             }
127             }
128              
129             =head2 errorReport()
130              
131             Build a map (subject => ..., content=> ...) with the errors after the run
132             return () if no error were detected
133              
134             =cut
135              
136             sub errorReport {
137 3     3 1 25 my @errors = grep {! $_->{success}} @report;
  10         46  
138              
139 3 100       26 unless (@errors) {
140 1         7 return ();
141             }
142              
143             (
144             subject => ''.scalar(@errors).' errors reported',
145 2         14 contents => join("\n", (map {$_->{url}." : ".$_->{message}} @errors))
  4         44  
146             );
147             }
148              
149             #
150             #
151             # PRIVATE METHODS
152              
153             sub p_addOnErrorLine {
154 11     11 0 4532 my ($line, $conf) = @_;
155              
156 11 50       87 die "ERROR: Cannot parse error line: $line"
157             unless $line=~/^onerror\.(.+?)=(.+)/i;
158              
159 11         45 my ($errorCat, $params) = ($1, $2);
160              
161 11 100       33 if ($errorCat eq 'mailto') {
162 2         9 my @tmp = split(/,/, $params);
163 2         9 $conf->{onError}{mailto}=\@tmp;
164 2         7 return;
165             }
166              
167 9 100       30 if ($errorCat eq 'console') {
168 8         58 $conf->{onError}{console}= $params =~ /\s*(y(es)?|t(rue)?|1)\s*$/i;
169 8         22 return;
170             }
171              
172 1         9 die "ERROR: Unknown onerror type [$errorCat]\n";
173             }
174              
175             sub p_addCheckLine {
176 5     5 0 8 my ($line, $conf) = @_;
177              
178 5 50       23 die "ERROR: Cannot parse error line: $line\n"
179             unless $line=~/^check\.(.+?)=(.+)/i;
180              
181 5         17 my ($cat, $params) = ($1, $2);
182              
183 5 100       13 if ($cat eq 'contains') {
184 3         5 push @{ $conf->{check}{contains}}, $params ;
  3         9  
185 3         23 return;
186             }
187              
188 2         8 $conf->{check}{$cat} = $params
189             }
190              
191             sub p_addUrl {
192 14     14 0 25 my ($line) = @_;
193              
194 14         55 my $h = {
195             url => $line,
196             check => {}
197             };
198              
199 14         22 push @{$config{urls}}, $h;
  14         35  
200 14         31 return $h;
201             }
202              
203             sub p_clearConfig {
204              
205 4     4 0 34 %config = (
206             default => { onError=>{} },
207             urls => [],
208             );
209             }
210              
211             sub p_runOneUrl {
212 10     10 0 22 my %urlConfig = %{$_[0]};
  10         63  
213              
214 10         29 my $url = $urlConfig{url};
215 10         48 my ($sec0, $micros0) = gettimeofday;
216 10         65 my $content = get($url);
217              
218 10 100       4983602 unless ($content) {
219             return {
220 2         29 url => $url,
221             success => 0,
222             message => 'cannot load content'
223             }
224             }
225              
226 8         57 my ($sec1, $micros1) = gettimeofday;
227 8         51 my $dtime = int(($sec1-$sec0)*1000 + ($micros1-$micros0)/1000);
228              
229 8 100 100     71 if ((exists $urlConfig{check}{overtime}) && ($urlConfig{check}{overtime} < $dtime)) {
230             return {
231 1         18 url => $url,
232             success => 0,
233             message => "overtime > $urlConfig{check}{overtime} (${dtime}ms)",
234             }
235             }
236              
237 7 100       29 if (exists $urlConfig{check}{contains}) {
238 2         8 my @searchFor = @{$urlConfig{check}{contains}};
  2         15  
239 2         10 foreach (@searchFor) {
240 2 100       31 if (index($content, $_) < 0) {
241             return {
242 1         16 url => $url,
243             success => 0,
244             message => "does not contains \"$_\"",
245             }
246             }
247             }
248             }
249              
250             return {
251 6         65 url => $url,
252             success => 1
253             };
254             }
255              
256             =head1 AUTHOR
257              
258             Alexandre Masselot, C<< >>
259              
260             Currently maintained by Mohammad S Anwar (MANWAR) C<< >>
261              
262             =head1 REPOSITORY
263              
264             L
265              
266             =head1 BUGS
267              
268             Please report any bugs or feature requests to C,
269             or through the web interface at L.
270             I will be notified, and then you'll automatically be notified of progress on
271             your bug as I make changes.
272              
273             =head1 SUPPORT
274              
275             You can find documentation for this module with the perldoc command.
276              
277             perldoc URL::Check
278              
279             You can also look for information at:
280              
281             =over 4
282              
283             =item * RT: CPAN's request tracker (report bugs here)
284              
285             L
286              
287             =item * AnnoCPAN: Annotated CPAN documentation
288              
289             L
290              
291             =item * CPAN Ratings
292              
293             L
294              
295             =item * Search CPAN
296              
297             L
298              
299             =back
300              
301             =head1 LICENSE AND COPYRIGHT
302              
303             Copyright 2011 Alexandre Masselot.
304              
305             This program is free software; you can redistribute it and/or modify it under the
306             terms of either: the GNU General Public License as published by the Free Software
307             Foundation; or the Artistic License.
308              
309             See http://dev.perl.org/licenses/ for more information.
310              
311             =cut
312              
313             1; # End of URL::Check