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

Update syslog display and backend #9228

Merged
merged 5 commits into from Sep 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
216 changes: 216 additions & 0 deletions LibreNMS/Util/Url.php
@@ -0,0 +1,216 @@
<?php
/**
* Url.php
*
* -Description-
*
* 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 2018 Tony Murray
* @author Tony Murray <murraytony@gmail.com>
*/

namespace LibreNMS\Util;

use App\Models\Device;
use Auth;
use Carbon\Carbon;
use LibreNMS\Config;

class Url
{
/**
* @param Device $device
* @param string $text
* @param array $vars
* @param int $start
* @param int $end
* @param int $escape_text
* @param int $overlib
* @return string
*/
public static function deviceLink($device, $text = null, $vars = [], $start = 0, $end = 0, $escape_text = 1, $overlib = 1)
{
if (!$start) {
$start = Carbon::now()->subDay(1)->timestamp;
}

if (!$end) {
$end = Carbon::now()->timestamp;
}

if (!$text) {
$text = $device->displayName();
}

if ($escape_text) {
$text = htmlentities($text);
}

$class = self::deviceLinkDisplayClass($device);
$graphs = self::getOverviewGraphsForDevice($device);
$url = Url::deviceUrl($device, $vars);

// beginning of overlib box contains large hostname followed by hardware & OS details
$contents = '<div><span class="list-large">' . $device->displayName() . '</span>';
if ($device->hardware) {
$contents .= ' - ' . htmlentities($device->hardware);
}

if ($device->os) {
$contents .= ' - ' . htmlentities(Config::getOsSetting($device->os, 'text'));
}

if ($device->version) {
$contents .= ' ' . htmlentities($device->version);
}

if ($device->features) {
$contents .= ' (' . htmlentities($device->features) . ')';
}

if ($device->location) {
$contents .= ' - ' . htmlentities($device->location);
}

$contents .= '</div>';

foreach ((array)$graphs as $entry) {
$graph = isset($entry['graph']) ? $entry['graph'] : 'unknown';
$graphhead = isset($entry['text']) ? $entry['text'] : 'unknown';
$contents .= '<div class="overlib-box">';
$contents .= '<span class="overlib-title">' . $graphhead . '</span><br />';
$contents .= Url::minigraphImage($device, $start, $end, $graph);
$contents .= Url::minigraphImage($device, Carbon::now()->subWeek(1)->timestamp, $end, $graph);
$contents .= '</div>';
}

if ($overlib == 0) {
$link = $contents;
} else {
// escape quotes
$contents = str_replace(["'", '"'], "\'", $contents);
$link = Url::overlibLink($url, $text, $contents, $class);
}

if (Auth::user()->hasGlobalRead() || $device->users()->where('devices_perms.user_id', Auth::id())->exists()) {
return $link;
} else {
return $device->displayName();
}
}

public static function deviceUrl($device, $vars = [])
{
return self::generate(['page' => 'device', 'device' => $device->device_id], $vars);
}

public static function generate($vars, $new_vars = [])
{
$vars = array_merge($vars, $new_vars);

$url = $vars['page'] . '/';
unset($vars['page']);

foreach ($vars as $var => $value) {
if ($value == '0' || $value != '' && !str_contains($var, 'opt') && !is_numeric($var)) {
$url .= $var . '=' . urlencode($value) . '/';
}
}

return $url;
}

public static function overlibLink($url, $text, $contents, $class = null)
{
$contents = "<div style=\'background-color: #FFFFFF;\'>" . $contents . '</div>';
$contents = str_replace('"', "\'", $contents);
if ($class === null) {
$output = '<a href="' . $url . '"';
} else {
$output = '<a class="' . $class . '" href="' . $url . '"';
}

if (Config::get('web_mouseover', true)) {
$defaults = Config::get('overlib_defaults', ",FGCOLOR,'#ffffff', BGCOLOR, '#e5e5e5', BORDER, 5, CELLPAD, 4, CAPCOLOR, '#555555', TEXTCOLOR, '#3e3e3e'");
$output .= " onmouseover=\"return overlib('$contents'$defaults,WRAP,HAUTO,VAUTO); \" onmouseout=\"return nd();\">";
} else {
$output .= '>';
}

$output .= $text . '</a>';

return $output;
}

/**
* Generate minigraph image url
*
* @param Device $device
* @param int $start
* @param int $end
* @param string $type
* @param string $legend
* @param int $width
* @param int $height
* @param string $sep
* @param string $class
* @param int $absolute_size
* @return string
*/
public static function minigraphImage($device, $start, $end, $type, $legend = 'no', $width = 275, $height = 100, $sep = '&amp;', $class = 'minigraph-image', $absolute_size = 0)
{
$vars = ['device=' . $device->device_id, "from=$start", "to=$end", "width=$width", "height=$height", "type=$type", "legend=$legend", "absolute=$absolute_size"];
return '<img class="' . $class . '" width="' . $width . '" height="' . $height . '" src="graph.php?' . implode($sep, $vars) . '">';
}

private static function getOverviewGraphsForDevice($device)
{
if ($device->snmp_disable) {
return Config::getOsSetting('ping', 'over');
}

if ($graphs = Config::getOsSetting($device->os, 'over')) {
return $graphs;
}

if ($os_group = Config::getOsSetting($device->os, 'os_group')) {
$name = key($os_group);
if (isset($os_group[$name]['over'])) {
return $os_group[$name]['over'];
}
}

return Config::getOsSetting('default', 'over');
}

/**
* @param Device $device
* @return string
*/
private static function deviceLinkDisplayClass($device)
{
if ($device->disabled) {
return 'list-device-disabled';
}

if ($device->ignore) {
return $device->status ? 'list-device-ignored-up' : 'list-device-ignored';
}

return $device->status ? 'list-device' : 'list-device-down';
}
}
125 changes: 125 additions & 0 deletions app/Http/Controllers/PaginatedAjaxController.php
@@ -0,0 +1,125 @@
<?php
/**
* AjaxController.php
*
* -Description-
*
* 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 2018 Tony Murray
* @author Tony Murray <murraytony@gmail.com>
*/

namespace App\Http\Controllers;

use Illuminate\Contracts\Pagination\Paginator;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Illuminate\Support\Collection;

abstract class PaginatedAjaxController extends Controller
{
/**
* Base rules for this controller.
*
* @return mixed
*/
abstract protected function baseRules();

/**
* Defines the base query for this resource
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder
*/
abstract public function baseQuery($request);

/**
* @param Paginator $paginator
* @return \Illuminate\Http\JsonResponse
*/
abstract protected function formatResponse($paginator);

/**
* Defines validation rules (will override base validation rules for select2 responses too)
*
* @return array
*/
public function rules()
{
return [];
}

/**
* Defines search fields will be searched in order
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function searchFields($request)
{
return [];
}

/**
* Format an item for display. Default is pass-through
*
* @param Model $model
* @return array|Collection|Model
*/
public function formatItem($model)
{
return $model;
}

/**
* @param string
* @param Builder $query
* @param array $fields
* @return Builder
*/
protected function search($search, $query, $fields)
{
if ($search) {
$query->where(function ($query) use ($fields, $search) {
/** @var Builder $query */
foreach ($fields as $field) {
$query->orWhere($field, 'like', '%' . $search . '%');
}
});
}

return $query;
}


/**
* Validate the given request with the given rules.
*
* @param \Illuminate\Http\Request $request
* @param array $rules
* @param array $messages
* @param array $customAttributes
* @return void
*/
public function validate(Request $request, array $rules = [], array $messages = [], array $customAttributes = [])
{
$full_rules = array_replace($this->baseRules(), $rules);

parent::validate($request, $full_rules, $messages, $customAttributes);
}
}
Expand Up @@ -4,11 +4,9 @@

use Illuminate\Http\Request;

class AjaxController extends Controller
class ResolutionController extends Controller
{
// FIXME do not just pile functions on this controller, create separate controllers

public function setResolution(Request $request)
public function set(Request $request)
{
$this->validate($request, [
'width' => 'required|numeric',
Expand Down