From 4202b7920f903d7e211640de05547fa8091c568d Mon Sep 17 00:00:00 2001 From: Brandon Boudrias Date: Fri, 24 Jun 2016 14:54:32 -0700 Subject: [PATCH 1/4] adding name to authors and signing contributors agreement I agree to the conditions of the Contributor Agreement contained in doc/General/Contributing.md. --- AUTHORS.md | 1 + 1 file changed, 1 insertion(+) diff --git a/AUTHORS.md b/AUTHORS.md index b6e9afd3394b..8dec3a2cafe8 100644 --- a/AUTHORS.md +++ b/AUTHORS.md @@ -109,6 +109,7 @@ LibreNMS contributors: - Vikram Adukia (justmedude) - Tijmen de Mes (tijmenNL) - Benjamin Busche (optic00) +- Brandon Boudrias (brandune) [1]: http://observium.org/ "Observium web site" Observium was written by: From 4ad75fb89d31f9b331accd18822780e1606fbaf8 Mon Sep 17 00:00:00 2001 From: Brandon Boudrias Date: Fri, 24 Jun 2016 14:56:45 -0700 Subject: [PATCH 2/4] create common method for generating type_wheres --- html/includes/api_functions.inc.php | 11 +---- html/includes/common/generic-graph.inc.php | 34 +++---------- html/includes/functions.inc.php | 55 ++++++++++++++++++++++ html/pages/iftype.inc.php | 24 +--------- 4 files changed, 65 insertions(+), 59 deletions(-) diff --git a/html/includes/api_functions.inc.php b/html/includes/api_functions.inc.php index 75aa966bbfb2..b1e5e5de958f 100644 --- a/html/includes/api_functions.inc.php +++ b/html/includes/api_functions.inc.php @@ -495,19 +495,10 @@ function get_graph_by_portgroup() { $vars['width'] = $_GET['width'] ?: 1075; $vars['height'] = $_GET['height'] ?: 300; $auth = '1'; - $type_where = ' ('; - $or = ''; - $type_param = array(); - foreach (explode(',', $group) as $type) { - $type_where .= " $or `port_descr_type` = ?"; - $or = 'OR'; - $type_param[] = $type; - } - $type_where .= ') '; + $ports = get_ports_from_type(explode(',', $group)); $if_list = ''; $seperator = ''; - $ports = dbFetchRows("SELECT * FROM `ports` as I, `devices` AS D WHERE $type_where AND I.device_id = D.device_id ORDER BY I.ifAlias", $type_param); foreach ($ports as $port) { $if_list .= $seperator.$port['port_id']; $seperator = ','; diff --git a/html/includes/common/generic-graph.inc.php b/html/includes/common/generic-graph.inc.php index 022fb0585cd4..91cb64f8d197 100644 --- a/html/includes/common/generic-graph.inc.php +++ b/html/includes/common/generic-graph.inc.php @@ -393,39 +393,19 @@ function switch_'.$unique_id.'(data) { elseif ($type == 'munin') { $param = 'device='.$widget_settings['graph_'.$type]['device_id'].'&plugin='.$widget_settings['graph_'.$type]['name']; } - elseif ($type == 'transit' || $type == 'peering' || $type == 'core') { - $type_where = ' ('; - if (is_array($config[$type.'_descr']) === false) { - $config[$type.'_descr'] = array($config[$type.'_descr']); + elseif ($type == 'transit' || $type == 'peering' || $type == 'core' || $type == 'custom') { + if ( $type == 'custom' ) { + $type = $widget_settings['graph_custom']; } - foreach ($config[$type.'_descr'] as $additional_type) { - if (!empty($additional_type)) { - $type_where .= " $or `port_descr_type` = ?"; - $or = 'OR'; - $type_param[] = $additional_type; - } - } - $type_where .= " $or `port_descr_type` = ?"; - $or = 'OR'; - $type_param[] = $type; - $type_where .= ') '; - foreach (dbFetchRows("SELECT port_id FROM `ports` WHERE $type_where ORDER BY ifAlias", $type_param) as $port) { - $tmp[] = $port['port_id']; - } - $param = 'id='.implode(',',$tmp); - $widget_settings['graph_type'] = 'multiport_bits_separate'; - if (empty($widget_settings['title'])) { - $widget_settings['title'] = 'Overall '.ucfirst($type).' Bits ('.$widget_settings['graph_range'].')'; - } - } - elseif ($type == 'custom') { - foreach (dbFetchRows("SELECT port_id FROM `ports` WHERE `port_descr_type` = ? ORDER BY ifAlias", array($widget_settings['graph_custom'])) as $port) { + + $ports = get_ports_from_type($type); + foreach ($ports as $port) { $tmp[] = $port['port_id']; } $param = 'id='.implode(',',$tmp); $widget_settings['graph_type'] = 'multiport_bits_separate'; if (empty($widget_settings['title'])) { - $widget_settings['title'] = 'Overall '.ucfirst(htmlspecialchars($widget_settings['graph_custom'])).' Bits ('.$widget_settings['graph_range'].')'; + $widget_settings['title'] = 'Overall '.ucfirst(htmlspecialchars($type)).' Bits ('.$widget_settings['graph_range'].')'; } } else { diff --git a/html/includes/functions.inc.php b/html/includes/functions.inc.php index 8952853e7f4f..046fb3a859af 100644 --- a/html/includes/functions.inc.php +++ b/html/includes/functions.inc.php @@ -1283,3 +1283,58 @@ function get_ripe_api_whois_data_json($ripe_data_param, $ripe_query_param) { return json_decode(file_get_contents($ripe_whois_url) , true); }//end get_ripe_api_whois_data_json() +/** + * Return the rows from 'ports' for all ports of a certain type as parsed by port_descr_parser. + * One or an array of strings can be provided as an argument; if an array is passed, all ports matching + * any of the types in the array are returned. + * @param $types mixed String or strings matching 'port_descr_type's. + * @return array Rows from the ports table for matching ports. + */ +function get_ports_from_type($given_types) { + global $config; + + # Make the arg an array if it isn't, so subsequent steps only have to handle arrays. + if(!is_array($given_types)) { + $given_types = array($given_types); + } + + # Check the config for a '_descr' entry for each argument. This is how a 'custom_descr' entry can + # be key/valued to some other string that's actually searched for in the DB. Merge or append the + # configured value if it's an array or a string. Or append the argument itself if there's no matching + # entry in config. + $search_types = array(); + foreach($given_types as $type) { + if(isset($config[$type.'_descr']) === true) { + if (is_array($config[$type.'_descr']) === true) { + $search_types = array_merge($search_types, $config[$type.'_descr']); + } + else { + $search_types[] = $config[$type.'_descr']; + } + } + else { + $search_types[] = $type; + } + } + + # Using the full list of strings to search the DB for, build the 'where' portion of a query that + # compares 'port_descr_type' with entry in the list. Also, since '@' is the convential wildcard, + # replace it with '%' so it functions as a wildcard in the SQL query. + $type_where = ' ('; + $or = ''; + $type_param = array(); + + foreach($search_types as $type) { + if (!empty($type)) { + $type = strtr($type, '@', '%'); + $type_where .= " $or `port_descr_type` LIKE ?"; + $or = 'OR'; + $type_param[] = $type; + } + } + $type_where .= ') '; + + # Run the query with the generated 'where' and necessary parameters, and send it back. + $ports = dbFetchRows("SELECT * FROM `ports` as I, `devices` AS D WHERE $type_where AND I.device_id = D.device_id ORDER BY I.ifAlias", $type_param); + return $ports; +} diff --git a/html/pages/iftype.inc.php b/html/pages/iftype.inc.php index 266b98fb6868..6c8495a0541a 100644 --- a/html/pages/iftype.inc.php +++ b/html/pages/iftype.inc.php @@ -16,27 +16,8 @@ $bg = '#ffffff'; } -$type_where = ' ('; -foreach (explode(',', $vars['type']) as $type) { - if (is_array($config[$type.'_descr']) === false) { - $config[$type.'_descr'] = array($config[$type.'_descr']); - } - - foreach ($config[$type.'_descr'] as $additional_type) { - if (!empty($additional_type)) { - $type_where .= " $or `port_descr_type` = ?"; - $or = 'OR'; - $type_param[] = $additional_type; - } - } - - $type_where .= " $or `port_descr_type` = ?"; - $or = 'OR'; - $type_param[] = $type; -} - -$type_where .= ') '; -$ports = dbFetchRows("SELECT * FROM `ports` as I, `devices` AS D WHERE $type_where AND I.device_id = D.device_id ORDER BY I.ifAlias", $type_param); +$types_array = explode(',', $vars['type']); +$ports = get_ports_from_type($types_array); foreach ($ports as $port) { $if_list .= $seperator.$port['port_id']; @@ -45,7 +26,6 @@ unset($seperator); -$types_array = explode(',', $vars['type']); for ($i = 0; $i < count($types_array); $i++) { $types_array[$i] = ucfirst($types_array[$i]); From 1aab030f37ba3d0f0b0f8ee24209f91b6a3a22f8 Mon Sep 17 00:00:00 2001 From: Brandon Boudrias Date: Fri, 24 Jun 2016 14:57:04 -0700 Subject: [PATCH 3/4] giving graph widgets option to manually specify port type --- html/includes/common/generic-graph.inc.php | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/html/includes/common/generic-graph.inc.php b/html/includes/common/generic-graph.inc.php index 91cb64f8d197..bd594b5ec7c2 100644 --- a/html/includes/common/generic-graph.inc.php +++ b/html/includes/common/generic-graph.inc.php @@ -74,6 +74,7 @@ + @@ -150,6 +151,14 @@ $common_output[] = ' +
+
+ +
+
+ '; + $common_output[] = '
+
@@ -393,9 +402,10 @@ function switch_'.$unique_id.'(data) { elseif ($type == 'munin') { $param = 'device='.$widget_settings['graph_'.$type]['device_id'].'&plugin='.$widget_settings['graph_'.$type]['name']; } - elseif ($type == 'transit' || $type == 'peering' || $type == 'core' || $type == 'custom') { - if ( $type == 'custom' ) { - $type = $widget_settings['graph_custom']; + elseif ($type == 'transit' || $type == 'peering' || $type == 'core' || $type == 'custom' || $type == 'manual') { + if ( $type == 'custom' || $type == 'manual') { + $type = $widget_settings['graph_'.$type]; + $type = explode(',', $type); } $ports = get_ports_from_type($type); From cccb07d2a746a172a335b53e6befc48f90ac02d3 Mon Sep 17 00:00:00 2001 From: Brandon Boudrias Date: Fri, 24 Jun 2016 14:58:10 -0700 Subject: [PATCH 4/4] matching default port description types with their names --- doc/Support/Configuration.md | 8 ++++---- includes/defaults.inc.php | 6 +++--- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/doc/Support/Configuration.md b/doc/Support/Configuration.md index 2a614c490ef9..a2699620b481 100644 --- a/doc/Support/Configuration.md +++ b/doc/Support/Configuration.md @@ -161,10 +161,10 @@ $config['show_services'] = 0; # Enable Services on menu $config['int_customers'] = 1; # Enable Customer Port Parsing $config['summary_errors'] = 0; # Show Errored ports in summary boxes on the dashboard $config['customers_descr'] = 'cust'; // The description to look for in ifDescr. Can be an array as well array('cust','cid'); -$config['transit_descr'] = ""; // Add custom transit descriptions (can be an array) -$config['peering_descr'] = ""; // Add custom peering descriptions (can be an array) -$config['core_descr'] = ""; // Add custom core descriptions (can be an array) -$config['custom_descr'] = ""; // Add custom interface descriptions (can be an array) +$config['transit_descr'] = 'transit'; // Add custom transit descriptions (can be an array) +$config['peering_descr'] = 'peering'; // Add custom peering descriptions (can be an array) +$config['core_descr'] = 'core'; // Add custom core descriptions (can be an array) +$config['custom_descr'] = ''; // Add custom interface descriptions (can be an array) $config['int_transit'] = 1; # Enable Transit Types $config['int_peering'] = 1; # Enable Peering Types $config['int_core'] = 1; # Enable Core Port Types diff --git a/includes/defaults.inc.php b/includes/defaults.inc.php index 3f01b976807d..a8121ded470c 100644 --- a/includes/defaults.inc.php +++ b/includes/defaults.inc.php @@ -138,11 +138,11 @@ function set_debug($debug) { $config['int_customers'] = 1; // Enable Customer Port Parsing $config['customers_descr'] = 'cust'; -$config['transit_descr'] = ''; +$config['transit_descr'] = 'transit'; // Add custom transit descriptions (can be an array) -$config['peering_descr'] = ''; +$config['peering_descr'] = 'peering'; // Add custom peering descriptions (can be an array) -$config['core_descr'] = ''; +$config['core_descr'] = 'core'; // Add custom core descriptions (can be an array) $config['custom_descr'] = ''; // Add custom interface descriptions (can be an array)