Ad Widget

Collapse

Add new host via cdp

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Evan.Anderson
    Member
    • Jun 2009
    • 42

    #1

    Add new host via cdp

    I've been trying to figure out how to do this for a long time and here it is, my contribution to the community. This is a script that will add cdp neighbors to zabbix from known hosts. You have to start this off with a "seed" host, just one manual host creation. It requires the use of the templates found here(https://github.com/jjmartres/Zabbix/.../zbx-cisco-cdp). Thank you jjmartres. After the cdp neighbors are discovered(for the "seed" host), start running this script via cron and it will find new network gear and add it to zabbix for you. Cron it daily to get new hosts daily, etc. You can also apply your templates, just look for my comments in the script. I'm a simple coder trying to be humble, my coding is like using a xerox machine and then finishing it off with finger painting, so if someone would like to impove on my script, I wouldn't doubt it. I would gladly appreciate anyones update to this script, please post your version for everyone to use. Yes, I realize I could do away with the mysql part of the script and use just use the api, but it is what it is and it works for me, please improve it and post your version. I hope everyone keeps updating this to make it better.

    Code:
    #!/usr/bin/perl
    use strict;
    use warnings;
    use Socket;
    use DBI;
    use 5.010;
    use strict;
    use warnings;
    use JSON::RPC::Client;
    use Data::Dumper;
    
    # script: cdp_host_create.sh 
    # version: 1.0
    # author: Evan.Anderson 
    # description: This script will add known cdp neighbors as zabbix hosts
    # license: GPL2
    
    #declare db variables
    my $host = "localhost";
    my $database = "zabbix";
    my $tablename = "items";
    my $user = ""; #Set this value yourself
    my $pw = ""; #Set this value yourself
    
    # PERL MYSQL CONNECT
    my $zabbix_server = DBI->connect("dbi:mysql:" . $database . ";host=" . $host, $user, $pw);
    my $query2 = "";
    my $sqlquery2 = "";
    my $rv2 = "";
    my @cdp_ne = "";
    my $results = "";
    my $results2 = "";
    my $rc = "";
    my $rc2 = "";
    my $ipaddress = "";
    my $snmpget = "/usr/local/bin/snmpget";
    my $loopcount = 0;
    my $skip = 0;
    my $skip2 = 0;
    my $i = "";
    my $totalnew = 0;
    my $char = "";
    my $fixedname = "";
    my @my_cdp_ne;
    my @devicetype = "";
    
    # Authenticate yourself
    my $client = new JSON::RPC::Client;
    my $url = 'http://localhost/zabbix/api_jsonrpc.php';
    my $authID;
    my $response;
    
    my $json = {
    jsonrpc => "2.0",
    method => "user.login",
    params => {
    user => "admin",
    password => "zabbix"
    },
    id => 1
    };
    $response = $client->call($url, $json);
    
    # Check if response was successful
    die "Authentication failed\n" unless $response->content->{'result'};
    
    $authID = $response->content->{'result'};
    print "Authentication successful. Auth ID: " . $authID . "\n";
    
    
    
    
    
    # get the known cdp ne from db
    my $query = "SELECT * FROM items WHERE snmp_oid LIKE '%.1.3.6.1.4.1.9.9.23.1.2.1.1.6%'";
    my $sqlquery = $zabbix_server->prepare($query);
    my $rv = $sqlquery->execute;
    
    #loop through the known cdp neighbors
    while (@cdp_ne = $sqlquery->fetchrow_array())
    {
    	$results = $cdp_ne[10];
            if ($results)
            {
    		if ($results =~ m/\(/) #check for parathesis in the hostname mds/nexus
                    {
                          	$fixedname = "";
                            $skip2 = 1;
                            foreach $char (split //, $results)
                            {
                            	if ($char eq "(") 
                                  	{
                                    	$skip2 = 0;
                                    }
                                    if ($skip2)
                                    {
                                    	$fixedname = $fixedname . $char;
                                  	}
                          	}
                                    #append your domain name here if needed
                                    $results = $fixedname . ".fqdn.com"; #set this value yourself
                    }
    
    		#query to check for known hosts in zabbix db
    		$query2 = "SELECT * FROM hosts WHERE host = '" . $results . "'";
    	        $sqlquery2 = $zabbix_server->prepare($query2);
    	        $rv2 = $sqlquery2->execute;
    	        $results2 = $sqlquery2->fetchrow_array();
    	        if ($results2)
    	        { #no need to add this host again
    	        }
    	        else
    	        { #this host is not in the database time to add via the api
    			@devicetype = `$snmpget -v2c -c nmmcit $results 1.3.6.1.2.1.47.1.1.1.1.2.1`;
    			if (@devicetype)
    			{ #host is up, add it
    				#Can't add it to the xml twice
    				$skip = 0;
    				if (@my_cdp_ne)
    				{
    					$i = 0;
    					foreach (@my_cdp_ne)
    					{						
    						if ($my_cdp_ne[$i] eq $results)
    						{
    							$skip = 1;
    						}
    						$i += 1;
    
    					}
    				}
    				if ($skip) 
    				{
    				}
    				else
    				{
    					#finally adding it
    					# Attempt to add hosts via zabbix api
    					$ipaddress = inet_ntoa(inet_aton($results));
    
    					$json = {
    						    jsonrpc => "2.0",
    						    method => "host.create",
    						    macros => [],
    						    inventory_mode => 1,
    						    inventory => {
    							notes => localtime(time)
    							},
    						    params => {
    						        host => "$results",
    						        interfaces => [
    						            {
    						                type => 1,
    						                main => 1,
    						                useip => 1,
    						                ip => "$ipaddress",
    						                dns => "$results",
    						                port => "10050"
    						            },
    							    {
                                                                    type => 2,
                                                                    main => 1,
                                                                    useip => 1,
                                                                    ip => "$ipaddress",
                                                                    dns => "$results",
                                                                    port => "161"
                                                                }
    
    						        ],
    						        groups => [
    						            {
    						                groupid => "9"
    						            }
    						        ],
    						        templates => [
    						            {
                                                                    #your templates #'s will be different, so look them up in your database
    						                templateid => "10089",
    								templateid => "10092"
    						            }
    							        ],
    						    },
    						    auth => "$authID",
    						    id => 2
    						};
    					$response = $client->call($url, $json);
    					# Check if response was successful
    					die "Host creation failed:  Dumper($response);\n" unless $response->content->{'result'};
    
    
    					$my_cdp_ne[$loopcount] = $results; #remember our neighbors for next time
    					$loopcount += 1; #count our neighbors
    					$totalnew += 1;
    				}
    			}
    			else
    			{ #no response from host
    				print "not adding $results\n\n";
    			}
    	        }
    		$rc2 = $sqlquery2->finish;
    	}
    
    }
    print "$totalnew new hosts added.\n\n";
    
    $rc = $sqlquery->finish;
    Last edited by Evan.Anderson; 12-03-2013, 04:19. Reason: typo - typo - typo
Working...