Merge pull request #3473 from laf/issue-3012

Add support for stopping devices being added with duplicate sysNames
This commit is contained in:
Tony Murray
2016-05-10 17:16:05 -05:00
4 changed files with 33 additions and 8 deletions

View File

@ -204,9 +204,13 @@ if (!empty($argv[1])) {
echo 'Added device '.$device['hostname'].' ('.$device_id.")\n";
exit;
}
}//end if
else {
print $console_color->convert("%rWe couldn't add this device, please check the snmp details%n\n");
}
}
else {
print $console_color->convert(
print $console_color->convert(
"\n".$config['project_name_version'].' Add Host Tool
Usage (SNMPv1/2c): ./addhost.php [-g <poller group>] [-f] [-p <port assoc mode>] <%Whostname%n> [community] [v1|v2c] [port] ['.implode('|', $config['snmp']['transports']).']
@ -224,4 +228,5 @@ print $console_color->convert(
%rRemember to run discovery for the host afterwards.%n
'
);
);
}

View File

@ -220,6 +220,12 @@ $config['addhost_alwayscheckip'] = FALSE; #TRUE - check for duplicate ips even
#FALSE- only check when adding host by ip.
```
By default we allow hosts to be added with duplicate sysName's, you can disable this with the following config:
```php
$config['allow_duplicate_sysName'] = false;
```
#### SNMP Settings
```php

View File

@ -793,6 +793,10 @@ $config['dateformat']['mysql']['time'] = '%H:%i:%s';
$config['enable_clear_discovery'] = 1;
// Set this to 0 if you want to disable the web option to rediscover devices
$config['force_ip_to_sysname'] = false;// Set to true if you want to use sysName in place of IPs
// Allow duplicate devices by sysName
$config['allow_duplicate_sysName'] = true;// Set to false if you want to only allow unique sysName's
$config['enable_port_relationship'] = true;
// Set this to false to not display neighbour relationships for ports
$config['enable_footer'] = 1;

View File

@ -299,7 +299,7 @@ function addHost($host, $snmpver, $port = '161', $transport = 'udp', $quiet = '0
if ($force_add == 1 || isSNMPable($device)) {
$snmphost = snmp_get($device, "sysName.0", "-Oqv", "SNMPv2-MIB");
if (empty($snmphost) or ($snmphost == $host || $hostshort = $host)) {
$device_id = createHost ($host, NULL, $snmpver, $port, $transport, $v3, $poller_group, $port_assoc_mode);
$device_id = createHost ($host, NULL, $snmpver, $port, $transport, $v3, $poller_group, $port_assoc_mode, $snmphost);
return $device_id;
}
else {
@ -325,7 +325,7 @@ function addHost($host, $snmpver, $port = '161', $transport = 'udp', $quiet = '0
if ($force_add == 1 || isSNMPable($device)) {
$snmphost = snmp_get($device, "sysName.0", "-Oqv", "SNMPv2-MIB");
if (empty($snmphost) || ($snmphost && ($snmphost == $host || $hostshort = $host))) {
$device_id = createHost ($host, $community, $snmpver, $port, $transport,array(),$poller_group, $port_assoc_mode);
$device_id = createHost ($host, $community, $snmpver, $port, $transport,array(),$poller_group, $port_assoc_mode, $snmphost);
return $device_id;
}
else {
@ -575,7 +575,7 @@ function getpollergroup($poller_group='0') {
}
}
function createHost($host, $community = NULL, $snmpver, $port = 161, $transport = 'udp', $v3 = array(), $poller_group='0', $port_assoc_mode = 'ifIndex') {
function createHost($host, $community = NULL, $snmpver, $port = 161, $transport = 'udp', $v3 = array(), $poller_group='0', $port_assoc_mode = 'ifIndex', $snmphost) {
global $config;
$host = trim(strtolower($host));
@ -604,7 +604,7 @@ function createHost($host, $community = NULL, $snmpver, $port = 161, $transport
if ($device['os']) {
if (host_exists($host) === false) {
if (host_exists($host, $snmphost) === false) {
$device_id = dbInsert($device, 'devices');
if ($device_id) {
oxidized_reload_nodes();
@ -1339,12 +1339,22 @@ function snmpTransportToAddressFamily($transport) {
* @return bool true if hostname already exists
* false if hostname doesn't exist
**/
function host_exists($hostname) {
function host_exists($hostname, $snmphost='') {
global $config;
$count = dbFetchCell("SELECT COUNT(*) FROM `devices` WHERE `hostname` = ?", array($hostname));
if ($count > 0) {
return true;
}
else {
if ($config['allow_duplicate_sysName'] === false && !empty($snmphost)) {
$count = dbFetchCell("SELECT COUNT(*) FROM `devices` WHERE `sysName` = ?", array($snmphost));
if ($count > 0) {
return true;
}
else {
return false;
}
}
return false;
}
}