From 66bed276c28d953262ecb6d61a028c2613a454b2 Mon Sep 17 00:00:00 2001 From: Tony Murray Date: Mon, 1 Aug 2016 22:01:01 -0500 Subject: [PATCH 1/3] Reconstruct addHost() so it is easier to understand Update addHost() to return either device_id or a string modify print_error(), print_message() to allow for $quiet variable Only call addHost() once in addhost.php, if the snmp version isn't set, addHost will cope Fix up snmp-scan.php and discovery-protocols.inc.php to detect when a device already exists change in ip_exists() to make it more readable --- addhost.php | 95 ++++------ html/includes/api_functions.inc.php | 4 +- html/pages/addhost.inc.php | 4 +- html/pages/edituser.inc.php | 4 +- includes/common.php | 17 +- includes/discovery/functions.inc.php | 12 +- includes/functions.php | 273 ++++++++++----------------- snmp-scan.php | 23 ++- 8 files changed, 177 insertions(+), 255 deletions(-) diff --git a/addhost.php b/addhost.php index 0b4401aca5..c7ed66ccf5 100755 --- a/addhost.php +++ b/addhost.php @@ -27,9 +27,10 @@ if (isset($options['g']) && $options['g'] >= 0) { array_shift($argv); array_unshift($argv, $cmd); $poller_group = $options['g']; -} -else if ($config['distributed_poller'] === true) { +} elseif ($config['distributed_poller'] === true) { $poller_group = $config['distributed_poller_group']; +} else { + $poller_group = 0; } if (isset($options['f']) && $options['f'] == 0) { @@ -37,6 +38,8 @@ if (isset($options['f']) && $options['f'] == 0) { array_shift($argv); array_unshift($argv, $cmd); $force_add = 1; +} else { + $force_add = 0; } $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)) { echo "Invalid port association mode '" . $port_assoc_mode . "'\n"; echo 'Valid modes: ' . join (', ', $valid_assoc_modes) . "\n"; - exit; + exit(2); } $cmd = array_shift($argv); @@ -76,6 +79,7 @@ if (!empty($argv[1])) { 'cryptoalgo' => 'AES', ); + // v3 if ($seclevel === 'nanp' or $seclevel === 'any' or $seclevel === 'noAuthNoPriv') { $v3['authlevel'] = 'noAuthNoPriv'; $v3args = array_slice($argv, 4); @@ -84,11 +88,9 @@ if (!empty($argv[1])) { // parse all remaining args if (is_numeric($arg)) { $port = $arg; - } - else if (preg_match('/^('.implode('|', $config['snmp']['transports']).')$/', $arg)) { + } elseif (preg_match('/^('.implode('|', $config['snmp']['transports']).')$/', $arg)) { $transport = $arg; - } - else { + } else { // should add a sanity check of chars allowed in user $user = $arg; } @@ -97,10 +99,7 @@ if (!empty($argv[1])) { if ($seclevel === 'nanp') { array_push($config['snmp']['v3'], $v3); } - - $device_id = addHost($host, $snmpver, $port, $transport, 0, $poller_group, $force_add, $port_assoc_mode); - } - else if ($seclevel === 'anp' or $seclevel === 'authNoPriv') { + } elseif ($seclevel === 'anp' or $seclevel === 'authNoPriv') { $v3['authlevel'] = 'authNoPriv'; $v3args = array_slice($argv, 4); $v3['authname'] = array_shift($v3args); @@ -110,23 +109,18 @@ if (!empty($argv[1])) { // parse all remaining args if (is_numeric($arg)) { $port = $arg; - } - else if (preg_match('/^('.implode('|', $config['snmp']['transports']).')$/i', $arg)) { + } elseif (preg_match('/^('.implode('|', $config['snmp']['transports']).')$/i', $arg)) { $transport = $arg; - } - else if (preg_match('/^(sha|md5)$/i', $arg)) { + } elseif (preg_match('/^(sha|md5)$/i', $arg)) { $v3['authalgo'] = $arg; - } - else { + } else { echo 'Invalid argument: '.$arg."\n"; - return; + exit(2); } } array_push($config['snmp']['v3'], $v3); - $device_id = addHost($host, $snmpver, $port, $transport, 0, $poller_group, $force_add, $port_assoc_mode); - } - else if ($seclevel === 'ap' or $seclevel === 'authPriv') { + } elseif ($seclevel === 'ap' or $seclevel === 'authPriv') { $v3['authlevel'] = 'authPriv'; $v3args = array_slice($argv, 4); $v3['authname'] = array_shift($v3args); @@ -137,41 +131,31 @@ if (!empty($argv[1])) { // parse all remaining args if (is_numeric($arg)) { $port = $arg; - } - else if (preg_match('/^('.implode('|', $config['snmp']['transports']).')$/i', $arg)) { + } elseif (preg_match('/^('.implode('|', $config['snmp']['transports']).')$/i', $arg)) { $transport = $arg; - } - else if (preg_match('/^(sha|md5)$/i', $arg)) { + } elseif (preg_match('/^(sha|md5)$/i', $arg)) { $v3['authalgo'] = $arg; - } - else if (preg_match('/^(aes|des)$/i', $arg)) { + } elseif (preg_match('/^(aes|des)$/i', $arg)) { $v3['cryptoalgo'] = $arg; - } - else { + } else { echo 'Invalid argument: '.$arg."\n"; - return; + exit(2); } }//end while array_push($config['snmp']['v3'], $v3); - $device_id = addHost($host, $snmpver, $port, $transport, 0, $poller_group, $force_add, $port_assoc_mode); } - else { - // Error or do nothing ? - }//end if - } - else { + } else { + // v2c or v1 $v2args = array_slice($argv, 2); while ($arg = array_shift($v2args)) { // parse all remaining args if (is_numeric($arg)) { $port = $arg; - } - else if (preg_match('/('.implode('|', $config['snmp']['transports']).')/i', $arg)) { + } elseif (preg_match('/('.implode('|', $config['snmp']['transports']).')/i', $arg)) { $transport = $arg; - } - else if (preg_match('/^(v1|v2c)$/i', $arg)) { + } elseif (preg_match('/^(v1|v2c)$/i', $arg)) { $snmpver = $arg; } } @@ -179,36 +163,20 @@ if (!empty($argv[1])) { if ($community) { $config['snmp']['community'] = array($community); } - - $device_id = addHost($host, $snmpver, $port, $transport, 0, $poller_group, $force_add, $port_assoc_mode); }//end if - if ($snmpver) { - $snmpversions[] = $snmpver; - } - else { - $snmpversions = array( - 'v2c', - 'v3', - 'v1', - ); - } + $result = addHost($host, $snmpver, $port, $transport, 0, $poller_group, $force_add, $port_assoc_mode); - while (!$device_id && count($snmpversions)) { - $snmpver = array_shift($snmpversions); - $device_id = addHost($host, $snmpver, $port, $transport, 0, $poller_group, $force_add, $port_assoc_mode); - } - - if ($device_id) { - $device = device_by_id_cache($device_id); + if (is_numeric($result)) { + $device = device_by_id_cache($result); echo 'Added device '.$device['hostname'].' ('.$device_id.")\n"; - exit; + exit(0); } 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( "\n".$config['project_name_version'].' Add Host Tool @@ -229,4 +197,5 @@ else { %rRemember to run discovery for the host afterwards.%n ' ); + exit(2); } diff --git a/html/includes/api_functions.inc.php b/html/includes/api_functions.inc.php index 51de73113b..d06d625f5b 100644 --- a/html/includes/api_functions.inc.php +++ b/html/includes/api_functions.inc.php @@ -290,13 +290,13 @@ function add_device() { } if (empty($message)) { $result = addHost($hostname, $snmpver, $port, $transport, 1, $poller_group, $force_add); - if ($result) { + if (is_numeric($result)) { $code = 201; $status = 'ok'; $message = "Device $hostname has been added successfully"; } else { - $message = "Failed adding $hostname"; + $message = $result; } } diff --git a/html/pages/addhost.inc.php b/html/pages/addhost.inc.php index fdbf9b0e32..f2bf274e6d 100644 --- a/html/pages/addhost.inc.php +++ b/html/pages/addhost.inc.php @@ -67,8 +67,10 @@ if ($_POST['hostname']) { $port_assoc_mode = $_POST['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)"); + } else { + print_error('Error: ' . $result); } } else { diff --git a/html/pages/edituser.inc.php b/html/pages/edituser.inc.php index 0966df70c2..0c96ecc788 100644 --- a/html/pages/edituser.inc.php +++ b/html/pages/edituser.inc.php @@ -438,11 +438,11 @@ if (passwordscanchange($users_details['username'])) { }//end if } else { - echo print_error('Error getting user details'); + print_error('Error getting user details'); }//end if } else { - echo print_error("Authentication method doesn't support updating users"); + print_error("Authentication method doesn't support updating users"); }//end if }//end if } diff --git a/includes/common.php b/includes/common.php index 3ff884a23b..f8b610ec13 100644 --- a/includes/common.php +++ b/includes/common.php @@ -80,9 +80,13 @@ function isCli() { } } -function print_error($text) { - global $console_color; +function print_error($text, $quiet = false) { + if ($quiet) { + return; + } + if (isCli()) { + global $console_color; print $console_color->convert("%r".$text."%n\n", false); } else { @@ -90,9 +94,14 @@ function print_error($text) { } } -function print_message($text) { +function print_message($text, $quiet = false) { + if ($quiet) { + return; + } + if (isCli()) { - print Console_Color2::convert("%g".$text."%n\n", false); + global $console_color; + print $console_color->convert("%g".$text."%n\n", false); } else { echo('
'.$text.'
'); diff --git a/includes/discovery/functions.inc.php b/includes/discovery/functions.inc.php index 0c2373cda7..460ef4cc2c 100644 --- a/includes/discovery/functions.inc.php +++ b/includes/discovery/functions.inc.php @@ -53,8 +53,9 @@ function discover_new_device($hostname, $device = '', $method = '', $interface = } if (match_network($config['nets'], $ip)) { - $remote_device_id = addHost($dst_host, '', '161', 'udp', '0', $config['distributed_poller_group']); - if ($remote_device_id) { + $result = addHost($dst_host, '', '161', 'udp', '0', $config['distributed_poller_group']); + if (is_numeric($result)) { + $remote_device_id = $result; $remote_device = device_by_id_cache($remote_device_id, 1); echo '+[' . $remote_device['hostname'] . '(' . $remote_device['device_id'] . ')]'; discover_device($remote_device); @@ -72,9 +73,10 @@ function discover_new_device($hostname, $device = '', $method = '', $interface = } return $remote_device_id; - } - else { - log_event("$method discovery of " . $dst_host . " ($ip) failed - Check ping and SNMP access", $device['device_id'], 'discovery'); + } else { + if(substr($result, 0, 12) !== 'Already have') { + log_event("$method discovery of " . $dst_host . " ($ip) failed - " . $result); + } } } else { d_echo("$ip not in a matched network - skipping\n"); diff --git a/includes/functions.php b/includes/functions.php index 84ba44deac..954385e4cf 100644 --- a/includes/functions.php +++ b/includes/functions.php @@ -246,155 +246,102 @@ function delete_device($id) { 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; - list($hostshort) = explode(".", $host); // Test Database Exists - if (host_exists($host) === false) { - // Valid port assoc mode - 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 (host_exists($host) === true) { + return "Already have host $host"; + } - if ($config['addhost_alwayscheckip'] === TRUE) { - $ip = gethostbyname($host); + // 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); + } else { + $ip = $host; + } + if (ip_exists($ip)) { + return "Already have host with this IP $host"; + } + + // 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") { + // Try each set of parameters from config + foreach ($config['snmp']['v3'] as $v3) { + $device = deviceArray($host, null, $snmpver, $port, $transport, $v3, $port_assoc_mode); + print_message("Trying v3 parameters " . $v3['authname'] . "/" . $v3['authlevel'] . " ... ", $quiet); + if ($force_add == 1 || isSNMPable($device)) { + $snmphost = snmp_get($device, "sysName.0", "-Oqv", "SNMPv2-MIB"); + $result = createHost($host, null, $snmpver, $port, $transport, $v3, $poller_group, $port_assoc_mode, $snmphost); + if ($result !== false) { + return $result; + } + } else { + print_error("No reply on credentials " . $v3['authname'] . "/" . $v3['authlevel'] . " using $snmpver", $quiet); + } + } + } elseif ($snmpver === "v2c" || $snmpver === "v1") { + // try each community from config + foreach ($config['snmp']['community'] as $community) { + $device = deviceArray($host, $community, $snmpver, $port, $transport, null, $port_assoc_mode); + print_message("Trying community $community ...", $quiet); + if ($force_add == 1 || isSNMPable($device)) { + $snmphost = snmp_get($device, "sysName.0", "-Oqv", "SNMPv2-MIB"); + $result = createHost($host, $community, $snmpver, $port, $transport, array(), $poller_group, $port_assoc_mode, $snmphost); + if ($result !== false) { + return $result; + } + } else { + print_error("No reply on community $community using $snmpver", $quiet); + } + } } else { - $ip = $host; - } - if (ip_exists($ip) === false) { - // Test reachability - $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; - } - } - - if ($snmpver === "v3") { - // Try each set of parameters from config - foreach ($config['snmp']['v3'] as $v3) { - $device = deviceArray($host, NULL, $snmpver, $port, $transport, $v3, $port_assoc_mode); - if($quiet == '0') { print_message("Trying v3 parameters " . $v3['authname'] . "/" . $v3['authlevel'] . " ... "); } - 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, $snmphost); - return $device_id; - } - else { - if($quiet == '0') { - print_error("Given hostname does not match SNMP-read hostname ($snmphost)!"); - } - } - } - 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 - foreach ($config['snmp']['community'] as $community) { - $device = deviceArray($host, $community, $snmpver, $port, $transport, NULL, $port_assoc_mode); - if($quiet == '0') { - print_message("Trying community $community ..."); - } - 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, $snmphost); - return $device_id; - } - else { - if($quiet == '0') { - print_error("Given hostname does not match SNMP-read hostname ($snmphost)!"); - } - } - } - else { - 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) { - // 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 "Unsupported SNMP Version \"$snmpver\", must be v1, v2c, or v3"; } } - return 0; + return "Could not connect, please check the snmp details and snmp reachability"; } -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 getAddHostErrorMessage($error) { + switch($error) { + case -1: + return "Already got host $host"; } + } function deviceArray($host, $community, $snmpver, $port = 161, $transport = 'udp', $v3, $port_assoc_mode = 'ifIndex') { @@ -507,7 +454,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 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()) { global $config; @@ -546,10 +493,6 @@ function isPingable($hostname, $address_family = AF_INET, $attribs = array()) { return($response); } -function is_odd($number) { - return $number & 1; // 0 = even, 1 = odd -} - function getpollergroup($poller_group='0') { //Is poller group an integer if (is_int($poller_group) || ctype_digit($poller_group)) { @@ -575,13 +518,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; $host = trim(strtolower($host)); $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 */ if (! is_int ($port_assoc_mode)) $port_assoc_mode = get_port_assoc_mode_id ($port_assoc_mode); @@ -603,24 +546,17 @@ function createHost($host, $community = NULL, $snmpver, $port = 161, $transport $device['os'] = getHostOS($device); if ($device['os']) { - if (host_exists($host, $snmphost) === false) { $device_id = dbInsert($device, 'devices'); if ($device_id) { oxidized_reload_nodes(); - return($device_id); - } - else { - return false; + return $device_id; } } - else { - return false; - } - } - else { - return FALSE; } + + // couldn't add the device + return false; } function isDomainResolves($domain) { @@ -1213,22 +1149,19 @@ function fix_integer_value($value) { return $return; } -function ip_exists($ip) { +function ip_exists($ip) +{ // Function to check if an IP exists in the DB already - 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))) { - return false; - } + if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6) !== false) { + $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); } - 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; + + // not an ipv4 or ipv6 address... + return false; } function fping($host,$params,$address_family = AF_INET) { diff --git a/snmp-scan.php b/snmp-scan.php index 9e54c9929c..4f38f69750 100755 --- a/snmp-scan.php +++ b/snmp-scan.php @@ -51,9 +51,8 @@ function perform_snmp_scan($net) { $end = ip2long($net->broadcast)-1; while ($start++ < $end) { $stats['count']++; - $device_id = false; - $host = long2ip($start); - $test = isPingable($host); + $host = long2ip($start); + $test = isPingable($host); if ($test['result'] === false) { echo '.'; continue; @@ -64,15 +63,23 @@ function perform_snmp_scan($net) { continue; } 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']++; 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']++; echo '-'; - break; } - $device_id = addHost(gethostbyaddr($host), '', $config['snmp']['port'], $transport, $quiet, $config['distributed_poller_group'], 0); } } echo PHP_EOL; @@ -106,7 +113,7 @@ 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 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; } else { echo 'Could not interpret supplied CIDR noted IP-Range: '.$opts['r'].PHP_EOL; From 231bb52ed1c6fee73701b02191bff6c570d4af7c Mon Sep 17 00:00:00 2001 From: Tony Murray Date: Tue, 2 Aug 2016 08:08:20 -0500 Subject: [PATCH 2/3] Accidental inclusion. --- includes/functions.php | 8 -------- 1 file changed, 8 deletions(-) diff --git a/includes/functions.php b/includes/functions.php index 954385e4cf..b7ce3b20f1 100644 --- a/includes/functions.php +++ b/includes/functions.php @@ -336,14 +336,6 @@ function addHost($host, $snmp_version = '', $port = '161', $transport = 'udp', $ return "Could not connect, please check the snmp details and snmp reachability"; } -function getAddHostErrorMessage($error) { - switch($error) { - case -1: - return "Already got host $host"; - } - -} - function deviceArray($host, $community, $snmpver, $port = 161, $transport = 'udp', $v3, $port_assoc_mode = 'ifIndex') { $device = array(); $device['hostname'] = $host; From 52d31369f04f545912d5b6d42a93a37b58a4df52 Mon Sep 17 00:00:00 2001 From: Tony Murray Date: Wed, 3 Aug 2016 15:16:30 -0500 Subject: [PATCH 3/3] addHost.php output improvements/fixes --- addhost.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/addhost.php b/addhost.php index c7ed66ccf5..8e799174df 100755 --- a/addhost.php +++ b/addhost.php @@ -169,11 +169,11 @@ if (!empty($argv[1])) { if (is_numeric($result)) { $device = device_by_id_cache($result); - echo 'Added device '.$device['hostname'].' ('.$device_id.")\n"; + echo 'Added device '.$device['hostname'].' ('.$result.")\n"; exit(0); } else { - print $console_color->convert("%rWe couldn't add this device:\n" . $result . "%n\n"); + print $console_color->convert("%rWe couldn't add this device:\n " . $result . "%n\n"); exit(1); } } else {