SNMP Discovery

Fred Albrecht Fred.Albrecht at za.tiscali.com
Sun Mar 16 00:51:42 CET 2003


Ok

Here's the script.  It's rough (without comments), but serves its purpose for me.

------------------begin-----------------

#!/usr/bin/perl  

use Socket;

sub resolve_ipaddress
{
        my ($address)=@_;
        my $name="";

        return $address if (!($address =~ /^\d/));

        open(O, "nslookup $address 2>>/dev/null|") or die "Could not do nslookup";
        while (<O>)
        {
                next if (!(/^Name:/));
                chomp;
                s/\s//g;
                ($ble,$name)=split(/:/, $_);
                last;
        }
        #close(O);

        return $name;
}

sub resolve_name
{
        my ($address)=@_;
        my $ad=0;
        my $name="";

        return $address if ($address =~ /^\d/);

        open(O, "nslookup $address 2>>/dev/null|") or die "Could not do nslookup";
        while (<O>)
        {
                next if (!(/^Address:/));
                $ad++;

                next if ($ad < 2);
                chomp;
                s/\s//g;
                ($ble, $name)=split(/:/, $_);
                last;
        }
        #close(O);

        return $name;
}

sub get_host_name
{
        my ($address)=@_;
        my $ip;
        my $name;

        #is address and ipaddress?
        if ($address =~ /^\d/)
        {
                #does the ip resolve?
                $name=resolve_ipaddress($address);
                if ($name ne "")
                {
                        return $name;
                }
        }
        return $address;
}

my %dependency=();
sub get_proper_router_name
{
        my ($address)=@_;
        my $ip;
        my $name;

        #is address and ipaddress?
        if ($address =~ /^\d/)
        {
                #does the ip resolve?
                $name=resolve_ipaddress($address);
                if ($name ne "")
                {
                        #does the name resolve?
                        $ip=resolve_name($name);
                        if ($ip ne "")
                        {
                                #to the same address?
                                if ($ip eq $address)
                                {
                                        return $name;
                                }
                        }
                }
        }
        return $address;
}
my %dependency=();

my %hostip=();
#my %router_exists_allready=();

open(ROUTERS, "$ARGV[0]") or die "Could not open $ARGV[0]";
while (<ROUTERS>)
{
        my @t=();
        my @s=();

        s/\s//g;
        next if (/^\#/);
        next if ($_ eq "");
        ($router)=split(/:/, $_);

        print "Busy with router $router\n";

        open(IN, "traceroute -m 10 -w 2 -q 1 $router 2>>/dev/null|") or die "Can't read";
        while (<IN>)
        {
                push @t, $_;
                $line=$_;
                next if (! ($line =~ /^\s\d/));
        
                $line=~s/^\s//g;
                $line=~s/\s\s/ /g;
                $line=~s/\(//g;
                $line=~s/\)//g;
        
                print "$line";
                @b=split(/\s/, $line);
                push @s, $b[2];
                last if ($b[2] eq $router);
                last if ($b[1] eq "*");
        }
        #close  (IN);

        pop @s;
        push @s, $router;

        $prev=" ";

        foreach $host (@s)
        {
                print "Traceroute Busy with router $host\n";
                $host_name=get_host_name($host);
                $hostip{$host_name}=$host;
                if ($host_name ne $host)
                {
                        $ip=resolve_name($host_name);
                        if ($ip ne "")
                        {
                                print "Router resolved to $host_name, replacing $host\n";
                                $hostip{$host_name}=$host_name;
                                $host=$host_name;
                        }
                        else
                        {
                                $hostip{$host}=$host;
                        }
                }

#               if (defined $router_exists_allready{$host})
#               {
#                       print "WARNING: Router $host has allready been added, IGNORING THIS INSTANCE\n";
#                       next;
#               }
#               $router_exists_allready{$host}=1;
                push @{$dependency{$host}},$prev;
                $prev=$host;
        }
}
print "==========================\n";
my $router_count=0;

( $sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst ) = localtime( time() );
open(HOSTS, ">rtg_hosts.cfg") or die "Could not create rtg_hosts.cfg";
printf HOSTS "# Generated %02d/%02d/%02d %02d:%02d by $0\n\n", $mon + 1, $mday, $year + 1900, $hour, $min;

print HOSTS "define service {\n";
print HOSTS "        name                           generic-client\n";
print HOSTS "        use                            generic-service\n";
print HOSTS "        check_command                  check-host-alive\n";
print HOSTS "        register                       0\n";
print HOSTS "        max_check_attempts             36\n";
print HOSTS "        notification_interval          0\n";
print HOSTS "        active_checks_enabled          1\n";
print HOSTS "        retry_check_interval           1\n";
print HOSTS "}\n\n";


open(HOSTGROUPS, ">rtg_hostgroups.cfg") or die "Could not create rtg_hostgroups.cfg";
printf HOSTGROUPS "# Generated %02d/%02d/%02d %02d:%02d by $0\n\n", $mon + 1, $mday, $year + 1900, $hour, $min;

print HOSTGROUPS "define hostgroup {\n";
print HOSTGROUPS "hostgroup_name                 routers\n";
print HOSTGROUPS "alias                          routers\n";
print HOSTGROUPS "name                           routers\n";
print HOSTGROUPS "register                       1\n";
print HOSTGROUPS "contact_groups                 NMSAdmin\n";
print HOSTGROUPS "members                        ";

foreach $router (keys %dependency)
{
        #print "$router<--->$dependency{$router}\n";
        #print "$router<--->";
        my %k=();

        print HOSTS "define host {\n";
        print HOSTS "\thost_name\t\t$router\n";
        print HOSTS "\talias\t\t\t$router\n";
        print HOSTS "\taddress\t\t\t$hostip{$router}\n";
        print HOSTS "\tregister\t\t1\n";
        print HOSTS "\tuse\t\t\tgeneric-router\n";
        print HOSTS "\tcheck_command\t\tcheck-host-alive\n";

        print "define host {\n";
        print "\thost_name\t\t$router\n";
        print "\talias\t\t\t$router\n";
        print "\taddress\t\t\t$hostip{$router}\n";
        print "\tregister\t\t1\n";
        print "\tuse\t\t\tgeneric-router\n";
        print "\tcheck_command\t\tcheck-host-alive\n";

        $c=0;
        foreach $parent (@{$dependency{$router}})
        {
                if (! defined $k{$parent})
                {
                        print HOSTS "\tparents\t\t\t" if (($c==0) && ($parent ne " "));
                        print "\tparents\t\t\t" if (($c==0) && ($parent ne " "));
                        $c++;
                        if ($parent ne " ")
                        {
                                print "," if ($c > 1);
                                print HOSTS "," if ($c > 1);
                                print "$parent";
                                print HOSTS "$parent";
                        }
                        $k{$parent}=1;
                }
        }
        print "\n";
        print "}\n\n";
        print HOSTS "\n";
        print HOSTS "}\n\n";

        print "define service {\n";
        print "\tuse                             generic-router\n";
        print "\thost_name                       $router\n";
        print "\tservice_description             PING\n";
        print "\tcheck_command                   check-host-alive\n";
        #print "\tactive_checks_enabled           0\n";
        #print "\tpassive_checks_enabled          0\n";
        print "}\n\n";

        print HOSTS "define service {\n";
        print HOSTS "\tuse                             generic-router\n";
        print HOSTS "\thost_name                       $router\n";
        print HOSTS "\tservice_description             PING\n";
        print HOSTS "\tcheck_command                   check-host-alive\n";
        #print HOSTS "\tactive_checks_enabled           0\n";
        #print HOSTS "\tpassive_checks_enabled          0\n";
        print HOSTS "}\n\n";

        $router_count++;
        print HOSTGROUPS "," if ($router_count > 1);
        print HOSTGROUPS "$router";
}
close(HOSTS);
print HOSTGROUPS "\n}\n\n";
close HOSTGROUPS;


----------end------------

Define another object file with the following in:

define host {
name                           generic-host     ; The name of this host template - referenced in other host definit
ions, used for template recursion/resolution
register                       0
notification_options           d,u,r
max_check_attempts             1
notifications_enabled          1
notification_interval          5
flap_detection_enabled         1
process_perf_data              1
retain_status_information      1
retain_nonstatus_information   1
}

define host {
host_name                      generic-router
name                           generic-router
register                       0
use                            generic-host     ; The name of this host template - referenced in other host definit
ions, used for template recursion/resolution
max_check_attempts             1
notification_interval          5
notification_period            24x7
notification_options           d,u,r
notifications_enabled          1
checks_enabled                 1
flap_detection_enabled         1
process_perf_data              1
retain_status_information      1
retain_nonstatus_information   1

------------

Store your list of router/host names one per line in a file.  Pass the filename as argument to the script.  The output gets written to stdout and a file called rtg_hosts.cfg.  The hostgroups live in rtg_hostgroups.cfg.

Note: the program rellys heavily on traceroute, your milleage might differ to mine.  The code is provided as is, etc, etc etc.

:)
fred

-----Original Message-----
From: Dario B. Bestetti [mailto:dbestetti at opservices.com.br]
Sent: 14 March 2003 03:59
To: Fred Albrecht; nagios-users at lists.sourceforge.net
Subject: RE: [Nagios-users] SNMP Discovery


Fred, can you send this program to the list ?

-----Original Message-----
From: nagios-users-admin at lists.sourceforge.net
[mailto:nagios-users-admin at lists.sourceforge.net] On Behalf Of Fred Albrecht
Sent: quinta-feira, 13 de março de 2003 19:47
To: Subhendu Ghosh; nagios-users at lists.sourceforge.net
Subject: RE: [Nagios-users] SNMP Discovery


Well, I kinda did it the hard way.

I supply the program I wrote with a list of addresses where my services
live.  Then I parse the output of the traceroute command to see how I get
from my Nagios machine to my destination.  The scrippy then writes my
hosts.cfg file (with parents and all) and puts a default check_ping service
in for each router along the way.  Looks kinda nice when the end result is a
nice statusmap all filled in for me (btw I'm lazy, that's why I do these
things ;)

-----Original Message-----
From: Subhendu Ghosh [mailto:sghosh at sghosh.org]
Sent: 13 March 2003 10:42
To: nagios-users at lists.sourceforge.net
Subject: Re: [Nagios-users] SNMP Discovery


On Thu, 13 Mar 2003, Samoied wrote:

> Good afternoon,
> 
> I've been testing Nagios here in the company and got great results in 
> the way it checks the hosts and show them and warn us about any 
> problem.
> 
> I want to replace the whole What's Up system by Nagios, but there's 
> only one little problem that I have, and it is in the discovery. If 
> you in the list know about any way to do the discovery using SNMP, 
> since my whole network is configured under SNMP, and if there was any 
> way to make Nagios folow the links configured in each router I'd be 
> very thankful.
> 
> I'm using nagios 1.0 and nagios-plugins-1.3.0-beta3
> 
> Greetings,
> 
> 

There is no easy way to do snmp discovery for Nagios currently.

BTW - nagios-plugins are a 1.3.0 release

-- 

-sg



-------------------------------------------------------
This SF.net email is sponsored by:Crypto Challenge is now open! 
Get cracking and register here for some mind boggling fun and 
the chance of winning an Apple iPod:
http://ads.sourceforge.net/cgi-bin/redirect.pl?thaw0031en
_______________________________________________
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


-------------------------------------------------------
This SF.net email is sponsored by:Crypto Challenge is now open! 
Get cracking and register here for some mind boggling fun and 
the chance of winning an Apple iPod:
http://ads.sourceforge.net/cgi-bin/redirect.pl?thaw0031en
_______________________________________________
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



-------------------------------------------------------
This SF.net email is sponsored by:Crypto Challenge is now open! 
Get cracking and register here for some mind boggling fun and 
the chance of winning an Apple iPod:
http://ads.sourceforge.net/cgi-bin/redirect.pl?thaw0031en
_______________________________________________
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