diff --git a/app/Http/Controllers/Table/OutagesController.php b/app/Http/Controllers/Table/OutagesController.php new file mode 100644 index 0000000000..1bdcde4fdd --- /dev/null +++ b/app/Http/Controllers/Table/OutagesController.php @@ -0,0 +1,134 @@ +. + * + * @package LibreNMS + * @link http://librenms.org + * @copyright 2020 Thomas Berberich + * @author Thomas Berberich + */ + +namespace App\Http\Controllers\Table; + +use App\Models\DeviceOutage; +use App\Models\Eventlog; +use Carbon\Carbon; +use LibreNMS\Config; +use LibreNMS\Util\Url; +use LibreNMS\Enum\Alert; +use Illuminate\Database\Eloquent\Builder; + +class OutagesController extends TableController +{ + public function rules() + { + return [ + 'device' => 'nullable|int', + 'to' => 'nullable|date', + 'from' => 'nullable|date', + ]; + } + + protected function filterFields($request) + { + return [ + 'device_id' => 'device', + ]; + } + + protected function sortFields($request) + { + return ['going_down', 'up_again', 'device_id']; + } + + /** + * Defines the base query for this resource + * + * @param \Illuminate\Http\Request $request + * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Query\Builder + */ + public function baseQuery($request) + { + return DeviceOutage::hasAccess($request->user()) + ->with('device') + ->when($request->from, function ($query) use ($request) { + $query->where('going_down', '>=', strtotime($request->from)); + }) + ->when($request->to, function ($query) use ($request) { + $query->where('going_down', '<=', strtotime($request->to)); + }); + } + + public function formatItem($outage) + { + $start = $this->formatDatetime($outage->going_down); + $end = $outage->up_again ? $this->formatDatetime($outage->up_again) : '-'; + $duration = ($outage->up_again ?: time()) - $outage->going_down; + + return [ + 'status' => $this->statusLabel($outage), + 'going_down' => $start, + 'up_again' => $end, + 'device_id' => $outage->device ? Url::deviceLink($outage->device, $outage->device->shortDisplayName()) : null, + 'duration' => $this->formatTime($duration), + ]; + } + + private function formatTime($duration) + { + $day_seconds = 86400; + + $duration_days = (int)($duration / $day_seconds); + $duration_time = $duration % $day_seconds; + + $output = ""; + if ($duration_days) { + $output .= $duration_days . 'd '; + } + $output .= (new Carbon($duration))->format(Config::get('dateformat.time')); + $output .= ""; + + return $output; + } + + private function formatDatetime($timestamp) + { + if (! $timestamp) { + $timestamp = 0; + } + + $output = ""; + $output .= (new Carbon($timestamp))->format(Config::get('dateformat.compact')); + $output .= ""; + + return $output; + } + + private function statusLabel($outage) + { + if (empty($outage->up_again)) { + $label = "label-danger"; + } else { + $label = "label-success"; + } + + $output = ""; + + return $output; + } +} diff --git a/app/Models/DeviceOutage.php b/app/Models/DeviceOutage.php index 6133446ae4..907e5807d3 100644 --- a/app/Models/DeviceOutage.php +++ b/app/Models/DeviceOutage.php @@ -27,7 +27,8 @@ namespace App\Models; use Illuminate\Database\Eloquent\Model; -class DeviceOutage extends Model +# class DeviceOutage extends Model +class DeviceOutage extends DeviceRelatedModel { public $timestamps = false; protected $primaryKey = null; diff --git a/includes/html/common/outages.inc.php b/includes/html/common/outages.inc.php new file mode 100644 index 0000000000..2246cf3968 --- /dev/null +++ b/includes/html/common/outages.inc.php @@ -0,0 +1,50 @@ + + + + + + + + + + + +
StartEndHostnameDuration
+ + +'; diff --git a/includes/html/pages/device/logs.inc.php b/includes/html/pages/device/logs.inc.php index 21c5f9ef03..acb74b1a7b 100644 --- a/includes/html/pages/device/logs.inc.php +++ b/includes/html/pages/device/logs.inc.php @@ -24,6 +24,17 @@ echo '
'; echo '
'; echo 'Logging » '; +if ($vars['section'] == 'outages') { + echo ''; +} + +echo generate_link('Outages', $vars, array('section' => 'outages')); +if ($vars['section'] == 'outages') { + echo ''; +} + +echo ' | '; + if ($vars['section'] == 'eventlog') { echo ''; } @@ -72,6 +83,10 @@ switch ($vars['section']) { case 'graylog': include 'includes/html/pages/device/logs/'.$vars['section'].'.inc.php'; break; + case 'outages': + $vars['fromdevice'] = true; + include 'includes/html/pages/outages.inc.php'; + break; default: echo '
'; diff --git a/includes/html/pages/outages.inc.php b/includes/html/pages/outages.inc.php new file mode 100644 index 0000000000..5b75506b0d --- /dev/null +++ b/includes/html/pages/outages.inc.php @@ -0,0 +1,128 @@ + +
+
+ Outages +
+ + +
+ + diff --git a/resources/views/layouts/menu.blade.php b/resources/views/layouts/menu.blade.php index 8d661e6b36..d1ad83bacc 100644 --- a/resources/views/layouts/menu.blade.php +++ b/resources/views/layouts/menu.blade.php @@ -126,6 +126,8 @@
  • @lang('Inventory')
  • +
  • @lang('Outages')
  • @if($package_count)
  • @lang('Packages') diff --git a/routes/web.php b/routes/web.php index 615ca08a27..bb1f6440c4 100644 --- a/routes/web.php +++ b/routes/web.php @@ -118,6 +118,7 @@ Route::group(['middleware' => ['auth'], 'guard' => 'auth'], function () { Route::post('customers', 'CustomersController'); Route::post('device', 'DeviceController'); Route::post('eventlog', 'EventlogController'); + Route::post('outages', 'OutagesController'); Route::post('fdb-tables', 'FdbTablesController'); Route::post('routes', 'RoutesTablesController'); Route::post('graylog', 'GraylogController');