If gethostbyname fails it never recovers

Discussion in 'Perl Misc' started by Andrew DeFaria, Oct 6, 2006.

  1. 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.

    --

    Andrew DeFaria <http://defaria.com>
    You have the right to remain silent. Anything you say will be misquoted,
    then used against you.
     
    Andrew DeFaria, Oct 6, 2006
    #1
    1. Advertising

  2. Andrew DeFaria wrote:
    > 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
     
    Mark Clements, Oct 6, 2006
    #2
    1. Advertising

  3. Mark Clements wrote:
    > Andrew DeFaria wrote:
    >> 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.

    Well in practice it does return 0 when successful and so far 2 when not
    successful. Once unsuccessful it keeps returning 2.
    >> 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 $?.

    I will try that...
    > Have you considered nagios?

    Nah, I wasn't looking for a kitchen sink solution....

    --

    Andrew DeFaria <http://defaria.com>
    Honk If you want to see my finger
     
    Andrew DeFaria, Oct 7, 2006
    #3
  4. Andrew DeFaria

    Sisyphus Guest

    "Andrew DeFaria" <> wrote in message
    ..
    ..
    > 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
     
    Sisyphus, Oct 7, 2006
    #4
  5. On 2006-10-07 00:21, Andrew DeFaria <> wrote:
    > Mark Clements wrote:
    >> Andrew DeFaria wrote:
    >>> 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.

    > 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

    --
    _ | Peter J. Holzer | > Wieso sollte man etwas erfinden was nicht
    |_|_) | Sysadmin WSR | > ist?
    | | | | Was sonst wäre der Sinn des Erfindens?
    __/ | http://www.hjp.at/ | -- P. Einstein u. V. Gringmuth in desd
     
    Peter J. Holzer, Oct 7, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Soren Kuula
    Replies:
    1
    Views:
    466
    Henry S. Thompson
    Dec 1, 2005
  2. Kevin
    Replies:
    4
    Views:
    439
    Irrwahn Grausewitz
    Oct 17, 2003
  3. Eric Brunel
    Replies:
    0
    Views:
    1,475
    Eric Brunel
    Oct 27, 2003
  4. Andrew DeFaria

    If gethostbyname fails it never recovers

    Andrew DeFaria, Oct 5, 2006, in forum: Perl
    Replies:
    2
    Views:
    2,326
    Andrew DeFaria
    Oct 6, 2006
  5. 'gethostbyname' fails Please Help

    , Jul 24, 2008, in forum: C Programming
    Replies:
    4
    Views:
    417
Loading...

Share This Page