Skip to content

Commit

Permalink
Merge pull request #3722 from brandune/rollup_graphs_redux
Browse files Browse the repository at this point in the history
deduping type_where code, adding manual types to graph widgets, updating default descriptions
  • Loading branch information
laf committed Jul 1, 2016
2 parents 516af08 + cccb07d commit e980c84
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 66 deletions.
1 change: 1 addition & 0 deletions AUTHORS.md
Expand Up @@ -109,6 +109,7 @@ LibreNMS contributors:
- Vikram Adukia <adukia@dropbox.com> (justmedude)
- Tijmen de Mes <tijmen@ag-projects.com> (tijmenNL)
- Benjamin Busche <benjamin.busche@gmail.com> (optic00)
- Brandon Boudrias <bt.boudrias@gmail.com> (brandune)

[1]: http://observium.org/ "Observium web site"
Observium was written by:
Expand Down
8 changes: 4 additions & 4 deletions doc/Support/Configuration.md
Expand Up @@ -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
Expand Down
11 changes: 1 addition & 10 deletions html/includes/api_functions.inc.php
Expand Up @@ -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 = ',';
Expand Down
44 changes: 17 additions & 27 deletions html/includes/common/generic-graph.inc.php
Expand Up @@ -74,6 +74,7 @@
<option value="peering"'.($widget_settings['graph_type'] == 'peering' ? ' selected' : '').'>&nbsp;&nbsp;&nbsp;Peering</option>
<option value="core"'.($widget_settings['graph_type'] == 'core' ? ' selected' : '').'>&nbsp;&nbsp;&nbsp;Core</option>
<option value="custom"'.($widget_settings['graph_type'] == 'custom' ? ' selected' : '').'>&nbsp;&nbsp;&nbsp;Custom Descr</option>
<option value="manual"'.($widget_settings['graph_type'] == 'manual' ? ' selected' : '').'>&nbsp;&nbsp;&nbsp;Manual Descr</option>
<option disabled></option>
<option value="bill_bits"'.($widget_settings['graph_type'] == 'bill_bits' ? ' selected' : '').'>Bill</option>
</select>
Expand Down Expand Up @@ -150,6 +151,14 @@
$common_output[] = ' </select>
</div>
</div>
<div class="form-group input_'.$unique_id.'" id="input_'.$unique_id.'_manual">
<div class="col-sm-2">
<label for="graph_manual" class="control-label">Manual Port-Desc: </label>
</div>
<div class="col-sm-10">
<input type="text" class="form-control input_'.$unique_id.'_manual" name="graph_manual" placeholder="Descr String" value="'.htmlspecialchars($widget_settings['graph_manual']).'">';
$common_output[] = ' </div>
</div>
<div class="form-group input_'.$unique_id.'" id="input_'.$unique_id.'_bill">
<div class="col-sm-2">
<label for="graph_bill" class="control-label">Bill: </label>
Expand Down Expand Up @@ -393,39 +402,20 @@ 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']);
}
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'];
elseif ($type == 'transit' || $type == 'peering' || $type == 'core' || $type == 'custom' || $type == 'manual') {
if ( $type == 'custom' || $type == 'manual') {
$type = $widget_settings['graph_'.$type];
$type = explode(',', $type);
}
$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 {
Expand Down
55 changes: 55 additions & 0 deletions html/includes/functions.inc.php
Expand Up @@ -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;
}
24 changes: 2 additions & 22 deletions html/pages/iftype.inc.php
Expand Up @@ -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'];
Expand All @@ -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]);
Expand Down
6 changes: 3 additions & 3 deletions includes/defaults.inc.php
Expand Up @@ -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)
Expand Down

0 comments on commit e980c84

Please sign in to comment.