Skip to content

Instantly share code, notes, and snippets.

@komeda-shinji
Created December 13, 2017 08:59
Show Gist options
  • Save komeda-shinji/e7f22e890f5b84e40d4cdc1f52d814e8 to your computer and use it in GitHub Desktop.
Save komeda-shinji/e7f22e890f5b84e40d4cdc1f52d814e8 to your computer and use it in GitHub Desktop.
LibreNMS device ports filter
diff --git a/html/pages/device/ports.inc.php b/html/pages/device/ports.inc.php
index 4110d66..7a66326 100644
--- a/html/pages/device/ports.inc.php
+++ b/html/pages/device/ports.inc.php
@@ -12,6 +12,23 @@ if (!$vars['view']) {
$vars['view'] = trim($config['ports_page_default'], '/');
}
+$quickfilter_conditions = array(
+ 'down' => FALSE,
+ 'shutdown' => FALSE,
+ 'ignored' => FALSE,
+ 'disabled' => FALSE,
+ 'deleted' => FALSE,
+);
+if (isset($vars['filters'])) {
+ foreach (explode(',', $vars['filters']) as $filter) {
+ $quickfilter_conditions[$filter] = TRUE;
+ }
+} else {
+ foreach (array('deleted', 'disabled', 'shutdown') as $filter) {
+ $quickfilter_conditions[$filter] = TRUE;
+ }
+}
+
$link_array = array(
'page' => 'device',
'device' => $device['device_id'],
@@ -88,8 +105,38 @@ foreach ($graph_types as $type => $descr) {
$type_sep = ' | ';
}//end foreach
+if (in_array($vars['view'], array('basic', 'details', 'graphs', 'minigraphs')))
+ echo '<div class="dropdown pull-right">
+<button class="btn btn-default dropdown-toggle" type="button" data-toggle="drop
+Quick Filters
+<span class="caret"></span></button>
+<ul class="dropdown-menu">';
+
+ foreach ($quickfilter_conditions as $name => $enabled) {
+ echo '<li>';
+ echo $enabled ? "<span class='pagemenu-selected'>" : "<span>";
+ $filter = $quickfilter_conditions;
+ $filter[$name] = !$filter[$name];
+ echo generate_link("Hide ".strtoupper($name), $link_array, array($option
+ echo "</span>";
+ echo "</li>\n";
+ }
+ echo "</ul></div>\n";
+}
+
print_optionbar_end();
+function port_filter($port)
+{
+ global $quickfilter_conditions;
+ $is_filtered =
+ ($quickfilter_conditions['down'] && $port['ifOperStatus'] != 'up' && $p
+ ($quickfilter_conditions['shutdown'] && $port['ifAdminStatus'] == 'down
+ ($quickfilter_conditions['ignored'] && $port['ignore']) ||
+ ($quickfilter_conditions['deleted'] && $port['deleted']);
+ return !$is_filtered;
+}
+
if ($vars['view'] == 'minigraphs') {
$timeperiods = array(
'-1day',
@@ -104,6 +151,7 @@ if ($vars['view'] == 'minigraphs') {
// FIXME - FIX THIS. UGLY.
foreach (dbFetchRows('select * from ports WHERE device_id = ? ORDER BY ifIn
$port = cleanPort($port, $device);
+ if (!port_filter($port)) { continue; }
echo "<div style='display: block; padding: 3px; margin: 3px; min-width:
<div style='font-weight: bold;'>".makeshortif($port['ifDescr']).'</
<a href="'.generate_port_url($port)."\" onmouseover=\"return overli
@@ -146,6 +194,8 @@ if ($vars['view'] == 'minigraphs') {
// As we've dragged the whole database, lets pre-populate our caches :)
// FIXME - we should probably split the fetching of link/stack/etc into fun
+$ports = array_filter($ports, port_filter);
+
foreach ($ports as $key => $port) {
$port_cache[$port['port_id']] = $port;
$port_index_cache[$port['device_id']][$port['ifIndex']] = $port;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment