mirror of
https://github.com/librenms/librenms.git
synced 2024-10-07 16:52:45 +00:00
Calculate downtime from device_outages table (#15397)
* Calculate downtime from device_outages table * refactor
This commit is contained in:
@@ -182,7 +182,7 @@ class ConnectivityHelper
|
||||
}
|
||||
|
||||
// check for open outage
|
||||
$open_outage = $this->device->outages()->whereNull('up_again')->orderBy('going_down', 'desc')->first();
|
||||
$open_outage = $this->device->getCurrentOutage();
|
||||
|
||||
if ($status) {
|
||||
if ($open_outage) {
|
||||
|
@@ -155,7 +155,7 @@ class DeviceController extends TableController
|
||||
'metrics' => $this->getMetrics($device),
|
||||
'hardware' => htmlspecialchars(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(), true),
|
||||
'uptime' => (! $device->status && ! $device->last_polled) ? __('Never polled') : Time::formatInterval($device->status ? $device->uptime : $device->downSince()->diffInSeconds(), true),
|
||||
'location' => htmlspecialchars($this->getLocation($device)),
|
||||
'actions' => view('device.actions', ['actions' => $this->getActions($device)])->__toString(),
|
||||
'device_id' => $device->device_id,
|
||||
|
@@ -3,6 +3,7 @@
|
||||
namespace App\Models;
|
||||
|
||||
use App\View\SimpleTemplate;
|
||||
use Carbon\Carbon;
|
||||
use Fico7489\Laravel\Pivot\Traits\PivotEventTrait;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
@@ -259,6 +260,24 @@ class Device extends BaseModel
|
||||
return $name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the current DeviceOutage if there is one (if device is down)
|
||||
*/
|
||||
public function getCurrentOutage(): ?DeviceOutage
|
||||
{
|
||||
return $this->relationLoaded('outages')
|
||||
? $this->outages->whereNull('up_again')->sortBy('going_down', descending: true)->first()
|
||||
: $this->outages()->whereNull('up_again')->orderBy('going_down', 'desc')->first();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the time this device went down
|
||||
*/
|
||||
public function downSince(): Carbon
|
||||
{
|
||||
return Carbon::createFromTimestamp((int) $this->getCurrentOutage()?->going_down);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if user can access this device.
|
||||
*
|
||||
|
@@ -5,6 +5,7 @@ use LibreNMS\Config;
|
||||
use LibreNMS\Exceptions\InvalidIpException;
|
||||
use LibreNMS\Util\Clean;
|
||||
use LibreNMS\Util\IP;
|
||||
use LibreNMS\Util\Time;
|
||||
|
||||
echo "<div class='row'>
|
||||
<div class='col-md-12'>
|
||||
@@ -103,13 +104,13 @@ if ($device['sysContact']) {
|
||||
|
||||
if (! empty($device['inserted']) && preg_match('/^0/', $device['inserted']) == 0) {
|
||||
$inserted_text = 'Device Added';
|
||||
$inserted = (\LibreNMS\Util\Time::formatInterval(-(time() - strtotime($device['inserted']))));
|
||||
$inserted = (Time::formatInterval(-(time() - strtotime($device['inserted']))));
|
||||
echo "<div class='row'><div class='col-sm-4'>$inserted_text</div><div class='col-sm-8' title='$inserted_text on " . $device['inserted'] . "'>$inserted</div></div>";
|
||||
}
|
||||
|
||||
if (! empty($device['last_discovered'])) {
|
||||
$last_discovered_text = 'Last Discovered';
|
||||
$last_discovered = (empty($device['last_discovered']) ? 'Never' : \LibreNMS\Util\Time::formatInterval(-(time() - strtotime($device['last_discovered']))));
|
||||
$last_discovered = (empty($device['last_discovered']) ? 'Never' : Time::formatInterval(-(time() - strtotime($device['last_discovered']))));
|
||||
echo "<div class='row'><div class='col-sm-4'>$last_discovered_text</div><div class='col-sm-8' title='$last_discovered_text at " . $device['last_discovered'] . "'>$last_discovered</div></div>";
|
||||
}
|
||||
|
||||
@@ -117,9 +118,12 @@ if (! empty($device['last_discovered'])) {
|
||||
if (! $device['status'] && ! $device['last_polled']) {
|
||||
$uptime = __('Never polled');
|
||||
$uptime_text = 'Uptime';
|
||||
} elseif ($device['status']) {
|
||||
$uptime = Time::formatInterval($device['uptime']);
|
||||
$uptime_text = 'Uptime';
|
||||
} else {
|
||||
$uptime = (\LibreNMS\Util\Time::formatInterval($device['status'] ? $device['uptime'] : time() - strtotime($device['last_polled'])));
|
||||
$uptime_text = ($device['status'] ? 'Uptime' : 'Downtime');
|
||||
$uptime = Time::formatInterval(DeviceCache::getPrimary()->downSince()->diffInSeconds());
|
||||
$uptime_text = 'Downtime';
|
||||
}
|
||||
|
||||
if ($uptime) {
|
||||
|
Reference in New Issue
Block a user