Skip to content

Commit

Permalink
Turn prepareStateName into more generic string utility function
Browse files Browse the repository at this point in the history
refs #8565
  • Loading branch information
majentsch committed Mar 31, 2015
1 parent 95a83a4 commit b0b0ae1
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 32 deletions.
41 changes: 41 additions & 0 deletions library/Icinga/Util/String.php
Expand Up @@ -79,4 +79,45 @@ public static function findSimilar($string, array $possibilities, $similarity =

return $matches;
}

/**
* Generates an array of strings that constitutes the cartesian product of all passed sets, with all
* string combinations concatenated using the passed join-operator.
*
* <pre>
* cartesianProduct(
* array(array('foo', 'bar'), array('mumble', 'grumble', null)),
* '_'
* );
* => array('foo_mumble', 'foo_grumble', 'bar_mumble', 'bar_grumble', 'foo', 'bar')
* </pre>
*
* @param array $sets An array of arrays containing all sets for which the cartesian
* product should be calculated.
* @param string $glue The glue used to join the strings, defaults to ''.
*
* @returns array The cartesian product in one array of strings.
*/
public static function cartesianProduct(array $sets, $glue = '')
{
$product = null;
foreach ($sets as $set) {
if (! isset($product)) {
$product = $set;
} else {
$newProduct = array();
foreach ($product as $strA) {
foreach ($set as $strB) {
if ($strB === null) {
$newProduct []= $strA;
} else {
$newProduct []= $strA . $glue . $strB;
}
}
}
$product = $newProduct;
}
}
return $product;
}
}
32 changes: 25 additions & 7 deletions modules/monitoring/library/Monitoring/Object/HostList.php
Expand Up @@ -3,6 +3,8 @@

namespace Icinga\Module\Monitoring\Object;

use Icinga\Util\String;

/**
* A host list
*/
Expand Down Expand Up @@ -33,13 +35,7 @@ protected function fetchObjects()
*/
public function getStateSummary()
{
$hostStates = $this->prepareStateNames('hosts_', array(
Host::getStateText(Host::STATE_UP),
Host::getStateText(Host::STATE_DOWN),
Host::getStateText(Host::STATE_UNREACHABLE),
Host::getStateText(Host::STATE_PENDING)
));

$hostStates = array_fill_keys(self::getHostStatesSummaryEmpty(), 0);
foreach ($this as $host) {
$unhandled = (bool) $host->problem === true && (bool) $host->handled === false;

Expand All @@ -52,4 +48,26 @@ public function getStateSummary()

return (object)$hostStates;
}

/**
* Return an empty array with all possible host state names
*
* @return array An array containing all possible host states as keys and 0 as values.
*/
public static function getHostStatesSummaryEmpty()
{
return String::cartesianProduct(
array(
array('hosts'),
array(
Host::getStateText(Host::STATE_UP),
Host::getStateText(Host::STATE_DOWN),
Host::getStateText(Host::STATE_UNREACHABLE),
Host::getStateText(Host::STATE_PENDING)
),
array(null, 'handled', 'unhandled')
),
'_'
);
}
}
11 changes: 0 additions & 11 deletions modules/monitoring/library/Monitoring/Object/ObjectList.php
Expand Up @@ -118,15 +118,4 @@ public function getUnhandledObjects()
}
return $unhandledObjects;
}

protected function prepareStateNames($prefix, array $names) {
$new = array();
foreach ($names as $name) {
$new[$prefix . $name] = 0;
$new[$prefix . $name . '_handled'] = 0;
$new[$prefix . $name . '_unhandled'] = 0;
}
$new[$prefix . 'total'] = 0;
return $new;
}
}
41 changes: 27 additions & 14 deletions modules/monitoring/library/Monitoring/Object/ServiceList.php
Expand Up @@ -3,6 +3,8 @@

namespace Icinga\Module\Monitoring\Object;

use Icinga\Util\String;

/**
* A service list
*/
Expand Down Expand Up @@ -33,20 +35,8 @@ protected function fetchObjects()
*/
public function getStateSummary()
{
$serviceStates = $this->prepareStateNames('services_', array(
Service::getStateText(Service::STATE_OK),
Service::getStateText(Service::STATE_WARNING),
Service::getStateText(Service::STATE_CRITICAL),
Service::getStateText(Service::STATE_UNKNOWN),
Service::getStateText(Service::STATE_PENDING),
));

$hostStates = $this->prepareStateNames('hosts_', array(
Host::getStateText(Host::STATE_UP),
Host::getStateText(Host::STATE_DOWN),
Host::getStateText(Host::STATE_UNREACHABLE),
Host::getStateText(Host::STATE_PENDING),
));
$serviceStates = array_fill_keys(self::getServiceStatesSummaryEmpty(), 0);
$hostStates = array_fill_keys(HostList::getHostStatesSummaryEmpty(), 0);

foreach ($this as $service) {
$unhandled = false;
Expand All @@ -67,4 +57,27 @@ public function getStateSummary()

return (object)$serviceStates;
}

/**
* Return an empty array with all possible host state names
*
* @return array An array containing all possible host states as keys and 0 as values.
*/
public static function getServiceStatesSummaryEmpty()
{
return String::cartesianProduct(
array(
array('services'),
array(
Service::getStateText(Service::STATE_OK),
Service::getStateText(Service::STATE_WARNING),
Service::getStateText(Service::STATE_CRITICAL),
Service::getStateText(Service::STATE_UNKNOWN),
Service::getStateText(Service::STATE_PENDING)
),
array(null, 'handled', 'unhandled')
),
'_'
);
}
}

0 comments on commit b0b0ae1

Please sign in to comment.