feature: Added ability to output more info for snmp-scan.php (#5838)

This commit is contained in:
Neil Lathwood
2017-02-09 12:35:27 +00:00
committed by Tony Murray
parent f301cf5efd
commit a9e70fb9ed

View File

@@ -43,7 +43,7 @@ if ($config['autodiscovery']['snmpscan'] === false) {
function perform_snmp_scan($net, $force_network, $force_broadcast) function perform_snmp_scan($net, $force_network, $force_broadcast)
{ {
global $stats, $config, $debug, $vdebug; global $stats, $config, $debug, $vdebug, $more_info;
echo 'Range: '.$net->network.'/'.$net->bitmask.PHP_EOL; echo 'Range: '.$net->network.'/'.$net->bitmask.PHP_EOL;
$config['snmp']['timeout'] = 1; $config['snmp']['timeout'] = 1;
$config['snmp']['retries'] = 0; $config['snmp']['retries'] = 0;
@@ -78,12 +78,12 @@ function perform_snmp_scan($net, $force_network, $force_broadcast)
$stats['count']++; $stats['count']++;
$host = long2ip($start); $host = long2ip($start);
if ($vdebug) { if ($vdebug || $more_info === true) {
echo "Scanning: ".$host.PHP_EOL; echo "Scanning: ".$host.PHP_EOL;
} }
if (match_network($config['autodiscovery']['nets-exclude'], $host)) { if (match_network($config['autodiscovery']['nets-exclude'], $host)) {
if ($vdebug) { if ($vdebug || $more_info === true) {
echo "Excluded by config.php".PHP_EOL.PHP_EOL; echo "Excluded by config.php".PHP_EOL.PHP_EOL;
} else { } else {
echo '|'; echo '|';
@@ -92,8 +92,8 @@ function perform_snmp_scan($net, $force_network, $force_broadcast)
} }
$test = isPingable($host); $test = isPingable($host);
if ($test['result'] === false) { if ($test['result'] === false) {
if ($vdebug) { if ($vdebug || $more_info === true) {
echo "Unpingable Device".PHP_EOL.PHP_EOL; echo "Unpingable Device $host".PHP_EOL.PHP_EOL;
} else { } else {
echo '.'; echo '.';
} }
@@ -101,8 +101,8 @@ function perform_snmp_scan($net, $force_network, $force_broadcast)
} }
if (ip_exists($host)) { if (ip_exists($host)) {
$stats['known']++; $stats['known']++;
if ($vdebug) { if ($vdebug || $more_info === true) {
echo "Known Device".PHP_EOL; echo "Known Device $host".PHP_EOL;
} else { } else {
echo '*'; echo '*';
} }
@@ -112,29 +112,29 @@ function perform_snmp_scan($net, $force_network, $force_broadcast)
try { try {
addHost(gethostbyaddr($host), '', $config['snmp']['port'], $transport, $config['distributed_poller_group']); addHost(gethostbyaddr($host), '', $config['snmp']['port'], $transport, $config['distributed_poller_group']);
$stats['added']++; $stats['added']++;
if ($vdebug) { if ($vdebug || $more_info === true) {
echo "Added Device".PHP_EOL.PHP_EOL; echo "Added Device $host".PHP_EOL.PHP_EOL;
} else { } else {
echo '+'; echo '+';
} }
break; break;
} catch (HostExistsException $e) { } catch (HostExistsException $e) {
$stats['known']++; $stats['known']++;
if ($vdebug) { if ($vdebug || $more_info === true) {
echo "Known Device".PHP_EOL.PHP_EOL; echo "Known Device $host".PHP_EOL.PHP_EOL;
} else { } else {
echo '*'; echo '*';
} }
break; break;
} catch (HostUnreachablePingException $e) { } catch (HostUnreachablePingException $e) {
if ($vdebug) { if ($vdebug || $more_info === true) {
echo "Unpingable Device".PHP_EOL.PHP_EOL; echo "Unpingable Device $host".PHP_EOL.PHP_EOL;
} else { } else {
echo '.'; echo '.';
} }
break; break;
} catch (HostUnreachableException $e) { } catch (HostUnreachableException $e) {
if ($debug) { if ($debug || $more_info === true) {
print_error($e->getMessage() . " over $transport"); print_error($e->getMessage() . " over $transport");
foreach ($e->getReasons() as $reason) { foreach ($e->getReasons() as $reason) {
echo " $reason".PHP_EOL; echo " $reason".PHP_EOL;
@@ -143,8 +143,8 @@ function perform_snmp_scan($net, $force_network, $force_broadcast)
if ($transport === 'tcp') { if ($transport === 'tcp') {
// tried both udp and tcp without success // tried both udp and tcp without success
$stats['failed']++; $stats['failed']++;
if ($vdebug) { if ($vdebug || $more_info === true) {
echo "Failed to Add Device".PHP_EOL.PHP_EOL; echo "Failed to Add Device $host".PHP_EOL.PHP_EOL;
} else { } else {
echo '-'; echo '-';
} }
@@ -155,7 +155,7 @@ function perform_snmp_scan($net, $force_network, $force_broadcast)
echo PHP_EOL; echo PHP_EOL;
} }
$opts = getopt('r:d::v::n::b::l::h::'); $opts = getopt('r:d::v::i::n::b::l::h::');
$stats = array('count'=> 0, 'known'=>0, 'added'=>0, 'failed'=>0); $stats = array('count'=> 0, 'known'=>0, 'added'=>0, 'failed'=>0);
$start = false; $start = false;
$debug = false; $debug = false;
@@ -174,6 +174,7 @@ if (isset($opts['h']) || (empty($opts) && (!isset($config['nets']) || empty($con
echo ' -b Force scan of broadcast address'.PHP_EOL; echo ' -b Force scan of broadcast address'.PHP_EOL;
echo ' -d Enable Debug'.PHP_EOL; echo ' -d Enable Debug'.PHP_EOL;
echo ' -v Enable verbose Debug'.PHP_EOL; echo ' -v Enable verbose Debug'.PHP_EOL;
echo ' -i Provide more information on actions'.PHP_EOL;
echo ' -l Show Legend'.PHP_EOL; echo ' -l Show Legend'.PHP_EOL;
echo ' -h Print this text'.PHP_EOL; echo ' -h Print this text'.PHP_EOL;
exit(0); exit(0);
@@ -196,6 +197,10 @@ if (isset($opts['b'])) {
$force_broadcast = true; $force_broadcast = true;
} }
if (isset($opts['i'])) {
$more_info = true;
}
if (isset($opts['r'])) { 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) {