Make WWW::Mechanize do not exit on GET Errors

When using PERL and WWW::Mechanize, your script can stop if there is some error in GETing a URL (such as 404 Not Found, 403 Permission Denied). Instead of resuming, the script terminates. If you want the script to continue resuming, you have to pass

autocheck => 0
in the Mechanize class constructor as shown below

my $mech = WWW::Mechanize->new(autocheck => 0);

This makes sure that the mechanize library does not check for errors and exit automatically. Now its upto the programmer to check the response status and to proceed or retry or do the next step.

$url = ""; $res = $mech->get($url); if($res->is_success()){ ## Success } else { ## Failure } 02 Mar, 2010
Comments (2)
  • What about "Nagios::Plugin::WWW::Mechanize" ?

    Hi Arun,

    I'm basically facing the problem you're dealing with in your post : a "Mechanized" plugin dying abruptly upon getting a 404 HTTP response code. Actually, I'd like to handle this case myself to customize the returned message. The only difference between what I'm trying to do and your post is that I'm working with "Nagios::Plugin::WWW::Mechanize" instead of "WWW::Mechanize".

    This is where your "autocheck => 0" tip looks so interesting. I've just tried it, but when executing my plugin, it get the message :

    The following parameter was passed in the call to Nagios::Plugin::new but was not listed in the validation options: autocheck at /usr/lib/perl5/site_perl/5.8.8/Nagios/ line 34

    I then edited /usr/lib/perl5/site_perl/5.8.8/Nagios/ so that "autocheck" becomes a valid parameter (added "autocheck => 0" to "%args"). Running my plugin now answers :

    The following parameter was passed in the call to Nagios::Plugin::Getopt::_init but was not listed in the validation options: autocheck at /usr/lib/perl5/site_perl/5.8.8/Nagios/Plugin/ line 448
    Now adding "autocheck =>{ default => 0 }" by line 460 of /usr/lib/perl5/site_perl/5.8.8/Nagios/Plugin/, and retry executing my plugin.

    This time, no more error messages, but the script keeps on "dying" on the 404 error, and there's nothing I can do with it :-(

    Any hint on what I am missing ?

    By Httqm on 06 Jul, 2010
    • You can pass a WWW::Mechanize object

      Hi I havent used the Nagios WWW Mechanize, but a quick look in the docs of Nagios::WWW::Mechanize it accepts an WWW::Mechanize object to use internally, so you can initialize the WWW::Mechanize object and pass it . See the excerpt from the doc below for details.
      Nagios::Plugin::WWW::Mechanize->new( %opts ) Takes %opts. If $opts{mech} is specified and is an object, will check if it is a WWW::Mechanize object and die if not. If $opts{mech} is a hash ref, will pass those to a WWW::Mechanize->new() call. Will create a WWW::Mechanize object with autocheck => 0, otherwise any failures are exited immediately. Also looks for $opts{include_time}. Defaults to 1 which means that performance data for time will be returned. All other options are passed to Nagios::Plugin.
      By digitalpbk on 07 Jul, 2010
You may also like
On Facebook
Email Newsletter