mirror of
https://github.com/librenms/librenms.git
synced 2024-10-07 16:52:45 +00:00
Display Up/Down time in Device List (#9951)
* 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
This commit is contained in:
@@ -48,4 +48,35 @@ class Time
|
||||
|
||||
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 Illuminate\Database\Eloquent\Builder;
|
||||
use LibreNMS\Config;
|
||||
use LibreNMS\Util\Rewrite;
|
||||
use LibreNMS\Util\Url;
|
||||
use LibreNMS\Util\Time;
|
||||
|
||||
class DeviceController extends TableController
|
||||
{
|
||||
@@ -125,7 +126,7 @@ class DeviceController extends TableController
|
||||
'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),
|
||||
];
|
||||
|
@@ -13,6 +13,7 @@ use LibreNMS\Util\IP;
|
||||
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 @@ class Device extends BaseModel
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -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 '</div><div class="panel-body">';
|
||||
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 @@ if ($format == "graph") {
|
||||
<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 @@ if ($format == "graph") {
|
||||
"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\Authentication\LegacyAuth;
|
||||
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\IPv6;
|
||||
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 (!empty($agent_data['uptime'])) {
|
||||
|
||||
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 @@ if ($uptime != 0 && $config['os'][$device['os']]['bad_uptime'] !== true) {
|
||||
|
||||
$graphs['uptime'] = true;
|
||||
|
||||
echo 'Uptime: ' . formatUptime($uptime) . PHP_EOL;
|
||||
echo 'Uptime: ' . Time::formatInterval($uptime) . PHP_EOL;
|
||||
|
||||
$update_array['uptime'] = $uptime;
|
||||
$device['uptime'] = $uptime;
|
||||
|
Reference in New Issue
Block a user