If gethostbyname fails it never recovers

A

Andrew DeFaria

I've been having problems with my ISP. One way it seems to manifest
itself is that I can not reach or contact my ISP's DNS servers. IOW a
simply nslookup google.com will fail.

So I tried writing a script that would monitor this. The script calls
gethostbyname for google.com every 15 minutes and logs the status. When
gethostbyname fails however it never comes back. My ISP and internet
connection may come back and nslookup at the command line will work
fine. But my script will report failure at the next and following 15
minute intervals. This is very unexpected. What am I doing wrong and is
there a way to "reset" things so that it will start working again.

Here's a snippet:

sub CheckDNS {
my $host = shift;

my @ipaddrs = gethostbyname $host;
my $status = $?;

if ($status ne 0 and $debug) {
debug "Host: $host ($status)";
} # if

return $status
} # CheckDNS

CheckDNS is called every 15 minutes with "google.com" as a parm. When a
failure happens $status = 2 and remains = 2 forever.
 
M

Mark Clements

Andrew said:
I've been having problems with my ISP. One way it seems to manifest
itself is that I can not reach or contact my ISP's DNS servers. IOW a
simply nslookup google.com will fail.

So I tried writing a script that would monitor this. The script calls
gethostbyname for google.com every 15 minutes and logs the status. When
gethostbyname fails however it never comes back. My ISP and internet
connection may come back and nslookup at the command line will work
fine. But my script will report failure at the next and following 15
minute intervals. This is very unexpected. What am I doing wrong and is
there a way to "reset" things so that it will start working again.

From

perldoc - f gethostbyname

For the gethost*() functions, if the "h_errno" variable is supported in
C, it will be returned to you via $? if the function call fails.

It appears to be undefined as to what $? will be set to if the call
*succeeds*, and thus it should not be relied upon to check for success
or failure of the call.

Here's a snippet:

sub CheckDNS {
my $host = shift;

my @ipaddrs = gethostbyname $host;
my $status = $?;

if ($status ne 0 and $debug) {
debug "Host: $host ($status)";
} # if

return $status
} # CheckDNS

CheckDNS is called every 15 minutes with "google.com" as a parm. When a
failure happens $status = 2 and remains = 2 forever.

I'd check to see if

my @ipaddrs = gethostbyname $host;

returns a defined result. If not, only then should you look at $?.

Have you considered nagios?

Mark
 
A

Andrew DeFaria

Mark said:
From

perldoc - f gethostbyname

For the gethost*() functions, if the "h_errno" variable is supported
in C, it will be returned to you via $? if the function call fails.

It appears to be undefined as to what $? will be set to if the call
*succeeds*, and thus it should not be relied upon to check for success
or failure of the call.
Well in practice it does return 0 when successful and so far 2 when not
successful. Once unsuccessful it keeps returning 2.
I'd check to see if

my @ipaddrs = gethostbyname $host;

returns a defined result. If not, only then should you look at $?. I will try that...
Have you considered nagios?
Nah, I wasn't looking for a kitchen sink solution....
 
S

Sisyphus

..
..
Well in practice it does return 0 when successful and so far 2 when not
successful. Once unsuccessful it keeps returning 2.

No - I suspect that once $? is set to 2, it stays set to 2 until the next
unsuccessful call - which probably just sets it to 2 again.

Instead of:

my @ipaddrs = gethostbyname $host;
my $status = $?;

try:

$? = 0; # set $? to zero (in case it's already set to another value)
my @ipaddrs = gethostbyname $host;
my $status = $?;

Better still - do what Mark Clements suggested and check $? only when
gethostname() fails.

Cheers,
Rob
 
P

Peter J. Holzer

Well in practice it does return 0 when successful and so far 2 when not
successful. Once unsuccessful it keeps returning 2.

No, it doesn't. In practice, $? is not changed when gethostbyname is
successful. For example, try this:

#!/usr/bin/perl
use warnings;
use strict;

`false`;

print "\$? after calling false: $?\n";

my @a = gethostbyname("google.com");

print "gethostbyname returned @{[ scalar @a - 4 ]} addresses.\n";
print "\$? after call to gethostbyname: $?\n";

For me it prints:

$? after calling false: 256
gethostbyname returned 3 addresses.
$? after call to gethostbyname: 256

256 is the expected value after invoking false. After the
successful call to gethostbyname, $? still has the value 256.

You have to check if gethostbyname failed explicitely:

my @a = gethostbyname("google.com");
unless (@a) {
handle error
}



hp
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top