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

Display Up/Down time in Device List #9951

Merged
merged 12 commits into from Mar 18, 2019
@@ -48,4 +48,35 @@ public static function legacyTimeSpecToSecs($description)
return isset($conversion[$description]) ? $conversion[$description] : 0;
}
public static function formatInterval($interval, $format = 'long')
{
$result = '';
$data = [
'years' => 31536000,
'days' => 86400,
'hours' => 3600,
'minutes' => 60,
'seconds' => 1,
];
foreach ($data as $k => $v) {
if ($interval >= $v) {
$diff = floor($interval / $v);
$result .= " $diff";
if ($format == 'short') {
$result .= substr($k, 0, 1);
} elseif ($diff > 1) {
$result .= ' ' . $k;
} else {
$result .= substr($k, 0, -1);
}
$interval -= $v * $diff;
}
}
return $result;
}
}
@@ -31,6 +31,7 @@
use LibreNMS\Config;
use LibreNMS\Util\Rewrite;
use LibreNMS\Util\Url;
use LibreNMS\Util\Time;
class DeviceController extends TableController
{
@@ -125,7 +126,7 @@ public function formatItem($device)
'metrics' => $this->getMetrics($device),
'hardware' => Rewrite::ciscoHardware($device),
'os' => $this->getOsText($device),
'uptime' => $device->formatUptime(true),
'uptime' => Time::formatInterval($device->getStatus() ? $device->uptime : $device->last_polled->diffInSeconds(), 'short'),
'location' => $this->getLocation($device),
'actions' => $this->getActions($device),
];
@@ -13,6 +13,7 @@
use LibreNMS\Util\IPv4;
use LibreNMS\Util\IPv6;
use LibreNMS\Util\Url;
use LibreNMS\Util\Time;
class Device extends BaseModel
{
@@ -21,7 +22,10 @@ class Device extends BaseModel
public $timestamps = false;
protected $primaryKey = 'device_id';
protected $fillable = ['hostname', 'ip', 'status', 'status_reason'];
protected $casts = ['status' => 'boolean'];
protected $casts = [
'last_polled' => 'datetime',
'status' => 'boolean',
];
/**
* Initialize this class
@@ -253,34 +257,7 @@ public function canAccess($user)
public function formatUptime($short = false)
{
$result = '';
$interval = $this->uptime;
$data = [
'years' => 31536000,
'days' => 86400,
'hours' => 3600,
'minutes' => 60,
'seconds' => 1,
];
foreach ($data as $k => $v) {
if ($interval >= $v) {
$diff = floor($interval / $v);
$result .= " $diff";
if ($short) {
$result .= substr($k, 0, 1);
} elseif ($diff > 1) {
$result .= $k;
} else {
$result .= substr($k, 0, -1);
}
$interval -= $v * $diff;
}
}
return $result;
return Time::formatInterval($this->uptime, $short);
}
/**
@@ -387,6 +364,16 @@ public function statusName()
// ---- Accessors/Mutators ----
public function getStatus()

This comment has been minimized.

Copy link
@murrant

murrant Mar 17, 2019

Member

Can you please remove getStatus() and getLastPolled(), just use the property accessors: ->status and ->last_polled

{
return $this->status;
}
public function getLastPolled()
This conversation was marked as resolved by PipoCanaja

This comment has been minimized.

Copy link
@murrant

murrant Mar 14, 2019

Member

last_polled should be a carbon object, so you don't need this function.

This comment has been minimized.

Copy link
@PipoCanaja

PipoCanaja Mar 14, 2019

Author Contributor

I cannot access $device->last_polled from DeviceController.php:
OK: 'uptime' => Time::formatInterval($device->getStatus() ? $device->uptime : time() - $device->getLastPolled(), 'short'),
NOK: 'uptime' => Time::formatInterval($device->getStatus() ? $device->uptime : time() - $device->last_polled, 'short'),

This comment has been minimized.

Copy link
@murrant

murrant Mar 15, 2019

Member

Try $device->lastPolled

This comment has been minimized.

Copy link
@PipoCanaja

PipoCanaja Mar 15, 2019

Author Contributor

Still not. This one does not fail but the $device->lastPolled is empty.

{
return strtotime($this->last_polled);
}
public function getIconAttribute($icon)
{
$this->loadOs();
@@ -1,9 +1,11 @@
<?php
use App\Models\Location;
use App\Models\Device;
use LibreNMS\Config;
use LibreNMS\Exceptions\InvalidIpException;
use LibreNMS\Util\IP;
use LibreNMS\Util\Time;
echo "<div class='row'>
<div class='col-md-12'>
@@ -21,13 +23,8 @@
echo '<script src="js/leaflet.js"></script>';
echo '<script src="js/L.Control.Locate.min.js"></script>';
$uptime = formatUptime($device['uptime']);
$uptime_text = 'Uptime';
if ($device['status'] == 0) {
// Rewrite $uptime to be downtime if device is down
$uptime = formatUptime(time() - strtotime($device['last_polled']));
$uptime_text = 'Downtime';
}
$uptime = (Time::formatInterval($device['status'] ? $device['uptime'] : time() - strtotime($device['last_polled'])));
$uptime_text = ($device['status'] ? 'Uptime' : 'Downtime');
if ($device['os'] == 'ios') {
formatCiscoHardware($device);
@@ -291,7 +291,7 @@
<th data-column-id="metrics" data-width="<?php echo $detailed ? '100px' : '150px'; ?>" data-sortable="false" data-searchable="false" data-visible="<?php echo $detailed ? 'true' : 'false'; ?>">Metrics</th>
<th data-column-id="hardware">Platform</th>
<th data-column-id="os">Operating System</th>
<th data-column-id="uptime">Uptime</th>
<th data-column-id="uptime" data-formatter="uptime">Up/Down Time</th>
<th data-column-id="location" data-visible="<?php echo $detailed ? 'true' : 'false'; ?>">Location</th>
<th data-column-id="actions" data-width="<?php echo $detailed ? '90px' : '200px'; ?>" data-sortable="false" data-searchable="false" data-header-css-class="device-table-header-actions">Actions</th>
</tr>
@@ -314,6 +314,13 @@
"device": function (column, row) {
return "<span>" + row.hostname + "</span>";
},
"uptime": function (column, row) {
if (row.status == 'down') {
return "<span class='alert-status-small label-danger'></span><span>" + row.uptime + "</span>";
} else {
return "<span class='alert-status-small label-success'></span><span>" + row.uptime + "</span>";
}
},
},
templates: {
header: "<div class=\"devices-headers-table-menu\" style=\"padding:6px 6px 0px 0px;\"><p class=\"{{css.actions}}\"></p></div><div class=\"row\"></div>"
@@ -30,6 +30,7 @@
use LibreNMS\Alert\AlertUtil;
use LibreNMS\Config;
use PHPMailer\PHPMailer\PHPMailer;
use LibreNMS\Util\Time;
/**
* @param $rule
@@ -411,8 +412,8 @@ function DescribeAlert($alert)
$obj['version'] = $device['version'];
$obj['location'] = $device['location'];
$obj['uptime'] = $device['uptime'];
$obj['uptime_short'] = formatUptime($device['uptime'], 'short');
$obj['uptime_long'] = formatUptime($device['uptime']);
$obj['uptime_short'] = Time::formatInterval($device['uptime'], 'short');
$obj['uptime_long'] = Time::formatInterval($device['uptime']);
$obj['description'] = $device['purpose'];
$obj['notes'] = $device['notes'];
$obj['alert_notes'] = $alert['note'];
@@ -26,6 +26,7 @@
use LibreNMS\Util\MemcacheLock;
use Symfony\Component\Process\Process;
use PHPMailer\PHPMailer\PHPMailer;
use LibreNMS\Util\Time;
if (!function_exists('set_debug')) {
/**
@@ -617,54 +618,10 @@ function deviceArray($host, $community, $snmpver, $port = 161, $transport = 'udp
return $device;
}
function formatUptime($diff, $format = "long")
{
$yearsDiff = floor($diff/31536000);
$diff -= $yearsDiff*31536000;
$daysDiff = floor($diff/86400);
$diff -= $daysDiff*86400;
$hrsDiff = floor($diff/60/60);
$diff -= $hrsDiff*60*60;
$minsDiff = floor($diff/60);
$diff -= $minsDiff*60;
$secsDiff = $diff;
$uptime = "";
if ($format == "short") {
if ($yearsDiff > '0') {
$uptime .= $yearsDiff . "y ";
}
if ($daysDiff > '0') {
$uptime .= $daysDiff . "d ";
}
if ($hrsDiff > '0') {
$uptime .= $hrsDiff . "h ";
}
if ($minsDiff > '0') {
$uptime .= $minsDiff . "m ";
}
if ($secsDiff > '0') {
$uptime .= $secsDiff . "s ";
}
} else {
if ($yearsDiff > '0') {
$uptime .= $yearsDiff . " years, ";
}
if ($daysDiff > '0') {
$uptime .= $daysDiff . " day" . ($daysDiff != 1 ? 's' : '') . ", ";
}
if ($hrsDiff > '0') {
$uptime .= $hrsDiff . "h ";
}
if ($minsDiff > '0') {
$uptime .= $minsDiff . "m ";
}
if ($secsDiff > '0') {
$uptime .= $secsDiff . "s ";
}
}
return trim($uptime);
return Time::formatInterval($diff, $format);
}
function isSNMPable($device)
@@ -14,6 +14,7 @@
use App\Models\Location;
use LibreNMS\Config;
use LibreNMS\RRD\RrdDefinition;
use LibreNMS\Util\Time;
$snmpdata = snmp_get_multi_oid($device, ['sysUpTime.0', 'sysLocation.0', 'sysContact.0', 'sysName.0', 'sysObjectID.0', 'sysDescr.0'], '-OQnUt', 'SNMPv2-MIB');
@@ -41,7 +42,7 @@
if ($uptime != 0 && $config['os'][$device['os']]['bad_uptime'] !== true) {
if ($uptime < $device['uptime']) {
log_event('Device rebooted after ' . formatUptime($device['uptime']) . " -> {$uptime}s", $device, 'reboot', 4, $device['uptime']);
log_event('Device rebooted after ' . Time::formatInterval($device['uptime']) . " -> {$uptime}s", $device, 'reboot', 4, $device['uptime']);
}
$tags = array(
@@ -51,7 +52,7 @@
$graphs['uptime'] = true;
echo 'Uptime: ' . formatUptime($uptime) . PHP_EOL;
echo 'Uptime: ' . Time::formatInterval($uptime) . PHP_EOL;
$update_array['uptime'] = $uptime;
$device['uptime'] = $uptime;
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.