Skip to content

Commit

Permalink
Merge branch 'master' into feature/setup-wizard-7163
Browse files Browse the repository at this point in the history
  • Loading branch information
Johannes Meyer committed Nov 12, 2014
2 parents c3e28a4 + 1cf32dd commit 7aae291
Show file tree
Hide file tree
Showing 29 changed files with 602 additions and 568 deletions.
3 changes: 1 addition & 2 deletions library/Icinga/Application/Cli.php
Expand Up @@ -53,8 +53,7 @@ protected function setupLogging()
new Config(
array(
'level' => Logger::INFO,
'log' => 'file',
'file' => 'php://stderr'
'log' => 'stdout',
)
)
);
Expand Down
2 changes: 1 addition & 1 deletion library/Icinga/Application/Config.php
Expand Up @@ -378,7 +378,7 @@ public function fromSection($section, $key, $default = null)
);
}

if ($default !== null) {
if ($value === null && $default !== null) {
$value = $default;
}

Expand Down
10 changes: 9 additions & 1 deletion library/Icinga/Application/Logger/LogWriter.php
Expand Up @@ -11,10 +11,18 @@
*/
abstract class LogWriter
{
/**
* @var Zend_Config
*/
protected $config;

/**
* Create a new log writer initialized with the given configuration
*/
abstract public function __construct(Config $config);
public function __construct(Config $config)
{
$this->config = $config;
}

/**
* Log a message with the given severity
Expand Down
50 changes: 50 additions & 0 deletions library/Icinga/Application/Logger/Writer/StdoutWriter.php
@@ -0,0 +1,50 @@
<?php

namespace Icinga\Application\Logger\Writer;

use Icinga\Cli\Screen;
use Icinga\Application\Logger\Logger;
use Icinga\Application\Logger\LogWriter;
use Zend_Config;

/**
* Class to write log messages to STDOUT
*/
class StdoutWriter extends LogWriter
{
protected $screen;

protected function screen()
{
if ($this->screen === null) {
$this->screen = Screen::instance();
}
return $this->screen;
}

/**
* Log a message with the given severity
*
* @param int $severity The severity to use
* @param string $message The message to log
*/
public function log($severity, $message)
{
$color = 'black';
switch ($severity) {
case Logger::$ERROR:
$color = 'red';
break;
case Logger::$WARNING:
$color = 'orange';
break;
case Logger::$INFO:
$color = 'green';
break;
case Logger::$DEBUG:
$color = 'blue';
break;
}
file_put_contents('php://stderr', $this->screen()->colorize($message, $color) . "\n");
}
}
5 changes: 4 additions & 1 deletion library/Icinga/Data/Db/DbConnection.php
Expand Up @@ -216,7 +216,10 @@ public function fetchAll(DbQuery $query)
*/
public function fetchRow(DbQuery $query)
{
return $this->dbAdapter->fetchRow($query->getSelectQuery());
Benchmark::measure('DB is fetching row');
$result = $this->dbAdapter->fetchRow($query->getSelectQuery());
Benchmark::measure('DB row done');
return $result;
}

/**
Expand Down
15 changes: 10 additions & 5 deletions library/Icinga/File/Ini/IniEditor.php
Expand Up @@ -433,6 +433,9 @@ private function formatKeyValuePair(array $key, $value)
*/
private function formatKey(array $key)
{
foreach ($key as $i => $separator) {
$key[$i] = $this->sanitize($separator);
}
return implode($this->nestSeparator, $key);
}

Expand Down Expand Up @@ -599,7 +602,7 @@ private function removeFromArray($array, $pos)
}

/**
* Prepare a value for INe
* Prepare a value for INI
*
* @param $value The value of the string
*
Expand All @@ -613,10 +616,12 @@ private function formatValue($value)
return $value;
} elseif (is_bool($value)) {
return ($value ? 'true' : 'false');
} elseif (strpos($value, '"') === false) {
return '"' . $value . '"';
} else {
return '"' . str_replace('"', '\"', $value) . '"';
}
return '"' . str_replace('"', '\"', $this->sanitize($value)) . '"';
}

private function sanitize($value)
{
return str_replace('\n', '', $value);
}
}
49 changes: 0 additions & 49 deletions library/Icinga/File/Ini/IniWriter.php
Expand Up @@ -52,45 +52,6 @@ public function __construct(array $options = null)
parent::__construct($options);
}

/**
* Find all keys containing dots and convert it to a nested configuration
*
* Ensure that configurations with the same ini representation the have
* similarly nested Zend_Config objects. The configuration may be altered
* during that process.
*
* @param Zend_Config $config The configuration to normalize
* @return Zend_Config The normalized config
*/
private function normalizeKeys(Zend_Config $config)
{
foreach ($config as $key => $value) {
if (preg_match('/\./', $key) > 0) {
// remove old key
unset ($config->$key);

// insert new key
$nests = explode('.', $key);
$current = $config;
$i = 0;
for (; $i < count($nests) - 1; $i++) {
if (! isset($current->{$nests[$i]})) {
// configuration key doesn't exist, create a new nesting level
$current->{$nests[$i]} = new Zend_Config (array(), true);
}
// move to next nesting level
$current = $current->{$nests[$i]};
}
// reached last nesting level, insert value
$current->{$nests[$i]} = $value;
}
if ($value instanceof Zend_Config) {
$config->$key = $this->normalizeKeys ($value);
}
}
return $config;
}

/**
* Render the Zend_Config into a config file string
*
Expand All @@ -104,16 +65,6 @@ public function render()
$oldconfig = new Zend_Config(array());
}

// create an internal copy of the given configuration, since the user of this class
// won't expect that a configuration will ever be altered during
// the rendering process.
$extends = $this->_config->getExtends();
$this->_config = new Zend_Config ($this->_config->toArray(), true);
foreach ($extends as $extending => $extended) {
$this->_config->setExtend($extending, $extended);
}
$this->_config = $this->normalizeKeys($this->_config);

$newconfig = $this->_config;
$editor = new IniEditor(@file_get_contents($this->_filename), $this->options);
$this->diffConfigs($oldconfig, $newconfig, $editor);
Expand Down
1 change: 1 addition & 0 deletions library/Icinga/Util/Translator.php
Expand Up @@ -224,6 +224,7 @@ public static function getAvailableLocaleCodes()
}
}
}
sort($codes);

return $codes;
}
Expand Down
90 changes: 90 additions & 0 deletions library/Icinga/Web/Menu/MonitoringMenuItemRenderer.php
@@ -0,0 +1,90 @@
<?php
// {{{ICINGA_LICENSE_HEADER}}}
// {{{ICINGA_LICENSE_HEADER}}}

namespace Icinga\Web\Menu;

use Icinga\Module\Monitoring\Backend\MonitoringBackend;
use Icinga\Web\Menu;
use Icinga\Web\Url;

class MonitoringMenuItemRenderer implements MenuItemRenderer {

protected static $summary;

protected $columns = array();

protected static function summary($column = null)
{
if (self::$summary === null) {
self::$summary = MonitoringBackend::instance()->select()->from(
'statusSummary',
array(
'hosts_down_unhandled',
'services_critical_unhandled'
)
)->getQuery()->fetchRow();
}

if ($column === null) {
return self::$summary;
} elseif (isset(self::$summary->$column)) {
return self::$summary->$column;
} else {
return null;
}
}

protected function getBadgeTitle()
{
$translations = array(
'hosts_down_unhandled' => mt('monitoring', '%d unhandled hosts down'),
'services_critical_unhandled' => mt('monitoring', '%d unhandled services critical')
);

$titles = array();
$sum = $this->summary();

foreach ($this->columns as $col) {
if (isset($sum->$col) && $sum->$col > 0) {
$titles[] = sprintf($translations[$col], $sum->$col);
}
}

return implode(', ', $titles);
}

protected function countItems()
{
$sum = self::summary();
$count = 0;

foreach ($this->columns as $col) {
if (isset($sum->$col)) {
$count += $sum->$col;
}
}

return $count;
}

public function render(Menu $menu)
{
$count = $this->countItems();
$badge = '';
if ($count) {
$badge = sprintf(
'<div title="%s" class="badge-container"><span class="badge badge-critical">%s</span></div>',
$this->getBadgeTitle(),
$count
);
}
return sprintf(
'<a href="%s">%s%s%s</a>',
$menu->getUrl() ?: '#',
$menu->getIcon() ? '<img src="' . Url::fromPath($menu->getIcon()) . '" class="icon" /> ' : '',
htmlspecialchars($menu->getTitle()),
$badge
);
}
}
40 changes: 6 additions & 34 deletions library/Icinga/Web/Menu/ProblemMenuItemRenderer.php
@@ -1,39 +1,11 @@
<?php
// {{{ICINGA_LICENSE_HEADER}}}
// {{{ICINGA_LICENSE_HEADER}}}

namespace Icinga\Web\Menu;

use Icinga\Module\Monitoring\Backend;
use Icinga\Web\Menu;
use Icinga\Web\Url;

class ProblemMenuItemRenderer implements MenuItemRenderer {

public function render(Menu $menu)
{
$statusSummary = Backend::createBackend()
->select()->from(
'statusSummary',
array(
'hosts_down_unhandled',
'services_critical_unhandled'
)
)->getQuery()->fetchRow();
$unhandled = $statusSummary->hosts_down_unhandled + $statusSummary->services_critical_unhandled;
$badge = '';
if ($unhandled) {
$badge = sprintf(
'<div class="badge-container"><span class="badge badge-critical">%s</span></div>',
$unhandled
);
}
return sprintf(
'<a href="%s">%s%s %s</a>',
$menu->getUrl() ?: '#',
$menu->getIcon() ? '<img src="' . Url::fromPath($menu->getIcon()) . '" class="icon" /> ' : '',
htmlspecialchars($menu->getTitle()),
$badge
);
}
class ProblemMenuItemRenderer extends MonitoringMenuItemRenderer
{
protected $columns = array(
'hosts_down_unhandled',
'services_critical_unhandled'
);
}
36 changes: 5 additions & 31 deletions library/Icinga/Web/Menu/UnhandledHostMenuItemRenderer.php
@@ -1,38 +1,12 @@
<?php
// {{{ICINGA_LICENSE_HEADER}}}
// {{{ICINGA_LICENSE_HEADER}}}

namespace Icinga\Web\Menu;

use Icinga\Module\Monitoring\Backend;
use Icinga\Web\Menu;
use Icinga\Web\Url;

class UnhandledHostMenuItemRenderer implements MenuItemRenderer {

public function render(Menu $menu)
{
$statusSummary = Backend::createBackend()
->select()->from(
'statusSummary',
array(
'hosts_down_unhandled'
)
)->getQuery()->fetchRow();
$badge = '';
if ($statusSummary->hosts_down_unhandled) {
$badge = sprintf(
'<div title="%s" class="badge-container"><span class="badge badge-critical">%s</span></div>',
t(sprintf('%d unhandled host problems', $statusSummary->hosts_down_unhandled)),
$statusSummary->hosts_down_unhandled
);
}
return sprintf(
'<a href="%s">%s%s %s</a>',
$menu->getUrl() ?: '#',
$menu->getIcon() ? '<img src="' . Url::fromPath($menu->getIcon()) . '" class="icon" /> ' : '',
htmlspecialchars($menu->getTitle()),
$badge
);
}
class UnhandledHostMenuItemRenderer extends MonitoringMenuItemRenderer
{
protected $columns = array(
'hosts_down_unhandled',
);
}

0 comments on commit 7aae291

Please sign in to comment.