mirror of
https://github.com/librenms/librenms.git
synced 2024-10-07 16:52:45 +00:00
Merge pull request #3929 from murrant/discovery-events
addHost: Reduce discovery event logs of existing devices and adds better error messages when adding hosts
This commit is contained in:
97
addhost.php
97
addhost.php
@@ -27,9 +27,10 @@ if (isset($options['g']) && $options['g'] >= 0) {
|
|||||||
array_shift($argv);
|
array_shift($argv);
|
||||||
array_unshift($argv, $cmd);
|
array_unshift($argv, $cmd);
|
||||||
$poller_group = $options['g'];
|
$poller_group = $options['g'];
|
||||||
}
|
} elseif ($config['distributed_poller'] === true) {
|
||||||
else if ($config['distributed_poller'] === true) {
|
|
||||||
$poller_group = $config['distributed_poller_group'];
|
$poller_group = $config['distributed_poller_group'];
|
||||||
|
} else {
|
||||||
|
$poller_group = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isset($options['f']) && $options['f'] == 0) {
|
if (isset($options['f']) && $options['f'] == 0) {
|
||||||
@@ -37,6 +38,8 @@ if (isset($options['f']) && $options['f'] == 0) {
|
|||||||
array_shift($argv);
|
array_shift($argv);
|
||||||
array_unshift($argv, $cmd);
|
array_unshift($argv, $cmd);
|
||||||
$force_add = 1;
|
$force_add = 1;
|
||||||
|
} else {
|
||||||
|
$force_add = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
$port_assoc_mode = $config['default_port_association_mode'];
|
$port_assoc_mode = $config['default_port_association_mode'];
|
||||||
@@ -46,7 +49,7 @@ if (isset ($options['p'])) {
|
|||||||
if (! in_array ($port_assoc_mode, $valid_assoc_modes)) {
|
if (! in_array ($port_assoc_mode, $valid_assoc_modes)) {
|
||||||
echo "Invalid port association mode '" . $port_assoc_mode . "'\n";
|
echo "Invalid port association mode '" . $port_assoc_mode . "'\n";
|
||||||
echo 'Valid modes: ' . join (', ', $valid_assoc_modes) . "\n";
|
echo 'Valid modes: ' . join (', ', $valid_assoc_modes) . "\n";
|
||||||
exit;
|
exit(2);
|
||||||
}
|
}
|
||||||
|
|
||||||
$cmd = array_shift($argv);
|
$cmd = array_shift($argv);
|
||||||
@@ -76,6 +79,7 @@ if (!empty($argv[1])) {
|
|||||||
'cryptoalgo' => 'AES',
|
'cryptoalgo' => 'AES',
|
||||||
);
|
);
|
||||||
|
|
||||||
|
// v3
|
||||||
if ($seclevel === 'nanp' or $seclevel === 'any' or $seclevel === 'noAuthNoPriv') {
|
if ($seclevel === 'nanp' or $seclevel === 'any' or $seclevel === 'noAuthNoPriv') {
|
||||||
$v3['authlevel'] = 'noAuthNoPriv';
|
$v3['authlevel'] = 'noAuthNoPriv';
|
||||||
$v3args = array_slice($argv, 4);
|
$v3args = array_slice($argv, 4);
|
||||||
@@ -84,11 +88,9 @@ if (!empty($argv[1])) {
|
|||||||
// parse all remaining args
|
// parse all remaining args
|
||||||
if (is_numeric($arg)) {
|
if (is_numeric($arg)) {
|
||||||
$port = $arg;
|
$port = $arg;
|
||||||
}
|
} elseif (preg_match('/^('.implode('|', $config['snmp']['transports']).')$/', $arg)) {
|
||||||
else if (preg_match('/^('.implode('|', $config['snmp']['transports']).')$/', $arg)) {
|
|
||||||
$transport = $arg;
|
$transport = $arg;
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
// should add a sanity check of chars allowed in user
|
// should add a sanity check of chars allowed in user
|
||||||
$user = $arg;
|
$user = $arg;
|
||||||
}
|
}
|
||||||
@@ -97,10 +99,7 @@ if (!empty($argv[1])) {
|
|||||||
if ($seclevel === 'nanp') {
|
if ($seclevel === 'nanp') {
|
||||||
array_push($config['snmp']['v3'], $v3);
|
array_push($config['snmp']['v3'], $v3);
|
||||||
}
|
}
|
||||||
|
} elseif ($seclevel === 'anp' or $seclevel === 'authNoPriv') {
|
||||||
$device_id = addHost($host, $snmpver, $port, $transport, 0, $poller_group, $force_add, $port_assoc_mode);
|
|
||||||
}
|
|
||||||
else if ($seclevel === 'anp' or $seclevel === 'authNoPriv') {
|
|
||||||
$v3['authlevel'] = 'authNoPriv';
|
$v3['authlevel'] = 'authNoPriv';
|
||||||
$v3args = array_slice($argv, 4);
|
$v3args = array_slice($argv, 4);
|
||||||
$v3['authname'] = array_shift($v3args);
|
$v3['authname'] = array_shift($v3args);
|
||||||
@@ -110,23 +109,18 @@ if (!empty($argv[1])) {
|
|||||||
// parse all remaining args
|
// parse all remaining args
|
||||||
if (is_numeric($arg)) {
|
if (is_numeric($arg)) {
|
||||||
$port = $arg;
|
$port = $arg;
|
||||||
}
|
} elseif (preg_match('/^('.implode('|', $config['snmp']['transports']).')$/i', $arg)) {
|
||||||
else if (preg_match('/^('.implode('|', $config['snmp']['transports']).')$/i', $arg)) {
|
|
||||||
$transport = $arg;
|
$transport = $arg;
|
||||||
}
|
} elseif (preg_match('/^(sha|md5)$/i', $arg)) {
|
||||||
else if (preg_match('/^(sha|md5)$/i', $arg)) {
|
|
||||||
$v3['authalgo'] = $arg;
|
$v3['authalgo'] = $arg;
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
echo 'Invalid argument: '.$arg."\n";
|
echo 'Invalid argument: '.$arg."\n";
|
||||||
return;
|
exit(2);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
array_push($config['snmp']['v3'], $v3);
|
array_push($config['snmp']['v3'], $v3);
|
||||||
$device_id = addHost($host, $snmpver, $port, $transport, 0, $poller_group, $force_add, $port_assoc_mode);
|
} elseif ($seclevel === 'ap' or $seclevel === 'authPriv') {
|
||||||
}
|
|
||||||
else if ($seclevel === 'ap' or $seclevel === 'authPriv') {
|
|
||||||
$v3['authlevel'] = 'authPriv';
|
$v3['authlevel'] = 'authPriv';
|
||||||
$v3args = array_slice($argv, 4);
|
$v3args = array_slice($argv, 4);
|
||||||
$v3['authname'] = array_shift($v3args);
|
$v3['authname'] = array_shift($v3args);
|
||||||
@@ -137,41 +131,31 @@ if (!empty($argv[1])) {
|
|||||||
// parse all remaining args
|
// parse all remaining args
|
||||||
if (is_numeric($arg)) {
|
if (is_numeric($arg)) {
|
||||||
$port = $arg;
|
$port = $arg;
|
||||||
}
|
} elseif (preg_match('/^('.implode('|', $config['snmp']['transports']).')$/i', $arg)) {
|
||||||
else if (preg_match('/^('.implode('|', $config['snmp']['transports']).')$/i', $arg)) {
|
|
||||||
$transport = $arg;
|
$transport = $arg;
|
||||||
}
|
} elseif (preg_match('/^(sha|md5)$/i', $arg)) {
|
||||||
else if (preg_match('/^(sha|md5)$/i', $arg)) {
|
|
||||||
$v3['authalgo'] = $arg;
|
$v3['authalgo'] = $arg;
|
||||||
}
|
} elseif (preg_match('/^(aes|des)$/i', $arg)) {
|
||||||
else if (preg_match('/^(aes|des)$/i', $arg)) {
|
|
||||||
$v3['cryptoalgo'] = $arg;
|
$v3['cryptoalgo'] = $arg;
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
echo 'Invalid argument: '.$arg."\n";
|
echo 'Invalid argument: '.$arg."\n";
|
||||||
return;
|
exit(2);
|
||||||
}
|
}
|
||||||
}//end while
|
}//end while
|
||||||
|
|
||||||
array_push($config['snmp']['v3'], $v3);
|
array_push($config['snmp']['v3'], $v3);
|
||||||
$device_id = addHost($host, $snmpver, $port, $transport, 0, $poller_group, $force_add, $port_assoc_mode);
|
|
||||||
}
|
}
|
||||||
else {
|
} else {
|
||||||
// Error or do nothing ?
|
// v2c or v1
|
||||||
}//end if
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
$v2args = array_slice($argv, 2);
|
$v2args = array_slice($argv, 2);
|
||||||
|
|
||||||
while ($arg = array_shift($v2args)) {
|
while ($arg = array_shift($v2args)) {
|
||||||
// parse all remaining args
|
// parse all remaining args
|
||||||
if (is_numeric($arg)) {
|
if (is_numeric($arg)) {
|
||||||
$port = $arg;
|
$port = $arg;
|
||||||
}
|
} elseif (preg_match('/('.implode('|', $config['snmp']['transports']).')/i', $arg)) {
|
||||||
else if (preg_match('/('.implode('|', $config['snmp']['transports']).')/i', $arg)) {
|
|
||||||
$transport = $arg;
|
$transport = $arg;
|
||||||
}
|
} elseif (preg_match('/^(v1|v2c)$/i', $arg)) {
|
||||||
else if (preg_match('/^(v1|v2c)$/i', $arg)) {
|
|
||||||
$snmpver = $arg;
|
$snmpver = $arg;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -179,36 +163,20 @@ if (!empty($argv[1])) {
|
|||||||
if ($community) {
|
if ($community) {
|
||||||
$config['snmp']['community'] = array($community);
|
$config['snmp']['community'] = array($community);
|
||||||
}
|
}
|
||||||
|
|
||||||
$device_id = addHost($host, $snmpver, $port, $transport, 0, $poller_group, $force_add, $port_assoc_mode);
|
|
||||||
}//end if
|
}//end if
|
||||||
|
|
||||||
if ($snmpver) {
|
$result = addHost($host, $snmpver, $port, $transport, 0, $poller_group, $force_add, $port_assoc_mode);
|
||||||
$snmpversions[] = $snmpver;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
$snmpversions = array(
|
|
||||||
'v2c',
|
|
||||||
'v3',
|
|
||||||
'v1',
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
while (!$device_id && count($snmpversions)) {
|
if (is_numeric($result)) {
|
||||||
$snmpver = array_shift($snmpversions);
|
$device = device_by_id_cache($result);
|
||||||
$device_id = addHost($host, $snmpver, $port, $transport, 0, $poller_group, $force_add, $port_assoc_mode);
|
echo 'Added device '.$device['hostname'].' ('.$result.")\n";
|
||||||
}
|
exit(0);
|
||||||
|
|
||||||
if ($device_id) {
|
|
||||||
$device = device_by_id_cache($device_id);
|
|
||||||
echo 'Added device '.$device['hostname'].' ('.$device_id.")\n";
|
|
||||||
exit;
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
print $console_color->convert("%rWe couldn't add this device, please check the snmp details%n\n");
|
print $console_color->convert("%rWe couldn't add this device:\n " . $result . "%n\n");
|
||||||
|
exit(1);
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
|
|
||||||
print $console_color->convert(
|
print $console_color->convert(
|
||||||
"\n".$config['project_name_version'].' Add Host Tool
|
"\n".$config['project_name_version'].' Add Host Tool
|
||||||
@@ -229,4 +197,5 @@ else {
|
|||||||
%rRemember to run discovery for the host afterwards.%n
|
%rRemember to run discovery for the host afterwards.%n
|
||||||
'
|
'
|
||||||
);
|
);
|
||||||
|
exit(2);
|
||||||
}
|
}
|
||||||
|
@@ -290,13 +290,13 @@ function add_device() {
|
|||||||
}
|
}
|
||||||
if (empty($message)) {
|
if (empty($message)) {
|
||||||
$result = addHost($hostname, $snmpver, $port, $transport, 1, $poller_group, $force_add);
|
$result = addHost($hostname, $snmpver, $port, $transport, 1, $poller_group, $force_add);
|
||||||
if ($result) {
|
if (is_numeric($result)) {
|
||||||
$code = 201;
|
$code = 201;
|
||||||
$status = 'ok';
|
$status = 'ok';
|
||||||
$message = "Device $hostname has been added successfully";
|
$message = "Device $hostname has been added successfully";
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
$message = "Failed adding $hostname";
|
$message = $result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -67,8 +67,10 @@ if ($_POST['hostname']) {
|
|||||||
|
|
||||||
$port_assoc_mode = $_POST['port_assoc_mode'];
|
$port_assoc_mode = $_POST['port_assoc_mode'];
|
||||||
$result = addHost($hostname, $snmpver, $port, $transport, 0, $poller_group, $force_add, $port_assoc_mode);
|
$result = addHost($hostname, $snmpver, $port, $transport, 0, $poller_group, $force_add, $port_assoc_mode);
|
||||||
if ($result) {
|
if (is_numeric($result)) {
|
||||||
print_message("Device added ($result)");
|
print_message("Device added ($result)");
|
||||||
|
} else {
|
||||||
|
print_error('Error: ' . $result);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
@@ -438,11 +438,11 @@ if (passwordscanchange($users_details['username'])) {
|
|||||||
}//end if
|
}//end if
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
echo print_error('Error getting user details');
|
print_error('Error getting user details');
|
||||||
}//end if
|
}//end if
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
echo print_error("Authentication method doesn't support updating users");
|
print_error("Authentication method doesn't support updating users");
|
||||||
}//end if
|
}//end if
|
||||||
}//end if
|
}//end if
|
||||||
}
|
}
|
||||||
|
@@ -80,9 +80,13 @@ function isCli() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function print_error($text) {
|
function print_error($text, $quiet = false) {
|
||||||
global $console_color;
|
if ($quiet) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (isCli()) {
|
if (isCli()) {
|
||||||
|
global $console_color;
|
||||||
print $console_color->convert("%r".$text."%n\n", false);
|
print $console_color->convert("%r".$text."%n\n", false);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@@ -90,9 +94,14 @@ function print_error($text) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function print_message($text) {
|
function print_message($text, $quiet = false) {
|
||||||
|
if ($quiet) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (isCli()) {
|
if (isCli()) {
|
||||||
print Console_Color2::convert("%g".$text."%n\n", false);
|
global $console_color;
|
||||||
|
print $console_color->convert("%g".$text."%n\n", false);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
echo('<div class="alert alert-success"><img src="images/16/tick.png" align="absmiddle"> '.$text.'</div>');
|
echo('<div class="alert alert-success"><img src="images/16/tick.png" align="absmiddle"> '.$text.'</div>');
|
||||||
|
@@ -53,8 +53,9 @@ function discover_new_device($hostname, $device = '', $method = '', $interface =
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (match_network($config['nets'], $ip)) {
|
if (match_network($config['nets'], $ip)) {
|
||||||
$remote_device_id = addHost($dst_host, '', '161', 'udp', '0', $config['distributed_poller_group']);
|
$result = addHost($dst_host, '', '161', 'udp', '0', $config['distributed_poller_group']);
|
||||||
if ($remote_device_id) {
|
if (is_numeric($result)) {
|
||||||
|
$remote_device_id = $result;
|
||||||
$remote_device = device_by_id_cache($remote_device_id, 1);
|
$remote_device = device_by_id_cache($remote_device_id, 1);
|
||||||
echo '+[' . $remote_device['hostname'] . '(' . $remote_device['device_id'] . ')]';
|
echo '+[' . $remote_device['hostname'] . '(' . $remote_device['device_id'] . ')]';
|
||||||
discover_device($remote_device);
|
discover_device($remote_device);
|
||||||
@@ -72,9 +73,10 @@ function discover_new_device($hostname, $device = '', $method = '', $interface =
|
|||||||
}
|
}
|
||||||
|
|
||||||
return $remote_device_id;
|
return $remote_device_id;
|
||||||
|
} else {
|
||||||
|
if(substr($result, 0, 12) !== 'Already have') {
|
||||||
|
log_event("$method discovery of " . $dst_host . " ($ip) failed - " . $result);
|
||||||
}
|
}
|
||||||
else {
|
|
||||||
log_event("$method discovery of " . $dst_host . " ($ip) failed - Check ping and SNMP access", $device['device_id'], 'discovery');
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
d_echo("$ip not in a matched network - skipping\n");
|
d_echo("$ip not in a matched network - skipping\n");
|
||||||
|
@@ -246,155 +246,94 @@ function delete_device($id) {
|
|||||||
return $ret;
|
return $ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
function addHost($host, $snmpver, $port = '161', $transport = 'udp', $quiet = '0', $poller_group = '0', $force_add = '0', $port_assoc_mode = 'ifIndex') {
|
/**
|
||||||
|
* Add a device to LibreNMS
|
||||||
|
*
|
||||||
|
* @param string $host dns name or ip address
|
||||||
|
* @param string $snmp_version If this is empty, try v2c,v3,v1. Otherwise, use this specific version.
|
||||||
|
* @param string $port the port to connect to for snmp
|
||||||
|
* @param string $transport udp or tcp
|
||||||
|
* @param int $quiet don't output anything
|
||||||
|
* @param string $poller_group the poller group this device will belong to
|
||||||
|
* @param string $force_add add even if the device isn't pingable
|
||||||
|
* @param string $port_assoc_mode snmp field to use to determine unique ports
|
||||||
|
* @return int|string returns the id of the added device, otherwise an error message
|
||||||
|
*/
|
||||||
|
function addHost($host, $snmp_version = '', $port = '161', $transport = 'udp', $quiet = 0, $poller_group = '0', $force_add = '0', $port_assoc_mode = 'ifIndex') {
|
||||||
global $config;
|
global $config;
|
||||||
|
|
||||||
list($hostshort) = explode(".", $host);
|
|
||||||
// Test Database Exists
|
// Test Database Exists
|
||||||
if (host_exists($host) === false) {
|
if (host_exists($host) === true) {
|
||||||
// Valid port assoc mode
|
return "Already have host $host";
|
||||||
if (! is_valid_port_assoc_mode ($port_assoc_mode)) {
|
|
||||||
if ($quiet == 0) {
|
|
||||||
print_error ("Invalid port association_mode '$port_assoc_mode'. Valid modes are: " . join (', ', get_port_assoc_modes ()));
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($config['addhost_alwayscheckip'] === TRUE) {
|
// Valid port assoc mode
|
||||||
|
if (!is_valid_port_assoc_mode($port_assoc_mode)) {
|
||||||
|
return "Invalid port association_mode '$port_assoc_mode'. Valid modes are: " . join(', ', get_port_assoc_modes());
|
||||||
|
}
|
||||||
|
|
||||||
|
// check if we have the host by IP
|
||||||
|
if ($config['addhost_alwayscheckip'] === true) {
|
||||||
$ip = gethostbyname($host);
|
$ip = gethostbyname($host);
|
||||||
} else {
|
} else {
|
||||||
$ip = $host;
|
$ip = $host;
|
||||||
}
|
}
|
||||||
if (ip_exists($ip) === false) {
|
if (ip_exists($ip)) {
|
||||||
// Test reachability
|
return "Already have host with this IP $host";
|
||||||
$address_family = snmpTransportToAddressFamily($transport);
|
|
||||||
if ($force_add == 1 || isPingable($host, $address_family)) {
|
|
||||||
if (empty($snmpver)) {
|
|
||||||
// Try SNMPv2c
|
|
||||||
$snmpver = 'v2c';
|
|
||||||
$ret = addHost($host, $snmpver, $port, $transport, $quiet, $poller_group, $force_add, $port_assoc_mode);
|
|
||||||
if (!$ret) {
|
|
||||||
//Try SNMPv3
|
|
||||||
$snmpver = 'v3';
|
|
||||||
$ret = addHost($host, $snmpver, $port, $transport, $quiet, $poller_group, $force_add, $port_assoc_mode);
|
|
||||||
if (!$ret) {
|
|
||||||
// Try SNMPv1
|
|
||||||
$snmpver = 'v1';
|
|
||||||
return addHost($host, $snmpver, $port, $transport, $quiet, $poller_group, $force_add, $port_assoc_mode);
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
return $ret;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
return $ret;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Test reachability
|
||||||
|
$address_family = snmpTransportToAddressFamily($transport);
|
||||||
|
$ping_result = isPingable($host, $address_family);
|
||||||
|
if ($force_add == 1 || !$ping_result['result']) {
|
||||||
|
return "Could not ping $host";
|
||||||
|
}
|
||||||
|
|
||||||
|
// if $snmpver isn't set, try each version of snmp
|
||||||
|
if (empty($snmp_version)) {
|
||||||
|
$snmpvers = array('v2c', 'v3', 'v1');
|
||||||
|
} else {
|
||||||
|
$snmpvers = array($snmp_version);
|
||||||
|
}
|
||||||
|
|
||||||
|
// try different snmp variables to add the device
|
||||||
|
foreach ($snmpvers as $snmpver) {
|
||||||
if ($snmpver === "v3") {
|
if ($snmpver === "v3") {
|
||||||
// Try each set of parameters from config
|
// Try each set of parameters from config
|
||||||
foreach ($config['snmp']['v3'] as $v3) {
|
foreach ($config['snmp']['v3'] as $v3) {
|
||||||
$device = deviceArray($host, NULL, $snmpver, $port, $transport, $v3, $port_assoc_mode);
|
$device = deviceArray($host, null, $snmpver, $port, $transport, $v3, $port_assoc_mode);
|
||||||
if($quiet == '0') { print_message("Trying v3 parameters " . $v3['authname'] . "/" . $v3['authlevel'] . " ... "); }
|
print_message("Trying v3 parameters " . $v3['authname'] . "/" . $v3['authlevel'] . " ... ", $quiet);
|
||||||
if ($force_add == 1 || isSNMPable($device)) {
|
if ($force_add == 1 || isSNMPable($device)) {
|
||||||
$snmphost = snmp_get($device, "sysName.0", "-Oqv", "SNMPv2-MIB");
|
$snmphost = snmp_get($device, "sysName.0", "-Oqv", "SNMPv2-MIB");
|
||||||
if (empty($snmphost) or ($snmphost == $host || $hostshort = $host)) {
|
$result = createHost($host, null, $snmpver, $port, $transport, $v3, $poller_group, $port_assoc_mode, $snmphost);
|
||||||
$device_id = createHost ($host, NULL, $snmpver, $port, $transport, $v3, $poller_group, $port_assoc_mode, $snmphost);
|
if ($result !== false) {
|
||||||
return $device_id;
|
return $result;
|
||||||
}
|
}
|
||||||
else {
|
} else {
|
||||||
if($quiet == '0') {
|
print_error("No reply on credentials " . $v3['authname'] . "/" . $v3['authlevel'] . " using $snmpver", $quiet);
|
||||||
print_error("Given hostname does not match SNMP-read hostname ($snmphost)!");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
} elseif ($snmpver === "v2c" || $snmpver === "v1") {
|
||||||
else {
|
|
||||||
if($quiet == '0') {
|
|
||||||
print_error("No reply on credentials " . $v3['authname'] . "/" . $v3['authlevel'] . " using $snmpver");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
elseif ($snmpver === "v2c" or $snmpver === "v1") {
|
|
||||||
// try each community from config
|
// try each community from config
|
||||||
foreach ($config['snmp']['community'] as $community) {
|
foreach ($config['snmp']['community'] as $community) {
|
||||||
$device = deviceArray($host, $community, $snmpver, $port, $transport, NULL, $port_assoc_mode);
|
$device = deviceArray($host, $community, $snmpver, $port, $transport, null, $port_assoc_mode);
|
||||||
if($quiet == '0') {
|
print_message("Trying community $community ...", $quiet);
|
||||||
print_message("Trying community $community ...");
|
|
||||||
}
|
|
||||||
if ($force_add == 1 || isSNMPable($device)) {
|
if ($force_add == 1 || isSNMPable($device)) {
|
||||||
$snmphost = snmp_get($device, "sysName.0", "-Oqv", "SNMPv2-MIB");
|
$snmphost = snmp_get($device, "sysName.0", "-Oqv", "SNMPv2-MIB");
|
||||||
if (empty($snmphost) || ($snmphost && ($snmphost == $host || $hostshort = $host))) {
|
$result = createHost($host, $community, $snmpver, $port, $transport, array(), $poller_group, $port_assoc_mode, $snmphost);
|
||||||
$device_id = createHost ($host, $community, $snmpver, $port, $transport,array(),$poller_group, $port_assoc_mode, $snmphost);
|
if ($result !== false) {
|
||||||
return $device_id;
|
return $result;
|
||||||
}
|
}
|
||||||
else {
|
} else {
|
||||||
if($quiet == '0') {
|
print_error("No reply on community $community using $snmpver", $quiet);
|
||||||
print_error("Given hostname does not match SNMP-read hostname ($snmphost)!");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
else {
|
return "Unsupported SNMP Version \"$snmpver\", must be v1, v2c, or v3";
|
||||||
if($quiet == '0') {
|
|
||||||
print_error("No reply on community $community using $snmpver");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
if($quiet == '0') {
|
|
||||||
print_error("Unsupported SNMP Version \"$snmpver\".");
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!$device_id) {
|
return "Could not connect, please check the snmp details and snmp reachability";
|
||||||
// Failed SNMP
|
|
||||||
if($quiet == '0') {
|
|
||||||
print_error("Could not reach $host with given SNMP community using $snmpver");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
// failed Reachability
|
|
||||||
if($quiet == '0') {
|
|
||||||
print_error("Could not ping $host");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
if ($quiet == 0) {
|
|
||||||
print_error("Already have host with this IP $host");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
// found in database
|
|
||||||
if($quiet == '0') {
|
|
||||||
print_error("Already got host $host");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
function scanUDP($host, $port, $timeout) {
|
|
||||||
$handle = fsockopen($host, $port, $errno, $errstr, 2);
|
|
||||||
socket_set_timeout ($handle, $timeout);
|
|
||||||
$write = fwrite($handle,"\x00");
|
|
||||||
if (!$write) {
|
|
||||||
next;
|
|
||||||
}
|
|
||||||
$startTime = time();
|
|
||||||
$endTime = time();
|
|
||||||
$timeDiff = $endTime - $startTime;
|
|
||||||
if ($timeDiff >= $timeout) {
|
|
||||||
fclose($handle);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
fclose($handle);
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function deviceArray($host, $community, $snmpver, $port = 161, $transport = 'udp', $v3, $port_assoc_mode = 'ifIndex') {
|
function deviceArray($host, $community, $snmpver, $port = 161, $transport = 'udp', $v3, $port_assoc_mode = 'ifIndex') {
|
||||||
@@ -507,7 +446,7 @@ function isSNMPable($device) {
|
|||||||
* @param int $address_family The address family (AF_INET for IPv4 or AF_INET6 for IPv6) to use. Defaults to IPv4. Will *not* be autodetected for IP addresses, so it has to be set to AF_INET6 when pinging an IPv6 address or an IPv6-only host.
|
* @param int $address_family The address family (AF_INET for IPv4 or AF_INET6 for IPv6) to use. Defaults to IPv4. Will *not* be autodetected for IP addresses, so it has to be set to AF_INET6 when pinging an IPv6 address or an IPv6-only host.
|
||||||
* @param array $attribs The device attributes
|
* @param array $attribs The device attributes
|
||||||
*
|
*
|
||||||
* @return bool TRUE if the host responded to at least one ping request, FALSE otherwise.
|
* @return array 'result' => bool pingable, 'last_ping_timetaken' => int time for last ping, 'db' => fping results
|
||||||
*/
|
*/
|
||||||
function isPingable($hostname, $address_family = AF_INET, $attribs = array()) {
|
function isPingable($hostname, $address_family = AF_INET, $attribs = array()) {
|
||||||
global $config;
|
global $config;
|
||||||
@@ -546,10 +485,6 @@ function isPingable($hostname, $address_family = AF_INET, $attribs = array()) {
|
|||||||
return($response);
|
return($response);
|
||||||
}
|
}
|
||||||
|
|
||||||
function is_odd($number) {
|
|
||||||
return $number & 1; // 0 = even, 1 = odd
|
|
||||||
}
|
|
||||||
|
|
||||||
function getpollergroup($poller_group='0') {
|
function getpollergroup($poller_group='0') {
|
||||||
//Is poller group an integer
|
//Is poller group an integer
|
||||||
if (is_int($poller_group) || ctype_digit($poller_group)) {
|
if (is_int($poller_group) || ctype_digit($poller_group)) {
|
||||||
@@ -575,13 +510,13 @@ function getpollergroup($poller_group='0') {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function createHost($host, $community = NULL, $snmpver, $port = 161, $transport = 'udp', $v3 = array(), $poller_group='0', $port_assoc_mode = 'ifIndex', $snmphost) {
|
function createHost($host, $community = NULL, $snmpver, $port = 161, $transport = 'udp', $v3 = array(), $poller_group='0', $port_assoc_mode = 'ifIndex', $snmphost = '') {
|
||||||
global $config;
|
global $config;
|
||||||
$host = trim(strtolower($host));
|
$host = trim(strtolower($host));
|
||||||
|
|
||||||
$poller_group=getpollergroup($poller_group);
|
$poller_group=getpollergroup($poller_group);
|
||||||
|
|
||||||
/* Get port_assoc_mode id if neccessary
|
/* Get port_assoc_mode id if necessary
|
||||||
* We can work with names of IDs here */
|
* We can work with names of IDs here */
|
||||||
if (! is_int ($port_assoc_mode))
|
if (! is_int ($port_assoc_mode))
|
||||||
$port_assoc_mode = get_port_assoc_mode_id ($port_assoc_mode);
|
$port_assoc_mode = get_port_assoc_mode_id ($port_assoc_mode);
|
||||||
@@ -603,25 +538,18 @@ function createHost($host, $community = NULL, $snmpver, $port = 161, $transport
|
|||||||
$device['os'] = getHostOS($device);
|
$device['os'] = getHostOS($device);
|
||||||
|
|
||||||
if ($device['os']) {
|
if ($device['os']) {
|
||||||
|
|
||||||
if (host_exists($host, $snmphost) === false) {
|
if (host_exists($host, $snmphost) === false) {
|
||||||
$device_id = dbInsert($device, 'devices');
|
$device_id = dbInsert($device, 'devices');
|
||||||
if ($device_id) {
|
if ($device_id) {
|
||||||
oxidized_reload_nodes();
|
oxidized_reload_nodes();
|
||||||
return($device_id);
|
return $device_id;
|
||||||
}
|
}
|
||||||
else {
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// couldn't add the device
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
else {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
return FALSE;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function isDomainResolves($domain) {
|
function isDomainResolves($domain) {
|
||||||
return (gethostbyname($domain) != $domain || count(dns_get_record($domain)) != 0);
|
return (gethostbyname($domain) != $domain || count(dns_get_record($domain)) != 0);
|
||||||
@@ -1213,23 +1141,20 @@ function fix_integer_value($value) {
|
|||||||
return $return;
|
return $return;
|
||||||
}
|
}
|
||||||
|
|
||||||
function ip_exists($ip) {
|
function ip_exists($ip)
|
||||||
|
{
|
||||||
// Function to check if an IP exists in the DB already
|
// Function to check if an IP exists in the DB already
|
||||||
if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) !== FALSE) {
|
if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) !== false) {
|
||||||
if (!dbFetchRow("SELECT `ipv6_address_id` FROM `ipv6_addresses` WHERE `ipv6_address` = ? OR `ipv6_compressed` = ?", array($ip,$ip))) {
|
$dbresult = dbFetchRow("SELECT `ipv6_address_id` FROM `ipv6_addresses` WHERE `ipv6_address` = ? OR `ipv6_compressed` = ?", array($ip, $ip));
|
||||||
|
return !empty($dbresult);
|
||||||
|
} elseif (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) !== false) {
|
||||||
|
$dbresult = dbFetchRow("SELECT `ipv4_address_id` FROM `ipv4_addresses` WHERE `ipv4_address` = ?", array($ip));
|
||||||
|
return !empty($dbresult);
|
||||||
|
}
|
||||||
|
|
||||||
|
// not an ipv4 or ipv6 address...
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
elseif (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) !== FALSE) {
|
|
||||||
if (!dbFetchRow("SELECT `ipv4_address_id` FROM `ipv4_addresses` WHERE `ipv4_address` = ?", array($ip))) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
function fping($host,$params,$address_family = AF_INET) {
|
function fping($host,$params,$address_family = AF_INET) {
|
||||||
|
|
||||||
|
@@ -51,7 +51,6 @@ function perform_snmp_scan($net) {
|
|||||||
$end = ip2long($net->broadcast)-1;
|
$end = ip2long($net->broadcast)-1;
|
||||||
while ($start++ < $end) {
|
while ($start++ < $end) {
|
||||||
$stats['count']++;
|
$stats['count']++;
|
||||||
$device_id = false;
|
|
||||||
$host = long2ip($start);
|
$host = long2ip($start);
|
||||||
$test = isPingable($host);
|
$test = isPingable($host);
|
||||||
if ($test['result'] === false) {
|
if ($test['result'] === false) {
|
||||||
@@ -64,15 +63,23 @@ function perform_snmp_scan($net) {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
foreach (array('udp','tcp') as $transport) {
|
foreach (array('udp','tcp') as $transport) {
|
||||||
if ($device_id !== false && $device_id > 0) {
|
$result = addHost(gethostbyaddr($host), '', $config['snmp']['port'], $transport, $quiet, $config['distributed_poller_group'], 0);
|
||||||
|
if (is_numeric($result)) {
|
||||||
$stats['added']++;
|
$stats['added']++;
|
||||||
echo '+';
|
echo '+';
|
||||||
} else if ($device_id === 0) {
|
break;
|
||||||
|
} elseif (substr($result, 0, 12) === 'Already have') {
|
||||||
|
$stats['known']++;
|
||||||
|
echo '*';
|
||||||
|
break;
|
||||||
|
} elseif (substr($result, 0 , 14) === 'Could not ping') {
|
||||||
|
echo '.';
|
||||||
|
break;
|
||||||
|
} elseif ($transport == 'tcp') {
|
||||||
|
// tried both udp and tcp without success
|
||||||
$stats['failed']++;
|
$stats['failed']++;
|
||||||
echo '-';
|
echo '-';
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
$device_id = addHost(gethostbyaddr($host), '', $config['snmp']['port'], $transport, $quiet, $config['distributed_poller_group'], 0);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
echo PHP_EOL;
|
echo PHP_EOL;
|
||||||
@@ -106,7 +113,7 @@ if (isset($opts['r'])) {
|
|||||||
$net = Net_IPv4::parseAddress($opts['r']);
|
$net = Net_IPv4::parseAddress($opts['r']);
|
||||||
if (ip2long($net->network) !== false) {
|
if (ip2long($net->network) !== false) {
|
||||||
perform_snmp_scan($net);
|
perform_snmp_scan($net);
|
||||||
echo 'Scanned '.$stats['count'].' IPs, Already know '.$stats['known'].' Devices, Added '.$stats['added'].' Devices, Failed to add '.$stats['failed'].' Devices.'.PHP_EOL;
|
echo 'Scanned '.$stats['count'].' IPs, Already known '.$stats['known'].' Devices, Added '.$stats['added'].' Devices, Failed to add '.$stats['failed'].' Devices.'.PHP_EOL;
|
||||||
echo 'Runtime: '.(microtime(true)-$ts).' secs'.PHP_EOL;
|
echo 'Runtime: '.(microtime(true)-$ts).' secs'.PHP_EOL;
|
||||||
} else {
|
} else {
|
||||||
echo 'Could not interpret supplied CIDR noted IP-Range: '.$opts['r'].PHP_EOL;
|
echo 'Could not interpret supplied CIDR noted IP-Range: '.$opts['r'].PHP_EOL;
|
||||||
|
Reference in New Issue
Block a user