mirror of
https://github.com/librenms/librenms.git
synced 2024-10-07 16:52:45 +00:00
* Implement an autoloader When cleaning up classes for psr2, things got a bit unwieldy, so I implemented a class autoloader. I created a PSR-0 compliant LibreNMS directory and moved all classes there that made sense. Implemented LibreNMS\ClassLoader which supports adding manual class mappings This reduces the file includes needed and only loads classes when needed. * Add teh autoloader to graph.php * Add a small bit of docs Fix incomplete class in includes/discovery/functions.inc.php
155 lines
5.4 KiB
PHP
Executable File
155 lines
5.4 KiB
PHP
Executable File
#!/usr/bin/env php
|
|
<?php
|
|
/*
|
|
* Copyright (C) 2015 Daniel Preussker <f0o@librenms.org>
|
|
* This program is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License as published by
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
/**
|
|
* SNMP Scan
|
|
* @author f0o <f0o@librenms.org>
|
|
* @copyright 2015 f0o, LibreNMS
|
|
* @license GPL
|
|
* @package LibreNMS
|
|
* @subpackage Discovery
|
|
*/
|
|
|
|
use LibreNMS\Exceptions\HostExistsException;
|
|
use LibreNMS\Exceptions\HostUnreachableException;
|
|
use LibreNMS\Exceptions\HostUnreachablePingException;
|
|
|
|
$ts = microtime(true);
|
|
|
|
chdir(dirname($argv[0]));
|
|
|
|
require 'includes/defaults.inc.php';
|
|
require 'config.php';
|
|
require 'includes/definitions.inc.php';
|
|
require 'includes/functions.php';
|
|
require 'includes/discovery/functions.inc.php';
|
|
|
|
if ($config['autodiscovery']['snmpscan'] == false) {
|
|
echo 'SNMP-Scan disabled.'.PHP_EOL;
|
|
exit(2);
|
|
}
|
|
|
|
function perform_snmp_scan($net) {
|
|
global $stats, $config, $debug, $vdebug;
|
|
echo 'Range: '.$net->network.'/'.$net->bitmask.PHP_EOL;
|
|
$config['snmp']['timeout'] = 1;
|
|
$config['snmp']['retries'] = 0;
|
|
$config['fping_options']['retries'] = 0;
|
|
$start = ip2long($net->network);
|
|
$end = ip2long($net->broadcast)-1;
|
|
while ($start++ < $end) {
|
|
$stats['count']++;
|
|
$host = long2ip($start);
|
|
if (match_network($config['autodiscovery']['nets-exclude'], $host)) {
|
|
echo '|';
|
|
continue;
|
|
}
|
|
$test = isPingable($host);
|
|
if ($test['result'] === false) {
|
|
echo '.';
|
|
continue;
|
|
}
|
|
if (ip_exists($host)) {
|
|
$stats['known']++;
|
|
echo '*';
|
|
continue;
|
|
}
|
|
foreach (array('udp','tcp') as $transport) {
|
|
try {
|
|
addHost(gethostbyaddr($host), '', $config['snmp']['port'], $transport, $config['distributed_poller_group']);
|
|
$stats['added']++;
|
|
echo '+';
|
|
break;
|
|
} catch (HostExistsException $e) {
|
|
$stats['known']++;
|
|
echo '*';
|
|
break;
|
|
} catch (HostUnreachablePingException $e) {
|
|
echo '.';
|
|
break;
|
|
} catch (HostUnreachableException $e) {
|
|
if ($debug) {
|
|
print_error($e->getMessage() . " over $transport");
|
|
foreach ($e->getReasons() as $reason) {
|
|
echo " $reason\n";
|
|
}
|
|
}
|
|
if ($transport == 'tcp') {
|
|
// tried both udp and tcp without success
|
|
$stats['failed']++;
|
|
echo '-';
|
|
}
|
|
}
|
|
}
|
|
}
|
|
echo PHP_EOL;
|
|
}
|
|
|
|
$opts = getopt('r:d::v::l::h::');
|
|
$stats = array('count'=> 0, 'known'=>0, 'added'=>0, 'failed'=>0);
|
|
$start = false;
|
|
$debug = false;
|
|
$quiet = 1;
|
|
$net = false;
|
|
|
|
if (isset($opts['h']) || (empty($opts) && (!isset($config['nets']) || empty($config['nets'])))) {
|
|
echo 'Usage: '.$argv[0].' -r <CIDR_Range> [-d] [-l] [-h]'.PHP_EOL;
|
|
echo ' -r CIDR_Range CIDR noted IP-Range to scan'.PHP_EOL;
|
|
echo ' This argument is only requied if $config[\'nets\'] is not set'.PHP_EOL;
|
|
echo ' Example: 192.168.0.0/24'.PHP_EOL;
|
|
echo ' -d Enable Debug'.PHP_EOL;
|
|
echo ' -v Enable verbose Debug'.PHP_EOL;
|
|
echo ' -l Show Legend'.PHP_EOL;
|
|
echo ' -h Print this text'.PHP_EOL;
|
|
exit(0);
|
|
}
|
|
if (isset($opts['d']) || isset($opts['v'])) {
|
|
if (isset($opts['v'])) {
|
|
$vdebug = true;
|
|
}
|
|
$debug = true;
|
|
}
|
|
if (isset($opts['l'])) {
|
|
echo ' * = Known Device; . = Unpingable Device; + = Added Device; - = Failed To Add Device; | = Excluded by config.php'.PHP_EOL;
|
|
}
|
|
if (isset($opts['r'])) {
|
|
$net = Net_IPv4::parseAddress($opts['r']);
|
|
if (ip2long($net->network) !== false) {
|
|
perform_snmp_scan($net);
|
|
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;
|
|
} else {
|
|
echo 'Could not interpret supplied CIDR noted IP-Range: '.$opts['r'].PHP_EOL;
|
|
exit(2);
|
|
}
|
|
} elseif (isset($config['nets']) && !empty($config['nets'])) {
|
|
if (!is_array($config['nets'])) {
|
|
$config['nets'] = array( $config['nets'] );
|
|
}
|
|
foreach( $config['nets'] as $subnet ) {
|
|
$net = Net_IPv4::parseAddress($subnet);
|
|
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 'Runtime: '.(microtime(true)-$ts).' secs'.PHP_EOL;
|
|
} else {
|
|
echo 'Please either add a range argument with \'-r <CIDR_RANGE>\' or define $config[\'nets\'] in your config.php'.PHP_EOL;
|
|
exit(2);
|
|
}
|
|
|