From b0b0ae157c344ec9f1bdfbc5efe9e14896608500 Mon Sep 17 00:00:00 2001 From: Matthias Jentsch Date: Tue, 31 Mar 2015 16:29:33 +0200 Subject: [PATCH] Turn prepareStateName into more generic string utility function refs #8565 --- library/Icinga/Util/String.php | 41 +++++++++++++++++++ .../library/Monitoring/Object/HostList.php | 32 +++++++++++---- .../library/Monitoring/Object/ObjectList.php | 11 ----- .../library/Monitoring/Object/ServiceList.php | 41 ++++++++++++------- 4 files changed, 93 insertions(+), 32 deletions(-) diff --git a/library/Icinga/Util/String.php b/library/Icinga/Util/String.php index 9fb3cb223..d9bd5a6d3 100644 --- a/library/Icinga/Util/String.php +++ b/library/Icinga/Util/String.php @@ -79,4 +79,45 @@ class String 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. + * + *
+     *  cartesianProduct(
+     *      array(array('foo', 'bar'), array('mumble', 'grumble', null)),
+     *      '_'
+     *  );
+     *     => array('foo_mumble', 'foo_grumble', 'bar_mumble', 'bar_grumble', 'foo', 'bar')
+     * 
+ * + * @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; + } } diff --git a/modules/monitoring/library/Monitoring/Object/HostList.php b/modules/monitoring/library/Monitoring/Object/HostList.php index 94157b6fc..b5235eb75 100644 --- a/modules/monitoring/library/Monitoring/Object/HostList.php +++ b/modules/monitoring/library/Monitoring/Object/HostList.php @@ -3,6 +3,8 @@ namespace Icinga\Module\Monitoring\Object; +use Icinga\Util\String; + /** * A host list */ @@ -33,13 +35,7 @@ class HostList extends ObjectList */ 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; @@ -52,4 +48,26 @@ class HostList extends ObjectList 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') + ), + '_' + ); + } } diff --git a/modules/monitoring/library/Monitoring/Object/ObjectList.php b/modules/monitoring/library/Monitoring/Object/ObjectList.php index f28dded91..62a15173c 100644 --- a/modules/monitoring/library/Monitoring/Object/ObjectList.php +++ b/modules/monitoring/library/Monitoring/Object/ObjectList.php @@ -118,15 +118,4 @@ abstract class ObjectList implements Countable, IteratorAggregate } 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; - } } diff --git a/modules/monitoring/library/Monitoring/Object/ServiceList.php b/modules/monitoring/library/Monitoring/Object/ServiceList.php index 0625ae0f8..861075004 100644 --- a/modules/monitoring/library/Monitoring/Object/ServiceList.php +++ b/modules/monitoring/library/Monitoring/Object/ServiceList.php @@ -3,6 +3,8 @@ namespace Icinga\Module\Monitoring\Object; +use Icinga\Util\String; + /** * A service list */ @@ -33,20 +35,8 @@ class ServiceList extends ObjectList */ 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; @@ -67,4 +57,27 @@ class ServiceList extends ObjectList 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') + ), + '_' + ); + } }