Added ability to override the ifSpeed values of a port #3752

Merged
merged 7 commits into from Jul 1, 2016

Projects

None yet

3 participants

@laf
Member
laf commented Jun 30, 2016

Fix #3208
Fix #1075

Same principle as overriding the port description.

Will run rrdtune if enabled.

Removed logfile() debug from update-ifalias.inc.php

laf added some commits Jun 24, 2016
@laf laf Initial setup for customising ifSpeed on devices 0eb35c0
@laf laf Added ability to manually set ifSpeed 6690e31
@laf laf Merge branch 'master' of https://github.com/librenms/librenms into if…
…Speed
2664dbb
@laf laf added the WebUI label Jun 30, 2016
@murrant murrant and 1 other commented on an outdated diff Jul 1, 2016
html/pages/device/edit/ports.inc.php
@@ -66,6 +67,46 @@
}
});
});
+ $(document).on('blur', "[name='if-speed']", function (){
@murrant
murrant Jul 1, 2016 edited Contributor

I would prefer to allow the enter key to save:

$(document).on('blur keyup', "[name='if-speed']", function (e){
    if (e.type === 'keyup' && e.keyCode !== 13) return;
@laf
laf Jul 1, 2016 Member

Done

@murrant murrant commented on an outdated diff Jul 1, 2016
html/includes/table/edit-ports.inc.php
@@ -71,6 +71,7 @@
<input type="hidden" name="oldign_'.$port['port_id'].'" value="'.($port['ignore'] ? 1 : 0).'"">',
'port_tune' => '<input type="checkbox" id="override_config" name="override_config" data-attrib="ifName_tune:'.$port['ifName'].'" data-device_id="'.$port['device_id'].'" data-size="small" '.$checked.'>',
'ifAlias' => '<div class="form-group"><input class="form-control input-sm" id="if-alias" name="if-alias" data-device_id="'.$port['device_id'].'" data-port_id="'.$port['port_id'].'" data-ifName="'.$port['ifName'].'" value="'.$port['ifAlias'].'"><span class="glyphicon form-control-feedback" aria-hidden="true"></span></div>',
+ 'ifSpeed' => '<div class="form-group"><input class="form-control input-sm" id="if-speed" name="if-speed" data-device_id="'.$port['device_id'].'" data-port_id="'.$port['port_id'].'" data-ifName="'.$port['ifName'].'" value="'.$port['ifSpeed'].'"><span class="glyphicon form-control-feedback" aria-hidden="true"></span></div>',
@murrant
murrant Jul 1, 2016 edited Contributor
  1. It might be nice to use some html input validation here <input type="text" pattern="[0-9]*" inputmode="numeric">.
  2. The form-group needs the class has-feedback to show the check, might as well fix that in the ifAlias line too.
@murrant murrant and 1 other commented on an outdated diff Jul 1, 2016
html/pages/device/edit/ports.inc.php
@@ -16,6 +16,7 @@
<th data-column-id='ifOperStatus'>Oper</th>
<th data-column-id='disabled' data-sortable='false'>Disable</th>
<th data-column-id='ignore' data-sortable='false'>Ignore</th>
+ <th data-column-id='ifSpeed'>ifSpeed</th>
@murrant
murrant Jul 1, 2016 Contributor

It would be nice if the header was ifSpeed (bits/s) or something like that.

@laf
laf Jul 1, 2016 Member

Done

@murrant murrant commented on the diff Jul 1, 2016
html/includes/forms/update-ifspeed.inc.php
+$speed = mres($_POST['speed']);
+$device_id = mres($_POST['device_id']);
+$ifName = mres($_POST['ifName']);
+$port_id = mres($_POST['port_id']);
+
+if (!empty($ifName) && is_numeric($port_id) && is_numeric($port_id)) {
+ // We have ifName and port id so update ifAlias
+ if (empty($speed)) {
+ $speed = 999999;
+ $high_speed = 999999;
+ // Set to 999999 so we avoid using ifDescr on port poll
+ }
+ else {
+ $high_speed = $speed / 1000000;
+ }
+ if (dbUpdate(array('ifSpeed'=>$speed, 'ifHighSpeed'=>$high_speed), 'ports', '`port_id`=?', array($port_id)) > 0) {
@murrant
murrant Jul 1, 2016 Contributor

Empty values are saving as 999999, so if I click on an empty ifSpeed field and click out, it will change to 999999

@laf
laf Jul 1, 2016 Member

Unchanged fields don't update - at least for me. However I'm not sure how I can fix this, I need to set a value so poller knows to re-populate, as ifSpeeds can already be blank I've chosen 999999. We won't know the difference between the user clearing the field and ifSpeed being blank already.

@murrant
murrant Jul 1, 2016 Contributor

You can detect unchanged values in JavaScript by storing the previous value in a data attribute.

@laf
laf Jul 1, 2016 Member

What I meant by my initial reply there was unchanged fields don't trigger an update for me now so if I click a field and out nothing gets updated. Admittedly I don't have any ports that don't have an ifSpeed to test :(

@murrant
murrant Jul 1, 2016 Contributor

Ah ok, well visually it stays empty, but when I refresh it shows 999999 because the dbUpdate ran.

@murrant murrant and 1 other commented on an outdated diff Jul 1, 2016
html/includes/forms/update-ifspeed.inc.php
+$port_id = mres($_POST['port_id']);
+
+if (!empty($ifName) && is_numeric($port_id) && is_numeric($port_id)) {
+ // We have ifName and port id so update ifAlias
+ if (empty($speed)) {
+ $speed = 999999;
+ $high_speed = 999999;
+ // Set to 999999 so we avoid using ifDescr on port poll
+ }
+ else {
+ $high_speed = $speed / 1000000;
+ }
+ if (dbUpdate(array('ifSpeed'=>$speed, 'ifHighSpeed'=>$high_speed), 'ports', '`port_id`=?', array($port_id)) > 0) {
+ $device = device_by_id_cache($device_id);
+ if ($speed == '999999') {
+ del_dev_attrib($device, 'ifSpeed:'.$ifName);
@murrant
murrant Jul 1, 2016 Contributor

Would it be better to use $ifIndex instead of $ifName since that is required to be unique by the db structure?

@laf
laf Jul 1, 2016 Member

ifindex can change so if it does then we lose the ifSpeed override.

@murrant
murrant Jul 1, 2016 Contributor

I thought so.

@murrant
Contributor
murrant commented Jul 1, 2016

Nice job! I just had a few tidy ups here and there.

You should update the value after setting. If you type in some letters and save, the letters stay there, but the database is updated with only a number, leaving them out of sync.

@laf
Member
laf commented Jul 1, 2016

I've done the return of numeric only via jquery however it won't get triggered really as the input is now limited to numeric anyway.

@laf laf updates as per comments from murrant
2bffc5c
@laf laf Updated form update
0991fbd
@murrant murrant and 1 other commented on an outdated diff Jul 1, 2016
html/pages/device/edit/ports.inc.php
@@ -66,6 +67,49 @@
}
});
});
+ $(document).on('blur keyup', "[name='if-speed']", function (e){
+ if (e.type === 'keyup' && e.keyCode !== 13) return;
+ var $this = $(this);
+ var speed = $this.val();
@murrant
murrant Jul 1, 2016 Contributor

var speed = $this.val().replace(/[^0-9]/gi, '');

@laf
laf Jul 1, 2016 Member

Done

@murrant murrant commented on an outdated diff Jul 1, 2016
html/pages/device/edit/ports.inc.php
+ if (e.type === 'keyup' && e.keyCode !== 13) return;
+ var $this = $(this);
+ var speed = $this.val();
+ var device_id = $this.data('device_id');
+ var port_id = $this.data('port_id');
+ var ifName = $this.data('ifname');
+ $.ajax({
+ type: 'POST',
+ url: 'ajax_form.php',
+ data: {type: "update-ifspeed", speed: speed, ifName: ifName, port_id: port_id, device_id: device_id},
+ dataType: "json",
+ success: function (data) {
+ if (data.status == 'ok') {
+ $this.closest('.form-group').addClass('has-success');
+ $this.next().addClass('glyphicon-ok');
+ var new_val = speed.text().replace(/[^0-9]/gi, '');
@murrant
murrant Jul 1, 2016 Contributor

Delete

@murrant murrant commented on an outdated diff Jul 1, 2016
html/pages/device/edit/ports.inc.php
+ var $this = $(this);
+ var speed = $this.val();
+ var device_id = $this.data('device_id');
+ var port_id = $this.data('port_id');
+ var ifName = $this.data('ifname');
+ $.ajax({
+ type: 'POST',
+ url: 'ajax_form.php',
+ data: {type: "update-ifspeed", speed: speed, ifName: ifName, port_id: port_id, device_id: device_id},
+ dataType: "json",
+ success: function (data) {
+ if (data.status == 'ok') {
+ $this.closest('.form-group').addClass('has-success');
+ $this.next().addClass('glyphicon-ok');
+ var new_val = speed.text().replace(/[^0-9]/gi, '');
+ $this.val(new_val);
@murrant
murrant Jul 1, 2016 Contributor

$this.val(speed);

laf added some commits Jul 1, 2016
@laf laf fix js error
3d77d1b
@laf laf fix js error
7f8e587
@murrant murrant merged commit bc7e5ff into librenms:master Jul 1, 2016

2 checks passed

Auto-Deploy Build finished.
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
@laf laf deleted the laf:ifSpeed branch Jul 1, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment