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

Add report Host Outage #45

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions library/Idoreports/HostSlaReport.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public function initConfigForm(Form $form)
]);
}

protected function fetchUptime(Timerange $timerange, array $config = null){}
protected function fetchSla(Timerange $timerange, array $config = null)
{
$rd = new ReportData();
Expand Down
186 changes: 186 additions & 0 deletions library/Idoreports/HostUptimeReport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
<?php
// Icinga IDO Reports | (c) 2018 Icinga GmbH | GPLv2

namespace Icinga\Module\Idoreports;

use Icinga\Module\Reporting\ReportData;
use Icinga\Module\Reporting\ReportRow;
use Icinga\Module\Reporting\Timerange;
use ipl\Html\Form;

class HostUptimeReport extends IdoReport
{
public function getName()
{
return 'Host Outage';
}

public function initConfigForm(Form $form)
{
$form->addElement('text', 'filter', [
'label' => 'Filter'
]);

$form->addElement('select', 'breakdown', [
'label' => 'Breakdown',
'options' => [
'none' => 'None',
'day' => 'Day',
'week' => 'Week',
'month' => 'Month'
]
]);

$form->addElement('number', 'threshold', [
'label' => 'Threshold',
'placeholder' => '99.5',
'step' => '0.01',
'min' => '1',
'max' => '100'
]);
}

private function secondsToString($seconds) {
$y = floor($seconds / 31536000);
$d = floor(($seconds % 31536000) / 86400);
$h = floor((($seconds % 31536000) % 86400) / 3600);
$i = floor(((($seconds % 31536000) % 86400) % 3600) / 60);
$s = ((($seconds % 31536000) % 86400) % 3600) % 60;
if ( $y > 0 ) return $y . " y " . $d . " d " . $h . " h " . $i . " m " . $s . " s";
if ( $d > 0 ) return $d . " d " . $h . " h " . $i . " m " . $s . " s";
if ( $h > 0 ) return $h . " h " . $i . " m " . $s . " s";
if ( $i > 0 ) return $i . " m " . $s . " s";
if ( $s > 0 ) return $s . " s";
}

protected function fetchHostUptime(Timerange $timerange, array $config)
{
$sla = $this->getBackend()->select()->from('hoststatus', ['host_display_name'])->order('host_display_name');

$this->applyFilterAndRestrictions($config['filter'] ?: '*', $sla);

/** @var \Zend_Db_Select $select */
$select = $sla->getQuery()->getSelectQuery();

$columns = $sla->getQuery()->getColumns();
$columns['outage'] = new \Zend_Db_Expr(\sprintf(
"idoreports_get_outage(%s, '%s', '%s', NULL)",
'ho.object_id',
$timerange->getStart()->format('Y-m-d H:i:s'),
$timerange->getEnd()->format('Y-m-d H:i:s')
));

$select->columns($columns);

return $this->getBackend()->getResource()->getDbAdapter()->query($select);
}

protected function fetchSla(Timerange $timerange, array $config = null)
{
$rd = new ReportData();

if (isset($config['breakdown']) && $config['breakdown'] !== 'none') {
switch ($config['breakdown']) {
case 'day':
$interval = new \DateInterval('P1D');
$format = 'Y-m-d';
break;
case 'week':
$interval = new \DateInterval('P1W');
$format = 'Y-\WW';
break;
case 'month':
$interval = new \DateInterval('P1M');
$format = 'Y-m';
break;
}

$rd
->setDimensions(['Hostname', ucfirst($config['breakdown'])])
->setValues(['SLA in %']);

$rows = [];

foreach ($this->yieldTimerange($timerange, $interval) as list($start, $end)) {
foreach ($this->fetchHostSla(new Timerange($start, $end), $config) as $row) {
$rows[] = (new ReportRow())
->setDimensions([$row->host_display_name, $start->format($format)])
->setValues([(float) $row->sla]);
}
}

$rd->setRows($rows);
} else {
$rd
->setDimensions(['Hostname'])
->setValues(['SLA in %']);

$rows = [];

foreach ($this->fetchHostSla($timerange, $config) as $row) {
$rows[] = (new ReportRow())
->setDimensions([$row->host_display_name])
->setValues([(float) $row->sla]);
}

$rd->setRows($rows);
}

return $rd;
}

protected function fetchUptime(Timerange $timerange, array $config = null)
{
$rd = new ReportData();

if (isset($config['breakdown']) && $config['breakdown'] !== 'none') {
switch ($config['breakdown']) {
case 'day':
$interval = new \DateInterval('P1D');
$format = 'Y-m-d';
break;
case 'week':
$interval = new \DateInterval('P1W');
$format = 'Y-\WW';
break;
case 'month':
$interval = new \DateInterval('P1M');
$format = 'Y-m';
break;
}

$rd
->setDimensions(['Hostname', ucfirst($config['breakdown'])])
->setValues(['Outage']);

$rows = [];

foreach ($this->yieldTimerange($timerange, $interval) as list($start, $end)) {
foreach ($this->fetchHostUptime(new Timerange($start, $end), $config) as $row) {
//var_dump($row);
$rows[] = (new ReportRow())
->setDimensions([$row->host_display_name, $start->format($format)])
->setValues([$this->secondsToString((int) $row->outage)]);
}
}

$rd->setRows($rows);
} else {
$rd
->setDimensions(['Hostname'])
->setValues(['Outage']);

$rows = [];

foreach ($this->fetchHostUptime($timerange, $config) as $row) {
$rows[] = (new ReportRow())
->setDimensions([$row->host_display_name])
->setValues([(int) $row->outage]);
}

$rd->setRows($rows);
}

return $rd;
}
}
45 changes: 32 additions & 13 deletions library/Idoreports/IdoReport.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ public function getData(Timerange $timerange, array $config = null)

public function getHtml(Timerange $timerange, array $config = null)
{
$data = $this->fetchSla($timerange, $config);

$data = [];
$data[] = $this->fetchSla($timerange, $config);
$data[] = $this->fetchUptime($timerange, $config);

if (! count($data)) {
return Html::tag('p', 'No data found.');
}
Expand All @@ -36,50 +38,66 @@ public function getHtml(Timerange $timerange, array $config = null)

$tableHeaderCells = [];

foreach ($data->getDimensions() as $dimension) {
foreach ($data[0]->getDimensions() as $dimension) {
$tableHeaderCells[] = Html::tag('th', null, $dimension);
}

foreach ($data->getValues() as $value) {
if(isset($data[1])){
foreach ($data[1]->getValues() as $value) {
$tableHeaderCells[] = Html::tag('th', null, $value);
}
}

foreach ($data[0]->getValues() as $value) {
$tableHeaderCells[] = Html::tag('th', null, $value);
}

$tableRows = [];

foreach ($data->getRows() as $row) {
$tableRows = [];
foreach ($data[0]->getRows() as $idx => $row) {
$cells = [];

foreach ($row->getDimensions() as $dimension) {
$cells[] = Html::tag('td', null, $dimension);
}

// We only have one metric
$sla = $row->getValues()[0];
$sla = $row->getValues()[0];

if ($sla < $threshold) {
$slaClass = 'nok';
} else {
$slaClass = 'ok';
}

if (isset($data[1])){
$outage = $data[1]->getRows()[$idx]->getValues()[0];
$cells[] = Html::tag('td', $outage);
}
$cells[] = Html::tag('td', ['class' => "sla-column $slaClass"], \round($sla, 2));

$tableRows[] = Html::tag('tr', null, $cells);
}

// We only have one average
$average = $data->getAverages()[0];
$average = $data[0]->getAverages()[0];

if ($average < $threshold) {
$slaClass = 'nok';
} else {
$slaClass = 'ok';
}

$tableRows[] = Html::tag('tr', null, [
Html::tag('td', ['colspan' => count($data->getDimensions())], 'Total'),
Html::tag('td', ['class' => "sla-column $slaClass"], \round($average, 2))
]);
if(isset($data[1])){
$tableRows[] = Html::tag('tr', null, [
Html::tag('td', ['colspan' => count($data[0]->getDimensions())+1], 'Total'),
Html::tag('td', ['class' => "sla-column $slaClass"], \round($average, 2)),
]);
}else{
$tableRows[] = Html::tag('tr', null, [
Html::tag('td', ['colspan' => count($data[0]->getDimensions())], 'Total'),
Html::tag('td', ['class' => "sla-column $slaClass"], \round($average, 2)),
]);
}

$table = Html::tag(
'table',
Expand Down Expand Up @@ -108,6 +126,7 @@ public function getHtml(Timerange $timerange, array $config = null)
* @return ReportData
*/
abstract protected function fetchSla(Timerange $timerange, array $config = null);
abstract protected function fetchUptime(Timerange $timerange, array $config = null);

protected function applyFilterAndRestrictions($filter, Filterable $filterable)
{
Expand Down
1 change: 1 addition & 0 deletions library/Idoreports/ServiceSlaReport.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public function initConfigForm(Form $form)
]);
}

protected function fetchUptime(Timerange $timerange, array $config = null){}
protected function fetchSla(Timerange $timerange, array $config = null)
{
$rd = new ReportData();
Expand Down
Loading