This is version 2.0 of my checkhost tool.
Significant changes from previous version (v1.1):

- Now has the ability to send a wake-on-LAN packet to each client before
checking for client connection
- Reads client hardware Ethernet addresses from a DB table (schema
documented in the tool)
- Now requires DBI.pm
- Significant code refactoring

This version is written for MySQL DB backends.  It will require minor
modification to work with PostgreSQL backends.

This tool is designed to be run as a run-before-job script by the Bacula
director.  You must set the Director's address in the script before
running it, and should set the broadcast address as well (default
broadcast address is set to 255.255.255.255).


-- 
  Phil Stracchino, CDK#2     DoD#299792458     ICBM: 43.5607, -71.355
  ala...@caerllewys.net   ala...@metrocast.net   p...@co.ordinate.org
  Renaissance Man, Unix ronin, Perl hacker, SQL wrangler, Free Stater
                 It's not the years, it's the mileage.
#!/usr/bin/perl
#
# checkhost version 1.1 by Phil Stracchino
# All rights assigned to the Bacula project
# Licensed under the GPL v2, or at your option any later version
#
# Check whether a bacula client is alive and responding on the network
#
# Checkhost does not perform a Bacula connection handshake or verify
# that passwords match; it only verifies that the client is reachable
# on the network and responding, and that the Bacula client is running
# and listening on port 9103.
#
# Usage:  checkhost [-r] [-v] [-i seconds] hostname|ipaddress
# Options:
#     -r, --retry:    Try three times to connect (default: try only once)
#     -i, --interval: Specify time in seconds to wait between retries
#                         (default: 30 seconds)
#     -v, --verbose:  output verbose status messages, for interactive
#                     use (default:  operate silently)


use strict;
use Getopt::Long;
use Socket;
use Net::Ping;
use Net::Telnet ();


# Return values:
#       -1  Program error or no host specified
#        0  Success, FD found and responding
#        1  Client alive but FD not listening
#        2  Client not found on network

my $ret = -1;
my ($host, $p, %opts);

Getopt::Long::Configure ("bundling");
GetOptions(\%opts,
           'interval|i=i',
           'retry|r',
           'verbose|v');

$host = shift || die "No host specified!\n";

if ($opts{verbose})
{
    if ($host =~ /^\d+\.\d+\.\d+\.\d+$/)
    {
        my $ip = inet_aton($host);
        printf("Host $host has name %s\n",
               gethostbyaddr($ip, AF_INET));
    }
    else
    {
        printf("Client $host has address %d.%d.%d.%d\n",
               unpack('C4',
                      (my @addrs = (gethostbyname($host))[4])[0]));
    }
}

$p = Net::Ping->new();

my $retrycount = ($opts{retry} ? 3 : 1);

while ($retrycount && ($ret != 0))
{
    if ($p->ping($host))
    {
        print "Host $host is alive\n" if ($opts{verbose});
        my $t = new Net::Telnet (Timeout => 10,
                                 Port    => 9102,
                                 Prompt  => '/bash\$ $/');
        if ($t->open($host))
        {
            print "Bacula-FD listening on port 9102\n" if ($opts{verbose});
            $ret = 0;
        }
        else
        {
            print "Bacula-FD not running on host $host\n";
           $ret = 1;
        }
        $t->close;
    }
    else
    {
        $ret = 2;
    }
    $retrycount--;
    if ($opts{retry} && ($ret != 0))
    {
        printf("\tNot found on try %d",
               3 - $retrycount) if ($opts{verbose});
        if ($retrycount)
        {
            printf("; sleeping for %s seconds before retrying\n",
                   $opts{interval} || 30) if ($opts{verbose});
            sleep($opts{interval} || 30);
        }
        else
        {
            print "\n" if ($opts{verbose});
        }
    }
}
$p->close();

print "Client $host not found on network\n" if ($ret == 2);

print "Returning value $ret\n" if ($opts{verbose});

exit ($ret);
------------------------------------------------------------------------------
Achieve unprecedented app performance and reliability
What every C/C++ and Fortran developer should know.
Learn how Intel has extended the reach of its next-generation tools
to help boost performance applications - inlcuding clusters.
http://p.sf.net/sfu/intel-dev2devmay
_______________________________________________
Bacula-users mailing list
Bacula-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/bacula-users

Reply via email to