. * * @package LibreNMS * @link http://librenms.org * @copyright 2019 Tony Murray * @author Tony Murray */ namespace App\Http\Controllers\Table; use App\Models\Device; use App\Models\Location; use Illuminate\Database\Eloquent\Builder; use LibreNMS\Config; use LibreNMS\Util\Rewrite; use LibreNMS\Util\Url; use LibreNMS\Util\Time; class DeviceController extends TableController { private $detailed; // display format is detailed protected function rules() { return [ 'format' => 'nullable|in:list_basic,list_detail', 'os' => 'nullable|string', 'version' => 'nullable|string', 'hardware' => 'nullable|string', 'features' => 'nullable|string', 'location' => 'nullable|string', 'type' => 'nullable|string', 'state' => 'nullable|in:0,1,up,down', 'disabled' => 'nullable|in:0,1', 'ignore' => 'nullable|in:0,1', 'group' => 'nullable|int', ]; } protected function filterFields($request) { return ['os', 'version', 'hardware', 'features', 'type', 'status' => 'state', 'disabled', 'ignore', 'location_id' => 'location']; } protected function searchFields($request) { return ['sysName', 'hostname', 'hardware', 'os', 'locations.location']; } /** * Defines the base query for this resource * * @param \Illuminate\Http\Request $request * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder */ protected function baseQuery($request) { /** @var Builder $query */ $query = Device::hasAccess($request->user())->with('location')->select('devices.*'); // if searching or sorting the location field, join the locations table if ($request->get('searchPhrase') || in_array('location', array_keys($request->get('sort', [])))) { $query->leftJoin('locations', 'locations.id', 'devices.location_id'); } // filter device group, not sure this is the most efficient query if ($group = $request->get('group')) { $query->whereHas('groups', function ($query) use ($group) { $query->where('id', $group); }); } return $query; } protected function adjustFilterValue($field, $value) { if ($field == 'location' && !is_numeric($value)) { return Location::query()->where('location', $value)->value('id'); } if ($field == 'state' && !is_numeric($value)) { return str_replace(['up', 'down'], [1, 0], $value); } return $value; } private function isDetailed() { if (is_null($this->detailed)) { $this->detailed = \Request::get('format', 'list_detail') == 'list_detail'; } return $this->detailed; } /** * @param Device $device * @return array|\Illuminate\Database\Eloquent\Model|\Illuminate\Support\Collection */ public function formatItem($device) { return [ 'extra' => $this->getLabel($device), 'status' => $device->statusName(), 'icon' => '', 'hostname' => $this->getHostname($device), 'metrics' => $this->getMetrics($device), 'hardware' => Rewrite::ciscoHardware($device), 'os' => $this->getOsText($device), 'uptime' => (!$device->status && !$device->last_polled) ? __('Never polled') : Time::formatInterval($device->status ? $device->uptime : $device->last_polled->diffInSeconds(), 'short'), 'location' => $this->getLocation($device), 'actions' => $this->getActions($device), ]; } /** * Get the status label class * @param Device $device * @return string */ private function getLabel($device) { if ($device->disabled) { return 'label-default'; } if ($device->ignore) { return 'label-default'; } return $device->status ? 'label-success' : 'label-danger'; } /** * @param Device $device * @return string */ private function getHostname($device) { $hostname = Url::deviceLink($device); if ($this->isDetailed()) { $hostname .= '
' . $device->name(); } return $hostname; } /** * @param Device $device * @return string */ private function getOsText($device) { $device->loadOs(); $os_text = Config::getOsSetting($device->os, 'text'); if ($this->isDetailed()) { $os_text .= '
' . $device->version . ($device->features ? " ($device->features)" : ''); } return $os_text; } /** * @param Device $device * @return string */ private function getMetrics($device) { $port_count = $device->ports()->count(); $sensor_count = $device->sensors()->count(); $wireless_count = $device->wirelessSensors()->count(); $metrics = []; if ($port_count) { $metrics[] = $this->formatMetric($device, $port_count, 'ports', 'fa-link'); } if ($sensor_count) { $metrics[] = $this->formatMetric($device, $sensor_count, 'health', 'fa-dashboard'); } if ($wireless_count) { $metrics[] = $this->formatMetric($device, $wireless_count, 'wireless', 'fa-wifi'); } $glue = $this->isDetailed() ? '
' : ' '; $metrics_content = implode(count($metrics) == 2 ? $glue : '', $metrics); return '
' . $metrics_content . '
'; } /** * @param $device * @param $count * @param $tab * @param $icon * @return string */ private function formatMetric($device, $count, $tab, $icon) { $html = ''; $html .= ' ' . $count; $html .= ' '; return $html; } /** * @param Device $device * @return string */ private function getLocation($device) { if ($device->location) { if (extension_loaded('mbstring')) { return mb_substr($device->location->location, 0, 32, 'utf8'); } else { return substr($device->location->location, 0, 32); } } return ''; } /** * @param Device $device * @return string */ private function getActions($device) { $actions = '
'; $actions .= ''; $actions .= ''; if (\Auth::user()->hasGlobalAdmin()) { $actions .= ''; } if ($this->isDetailed()) { $actions .= '
'; } $actions .= '
'; if ($server = Config::get('gateone.server')) { if (Config::get('gateone.use_librenms_user')) { $actions .= '
'; } else { $actions .= '
'; } } else { $actions .= '
'; } $actions .= '
'; $actions .= '
'; return $actions; } }