mirror of
https://github.com/librenms/librenms.git
synced 2024-10-07 16:52:45 +00:00
Merge pull request #11866 from SourceDoctor/fix_availability_device_query
Alternative/Better Availability Calculation Method
This commit is contained in:
@@ -61,46 +61,21 @@ class Availability
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the availability of this device
|
||||
* addition of all recorded outages in seconds
|
||||
*
|
||||
* @param int $duration timeperiod in seconds
|
||||
* @param int $precision after comma precision
|
||||
* @return availability in %
|
||||
* @param object $found_outages filtered database object with all recorded outages
|
||||
* @param int $duration time period to calculate for
|
||||
* @param int $now timestamp for 'now'
|
||||
* @return sum of all matching outages in seconds
|
||||
*/
|
||||
public static function availability($device, $duration, $precision = 3)
|
||||
protected static function outageSummary($found_outages, $duration, $now = null)
|
||||
{
|
||||
if (!is_numeric($device['uptime'])) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$now = time();
|
||||
|
||||
$query = DeviceOutage::where('device_id', '=', $device['device_id'])
|
||||
->where('up_again', '>=', $now - $duration)
|
||||
->orderBy('going_down');
|
||||
|
||||
$found_outages = $query->get();
|
||||
|
||||
# no recorded outages found, so use current uptime
|
||||
if (!count($found_outages)) {
|
||||
# uptime is greater duration interval -> full availability
|
||||
if ($device['uptime'] >= $duration) {
|
||||
return 100 * 1;
|
||||
} else {
|
||||
return round(100 * $device['uptime'] / $duration, $precision);
|
||||
}
|
||||
}
|
||||
|
||||
$oldest_date_going_down = $query->first()->value('going_down');
|
||||
$oldest_uptime = $query->first()->value('uptime');
|
||||
$recorded_duration = $now - ($oldest_date_going_down - $oldest_uptime);
|
||||
|
||||
if ($recorded_duration > $duration) {
|
||||
$recorded_duration = $duration;
|
||||
if (!is_numeric($now)) {
|
||||
$now = time();
|
||||
}
|
||||
|
||||
# sum up time period of all outages
|
||||
$outage_summary = 0;
|
||||
$outage_sum = 0;
|
||||
foreach ($found_outages as $outage) {
|
||||
# if device is still down, outage goes till $now
|
||||
$up_again = $outage->up_again ?: $now;
|
||||
@@ -112,9 +87,40 @@ class Availability
|
||||
# outage partial in duration period, so consider only relevant part
|
||||
$going_down = $now - $duration;
|
||||
}
|
||||
$outage_summary += ($up_again - $going_down);
|
||||
$outage_sum += ($up_again - $going_down);
|
||||
}
|
||||
return $outage_sum;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the availability (decreasing) of this device
|
||||
* means, starting with 100% as default
|
||||
* substracts recorded outages
|
||||
*
|
||||
* @param array $device device to be looked at
|
||||
* @param int $duration time period to calculate for
|
||||
* @param int $precision float precision for calculated availability
|
||||
* @return float calculated availability
|
||||
*/
|
||||
public static function availability($device, $duration, $precision = 3, $now = null)
|
||||
{
|
||||
if (!is_numeric($now)) {
|
||||
$now = time();
|
||||
}
|
||||
|
||||
return round(100 * ($recorded_duration - $outage_summary) / $duration, $precision);
|
||||
$query = DeviceOutage::where('device_id', '=', $device['device_id'])
|
||||
->where('up_again', '>=', $now - $duration)
|
||||
->orderBy('going_down');
|
||||
|
||||
$found_outages = $query->get();
|
||||
|
||||
# no recorded outages found, so use current uptime
|
||||
if (!count($found_outages)) {
|
||||
return 100 * 1;
|
||||
}
|
||||
|
||||
$outage_summary = self::outageSummary($found_outages, $duration, $now);
|
||||
|
||||
return round(100 * ($duration - $outage_summary) / $duration, $precision);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user