File Coverage

blib/lib/Nagios/Plugin/WWW/Mechanize.pm
Criterion Covered Total %
statement 76 76 100.0
branch 20 20 100.0
condition 4 5 80.0
subroutine 21 21 100.0
pod 6 12 50.0
total 127 134 94.7


line stmt bran cond sub pod time code
1             package Nagios::Plugin::WWW::Mechanize;
2 2     2   30795 use 5.006;
  2         8  
  2         86  
3 2     2   10 use warnings;
  2         2  
  2         74  
4 2     2   11 use strict;
  2         9  
  2         80  
5 2     2   2041 use Nagios::Plugin::Functions qw(:codes %ERRORS %STATUS_TEXT @STATUS_CODES);
  2         117825  
  2         395  
6 2     2   1081 use Nagios::Plugin;
  2         13  
  2         49  
7 2     2   1039 use WWW::Mechanize;
  2         20  
  2         59  
8 2     2   2650 use Time::HiRes qw(gettimeofday tv_interval);
  2         4068  
  2         10  
9 2     2   361 use Exporter;
  2         3  
  2         80  
10 2     2   13 use base qw(Exporter Nagios::Plugin);
  2         3  
  2         1578  
11              
12             our @EXPORT = (@STATUS_CODES);
13              
14             our $VERSION = "0.13";
15              
16             =head1 NAME
17              
18             Nagios::Plugin::WWW::Mechanize - Login to a web page as a user and get data as a Nagios plugin
19              
20             =head1 SYNOPSIS
21              
22             use Nagios::Plugin::WWW::Mechanize;
23             $np = Nagios::Plugin::WWW::Mechanize->new(
24             usage => "Checks number of mailing list users"
25             );
26             $np->getopts;
27              
28             $np->get( "http://lists.opsview.org/lists/admin/opsview-users/members" );
29             $np->submit_form( form_name => "f", fields => { adminpw => "****" } );
30             $content = $np->content;
31             ($number_of_users) = ($content =~ /(\d+) members total/);
32             $np->nagios_exit( CRITICAL, "Cannot get number of users" ) unless defined $number_of_users;
33              
34             $np->add_perfdata(
35             label => "users",
36             value => $number_of_users,
37             );
38             $np->nagios_exit(
39             OK,
40             "Number of mailing list users: $number_of_users"
41             );
42              
43             =head1 DESCRIPTION
44              
45             This module ties Nagios::Plugin with WWW::Mechanize so that there's less
46             code in your perl script and the most common work is done for you.
47              
48             For example, the plugin will automatically call nagios_exit(CRITICAL, ...) if
49             a page is unavailable or a submit_form fails. The plugin will also keep a track
50             of the time for responses from the remote web server and output that as
51             performance data.
52              
53             =head1 INITIALISE
54              
55             =over 4
56              
57             =item Nagios::Plugin::WWW::Mechanize->new( %opts )
58              
59             Takes %opts. If $opts{mech} is specified and is an object, will check if it is a WWW::Mechanize object and die if not.
60             If $opts{mech} is a hash ref, will pass those to a WWW::Mechanize->new() call. Will create a WWW::Mechanize object
61             with autocheck => 0, otherwise any failures are exited immediately.
62              
63             Also looks for $opts{include_time}. Defaults to 1 which means that performance data for time will be returned.
64              
65             All other options are passed to Nagios::Plugin.
66              
67             =cut
68              
69             sub new {
70 12     12 1 4170 my ($class, %opts) = @_;
71 12         22 my $include_time = 1;
72 12         16 my $mech;
73 12 100       56 if ($_ = delete $opts{mech}) {
74 4 100       40 if (ref $_ eq "HASH") {
    100          
75 1         10 $mech = WWW::Mechanize->new(%$_);
76             } elsif ( $_->isa("WWW::Mechanize") ) {
77 1         2 $mech = $_;
78             } else {
79 1         8 die "Invalid object passed into mech option";
80             }
81             }
82 10 100       43 unless ($mech) {
83 8         39 $mech = WWW::Mechanize->new( autocheck => 0 );
84             }
85 10 100       98 if (exists $opts{include_time}) {
86 2         5 $include_time = delete $opts{include_time};
87             }
88 10         65 my $np = $class->SUPER::new( %opts );
89 10         126 $np->include_time($include_time);
90 10         27 $np->mech($mech);
91 10         23 $np->total_time(0);
92 10         30 $np;
93             }
94              
95             sub include_time {
96 21     21 0 44 my $self = shift;
97 21 100       53 if (@_) { $self->{include_time} = shift } else { $self->{include_time} }
  12         34  
  9         47  
98             }
99              
100             sub total_time {
101 36 100   36 0 3953 my $self = shift; if (@_) { $self->{total_time} = shift } else { $self->{total_time} };
  36         70  
  15         33  
  21         130  
102             }
103              
104             sub add_to_total_time {
105 5   50 5 0 75 my $self = shift; $self->total_time( $self->total_time + (shift || 0) );
  5         17  
106             }
107              
108             sub timer_start {
109 5     5 0 9 my $self = shift; $self->{timer_start} = [gettimeofday()];
  5         44  
110             }
111              
112             sub timer_end {
113 5     5 0 10 my $self = shift; $self->add_to_total_time( tv_interval( $self->{timer_start} ) );
  5         54  
114             }
115              
116             =head1 METHODS
117              
118             =over 4
119              
120             =item mech
121              
122             Returns the WWW::Mechanize object
123              
124             =cut
125              
126             sub mech {
127 40     40 1 3111 my $self = shift;
128 40 100       77 if (@_) { $self->{mech} = shift } else { $self->{mech} }
  10         25  
  30         173  
129             }
130              
131             =item get( @args )
132              
133             Calls $self->mech->get( @args ). If $self->include_time is set, will start a timer before the get, calculate the duration, and adds
134             it to a total timer.
135              
136             If the mech->get call failed, will call nagios_exit with a CRITICAL error.
137              
138             Returns the value from mech->get.
139              
140             =item submit_form( @args )
141              
142             Similar to get.
143              
144             =cut
145              
146             sub wrap_mech_call {
147 4     4 0 12 my ($self, $method, @args ) = @_;
148 4         12 $self->timer_start;
149 4         10 my $res = $self->mech->$method( @args );
150 4 100       25 unless ($self->mech->success) {
151 2         11 $self->nagios_exit( CRITICAL, $self->mech->content );
152             }
153 4         30 $self->timer_end;
154 4         15 $res;
155             }
156              
157 2     2 1 12 sub submit_form { shift->wrap_mech_call("submit_form", @_); }
158 2     2 1 13 sub get { shift->wrap_mech_call("get", @_); }
159              
160             =item content
161              
162             Shortcut for $self->mech->content.
163              
164             =cut
165              
166 1     1 1 6 sub content { shift->mech->content }
167              
168             =item nagios_exit
169              
170             Override to add performance data for time if required
171              
172             =cut
173              
174             sub nagios_exit {
175 6     6 1 1374 my ($self, @args) = @_;
176             # Only add the performance data if the last mech call was successful
177             # IE, only print time if everything was okay
178 6 100 100     18 if ($self->include_time && $self->mech->success) {
179 1         9 $self->add_perfdata(
180             label => "time",
181             value => sprintf("%0.3f", $self->total_time),
182             uom => "s",
183             );
184             }
185 6         61 $self->SUPER::nagios_exit( @args );
186             }
187              
188             =head1 AUTHOR
189              
190             Ton Voon, Eton.voon@opsera.comE
191              
192             =head1 COPYRIGHT AND LICENSE
193              
194             Copyright (C) 2006-2009 Opsera Limited. All rights reserved
195              
196             This library is free software; you can redistribute it and/or modify
197             it under the same terms as Perl itself, either Perl version 5.8.4 or,
198             at your option, any later version of Perl 5 you may have available.
199              
200             =cut
201              
202             1;