| 1 | package Nagios::Plugin::WWW::Mechanize; | ||
| 2 | 2 | ▶ | use 5.006; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 2 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 2 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 0 | ▶ | use Nagios::Plugin::Functions qw(:codes %ERRORS %STATUS_TEXT @STATUS_CODES); |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 6 | use Nagios::Plugin; | ||
| 7 | use WWW::Mechanize; | ||
| 8 | use Time::HiRes qw(gettimeofday tv_interval); | ||
| 9 | use Exporter; | ||
| 10 | use base qw(Exporter Nagios::Plugin); | ||
| 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 | my ($class, %opts) = @_; | ||
| 71 | my $include_time = 1; | ||
| 72 | my $mech; | ||
| 73 | if ($_ = delete $opts{mech}) { | ||
| 74 | if (ref $_ eq "HASH") { | ||
| 75 | $mech = WWW::Mechanize->new(%$_); | ||
| 76 | } elsif ( $_->isa("WWW::Mechanize") ) { | ||
| 77 | $mech = $_; | ||
| 78 | } else { | ||
| 79 | die "Invalid object passed into mech option"; | ||
| 80 | } | ||
| 81 | } | ||
| 82 | unless ($mech) { | ||
| 83 | $mech = WWW::Mechanize->new( autocheck => 0 ); | ||
| 84 | } | ||
| 85 | if (exists $opts{include_time}) { | ||
| 86 | $include_time = delete $opts{include_time}; | ||
| 87 | } | ||
| 88 | my $np = $class->SUPER::new( %opts ); | ||
| 89 | $np->include_time($include_time); | ||
| 90 | $np->mech($mech); | ||
| 91 | $np->total_time(0); | ||
| 92 | $np; | ||
| 93 | } | ||
| 94 | |||
| 95 | sub include_time { | ||
| 96 | my $self = shift; | ||
| 97 | if (@_) { $self->{include_time} = shift } else { $self->{include_time} } | ||
| 98 | } | ||
| 99 | |||
| 100 | sub total_time { | ||
| 101 | my $self = shift; if (@_) { $self->{total_time} = shift } else { $self->{total_time} }; | ||
| 102 | } | ||
| 103 | |||
| 104 | sub add_to_total_time { | ||
| 105 | my $self = shift; $self->total_time( $self->total_time + (shift || 0) ); | ||
| 106 | } | ||
| 107 | |||
| 108 | sub timer_start { | ||
| 109 | my $self = shift; $self->{timer_start} = [gettimeofday()]; | ||
| 110 | } | ||
| 111 | |||
| 112 | sub timer_end { | ||
| 113 | my $self = shift; $self->add_to_total_time( tv_interval( $self->{timer_start} ) ); | ||
| 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 | my $self = shift; | ||
| 128 | if (@_) { $self->{mech} = shift } else { $self->{mech} } | ||
| 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 | my ($self, $method, @args ) = @_; | ||
| 148 | $self->timer_start; | ||
| 149 | my $res = $self->mech->$method( @args ); | ||
| 150 | unless ($self->mech->success) { | ||
| 151 | $self->nagios_exit( CRITICAL, $self->mech->content ); | ||
| 152 | } | ||
| 153 | $self->timer_end; | ||
| 154 | $res; | ||
| 155 | } | ||
| 156 | |||
| 157 | sub submit_form { shift->wrap_mech_call("submit_form", @_); } | ||
| 158 | sub get { shift->wrap_mech_call("get", @_); } | ||
| 159 | |||
| 160 | =item content | ||
| 161 | |||
| 162 | Shortcut for $self->mech->content. | ||
| 163 | |||
| 164 | =cut | ||
| 165 | |||
| 166 | 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 | 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 | if ($self->include_time && $self->mech->success) { | ||
| 179 | $self->add_perfdata( | ||
| 180 | label => "time", | ||
| 181 | value => sprintf("%0.3f", $self->total_time), | ||
| 182 | uom => "s", | ||
| 183 | ); | ||
| 184 | } | ||
| 185 | $self->SUPER::nagios_exit( @args ); | ||
| 186 | } | ||
| 187 | |||
| 188 | =head1 AUTHOR | ||
| 189 | |||
| 190 | Ton Voon, E<lt>ton.voon@opsera.comE<gt> | ||
| 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; | ||