add option to limit snmp node search to current device only

This commit is contained in:
Oliver Gorwits
2022-08-09 10:19:05 +01:00
parent 162925f6eb
commit c4e4bd7633
2 changed files with 52 additions and 43 deletions

View File

@@ -54,18 +54,19 @@ ajax '/ajax/data/device/:ip/snmptree/:base' => require_login sub {
to_json $items; to_json $items;
}; };
# TODO add form option for limiting to this device, so leave :ip ajax '/ajax/data/snmp/typeahead' => require_login sub {
ajax '/ajax/data/device/:ip/typeahead' => require_login sub {
my $device = try { schema('netdisco')->resultset('Device')
->find( param('ip') ) }
or send_error('Bad Device', 404);
my $term = param('term') or return to_json []; my $term = param('term') or return to_json [];
$term = '%'. $term .'%';
my @found = schema('netdisco')->resultset('SNMPObject') my $device = param('ip');
->search({ leaf => { -ilike => $term } }, my $deviceonly = param('deviceonly');
{ rows => 25, columns => 'leaf' }) my $table = ($deviceonly ? 'DeviceBrowser' : 'SNMPObject');
my @found = schema('netdisco')->resultset($table)
->search({ -or => [ oid => $term,
oid => { -like => ($term .'.%') },
leaf => { -ilike => ('%'. $term .'%') } ],
(($deviceonly and $device) ? (ip => $device) : ()), },
{ rows => 25, columns => 'leaf', order_by => 'oid_parts' })
->get_column('leaf')->all; ->get_column('leaf')->all;
return to_json [] unless scalar @found; return to_json [] unless scalar @found;
@@ -73,25 +74,24 @@ ajax '/ajax/data/device/:ip/typeahead' => require_login sub {
to_json [ sort @found ]; to_json [ sort @found ];
}; };
# TODO add form option for limiting to this device, so leave :ip ajax '/ajax/data/snmp/nodesearch' => require_login sub {
ajax '/ajax/data/device/:ip/snmpnodesearch' => require_login sub { my $to_match = param('str') or return to_json [];
my $device = try { schema('netdisco')->resultset('Device')
->find( param('ip') ) }
or send_error('Bad Device', 404);
my $to_match = param('str');
my $partial = param('partial'); my $partial = param('partial');
my $excludeself = param('excludeself');
return to_json [] unless $to_match or length($to_match);
$to_match = $to_match . '%' if $partial;
my $found = undef; my $found = undef;
if ($partial) {
my $op = ($partial ? '-ilike' : '=');
$found = schema('netdisco')->resultset('SNMPObject') $found = schema('netdisco')->resultset('SNMPObject')
->search({ -or => [ oid => { $op => $to_match }, leaf => { $op => $to_match } ] }, ->search({ -or => [ oid => $to_match,
oid => { -like => ($to_match .'.%') },
leaf => { -ilike => ($to_match .'%') } ] },
{ rows => 1, order_by => 'oid_parts' })->first; { rows => 1, order_by => 'oid_parts' })->first;
}
else {
$found = schema('netdisco')->resultset('SNMPObject')
->search({ -or => [ oid => $to_match,
leaf => $to_match ] },
{ rows => 1, order_by => 'oid_parts' })->first;
}
return to_json [] unless $found; return to_json [] unless $found;
$found = $found->oid; $found = $found->oid;

View File

@@ -3,15 +3,20 @@
<div id="jstree" class="nd_scrollable"></div> <div id="jstree" class="nd_scrollable"></div>
</div> </div>
<div id="snmpnodecontainer" class="span8"> <div id="snmpnodecontainer" class="span8">
<form id="searchTreeForm" class="form-inline col-md-4"> <form id="nd_snmp_search_form" class="form-inline col-md-4">
<span class="form-group"> <span class="form-group">
<input id="treeSearchText" type="text" class="form-control" size="30" required placeholder="Search for label or OID"> <input id="nd_snmp_search_text" type="text" class="form-control nd_snmp_search_param" name="term" size="30" required placeholder="Search for label or OID">
<button type="submit" class="btn btn-default">Search</button>
<label class="checkbox-inline" <label class="checkbox-inline"
rel="tooltip" data-placement="top" data-offset="5" data-title="Anchored to the beginning"> rel="tooltip" data-placement="top" data-offset="5" data-title="Anchored to the beginning">
<input type="checkbox" id="snmpPartialSearch" value="partial"> Partial </input> <input type="checkbox" id="nd_snmp_search_partial" name="partial" class="nd_snmp_search_param"> Partial </input>
</label> </label>
<input type="checkbox" id="nd_snmp_search_deviceonly" name="deviceonly" class="nd_snmp_search_param" checked="checked"> Only this device </input>
<input type="hidden" id="nd_snmp_search_ip" name="ip" class="nd_snmp_search_param" value="[% device %]" />
</span> </span>
<button type="submit" class="btn btn-default">Search</button>
</form> </form>
<div id="node"> <div id="node">
<table class="table table-bordered"> <table class="table table-bordered">
@@ -73,7 +78,7 @@
var name = node.text.toLowerCase(); var name = node.text.toLowerCase();
var oid = node.id.toLowerCase(); var oid = node.id.toLowerCase();
if (document.getElementById('snmpPartialSearch').checked) { if (document.getElementById('nd_snmp_search_partial').checked) {
if ((name.indexOf(pattern) == 0) || (oid.indexOf(pattern) == 0)) { if ((name.indexOf(pattern) == 0) || (oid.indexOf(pattern) == 0)) {
return true; return true;
} }
@@ -103,10 +108,9 @@
'plugins': ['search'], 'plugins': ['search'],
'search': { 'search': {
'ajax' : { 'ajax' : {
'url' : '[% uri_base | none %]/ajax/data/device/[% device %]/snmpnodesearch', 'url' : '[% uri_base | none %]/ajax/data/snmp/nodesearch',
'beforeSend' : function(jqXHR, settings) { 'beforeSend' : function(jqXHR, settings) {
settings.url = settings.url + '&excludeself=on'; if (document.getElementById('nd_snmp_search_partial').checked) {
if (document.getElementById('snmpPartialSearch').checked) {
settings.url = settings.url + '&partial=on'; settings.url = settings.url + '&partial=on';
} }
return true; return true;
@@ -138,12 +142,17 @@
document.getElementById( parent ).scrollIntoView(); document.getElementById( parent ).scrollIntoView();
} }
}); });
$("#searchTreeForm").submit(function(e) { $("#nd_snmp_search_form").submit(function(e) {
$("#jstree").jstree("search", $("#treeSearchText").val()); $("#jstree").jstree("search", $("#nd_snmp_search_text").val());
e.preventDefault(); e.preventDefault();
}); });
$('#treeSearchText').autocomplete({ $('#nd_snmp_search_text').autocomplete({
source: uri_base + '/ajax/data/device/[% device %]/typeahead' source: function (request, response) {
var query = $('.nd_snmp_search_param').serialize();
return $.get( uri_base + '/ajax/data/snmp/typeahead', query, function (data) {
return response(data);
});
}
,delay: 150 ,delay: 150
,minLength: 2 ,minLength: 2
}); });