Merge pull request #4497 from murrant/debug-web

feature: Capture device troubleshooting info (discovery, poller, snmpwalk)
This commit is contained in:
Neil Lathwood
2016-09-20 16:01:30 +01:00
committed by GitHub
5 changed files with 223 additions and 3 deletions

View File

@ -189,14 +189,16 @@ You will also need to supply a test unit within `tests/OSDiscoveryTest.php`. Ple
#### <a name="faq20"> What information do you need to add a new OS?</a>
Please provide the following output as seperate non-expiring pastebin.com links.
Under the device, click the gear and select Capture.
Please provide the output of Discovery, Poller, and Snmpwalk as separate non-expiring pastebin.com links.
Replace the relevant information in these commands such as HOSTNAME and COMMUNITY.
You can also use the command line to obtain the information. Especially, if snmpwalk results in a large amount of data.
Replace the relevant information in these commands such as HOSTNAME and COMMUNITY. Use `snmpwalk` instead of `snmpbulkwalk` for v1 devices.
```bash
./discovery.php -h HOSTNAME -d -m os
./poller.php -h HOSTNAME -r -f -d -m os
snmpbulkwalk -On -v2c -c COMMUNITY HOSTNAME .
snmpbulkwalk -Onet -v2c -c COMMUNITY HOSTNAME .
```
If possible please also provide what the OS name should be if it doesn't exist already.

29
html/ajax_output.php Normal file
View File

@ -0,0 +1,29 @@
<?php
/*
* LibreNMS
*
* Copyright (c) 2014 Neil Lathwood <https://github.com/laf/ http://www.lathwood.co.uk/fa>
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or (at your
* option) any later version. Please see LICENSE.txt at the top level of
* the source code distribution for details.
*/
require_once '../includes/defaults.inc.php';
require_once '../config.php';
require_once '../includes/definitions.inc.php';
require_once 'includes/functions.inc.php';
require_once '../includes/functions.php';
require_once 'includes/authenticate.inc.php';
set_debug($_REQUEST['debug']);
$id = mres($_REQUEST['id']);
if (isset($id)) {
if (file_exists("includes/output/$id.inc.php")) {
include_once "includes/output/$id.inc.php";
}
}

View File

@ -0,0 +1,93 @@
<?php
/**
* output.php
*
* runs the requested command and outputs as a file or json
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @package LibreNMS
* @link http://librenms.org
* @copyright 2016 Tony Murray
* @author Tony Murray <murraytony@gmail.com>
*/
if (!is_admin()) {
echo("Insufficient Privileges");
exit();
}
$hostname = escapeshellcmd($_REQUEST['hostname']);
$type = $_REQUEST['type'];
switch ($type) {
case 'poller':
$cmd = "php ${config['install_dir']}/poller.php -h $hostname -r -f -d";
$filename = "poller-$hostname.txt";
break;
case 'snmpwalk':
$device = device_by_name(mres($hostname));
$cmd = gen_snmpwalk_cmd($device, '.', ' -Onet');
if ($debug) {
$cmd .= ' 2>&1';
}
$filename = $device['os'] . '-' . $device['hostname'] . '.snmpwalk';
break;
case 'discovery':
$cmd = "php ${config['install_dir']}/discovery.php -h $hostname -d";
$filename = "discovery-$hostname.txt";
break;
default:
echo 'You must specify a valid type';
exit();
}
// ---- Output ----
if ($_GET['format'] == 'text') {
header("Content-type: text/plain");
header('X-Accel-Buffering: no');
if (($fp = popen($cmd, "r"))) {
while (!feof($fp)) {
$line = stream_get_line($fp, 1024, PHP_EOL);
echo preg_replace('/\033\[[\d;]+m/', '', $line) . PHP_EOL;
ob_flush();
flush(); // you have to flush buffer
}
fclose($fp);
}
} elseif ($_GET['format'] == 'download') {
ob_start();
$output = shell_exec($cmd);
ob_end_clean();
$output = preg_replace('/\033\[[\d;]+m/', '', $output);
$length = strlen($output);
header('Content-Description: File Transfer');
header('Content-Type: text/plain');
header("Content-Disposition: attachment; filename=$filename");
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . $length);
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Expires: 0');
header('Pragma: public');
echo $output;
}

View File

@ -417,6 +417,10 @@ if (device_permitted($vars['device']) || $check_device == $vars['device']) {
Edit
</a>
</li>';
echo '<li><a href="'.generate_device_url($device, array('tab' => 'capture')).'">
<img src="images/16/brick_error.png" align="absmiddle" border="0" /> Capture
</a></li>';
}
echo '</ul>
</div>';

View File

@ -0,0 +1,92 @@
<?php
/**
* capture.inc.php
*
* View and download troubleshooting information
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @package LibreNMS
* @link http://librenms.org
* @copyright 2016 Tony Murray
* @author Tony Murray <murraytony@gmail.com>
*/
$no_refresh = true;
$pagetitle[] = "Capture";
if (!is_admin()) {
print_error("Insufficient Privileges");
} else {
?>
<h2>Capture Debug Information</h2>
<ul class="nav nav-tabs">
<li role="presentation" class="active"><a data-toggle="tab" href="#discovery">Discovery</a></li>
<li role="presentation"><a data-toggle="tab" href="#poller">Poller</a></li>
<li role="presentation"><a data-toggle="tab" href="#snmp">SNMP</a></li>
</ul>
<div class="tab-content">
<?php
$tabs = array(
'discovery' => 'ajax_output.php?id=capture&format=text&type=discovery&hostname='.$device['hostname'],
'poller' => 'ajax_output.php?id=capture&format=text&type=poller&hostname='.$device['hostname'],
'snmp' => 'ajax_output.php?id=capture&format=text&type=snmpwalk&hostname='.$device['hostname'],
);
foreach ($tabs as $tab => $url) {
?>
<div id="<?php echo $tab ?>" class="tab-pane fade <?php echo ($tab == 'discovery' ? ' in active' : '') ?>">
<div class="row"><div class="col-md-12">
<div class="btn-toolbar" role="toolbar" style="margin:5px 0 5px 0">
<button type="button" class="btn btn-success" id="run-<?php echo $tab ?>"><i class="fa fa-play fa-lg"></i> Run</button>
<button type="button" class="btn btn-primary" id="copy-<?php echo $tab ?>"><i class="fa fa-clipboard fa-lg"></i> Copy</button>
<a class="btn btn-warning" href="<?php echo str_replace('text', 'download', $url) ?>"><i class="fa fa-download fa-lg"></i> Download</a>
</div></div></div>
<div class="row"><div class="col-md-12">
<textarea readonly id="output-<?php echo $tab ?>" class="form-control" rows="30" placeholder="Output" style="resize:vertical;"></textarea>
</div></div>
</div>
<script type="text/javascript">
document.getElementById('copy-<?php echo $tab ?>').onclick = function() {
output = document.getElementById("output-<?php echo $tab ?>");
output.select();
try {
document.execCommand('copy');
} catch (err) {
alert('Unsupported Browser!');
}
};
document.getElementById('run-<?php echo $tab ?>').onclick = function () {
output = document.getElementById("output-<?php echo $tab ?>");
xhr = new XMLHttpRequest();
xhr.open("GET", "<?php echo $url ?>", true);
xhr.onprogress = function (e) {
output.innerHTML = e.currentTarget.responseText;
output.scrollTop = output.scrollHeight - output.clientHeight; // scrolls the output area
};
xhr.onreadystatechange = function () {
if (xhr.readyState == 4) {
console.log("Complete");
}
};
xhr.send();
};
</script>
<?php
}
echo '</div>';
}