Turn prepareStateName into more generic string utility function
refs #8565
This commit is contained in:
parent
95a83a41bd
commit
b0b0ae157c
|
@ -79,4 +79,45 @@ class String
|
||||||
|
|
||||||
return $matches;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,8 @@
|
||||||
|
|
||||||
namespace Icinga\Module\Monitoring\Object;
|
namespace Icinga\Module\Monitoring\Object;
|
||||||
|
|
||||||
|
use Icinga\Util\String;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A host list
|
* A host list
|
||||||
*/
|
*/
|
||||||
|
@ -33,13 +35,7 @@ class HostList extends ObjectList
|
||||||
*/
|
*/
|
||||||
public function getStateSummary()
|
public function getStateSummary()
|
||||||
{
|
{
|
||||||
$hostStates = $this->prepareStateNames('hosts_', array(
|
$hostStates = array_fill_keys(self::getHostStatesSummaryEmpty(), 0);
|
||||||
Host::getStateText(Host::STATE_UP),
|
|
||||||
Host::getStateText(Host::STATE_DOWN),
|
|
||||||
Host::getStateText(Host::STATE_UNREACHABLE),
|
|
||||||
Host::getStateText(Host::STATE_PENDING)
|
|
||||||
));
|
|
||||||
|
|
||||||
foreach ($this as $host) {
|
foreach ($this as $host) {
|
||||||
$unhandled = (bool) $host->problem === true && (bool) $host->handled === false;
|
$unhandled = (bool) $host->problem === true && (bool) $host->handled === false;
|
||||||
|
|
||||||
|
@ -52,4 +48,26 @@ class HostList extends ObjectList
|
||||||
|
|
||||||
return (object)$hostStates;
|
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')
|
||||||
|
),
|
||||||
|
'_'
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -118,15 +118,4 @@ abstract class ObjectList implements Countable, IteratorAggregate
|
||||||
}
|
}
|
||||||
return $unhandledObjects;
|
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,6 +3,8 @@
|
||||||
|
|
||||||
namespace Icinga\Module\Monitoring\Object;
|
namespace Icinga\Module\Monitoring\Object;
|
||||||
|
|
||||||
|
use Icinga\Util\String;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A service list
|
* A service list
|
||||||
*/
|
*/
|
||||||
|
@ -33,20 +35,8 @@ class ServiceList extends ObjectList
|
||||||
*/
|
*/
|
||||||
public function getStateSummary()
|
public function getStateSummary()
|
||||||
{
|
{
|
||||||
$serviceStates = $this->prepareStateNames('services_', array(
|
$serviceStates = array_fill_keys(self::getServiceStatesSummaryEmpty(), 0);
|
||||||
Service::getStateText(Service::STATE_OK),
|
$hostStates = array_fill_keys(HostList::getHostStatesSummaryEmpty(), 0);
|
||||||
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),
|
|
||||||
));
|
|
||||||
|
|
||||||
foreach ($this as $service) {
|
foreach ($this as $service) {
|
||||||
$unhandled = false;
|
$unhandled = false;
|
||||||
|
@ -67,4 +57,27 @@ class ServiceList extends ObjectList
|
||||||
|
|
||||||
return (object)$serviceStates;
|
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')
|
||||||
|
),
|
||||||
|
'_'
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue