Skip to content

Commit

Permalink
Prototyping alert history
Browse files Browse the repository at this point in the history
refs #4074
  • Loading branch information
mxhash committed Oct 8, 2014
1 parent f77a0e9 commit 87a8765
Show file tree
Hide file tree
Showing 5 changed files with 284 additions and 0 deletions.
219 changes: 219 additions & 0 deletions modules/monitoring/application/controllers/AlertsummaryController.php
@@ -0,0 +1,219 @@
<?php
// {{{ICINGA_LICENSE_HEADER}}}
// {{{ICINGA_LICENSE_HEADER}}}

use Icinga\Chart\GridChart;
use Icinga\Chart\Unit\StaticAxis;
use Icinga\Module\Monitoring\Controller;
use Icinga\Module\Monitoring\Web\Widget\SelectBox;

class Monitoring_AlertsummaryController extends Controller
{
public function indexAction()
{
$this->view->intervalBox = $this->createIntervalBox();
$this->view->recentAlerts = $this->createRecentAlerts();
$this->view->interval = $this->getInterval();
}

public function defectimageAction()
{
$gridChart = new GridChart();
$interval = $this->getInterval();

$gridChart->alignTopLeft();
$gridChart->setAxisLabel('', t('Services'))
->setXAxis(new StaticAxis())
->setAxisMin(null, 0)
->setYAxis(new \Icinga\Chart\Unit\LinearUnit(10));

$query = $this->backend->select()->from('notification', array(
'host',
'service',
'notification_output',
'notification_contact',
'notification_start_time',
'notification_state'
));

$query->setFilter(
new Icinga\Data\Filter\FilterExpression(
'n.start_time',
'>=',
$this->getBeginDate($interval)->format('Y-m-d H:i:s')
)
);

$query->order('notification_start_time', 'asc');


$records = $query->paginate(10000);
$data = array();
$defects = array();
$period = $this->createPeriod($interval);

foreach ($period as $entry) {
$id = $this->getPeriodFormat($interval, $entry->getTimestamp());
$data[$id] = array($id, 0);
$defects[$id] = array($id, 0);
}

foreach ($records as $item) {
$id = $this->getPeriodFormat($interval, $item->notification_start_time);
if (empty($data[$id])) {
$data[$id] = array($id, 0);
}

$data[$id][1]++;
}

$gridChart->drawBars(
array(
'label' => $this->translate('Notifications'),
'color' => 'green',
'data' => $data,
'showPoints' => true
)
);

$query = null;
$records = null;
$item = null;

$query = $this->backend->select()->from('eventhistory', array(
'host_name',
'service_description',
'object_type',
'timestamp',
'state',
'attempt',
'max_attempts',
'output',
'type',
'host',
'service',
'service_host_name'
));

$query->addFilter(
new Icinga\Data\Filter\FilterExpression(
'timestamp',
'>=',
$this->getBeginDate($interval)->getTimestamp()
)
);

$query->addFilter(
new Icinga\Data\Filter\FilterExpression(
'state',
'>',
0
)
);

$records = $query->paginate(10000);

foreach ($records as $item) {
$id = $this->getPeriodFormat($interval, $item->timestamp);
if (empty($data[$id])) {
$defects[$id] = array($id, 0);
}

$defects[$id][1]++;
}

$gridChart->drawLines(
array(
'label' => $this->translate('Defects'),
'color' => 'red',
'data' => $defects,
'showPoints' => true
)
);

$this->view->chart = $gridChart;
}

private function createRecentAlerts()
{
$query = $this->backend->select()->from('notification', array(
'host',
'service',
'notification_output',
'notification_contact',
'notification_start_time',
'notification_state'
));

$query->order('notification_start_time', 'desc');

return $query->paginate(5);
}

private function createIntervalBox()
{
$box = new SelectBox(
'intervalBox',
array(
'1d' => t('One day'),
'1w' => t('One week'),
'1m' => t('One month'),
'1y' => t('One year')
),
t('Report interval'),
'interval'
);
$box->applyRequest($this->getRequest());
return $box;
}

private function getPeriodFormat($interval, $timestamp)
{
$format = '';
if ($interval === '1d') {
$format = '%H:00:00';
} elseif ($interval === '1w') {
$format = '%Y-%m-%d';
} elseif ($interval === '1m') {
$format = '%Y-%m-%d';
} elseif ($interval === '1y') {
$format = '%Y-%m';
}

return strftime($format, $timestamp);
}

private function createPeriod($interval)
{
if ($interval === '1d') {
return new DatePeriod($this->getBeginDate($interval), new DateInterval('PT1H'), 24);
} elseif ($interval === '1w') {
return new DatePeriod($this->getBeginDate($interval), new DateInterval('P1D'), 7);
} elseif ($interval === '1m') {
return new DatePeriod($this->getBeginDate($interval), new DateInterval('P1D'), 30);
} elseif ($interval === '1y') {
return new DatePeriod($this->getBeginDate($interval), new DateInterval('P1M'), 12);
}
}

private function getBeginDate($interval)
{
$new = new DateTime();
if ($interval === '1d') {
return $new->sub(new DateInterval('P1D'));
} elseif ($interval === '1w') {
return $new->sub(new DateInterval('P1W'));
} elseif ($interval === '1m') {
return $new->sub(new DateInterval('P1M'));
} elseif ($interval === '1y') {
return $new->sub(new DateInterval('P1Y'));
}

return null;
}

private function getInterval()
{
return $this->getParam('interval', '1d');
}
}
Expand Up @@ -22,6 +22,15 @@ public function monitoringState($object, $type = 'service')
}
}

public function monitoringStateById($id, $type = 'service')
{
if ($type === 'service') {
return $this->servicestates[$id];
} elseif ($type === 'host') {
return $this->hoststates[$id];
}
}

/**
* @deprecated Monitoring colors are clustered.
*/
Expand Down
@@ -0,0 +1,7 @@
<?php if (! $this->compact) { ?>
<div class="svg-container-responsive">
<?= $chart->render(); ?>
</div>
<?php } else { ?>
<?= $chart->render(); ?>
<?php } ?>
@@ -0,0 +1,37 @@
<?php
$helper = $this->getHelper('MonitoringState');
?>
<div class="controls">
<div style="margin: 1em;" class="dontprint">
<?= $intervalBox; ?>
</div>
</div>

<div class="content alertsummary">
<h1>Alert summary</h1>

<h2>Notifications</h2>
<div>
<?= $this->action('defectimage', 'alertsummary', 'monitoring', array('interval' => $this->interval)); ?>
</div>

<?php if ($this->recentAlerts): ?>
<h2><?= $this->translate('Top 5 recent alerts'); ?></h2>
<table>
<tr>
<th><?= $this->translate('Host'); ?></th>
<th><?= $this->translate('Service'); ?></th>
<th><?= $this->translate('State'); ?></th>
<th><?= $this->translate('Timestamp'); ?></th>
</tr>
<?php foreach ($this->recentAlerts as $alert): ?>
<tr>
<td><?= $alert->host; ?></td>
<td><?= $alert->service; ?></td>
<td><?= ucfirst($helper->monitoringStateById($alert->notification_state, $alert->service ? 'service' : 'host')) ?></td>
<td><?= $this->prefixedTimeSince($alert->notification_start_time, true) ?></td>
</tr>
<?php endforeach; ?>
</table>
<?php endif; ?>
</div>
12 changes: 12 additions & 0 deletions modules/monitoring/configuration.php
Expand Up @@ -122,6 +122,18 @@
));
$section->add($this->translate('Timeline'))->setUrl('monitoring/timeline');

/*
* Reporting Section
*/
$section = $this->menuSection($this->translate('Reporting'), array(
'icon' => 'img/icons/hostgroup.png',
'priority' => 100
));

$section->add($this->translate('Alert Summary'), array(
'url' => 'monitoring/alertsummary/index'
));

/*
* System Section
*/
Expand Down

0 comments on commit 87a8765

Please sign in to comment.