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 => 0in 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 = "http://www.google.com";
$res = $mech->get($url);
if($res->is_success()){
## Success
}
else {
## Failure
}
02 Mar, 2010
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/Plugin.pm line 34
I then edited /usr/lib/perl5/site_perl/5.8.8/Nagios/Plugin.pm 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/Getopt.pm line 448
Now adding "autocheck =>{ default => 0 }" by line 460 of /usr/lib/perl5/site_perl/5.8.8/Nagios/Plugin/Getopt.pm, 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 ?
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.