Skip to content

Commit

Permalink
Display Up/Down time in Device List (#9951)
Browse files Browse the repository at this point in the history
* First attempt of Up/Down time

* First attempt of Up/Down time

* Moved to Time::formatInterval()

* cleaning

* last polled in casts

* Cleaning the variants of formatUptime()

* Cleaning in ./html/includes/dev-overview-data.inc.php

* Cleaning in ./html/includes/dev-overview-data.inc.php

* Cleaning in ./html/includes/dev-overview-data.inc.php

* updated includes/polling/core.inc.php

* updated includes/alerts.inc.php

* clean accessors
  • Loading branch information
PipoCanaja authored and murrant committed Mar 18, 2019
1 parent 3f5d8e3 commit a2d378e
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 88 deletions.
31 changes: 31 additions & 0 deletions LibreNMS/Util/Time.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
3 changes: 2 additions & 1 deletion app/Http/Controllers/Table/DeviceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
use LibreNMS\Config;
use LibreNMS\Util\Rewrite;
use LibreNMS\Util\Url;
use LibreNMS\Util\Time;

class DeviceController extends TableController
{
Expand Down Expand Up @@ -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->status ? $device->uptime : $device->last_polled->diffInSeconds(), 'short'),
'location' => $this->getLocation($device),
'actions' => $this->getActions($device),
];
Expand Down
35 changes: 6 additions & 29 deletions app/Models/Device.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use LibreNMS\Util\IPv4;
use LibreNMS\Util\IPv6;
use LibreNMS\Util\Url;
use LibreNMS\Util\Time;

class Device extends BaseModel
{
Expand All @@ -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
Expand Down Expand Up @@ -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);
}

/**
Expand Down
11 changes: 4 additions & 7 deletions html/includes/dev-overview-data.inc.php
Original file line number Diff line number Diff line change
@@ -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'>
Expand All @@ -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);
Expand Down
9 changes: 8 additions & 1 deletion html/pages/devices.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -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>"
Expand Down
5 changes: 3 additions & 2 deletions includes/alerts.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
use LibreNMS\Alert\AlertUtil;
use LibreNMS\Config;
use PHPMailer\PHPMailer\PHPMailer;
use LibreNMS\Util\Time;

/**
* @param $rule
Expand Down Expand Up @@ -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'];
Expand Down
49 changes: 3 additions & 46 deletions includes/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -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')) {
/**
Expand Down Expand Up @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions includes/polling/core.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down Expand Up @@ -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(
Expand All @@ -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;
Expand Down

0 comments on commit a2d378e

Please sign in to comment.