2015-08-10 13:36:55 -07:00
|
|
|
<?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.
|
2017-12-04 09:56:13 +02:00
|
|
|
*
|
|
|
|
* @package LibreNMS
|
|
|
|
* @subpackage webui
|
|
|
|
* @link http://librenms.org
|
2018-03-25 22:50:09 +02:00
|
|
|
* @copyright 2018 LibreNMS
|
2017-12-04 09:56:13 +02:00
|
|
|
* @author LibreNMS Contributors
|
|
|
|
*/
|
2015-08-10 13:36:55 -07:00
|
|
|
|
2019-06-23 00:29:12 -05:00
|
|
|
use LibreNMS\Config;
|
|
|
|
|
2018-03-25 22:50:09 +02:00
|
|
|
$filter_hostname = mres($vars['hostname']);
|
|
|
|
$filter_range = mres($vars['range']);
|
2015-08-10 13:36:55 -07:00
|
|
|
|
|
|
|
if (isset($searchPhrase) && !empty($searchPhrase)) {
|
|
|
|
$query = 'message:"'.$searchPhrase.'"';
|
2016-08-18 20:28:22 -05:00
|
|
|
} else {
|
2015-08-10 13:36:55 -07:00
|
|
|
$query = '*';
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($current)) {
|
|
|
|
$offset = ($current * $rowCount) - ($rowCount);
|
|
|
|
$limit = $rowCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($rowCount != -1) {
|
|
|
|
$extra_query = "&limit=$limit&offset=$offset";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($filter_hostname)) {
|
|
|
|
if (!empty($query)) {
|
|
|
|
$query .= ' && ';
|
|
|
|
}
|
2015-08-17 10:46:40 +00:00
|
|
|
$ip = gethostbyname($filter_hostname);
|
2016-06-21 20:45:03 +01:00
|
|
|
$device = device_by_name($filter_hostname);
|
2015-08-17 10:46:40 +00:00
|
|
|
$query .= 'source:"'.$filter_hostname.'" || source:"'.$ip.'"';
|
2016-06-22 19:39:47 +01:00
|
|
|
if (isset($device['ip']) && $ip != $device['ip']) {
|
2016-06-21 20:45:03 +01:00
|
|
|
$query .= ' || source:"'.$device['ip'].'"';
|
|
|
|
}
|
2015-08-10 13:36:55 -07:00
|
|
|
}
|
|
|
|
|
2019-06-23 00:29:12 -05:00
|
|
|
if (Config::has('graylog.base_uri')) {
|
|
|
|
$graylog_base = Config::get('graylog.base_uri');
|
|
|
|
} elseif (version_compare(Config::get('graylog.version'), '2.1', '>=')) {
|
2016-09-14 04:23:52 +01:00
|
|
|
$graylog_base = '/api/search/universal/relative';
|
|
|
|
} else {
|
|
|
|
$graylog_base = '/search/universal/relative';
|
|
|
|
}
|
|
|
|
|
2019-06-23 00:29:12 -05:00
|
|
|
$graylog_url = Config::get('graylog.server') . ':' . Config::get('graylog.port') . $graylog_base . '?query=' . urlencode($query) . '&range=' . $filter_range . $extra_query;
|
2015-08-10 13:36:55 -07:00
|
|
|
|
|
|
|
$context = stream_context_create(array(
|
|
|
|
'http' => array(
|
2019-06-23 00:29:12 -05:00
|
|
|
'header' => "Authorization: Basic " . base64_encode(Config::get('graylog.username') . ':' . Config::get('graylog.password')) . "\r\n" .
|
2015-08-11 14:50:05 -07:00
|
|
|
"Accept: application/json",
|
2015-08-10 13:36:55 -07:00
|
|
|
)
|
|
|
|
));
|
|
|
|
|
2016-08-18 20:28:22 -05:00
|
|
|
$messages = json_decode(file_get_contents($graylog_url, false, $context), true);
|
2015-08-10 13:36:55 -07:00
|
|
|
|
|
|
|
foreach ($messages['messages'] as $message) {
|
2019-06-23 00:29:12 -05:00
|
|
|
if (Config::has('graylog.timezone')) {
|
|
|
|
$userTimezone = new DateTimeZone(Config::get('graylog.timezone'));
|
2017-12-04 09:56:13 +02:00
|
|
|
$graylogTime = new DateTime($message['message']['timestamp']);
|
|
|
|
$offset = $userTimezone->getOffset($graylogTime);
|
|
|
|
|
|
|
|
$timeInterval = DateInterval::createFromDateString((string)$offset . 'seconds');
|
|
|
|
$graylogTime->add($timeInterval);
|
|
|
|
$displayTime = $graylogTime->format('Y-m-d H:i:s');
|
|
|
|
} else {
|
|
|
|
$displayTime = $message['message']['timestamp'];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-08-10 13:36:55 -07:00
|
|
|
$response[] = array(
|
2018-01-09 22:47:33 +02:00
|
|
|
'timestamp' => graylog_severity_label($message['message']['level']) . $displayTime,
|
2017-12-24 21:59:16 +02:00
|
|
|
'source' => '<a href="'.generate_url(array('page'=>'device', 'device'=>$message['message']['source'])).'">'.$message['message']['source'].'</a>',
|
|
|
|
'message' => $message['message']['message'],
|
|
|
|
'facility' => $message['message']['facility'],
|
|
|
|
'level' => $message['message']['level'],
|
2015-08-10 13:36:55 -07:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-08-17 10:46:40 +00:00
|
|
|
if (empty($messages['total_results'])) {
|
|
|
|
$total = 0;
|
2016-08-18 20:28:22 -05:00
|
|
|
} else {
|
2015-08-17 10:46:40 +00:00
|
|
|
$total = $messages['total_results'];
|
|
|
|
}
|
2015-08-10 13:36:55 -07:00
|
|
|
|
|
|
|
$output = array('current'=>$current,'rowCount'=>$rowCount,'rows'=>$response,'total'=>$total);
|
|
|
|
echo _json_encode($output);
|