} else { if ($update_message) { print_error($update_message); } } $max_repeaters = get_dev_attrib($device, 'snmp_max_repeaters'); echo "\n <form id='edit' name='edit' method='post' action='' role='form' class='form-horizontal'>\n <input type=hidden name='editing' value='yes'>\n <div class='form-group'>\n <label for='snmpver' class='col-sm-2 control-label'>SNMP Details</label>\n <div class='col-sm-1'>\n <select id='snmpver' name='snmpver' class='form-control input-sm' onChange='changeForm();'>\n <option value='v1'>v1</option>\n <option value='v2c' " . ($device['snmpver'] == 'v2c' ? 'selected' : '') . ">v2c</option>\n <option value='v3' " . ($device['snmpver'] == 'v3' ? 'selected' : '') . ">v3</option>\n </select>\n </div>\n <div class='col-sm-2'>\n <input type='text' name='port' placeholder='port' class='form-control input-sm' value='" . ($device['port'] == $config['snmp']['port'] ? "" : $device['port']) . "'>\n </div>\n <div class='col-sm-1'>\n <select name='transport' id='transport' class='form-control input-sm'>"; foreach ($config['snmp']['transports'] as $transport) { echo "<option value='" . $transport . "'"; if ($transport == $device['transport']) { echo " selected='selected'"; } echo '>' . $transport . '</option>'; } echo " </select>\n </div>\n </div>\n <div class='form-group'>\n <div class='col-sm-2'>\n </div>\n <div class='col-sm-1'>\n <input id='timeout' name='timeout' class='form-control input-sm' value='" . ($device['timeout'] ? $device['timeout'] : '') . "' placeholder='seconds' />\n </div>\n <div class='col-sm-1'>\n <input id='retries' name='retries' class='form-control input-sm' value='" . ($device['timeout'] ? $device['retries'] : '') . "' placeholder='retries' />\n </div>\n </div>\n <div class='form-group'>\n <label for='port_assoc_mode' class='col-sm-2 control-label'>Port Association Mode</label>\n <div class='col-sm-1'>\n <select name='port_assoc_mode' id='port_assoc_mode' class='form-control input-sm'>\n"; foreach (get_port_assoc_modes() as $pam) { $pam_id = get_port_assoc_mode_id($pam); echo " <option value='{$pam_id}'"; if ($pam_id == $device['port_association_mode']) { echo " selected='selected'"; } echo ">{$pam}</option>\n"; } echo " </select>\n </div>\n </div>\n <div class='form-group'>\n <label for='max_repeaters' class='col-sm-2 control-label'>Max Repeaters</label>\n <div class='col-sm-1'>\n <input id='max_repeaters' name='max_repeaters' class='form-control input-sm' value='" . $max_repeaters . "' placeholder='max rep' />\n </div>\n </div>\n <div id='snmpv1_2'>\n <div class='form-group'>\n <label class='col-sm-3 control-label text-left'><h4><strong>SNMPv1/v2c Configuration</strong></h4></label>\n </div>\n <div class='form-group'>\n <label for='community' class='col-sm-2 control-label'>SNMP Community</label>\n <div class='col-sm-4'>\n <input id='community' class='form-control' name='community' value='" . $device['community'] . "'/>\n </div>\n </div>\n </div>\n <div id='snmpv3'>\n <div class='form-group'>\n <label class='col-sm-3 control-label'><h4><strong>SNMPv3 Configuration</strong></h4></label>\n </div>\n <div class='form-group'>\n <label for='authlevel' class='col-sm-2 control-label'>Auth Level</label>\n <div class='col-sm-4'>\n <select id='authlevel' name='authlevel' class='form-control'>\n <option value='noAuthNoPriv'>noAuthNoPriv</option>\n <option value='authNoPriv' " . ($device['authlevel'] == 'authNoPriv' ? 'selected' : '') . ">authNoPriv</option>\n <option value='authPriv' " . ($device['authlevel'] == 'authPriv' ? 'selected' : '') . ">authPriv</option>\n </select>\n </div>\n </div>\n <div class='form-group'>\n <label for='authname' class='col-sm-2 control-label'>Auth User Name</label>\n <div class='col-sm-4'>\n <input type='text' id='authname' name='authname' class='form-control' value='" . $device['authname'] . "'>\n </div>\n </div>\n <div class='form-group'>\n <label for='authpass' class='col-sm-2 control-label'>Auth Password</label>\n <div class='col-sm-4'>\n <input type='password' id='authpass' name='authpass' class='form-control' value='" . $device['authpass'] . "'>\n </div>\n </div>\n <div class='form-group'>\n <label for='authalgo' class='col-sm-2 control-label'>Auth Algorithm</label>\n <div class='col-sm-4'>\n <select id='authalgo' name='authalgo' class='form-control'>\n <option value='MD5'>MD5</option>\n <option value='SHA' " . ($device['authalgo'] === 'SHA' ? 'selected' : '') . ">SHA</option>\n </select>\n </div>\n </div>\n <div class='form-group'>\n <label for='cryptopass' class='col-sm-2 control-label'>Crypto Password</label>\n <div class='col-sm-4'>\n <input type='password' id='cryptopass' name='cryptopass' class='form-control' value='" . $device['cryptopass'] . "'>\n </div>\n </div>\n <div class='form-group'>\n <label for='cryptoalgo' class='col-sm-2 control-label'>Crypto Algorithm</label>\n <div class='col-sm-4'>\n <select id='cryptoalgo' name='cryptoalgo' class='form-control'>\n <option value='AES'>AES</option>\n <option value='DES' " . ($device['cryptoalgo'] === 'DES' ? 'selected' : '') . ">DES</option>\n </select>\n </div>\n </div>\n </div>"; if ($config['distributed_poller'] === true) { echo ' <div class="form-group"> <label for="poller_group" class="col-sm-2 control-label">Poller Group</label> <div class="col-sm-4"> <select name="poller_group" id="poller_group" class="form-control input-sm"> <option value="0"> Default poller group</option>
/** * Validate port_association_mode * @param string $port_assoc_mode * @return bool */ function is_valid_port_assoc_mode($port_assoc_mode) { return in_array($port_assoc_mode, get_port_assoc_modes()); }
$poller_group = $options['g']; } elseif ($config['distributed_poller'] === true) { $poller_group = $config['distributed_poller_group']; } else { $poller_group = 0; } if (isset($options['f']) && $options['f'] == 0) { $cmd = array_shift($argv); array_shift($argv); array_unshift($argv, $cmd); $force_add = true; } else { $force_add = false; } $port_assoc_mode = $config['default_port_association_mode']; $valid_assoc_modes = get_port_assoc_modes(); if (isset($options['p'])) { $port_assoc_mode = $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(1); } $cmd = array_shift($argv); array_shift($argv); array_shift($argv); array_unshift($argv, $cmd); } if (!empty($argv[1])) { $host = strtolower($argv[1]); $community = $argv[2];
function addHost($host, $snmpver, $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 ($config['addhost_alwayscheckip'] === TRUE) { $ip = gethostbyname($host); } 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); 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); 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 0; }
echo "<option value='" . $transport . "'"; if ($transport == $device['transport']) { echo " selected='selected'"; } echo '>' . $transport . '</option>'; } ?> </select> </div> </div> <div class="form-group"> <label for="port_association_mode" class="col-sm-3 control-label">Port Association Mode</label> <div class="col-sm-3"> <select name="port_assoc_mode" id="port_assoc_mode" class="form-control input-sm"> <?php foreach (get_port_assoc_modes() as $mode) { $selected = ""; if ($mode == $config['default_port_association_mode']) { $selected = "selected"; } echo " <option value=\"{$mode}\" {$selected}>{$mode}</option>\n"; } ?> </select> </div> </div> <div id="snmpv1_2"> <div class="form-group"> <div class="col-sm-12 alert alert-info"> <label class="control-label text-left input-sm">SNMPv1/2c Configuration</label> </div>
/** * 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 string $poller_group the poller group this device will belong to * @param boolean $force_add add even if the device isn't reachable * @param string $port_assoc_mode snmp field to use to determine unique ports * * @return int returns the device_id of the added device * * @throws HostExistsException This hostname already exists * @throws HostIpExistsException We already have a host with this IP * @throws HostUnreachableException We could not reach this device is some way * @throws HostUnreachablePingException We could not ping the device * @throws InvalidPortAssocModeException The given port association mode was invalid * @throws SnmpVersionUnsupportedException The given snmp version was invalid */ function addHost($host, $snmp_version = '', $port = '161', $transport = 'udp', $poller_group = '0', $force_add = false, $port_assoc_mode = 'ifIndex') { global $config; // Test Database Exists if (host_exists($host) === true) { throw new HostExistsException("Already have host {$host}"); } // Valid port assoc mode if (!is_valid_port_assoc_mode($port_assoc_mode)) { throw new InvalidPortAssocModeException("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)) { throw new HostIpExistsException("Already have host with this IP {$host}"); } // Test reachability if (!$force_add) { $address_family = snmpTransportToAddressFamily($transport); $ping_result = isPingable($host, $address_family); if (!$ping_result['result']) { throw new HostUnreachablePingException("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); } $host_unreachable_exception = new HostUnreachableException("Could not connect, please check the snmp details and snmp reachability"); // 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); if ($force_add === true || isSNMPable($device)) { if ($force_add !== true) { $snmphost = snmp_get($device, "sysName.0", "-Oqv", "SNMPv2-MIB"); } $result = createHost($host, null, $snmpver, $port, $transport, $v3, $poller_group, $port_assoc_mode, $snmphost, $force_add); if ($result !== false) { return $result; } } else { $host_unreachable_exception->addReason("SNMP {$snmpver}: No reply with credentials " . $v3['authname'] . "/" . $v3['authlevel']); } } } 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); if ($force_add === true || isSNMPable($device)) { if ($force_add !== true) { $snmphost = snmp_get($device, "sysName.0", "-Oqv", "SNMPv2-MIB"); } $result = createHost($host, $community, $snmpver, $port, $transport, array(), $poller_group, $port_assoc_mode, $snmphost, $force_add); if ($result !== false) { return $result; } } else { $host_unreachable_exception->addReason("SNMP {$snmpver}: No reply with community {$community}"); } } } else { throw new SnmpVersionUnsupportedException("Unsupported SNMP Version \"{$snmpver}\", must be v1, v2c, or v3"); } } throw $host_unreachable_exception; }