Net discovery

Jeff McCombs jeffm at nicusa.com
Wed Mar 23 17:13:36 CET 2005


Nope, there¹s not really any way to do automatic network discovery with
Nagios. Though you could write a simple perl or shell script that will parse
the output.. 

Actually, after thinking about this.. It would be pretty useful. A Korn
script is below. :)

To run, "./generate-nagios-hosts.sh <ip address range>"
E.g.:
    ./generate-nagios-hosts.sh 10.0.0.0/24


Basic sh/ksh script. Requires NMAP, awk, & sed, and a template file for
basic hostname generation. Edit the template as needed. Use the following
key words for your substitutions.

@@FQDN@@ = fully qualified domain name of the host (if known)
@@ALIAS@@ = just the hostname of the host (if known)
@@HOSTIP@@ = Host's IP address.

Example host-template.cfg:
--- Begin host-template.cfg
Define host {
    use             generic-host
    host_name       @@FQDN@@
    alias           @@ALIAS@@
    address         @@HOSTIP@@
    check_command   check-host-alive
}

-- End host-template.cfg

The script is short & sweet, about 50 lines including comments & whitespace:
-- Begin Script
#!/bin/sh
# Set some basic defaults used by the script.

# Where is the host template file?
TEMPLATE=/home/jeffm/src/nagios-autodisc/host-template.cfg

# Where is SED?
SED=/usr/bin/sed

# Where is AWK?
AWK=/usr/local/bin/awk

# Where are we storing our output?
# Suggest you don't use your existing hosts.cfg, create a NEW
# file and merge it in later!
HOSTCFG="/home/jeffm/src/nagios-autodisc/autogen.cfg"

# NMAP location and options to use EXCEPT for the range to scan.
# which is picked up by the argument on the command line.
# -S is used to specify source IP. You'll want to update
# this IP address, I'm sure. :)
NMAP="/usr/local/bin/nmap -sP -T Normal -S 10.0.0.23 "

# IF no arguments were passed, exit with a usage error.
if [ "$#" -ne 1 ]
then
        echo "USAGE: `basename $0` [ip range]"
        echo "EXAMPLE: `basename $0` 10.0.0.0/24"
        exit 1
fi

# Fire off nmap, and grep for "up"
$NMAP $1 | grep up |grep -v scanned |grep -v 255 |
while read a b c d
do
        # NMAP output from the above should be something like:
        # Host my.host.com (10.0.0.1) appears to be up.
        #
        # so;
        #       a = "Host"
        #       b = "my.host.com"
        #       c = "(10.0.0.1)"
        #       d = "appears to be up"
        # in theory. :)
        FQ=$b
        hostip=`echo $c | $SED -e 's/(//g' -e 's/)//g'`
        nodename=`echo $b | $AWK -F. '{print $1}'`

        echo "Found host: $FQ ($hostip) - using alias: $nodename"

        # OK. SED pisses me off. -e 's/something/$variable/' doesn't work
        # because SED refuses to expand $variable. So we have to create
        # a stupid script file.
        echo "s/@@FQDN@@/$FQ/" > /tmp/sed.script
        echo "s/@@HOSTIP@@/$hostip/" >> /tmp/sed.script
        echo "s/@@ALIAS@@/$nodename/" >> /tmp/sed.script

        $SED -f /tmp/sed.script $TEMPLATE >> $HOSTCFG

done
rm /tmp/sed.script
# All done. 
# EOF

-- End script

You could probably extend this to check for services on well-known ports
(http/https, smtp, ftp, etc). Right now all the thing does is ping.

Enjoy, improve, or ridicule as needed. :)


On 3/22/05 4:02 AM, "Jose Moreno" <nagiosjd at yahoo.es> wrote:

> Hi all,
>  
> I would like to know if Nagios has some utility for automatic net discovery or
> it only be possible if I put the hosts name in the file "host.cfg".
>  
> Thanks
> 
> 
>  <http://es.rd.yahoo.com/mail_es/tagline/mail/image/*http://es.mail.yahoo.com>


-- 
Jeff McCombs                 |                                    NIC, Inc
Systems Administrator        |                       http://www.nicusa.com
jeffm at nicusa.com             |                                NASDAQ: EGOV
Phone: (703) 909-3277        |        "NIC - the People Behind eGovernment"
--
  "I have travelled the length and breadth of this country and talked
   with the best people, and I can assure you that data processing is
   a fad that won't last out the year."
        - Editor in charge of business books for Prentice-Hall, 1957




-------------------------------------------------------
This SF.net email is sponsored by Microsoft Mobile & Embedded DevCon 2005
Attend MEDC 2005 May 9-12 in Vegas. Learn more about the latest Windows
Embedded(r) & Windows Mobile(tm) platforms, applications & content.  Register
by 3/29 & save $300 http://ads.osdn.com/?ad_idh83&alloc_id149&op=click
_______________________________________________
Nagios-users mailing list
Nagios-users at lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/nagios-users
::: Please include Nagios version, plugin version (-v) and OS when reporting any issue. 
::: Messages without supporting info will risk being sent to /dev/null





More information about the Users mailing list