Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Added alerts output to capture system #4574

Merged
merged 5 commits into from
Sep 26, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions html/ajax_output.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
require_once 'includes/functions.inc.php';
require_once '../includes/functions.php';
require_once 'includes/authenticate.inc.php';
require_once '../includes/alerts.inc.php';

set_debug($_REQUEST['debug']);
$id = mres($_REQUEST['id']);
Expand Down
18 changes: 18 additions & 0 deletions html/includes/functions.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -1360,3 +1360,21 @@ function get_auth_ad_group_filter($groupname)
}
return $group_filter;
}

/**
* @param $filename
* @param $output
*/
function file_download($filename, $output)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rename this variable to $content

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated and pushed.

{
$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;
}
13 changes: 1 addition & 12 deletions html/includes/output/capture.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,5 @@

$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;
file_download($filename, $output);
}
68 changes: 68 additions & 0 deletions html/includes/output/query.inc.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php
/**
* output.php
*
* runs the requested query and outputs as a file or text
*
* 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 Neil Lathwood
* @author Neil Lathwood <neil@lathwood.co.uk>
*/

if (!is_admin()) {
echo("Insufficient Privileges");
exit();
}

$hostname = escapeshellcmd($_REQUEST['hostname']);
$type = $_REQUEST['type'];

switch ($type) {
case 'alerts':
$filename = "alerts-$hostname.txt";
$device_id = getidbyname($hostname);
$device = device_by_id_cache($device_id);
$rules = GetRules($device_id);
$output = '';
foreach ($rules as $rule) {
$sql = GenSQL($rule['rule']);
$qry = dbFetchRow($sql, array($device_id));
if (is_array($qry)) {
$response = 'matches';
} else {
$response = 'no match';
}
$output .= 'Rule name: ' . $rule['name'] . PHP_EOL;
$output .= 'Alert rule: ' . $rule['rule'] . PHP_EOL;
$output .= 'Rule match: ' . $response . PHP_EOL . PHP_EOL;
}
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');

echo $output;
} elseif ($_GET['format'] == 'download') {
file_download($filename, $output);
}
6 changes: 4 additions & 2 deletions html/pages/device/capture.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,15 @@
<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>
<li role="presentation"><a data-toggle="tab" href="#alerts">Alerts</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'],
'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'],
'alerts' => 'ajax_output.php?id=query&format=text&type=alerts&hostname='.$device['hostname'],
);

foreach ($tabs as $tab => $url) {
Expand Down