issue #52: adding uptime percentage to uptime graph
parent
1108220678
commit
107b6ebb00
|
@ -130,6 +130,7 @@ $sm_lang = array(
|
||||||
'latency_max' => 'Latency (maximum)',
|
'latency_max' => 'Latency (maximum)',
|
||||||
'latency_min' => 'Latency (minimum)',
|
'latency_min' => 'Latency (minimum)',
|
||||||
'latency_avg' => 'Latency (average)',
|
'latency_avg' => 'Latency (average)',
|
||||||
|
'uptime' => 'Uptime',
|
||||||
'year' => 'Year',
|
'year' => 'Year',
|
||||||
'month' => 'Month',
|
'month' => 'Month',
|
||||||
'week' => 'Week',
|
'week' => 'Week',
|
||||||
|
|
|
@ -130,6 +130,7 @@ $sm_lang = array(
|
||||||
'latency_max' => 'Latency (maximum)',
|
'latency_max' => 'Latency (maximum)',
|
||||||
'latency_min' => 'Latency (minimum)',
|
'latency_min' => 'Latency (minimum)',
|
||||||
'latency_avg' => 'Latency (average)',
|
'latency_avg' => 'Latency (average)',
|
||||||
|
'uptime' => 'Uptime',
|
||||||
'year' => 'Year',
|
'year' => 'Year',
|
||||||
'month' => 'Month',
|
'month' => 'Month',
|
||||||
'week' => 'Week',
|
'week' => 'Week',
|
||||||
|
|
|
@ -130,6 +130,7 @@ $sm_lang = array(
|
||||||
'latency_max' => 'Latency (maximum)',
|
'latency_max' => 'Latency (maximum)',
|
||||||
'latency_min' => 'Latency (minimum)',
|
'latency_min' => 'Latency (minimum)',
|
||||||
'latency_avg' => 'Latency (average)',
|
'latency_avg' => 'Latency (average)',
|
||||||
|
'uptime' => 'Uptime',
|
||||||
'year' => 'Year',
|
'year' => 'Year',
|
||||||
'month' => 'Month',
|
'month' => 'Month',
|
||||||
'week' => 'Week',
|
'week' => 'Week',
|
||||||
|
|
|
@ -130,6 +130,7 @@ $sm_lang = array(
|
||||||
'latency_max' => 'Temps de réponse maximum',
|
'latency_max' => 'Temps de réponse maximum',
|
||||||
'latency_min' => 'Temps de réponse minimum',
|
'latency_min' => 'Temps de réponse minimum',
|
||||||
'latency_avg' => 'Temps de réponse moyen',
|
'latency_avg' => 'Temps de réponse moyen',
|
||||||
|
'uptime' => 'Uptime',
|
||||||
'year' => 'Année',
|
'year' => 'Année',
|
||||||
'month' => 'Mois',
|
'month' => 'Mois',
|
||||||
'week' => 'Semaine',
|
'week' => 'Semaine',
|
||||||
|
|
|
@ -130,6 +130,7 @@ $sm_lang = array(
|
||||||
'latency_max' => 'Latency (maximum)',
|
'latency_max' => 'Latency (maximum)',
|
||||||
'latency_min' => 'Latency (minimum)',
|
'latency_min' => 'Latency (minimum)',
|
||||||
'latency_avg' => 'Latency (average)',
|
'latency_avg' => 'Latency (average)',
|
||||||
|
'uptime' => 'Uptime',
|
||||||
'year' => 'Year',
|
'year' => 'Year',
|
||||||
'month' => 'Month',
|
'month' => 'Month',
|
||||||
'week' => 'Week',
|
'week' => 'Week',
|
||||||
|
|
|
@ -130,6 +130,7 @@ $sm_lang = array(
|
||||||
'latency_max' => 'Latency (maximum)',
|
'latency_max' => 'Latency (maximum)',
|
||||||
'latency_min' => 'Latency (minimum)',
|
'latency_min' => 'Latency (minimum)',
|
||||||
'latency_avg' => 'Latency (gemiddeld)',
|
'latency_avg' => 'Latency (gemiddeld)',
|
||||||
|
'uptime' => 'Uptime',
|
||||||
'year' => 'Jaar',
|
'year' => 'Jaar',
|
||||||
'month' => 'Maand',
|
'month' => 'Maand',
|
||||||
'week' => 'Week',
|
'week' => 'Week',
|
||||||
|
|
|
@ -130,6 +130,7 @@ $sm_lang = array(
|
||||||
'latency_max' => 'Latência (máxima)',
|
'latency_max' => 'Latência (máxima)',
|
||||||
'latency_min' => 'Latência (minima)',
|
'latency_min' => 'Latência (minima)',
|
||||||
'latency_avg' => 'Latência (média)',
|
'latency_avg' => 'Latência (média)',
|
||||||
|
'uptime' => 'Uptime',
|
||||||
'year' => 'Ano',
|
'year' => 'Ano',
|
||||||
'month' => 'Mês',
|
'month' => 'Mês',
|
||||||
'week' => 'Semana',
|
'week' => 'Semana',
|
||||||
|
|
|
@ -130,6 +130,7 @@ $sm_lang = array(
|
||||||
'latency_max' => '延迟(最大)',
|
'latency_max' => '延迟(最大)',
|
||||||
'latency_min' => '延迟(最小)',
|
'latency_min' => '延迟(最小)',
|
||||||
'latency_avg' => '延迟(平均)',
|
'latency_avg' => '延迟(平均)',
|
||||||
|
'uptime' => 'Uptime',
|
||||||
'year' => '年',
|
'year' => '年',
|
||||||
'month' => '月',
|
'month' => '月',
|
||||||
'week' => '周',
|
'week' => '周',
|
||||||
|
|
|
@ -64,13 +64,32 @@ class HistoryGraph {
|
||||||
0 => $this->generateGraphUptime($server_id),
|
0 => $this->generateGraphUptime($server_id),
|
||||||
1 => $this->generateGraphHistory($server_id),
|
1 => $this->generateGraphHistory($server_id),
|
||||||
);
|
);
|
||||||
|
$info_fields = array(
|
||||||
|
'latency_avg' => '%01.4f',
|
||||||
|
'uptime' => '%01.3f%%',
|
||||||
|
);
|
||||||
|
|
||||||
|
foreach($graphs as $i => &$graph) {
|
||||||
|
// add subarray for info fields
|
||||||
|
$graph['info'] = array();
|
||||||
|
|
||||||
|
foreach($info_fields as $field => $format) {
|
||||||
|
if(!isset($graph[$field])) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
$graph['info'][] = array(
|
||||||
|
'label' => psm_get_lang('servers', $field),
|
||||||
|
'value' => sprintf($format, $graph[$field]),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
$this->tpl->addTemplateDataRepeat($tpl_id, 'graphs', $graphs);
|
$this->tpl->addTemplateDataRepeat($tpl_id, 'graphs', $graphs);
|
||||||
|
|
||||||
$this->tpl->addTemplateData(
|
$this->tpl->addTemplateData(
|
||||||
$tpl_id,
|
$tpl_id,
|
||||||
array(
|
array(
|
||||||
'label_server' => psm_get_lang('servers', 'server'),
|
'label_server' => psm_get_lang('servers', 'server'),
|
||||||
'label_latency_avg' => psm_get_lang('servers', 'latency_avg'),
|
|
||||||
'day_format' => psm_get_lang('servers', 'chart_day_format'),
|
'day_format' => psm_get_lang('servers', 'chart_day_format'),
|
||||||
'long_date_format' => psm_get_lang('servers', 'chart_long_date_format'),
|
'long_date_format' => psm_get_lang('servers', 'chart_long_date_format'),
|
||||||
'short_date_format' => psm_get_lang('servers', 'chart_short_date_format'),
|
'short_date_format' => psm_get_lang('servers', 'chart_short_date_format'),
|
||||||
|
@ -86,55 +105,26 @@ class HistoryGraph {
|
||||||
* @param int $server_id
|
* @param int $server_id
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
protected function generateGraphUptime($server_id) {
|
public function generateGraphUptime($server_id) {
|
||||||
$uptimes = $this->db->select(PSM_DB_PREFIX.'servers_uptime' , array('server_id' => $server_id), null, '', 'date');
|
$lines = array(
|
||||||
$last_date = 0;
|
'latency' => array(),
|
||||||
$latency_avg = 0;
|
|
||||||
// Create the list of points and server down zones
|
|
||||||
$line = array();
|
|
||||||
$lines = array();
|
|
||||||
$down = array();
|
|
||||||
foreach ($uptimes as $uptime) {
|
|
||||||
$latency_avg += (float) $uptime['latency'];
|
|
||||||
|
|
||||||
$time = strtotime($uptime['date']) * 1000;
|
|
||||||
if($uptime['status']) {
|
|
||||||
// The server is up
|
|
||||||
$line[] = '[' . $time . ',' . round((float)$uptime['latency'], 4) . ']';
|
|
||||||
if($last_date) {
|
|
||||||
// Was down before.
|
|
||||||
// Record the first and last date as a string in the down array
|
|
||||||
$down[] = '[' . $last_date . ',' . $time . ']';
|
|
||||||
$last_date = 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
if(!$last_date) {
|
|
||||||
$last_date = $time;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if(!empty($line)) {
|
|
||||||
$lines[] = '[' . implode(',', $line) . ']';
|
|
||||||
}
|
|
||||||
if($last_date) {
|
|
||||||
$down[] = '[' . $last_date . ',0]';
|
|
||||||
}
|
|
||||||
$buttons = array();
|
|
||||||
$buttons[] = array('mode' => 'hour', 'label' => psm_get_lang('servers', 'hour'), 'class_active' => 'btn-info');
|
|
||||||
$buttons[] = array('mode' => 'day', 'label' => psm_get_lang('servers', 'day'));
|
|
||||||
$buttons[] = array('mode' => 'week', 'label' => psm_get_lang('servers', 'week'));
|
|
||||||
|
|
||||||
$data = array(
|
|
||||||
'title' => psm_get_lang('servers', 'chart_last_week'),
|
|
||||||
'latency_avg' => count($uptimes) > 0 ? round(($latency_avg / count($uptimes)), 4) : 0,
|
|
||||||
'server_lines' => sizeof($lines) ? '[' . implode(',', $lines) . ']' : '',
|
|
||||||
'server_down' => sizeof($down) ? '[' . implode(',', $down) . ']' : '',
|
|
||||||
'series' => "[{label: '".psm_get_lang('servers', 'latency')."'}]",
|
|
||||||
'plotmode' => 'hour',
|
|
||||||
'buttons' => $buttons,
|
|
||||||
'chart_id' => $server_id . '_uptime',
|
|
||||||
);
|
);
|
||||||
|
$cb_if_up = function($uptime_record) {
|
||||||
|
return ($uptime_record['status'] == 1);
|
||||||
|
};
|
||||||
|
$records = $this->getRecords('uptime', $server_id);
|
||||||
|
|
||||||
|
$data = $this->generateGraphLines($records, $lines, $cb_if_up, 'latency', true);
|
||||||
|
|
||||||
|
$data['title'] = psm_get_lang('servers', 'chart_last_week');
|
||||||
|
$data['plotmode'] = 'hour';
|
||||||
|
$data['buttons'] = array();
|
||||||
|
$data['buttons'][] = array('mode' => 'hour', 'label' => psm_get_lang('servers', 'hour'), 'class_active' => 'btn-info');
|
||||||
|
$data['buttons'][] = array('mode' => 'day', 'label' => psm_get_lang('servers', 'day'));
|
||||||
|
$data['buttons'][] = array('mode' => 'week', 'label' => psm_get_lang('servers', 'week'));
|
||||||
|
// make sure to add chart id after buttons so its added to those tmeplates as well
|
||||||
|
$data['chart_id'] = $server_id . '_uptime';
|
||||||
|
|
||||||
return $data;
|
return $data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -143,41 +133,102 @@ class HistoryGraph {
|
||||||
* @param int $server_id
|
* @param int $server_id
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
protected function generateGraphHistory($server_id) {
|
public function generateGraphHistory($server_id) {
|
||||||
$uptimes = $this->db->select(PSM_DB_PREFIX.'servers_history' , array('server_id' => $server_id), null, '', 'date');
|
|
||||||
|
|
||||||
$last_date = 0;
|
|
||||||
// Create the list of points and server down zones
|
|
||||||
$lines = array(
|
$lines = array(
|
||||||
'latency_avg' => array(),
|
'latency_avg' => array(),
|
||||||
'latency_max' => array(),
|
'latency_max' => array(),
|
||||||
'latency_min' => array(),
|
'latency_min' => array(),
|
||||||
);
|
);
|
||||||
|
$server = $this->db->selectRow(PSM_DB_PREFIX.'servers', array('server_id' => $server_id), array('warning_threshold'));
|
||||||
|
|
||||||
|
$cb_if_up = function($uptime_record) use($server) {
|
||||||
|
return ($uptime_record['checks_failed'] < $server['warning_threshold']);
|
||||||
|
};
|
||||||
|
$records = $this->getRecords('history', $server_id);
|
||||||
|
|
||||||
|
// dont add uptime for now because we have no way to calculate accurate uptimes for archived records
|
||||||
|
$data = $this->generateGraphLines($records, $lines, $cb_if_up, 'latency_avg', false);
|
||||||
|
|
||||||
|
$data['title'] = psm_get_lang('servers', 'chart_history');
|
||||||
|
$data['plotmode'] = 'month';
|
||||||
|
$data['buttons'] = array();
|
||||||
|
$data['buttons'][] = array('mode' => 'week2', 'label' => psm_get_lang('servers', 'week'));
|
||||||
|
$data['buttons'][] = array('mode' => 'month', 'label' => psm_get_lang('servers', 'month'), 'class_active' => 'btn-info');
|
||||||
|
$data['buttons'][] = array('mode' => 'year', 'label' => psm_get_lang('servers', 'year'));
|
||||||
|
// make sure to add chart id after buttons so its added to those tmeplates as well
|
||||||
|
$data['chart_id'] = $server_id . '_history';
|
||||||
|
|
||||||
|
return $data;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get all uptime/history records for a server
|
||||||
|
* @param string $type
|
||||||
|
* @param int $server_id
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
protected function getRecords($type, $server_id) {
|
||||||
|
if(!in_array($type, array('history', 'uptime'))) {
|
||||||
|
return array();
|
||||||
|
}
|
||||||
|
$records = $this->db->select(PSM_DB_PREFIX.'servers_'.$type , array('server_id' => $server_id), null, '', 'date');
|
||||||
|
return $records;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generate data arrays for graphs
|
||||||
|
* @param array $records all uptime records to parse
|
||||||
|
* @param array $lines array with keys as line ids to prepare (key must be available in uptime records)
|
||||||
|
* @param callable $cb_if_up function to check if the server is up or down
|
||||||
|
* @param string $latency_avg_key which key from uptime records to use for calculating averages
|
||||||
|
* @param boolean $add_uptime add uptime calculation?
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
protected function generateGraphLines($records, $lines, callable $cb_if_up, $latency_avg_key, $add_uptime = false) {
|
||||||
|
$data = array();
|
||||||
|
|
||||||
|
// PLEASE NOTE: all times are in microseconds! because of javascript.
|
||||||
|
$last_date = 0;
|
||||||
$latency_avg = 0;
|
$latency_avg = 0;
|
||||||
$series = array();
|
$series = array();
|
||||||
|
// lowest timestamp of the graph (start time)
|
||||||
|
$time_start = 0;
|
||||||
|
// highest timestamp of the graph (end time)
|
||||||
$time_end = 0;
|
$time_end = 0;
|
||||||
|
// number of microseconds of downtime
|
||||||
|
$time_down = 0;
|
||||||
|
|
||||||
$down = array();
|
$down = array();
|
||||||
foreach ($uptimes as $uptime) {
|
|
||||||
|
// Create the list of points and server down zones
|
||||||
|
foreach ($records as $i => $uptime) {
|
||||||
$time = strtotime($uptime['date']) * 1000;
|
$time = strtotime($uptime['date']) * 1000;
|
||||||
// keep track of highest timestamp to use as end-date for graphs
|
|
||||||
|
// keep track of lowest and highest timestamp to use as end-date for graphs
|
||||||
|
// and for calculating uptime
|
||||||
|
if($i == 0 || $time < $time_start) {
|
||||||
|
$time_start = $time;
|
||||||
|
}
|
||||||
if($time > $time_end) {
|
if($time > $time_end) {
|
||||||
$time_end = $time;
|
$time_end = $time;
|
||||||
}
|
}
|
||||||
$latency_avg += (float) $uptime['latency_avg'];
|
// use the first line to calculate average latency
|
||||||
|
$latency_avg += (float) $uptime[$latency_avg_key];
|
||||||
|
|
||||||
if($uptime['checks_failed'] == 0) {
|
if($cb_if_up($uptime)) {
|
||||||
// The server is up
|
// The server is up
|
||||||
foreach($lines as $key => &$value) {
|
foreach($lines as $key => &$value) {
|
||||||
// add the value for each of the different lines
|
// add the value for each of the different lines
|
||||||
if(isset($uptime[$key])) {
|
if(isset($uptime[$key])) {
|
||||||
$value[] = '[' . $time . ',' . round((float)$uptime[$key], 4) . ']';
|
$value[] = '[' . $time . ',' . round((float) $uptime[$key], 4) . ']';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if($last_date) {
|
if($last_date) {
|
||||||
// Was down before.
|
// Was down before.
|
||||||
// Record the first and last date as a string in the down array
|
// Record the first and last date as a string in the down array
|
||||||
$down[] = '[' . $last_date . ',' . $time . ']';
|
$down[] = '[' . $last_date . ',' . $time . ']';
|
||||||
|
// add the number of microseconds of downtime to counter for %
|
||||||
|
$time_down += ($time - $last_date);
|
||||||
$last_date = 0;
|
$last_date = 0;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -199,23 +250,17 @@ class HistoryGraph {
|
||||||
if($last_date) {
|
if($last_date) {
|
||||||
$down[] = '[' . $last_date . ',0]';
|
$down[] = '[' . $last_date . ',0]';
|
||||||
}
|
}
|
||||||
$buttons = array();
|
|
||||||
$buttons[] = array('mode' => 'week2', 'label' => psm_get_lang('servers', 'week'));
|
|
||||||
$buttons[] = array('mode' => 'month', 'label' => psm_get_lang('servers', 'month'), 'class_active' => 'btn-info');
|
|
||||||
$buttons[] = array('mode' => 'year', 'label' => psm_get_lang('servers', 'year'));
|
|
||||||
|
|
||||||
$data = array(
|
if($add_uptime && $time_end > $time_start) {
|
||||||
'title' => psm_get_lang('servers', 'chart_history'),
|
$data['uptime'] = 100 - (($time_down / ($time_end - $time_start)) * 100);
|
||||||
'latency_avg' => count($uptimes) > 0 ? round(($latency_avg / count($uptimes)), 4) : 0,
|
}
|
||||||
'server_lines' => sizeof($lines_merged) ? '[' . implode(',', $lines_merged) . ']' : '',
|
|
||||||
'server_down' => sizeof($down) ? '[' . implode(',', $down) . ']' : '',
|
$data['latency_avg'] = count($records) > 0 ? ($latency_avg / count($records)) : 0;
|
||||||
'series' => sizeof($series) ? '[' . implode(',', $series) . ']' : '',
|
$data['server_lines'] = sizeof($lines_merged) ? '[' . implode(',', $lines_merged) . ']' : '';
|
||||||
'plotmode' => 'month',
|
$data['server_down'] = sizeof($down) ? '[' . implode(',', $down) . ']' : '';
|
||||||
'end_timestamp' => $time_end ? $time_end : '',
|
$data['series'] = sizeof($series) ? '[' . implode(',', $series) . ']' : '';
|
||||||
'buttons' => $buttons,
|
$data['end_timestamp'] = $time_end ? $time_end : '';
|
||||||
// make sure to add chart id after buttons so its added to those tmeplates as well
|
|
||||||
'chart_id' => $server_id . '_history',
|
|
||||||
);
|
|
||||||
return $data;
|
return $data;
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -28,12 +28,13 @@
|
||||||
<div class="info-content">
|
<div class="info-content">
|
||||||
<div class="info-dropdown btn-group">
|
<div class="info-dropdown btn-group">
|
||||||
<button class="btn dropdown-toggle" data-toggle="dropdown"><i class="icon-info-sign"></i></button>
|
<button class="btn dropdown-toggle" data-toggle="dropdown"><i class="icon-info-sign"></i></button>
|
||||||
<ul class="dropdown-menu">
|
<!--%tpl_repeat_info-->
|
||||||
<li><span>{label_latency_avg}: {latency_avg}</span></li>
|
<li><span>{label}: {value}</span></li>
|
||||||
</ul>
|
<!--%%tpl_repeat_info-->
|
||||||
|
<ul class="dropdown-menu">{info}</ul>
|
||||||
</div>
|
</div>
|
||||||
<div class="server-info">
|
<div class="server-info">
|
||||||
{label_latency_avg}: {latency_avg}<br/>
|
<ul>{info}</ul>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="chart-selector">
|
<div class="chart-selector">
|
||||||
|
|
|
@ -39,6 +39,10 @@
|
||||||
padding-top: 40px;
|
padding-top: 40px;
|
||||||
height: 200px;
|
height: 200px;
|
||||||
}
|
}
|
||||||
|
.server-info ul {
|
||||||
|
list-style: none;
|
||||||
|
margin: 0;
|
||||||
|
}
|
||||||
|
|
||||||
.chart-selector {
|
.chart-selector {
|
||||||
}
|
}
|
||||||
|
@ -47,11 +51,11 @@
|
||||||
.chart-container {
|
.chart-container {
|
||||||
margin-right: 0;
|
margin-right: 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
.chart-content {
|
.chart-content {
|
||||||
margin-right: 0;
|
margin-right: 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
.info-container {
|
.info-container {
|
||||||
float: none;
|
float: none;
|
||||||
clear: both;
|
clear: both;
|
||||||
|
@ -62,11 +66,11 @@
|
||||||
display: block;
|
display: block;
|
||||||
float: left;
|
float: left;
|
||||||
}
|
}
|
||||||
|
|
||||||
.server-info {
|
.server-info {
|
||||||
display: none;
|
display: none;
|
||||||
}
|
}
|
||||||
|
|
||||||
.chart-selector {
|
.chart-selector {
|
||||||
float: right;
|
float: right;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue