Remove legacy addhost code (#15376)

* Remove legacy addhost code
Update webui addhost
Update device snmp settings page
Update discovery protocols device discovery (no longer force full new device discovery in process)

* remove baseline exceptions

* Apply fixes from StyleCI

---------

Co-authored-by: StyleCI Bot <bot@styleci.io>
This commit is contained in:
Tony Murray
2023-10-07 22:02:45 -05:00
committed by GitHub
parent 685d11d4f1
commit c87c6e8b8e
7 changed files with 126 additions and 526 deletions

View File

@@ -3,87 +3,64 @@
use LibreNMS\Config;
use LibreNMS\Enum\PortAssociationMode;
$device = DeviceCache::getPrimary();
if ($_POST['editing']) {
if (Auth::user()->hasGlobalAdmin()) {
$force_save = ($_POST['force_save'] == 'on');
$poller_group = isset($_POST['poller_group']) ? $_POST['poller_group'] : 0;
$device->poller_group = isset($_POST['poller_group']) ? $_POST['poller_group'] : 0;
$snmp_enabled = ($_POST['snmp'] == 'on');
if ($snmp_enabled) {
$device->snmp_disable = 0;
$device->snmpver = $_POST['snmpver'];
$device->port = $_POST['port'] ? $_POST['port'] : Config::get('snmp.port');
$device->transport = $_POST['transport'] ? $_POST['transport'] : $transport = 'udp';
$device->port_association_mode = $_POST['port_assoc_mode'];
$max_repeaters = $_POST['max_repeaters'];
$max_oid = $_POST['max_oid'];
$port = $_POST['port'] ? $_POST['port'] : Config::get('snmp.port');
$port_assoc_mode = $_POST['port_assoc_mode'];
$retries = $_POST['retries'];
$snmpver = $_POST['snmpver'];
$transport = $_POST['transport'] ? $_POST['transport'] : $transport = 'udp';
$timeout = $_POST['timeout'];
$device->retries = $_POST['retries'] ?: null;
$device->timeout = $_POST['timeout'] ?: null;
$update = [
'poller_group' => $poller_group,
'port' => $port,
'port_association_mode' => $port_assoc_mode,
'snmp_disable' => 0,
'snmpver' => $snmpver,
'transport' => $transport,
];
if ($retries) {
$update['retries'] = $retries;
} else {
$update['retries'] = ['NULL'];
}
if ($snmpver != 'v3' && $_POST['community'] != '********') {
$community = $_POST['community'];
$update['community'] = $community;
}
if ($timeout) {
$update['timeout'] = $timeout;
} else {
$update['timeout'] = ['NULL'];
}
$v3 = [];
if ($snmpver == 'v3') {
$community = ''; // if v3 works, we don't need a community
$v3['authalgo'] = $_POST['authalgo'];
$v3['authlevel'] = $_POST['authlevel'];
$v3['authname'] = $_POST['authname'];
$v3['authpass'] = $_POST['authpass'];
$v3['cryptoalgo'] = $_POST['cryptoalgo'];
$v3['cryptopass'] = $_POST['cryptopass'];
$update = array_merge($update, $v3);
$device->community = ''; // if v3 works, we don't need a community
$device->authalgo = $_POST['authalgo'];
$device->authlevel = $_POST['authlevel'];
$device->authname = $_POST['authname'];
$device->authpass = $_POST['authpass'];
$device->cryptoalgo = $_POST['cryptoalgo'];
$device->cryptopass = $_POST['cryptopass'];
} elseif ($_POST['community'] != '********') {
$device->community = $_POST['community'];
}
} else {
// snmp is disabled
$update['features'] = null;
$update['hardware'] = $_POST['hardware'];
$update['icon'] = null;
$update['os'] = $_POST['os'] ? $_POST['os_id'] : 'ping';
$update['poller_group'] = $poller_group;
$update['snmp_disable'] = 1;
$update['sysName'] = $_POST['sysName'] ? $_POST['sysName'] : null;
$update['version'] = null;
$device->features = null;
$device->hardware = $_POST['hardware'];
$device->icon = null;
$device->os = $_POST['os'] ? $_POST['os_id'] : 'ping';
$device->poller_group = $poller_group;
$device->snmp_disable = 1;
$device->sysName = $_POST['sysName'] ? $_POST['sysName'] : null;
$device->version = null;
}
$device_is_snmpable = false;
$rows_updated = 0;
$device_updated = false;
if ($force_save !== true && $snmp_enabled) {
$device_snmp_details = deviceArray($device['hostname'], $community, $snmpver, $port, $transport, $v3, $port_assoc_mode);
$device_issnmpable = isSNMPable($device_snmp_details);
$helper = new \LibreNMS\Polling\ConnectivityHelper($device);
$device_is_snmpable = $helper->isSNMPable();
}
if ($force_save === true || ! $snmp_enabled || $device_issnmpable) {
if ($force_save === true || ! $snmp_enabled || $device_is_snmpable) {
// update devices table
$rows_updated = dbUpdate($update, 'devices', '`device_id` = ?', [$device['device_id']]);
$device_updated = $device->save();
} else {
$device->refresh(); // forget all pending changes
}
if ($snmp_enabled && ($force_save === true || $device_issnmpable)) {
if ($snmp_enabled && ($force_save === true || $device_is_snmpable)) {
// update devices_attribs table
// note:
@@ -116,23 +93,24 @@ if ($_POST['editing']) {
$set_devices_attrib = false; // testing $set_devices_attrib === false is not a true indicator of a failure
if ($form_value != $get_devices_attrib && $form_value_is_numeric && is_numeric($form_value) && $form_value != 0) {
$set_devices_attrib = set_dev_attrib($device, $devices_attrib, $form_value);
$device->setAttrib($devices_attrib, $form_value);
}
if ($form_value != $get_devices_attrib && ! $form_value_is_numeric) {
$set_devices_attrib = set_dev_attrib($device, $devices_attrib, $form_value);
$device->setAttrib($devices_attrib, $form_value);
}
if ($form_value != $get_devices_attrib && $form_value_is_numeric && ! is_numeric($form_value)) {
$set_devices_attrib = del_dev_attrib($device, $devices_attrib);
$device->forgetAttrib($devices_attrib);
}
if ($form_value != $get_devices_attrib && ! $form_value_is_numeric && $form_value == '') {
$set_devices_attrib = del_dev_attrib($device, $devices_attrib);
$device->forgetAttrib($devices_attrib);
}
if ($form_value != $get_devices_attrib && $set_devices_attrib) {
$set_devices_attrib = get_dev_attrib($device, $devices_attrib); // re-check the db value
unset($device->attribs); // unload relation
$set_devices_attrib = $device->getAttrib($devices_attrib); // re-check the db value
}
if ($form_value != $get_devices_attrib && $form_value == $set_devices_attrib && (is_null($set_devices_attrib) || $set_devices_attrib == '')) {
@@ -152,16 +130,16 @@ if ($_POST['editing']) {
unset($devices_attrib);
}
if ($rows_updated > 0) {
if ($device_updated) {
$update_message[] = 'Device record updated';
}
if ($snmp_enabled && ($force_save !== true && ! $device_issnmpable)) {
$update_failed_message[] = 'Could not connect to ' . htmlspecialchars($device['hostname']) . ' with those SNMP settings. To save anyway, turn on Force Save.';
if ($snmp_enabled && ($force_save !== true && ! $device_is_snmpable)) {
$update_failed_message[] = 'Could not connect to ' . htmlspecialchars($device->hostname) . ' with those SNMP settings. To save anyway, turn on Force Save.';
$update_message[] = 'SNMP settings reverted';
}
if ($rows_updated == 0 && ! isset($update_message) && ! isset($update_failed_message)) {
if (! $device_updated && ! isset($update_message) && ! isset($update_failed_message)) {
$update_message[] = 'SNMP settings did not change';
}
}//end if (Auth::user()->hasGlobalAdmin())
@@ -175,9 +153,8 @@ unset($force_save, $poller_group, $snmp_enabled);
unset($community, $max_repeaters, $max_oid, $port, $port_assoc_mode, $retries, $snmpver, $transport, $timeout);
// get up-to-date database values for use on the form
$device = dbFetchRow('SELECT * FROM `devices` WHERE `device_id` = ?', [$device['device_id']]);
$max_oid = get_dev_attrib($device, 'snmp_max_oid');
$max_repeaters = get_dev_attrib($device, 'snmp_max_repeaters');
$max_oid = $device->getAttrib('snmp_max_oid');
$max_repeaters = $device->getAttrib('snmp_max_repeaters');
// use PHP Flasher to print normal (success) messages, similar to Device Settings
if (isset($update_message)) {
@@ -219,49 +196,49 @@ echo "
<div class='form-group'>
<label for='hardware' class='col-sm-2 control-label'>SNMP</label>
<div class='col-sm-4'>
<input type='checkbox' id='snmp' name='snmp' data-size='small' onChange='disableSnmp(this);'" . ($device['snmp_disable'] ? '' : ' checked') . ">
<input type='checkbox' id='snmp' name='snmp' data-size='small' onChange='disableSnmp(this);'" . ($device->snmp_disable ? '' : ' checked') . ">
</div>
</div>
<div id='snmp_override' style='display: " . ($device['snmp_disable'] ? 'block' : 'none') . ";'>
<div id='snmp_override' style='display: " . ($device->snmp_disable ? 'block' : 'none') . ";'>
<div class='form-group'>
<label for='sysName' class='col-sm-2 control-label'>sysName (optional)</label>
<div class='col-sm-4'>
<input id='sysName' class='form-control' name='sysName' value='" . htmlspecialchars($device['sysName']) . "'/>
<input id='sysName' class='form-control' name='sysName' value='" . htmlspecialchars($device->sysName) . "'/>
</div>
</div>
<div class='form-group'>
<label for='hardware' class='col-sm-2 control-label'>Hardware (optional)</label>
<div class='col-sm-4'>
<input id='hardware' class='form-control' name='hardware' value='" . htmlspecialchars($device['hardware']) . "'/>
<input id='hardware' class='form-control' name='hardware' value='" . htmlspecialchars($device->hardware) . "'/>
</div>
</div>
<div class='form-group'>
<label for='os' class='col-sm-2 control-label'>OS (optional)</label>
<div class='col-sm-4'>
<input id='os' class='form-control' name='os' value='" . htmlspecialchars(Config::get("os.{$device['os']}.text")) . "'/>
<input type='hidden' id='os_id' class='form-control' name='os_id' value='" . $device['os'] . "'/>
<input id='os' class='form-control' name='os' value='" . htmlspecialchars(Config::get("os.{$device->os}.text")) . "'/>
<input type='hidden' id='os_id' class='form-control' name='os_id' value='" . $device->os . "'/>
</div>
</div>
</div>
<div id='snmp_conf' style='display: " . ($device['snmp_disable'] ? 'none' : 'block') . ";'>
<div id='snmp_conf' style='display: " . ($device->snmp_disable ? 'none' : 'block') . ";'>
<input type=hidden name='editing' value='yes'>
<div class='form-group'>
<label for='snmpver' class='col-sm-2 control-label'>SNMP Details</label>
<div class='col-sm-1'>
<select id='snmpver' name='snmpver' class='form-control input-sm' onChange='changeForm();'>
<option value='v1'>v1</option>
<option value='v2c' " . ($device['snmpver'] == 'v2c' ? 'selected' : '') . ">v2c</option>
<option value='v3' " . ($device['snmpver'] == 'v3' ? 'selected' : '') . ">v3</option>
<option value='v2c' " . ($device->snmpver == 'v2c' ? 'selected' : '') . ">v2c</option>
<option value='v3' " . ($device->snmpver == 'v3' ? 'selected' : '') . ">v3</option>
</select>
</div>
<div class='col-sm-2'>
<input type='number' name='port' placeholder='port' class='form-control input-sm' value='" . htmlspecialchars($device['port'] == Config::get('snmp.port') ? '' : $device['port']) . "'>
<input type='number' name='port' placeholder='port' class='form-control input-sm' value='" . htmlspecialchars($device->port == Config::get('snmp.port') ? '' : $device->port) . "'>
</div>
<div class='col-sm-1'>
<select name='transport' id='transport' class='form-control input-sm'>";
foreach (Config::get('snmp.transports') as $transport) {
echo "<option value='" . $transport . "'";
if ($transport == $device['transport']) {
if ($transport == $device->transport) {
echo " selected='selected'";
}
@@ -275,10 +252,10 @@ echo " </select>
<div class='col-sm-2'>
</div>
<div class='col-sm-1'>
<input type='number' id='timeout' name='timeout' class='form-control input-sm' value='" . htmlspecialchars($device['timeout'] ? $device['timeout'] : '') . "' placeholder='seconds' />
<input type='number' id='timeout' name='timeout' class='form-control input-sm' value='" . htmlspecialchars($device->timeout ?: '') . "' placeholder='seconds' />
</div>
<div class='col-sm-1'>
<input type='number' id='retries' name='retries' class='form-control input-sm' value='" . htmlspecialchars($device['timeout'] ? $device['retries'] : '') . "' placeholder='retries' />
<input type='number' id='retries' name='retries' class='form-control input-sm' value='" . htmlspecialchars($device->retries ?: '') . "' placeholder='retries' />
</div>
</div>
<div class='form-group'>
@@ -290,7 +267,7 @@ echo " </select>
foreach (PortAssociationMode::getModes() as $pam_id => $pam) {
echo " <option value='$pam_id'";
if ($pam_id == $device['port_association_mode']) {
if ($pam_id == $device->port_association_mode) {
echo " selected='selected'";
}
@@ -319,7 +296,7 @@ echo " </select>
<div class='form-group'>
<label for='community' class='col-sm-2 control-label'>SNMP Community</label>
<div class='col-sm-4'>
<input id='community' class='form-control' name='community' value='********' onfocus='this.value=(this.value==\"********\" ? decodeURIComponent(\"" . rawurlencode($device['community']) . "\") : this.value);'/>
<input id='community' class='form-control' name='community' value='********' onfocus='this.value=(this.value==\"********\" ? decodeURIComponent(\"" . rawurlencode($device->community) . "\") : this.value);'/>
</div>
</div>
</div>
@@ -332,21 +309,21 @@ echo " </select>
<div class='col-sm-4'>
<select id='authlevel' name='authlevel' class='form-control'>
<option value='noAuthNoPriv'>noAuthNoPriv</option>
<option value='authNoPriv' " . ($device['authlevel'] == 'authNoPriv' ? 'selected' : '') . ">authNoPriv</option>
<option value='authPriv' " . ($device['authlevel'] == 'authPriv' ? 'selected' : '') . ">authPriv</option>
<option value='authNoPriv' " . ($device->authlevel == 'authNoPriv' ? 'selected' : '') . ">authNoPriv</option>
<option value='authPriv' " . ($device->authlevel == 'authPriv' ? 'selected' : '') . ">authPriv</option>
</select>
</div>
</div>
<div class='form-group'>
<label for='authname' class='col-sm-2 control-label'>Auth User Name</label>
<div class='col-sm-4'>
<input type='text' id='authname' name='authname' class='form-control' value='" . htmlspecialchars($device['authname']) . "' autocomplete='off'>
<input type='text' id='authname' name='authname' class='form-control' value='" . htmlspecialchars($device->authname) . "' autocomplete='off'>
</div>
</div>
<div class='form-group'>
<label for='authpass' class='col-sm-2 control-label'>Auth Password</label>
<div class='col-sm-4'>
<input type='password' id='authpass' name='authpass' class='form-control' value='" . htmlspecialchars($device['authpass']) . "' autocomplete='off'>
<input type='password' id='authpass' name='authpass' class='form-control' value='" . htmlspecialchars($device->authpass) . "' autocomplete='off'>
</div>
</div>
<div class='form-group'>
@@ -354,7 +331,7 @@ echo " </select>
<div class='col-sm-4'>
<select id='authalgo' name='authalgo' class='form-control'>";
foreach (\LibreNMS\SNMPCapabilities::authAlgorithms() as $algo => $enabled) {
echo "<option value='$algo' " . ($device['authalgo'] === $algo ? 'selected' : '') . ($enabled ? '' : ' disabled') . ">$algo</option>\n";
echo "<option value='$algo' " . ($device->authalgo === $algo ? 'selected' : '') . ($enabled ? '' : ' disabled') . ">$algo</option>\n";
}
echo '</select>';
@@ -367,7 +344,7 @@ echo "
<div class='form-group'>
<label for='cryptopass' class='col-sm-2 control-label'>Crypto Password</label>
<div class='col-sm-4'>
<input type='password' id='cryptopass' name='cryptopass' class='form-control' value='" . htmlspecialchars($device['cryptopass']) . "' autocomplete='off'>
<input type='password' id='cryptopass' name='cryptopass' class='form-control' value='" . htmlspecialchars($device->cryptopass) . "' autocomplete='off'>
</div>
</div>
<div class='form-group'>
@@ -376,7 +353,7 @@ echo "
<select id='cryptoalgo' name='cryptoalgo' class='form-control'>";
foreach (\LibreNMS\SNMPCapabilities::cryptoAlgoritms() as $algo => $enabled) {
echo "<option value='$algo' " . ($device['cryptoalgo'] === $algo ? 'selected' : '') . ($enabled ? '' : ' disabled') . ">$algo</option>\n";
echo "<option value='$algo' " . ($device->cryptoalgo === $algo ? 'selected' : '') . ($enabled ? '' : ' disabled') . ">$algo</option>\n";
}
echo '</select>
';
@@ -403,7 +380,7 @@ if (Config::get('distributed_poller') === true) {
foreach (dbFetchRows('SELECT `id`,`group_name` FROM `poller_groups`') as $group) {
echo '<option value="' . $group['id'] . '"';
if ($device['poller_group'] == $group['id']) {
if ($device->poller_group == $group['id']) {
echo ' selected';
}
@@ -500,7 +477,7 @@ $("#os").on("typeahead:selected typeahead:autocompleted", function(e,datum) {
$("[name='snmp']").bootstrapSwitch('offColor','danger');
<?php
if ($device['snmpver'] == 'v3') {
if ($device->snmpver == 'v3') {
echo "$('#snmpv1_2').hide();";
echo "$('#snmpv3').show();";
} else {