[icinga-checkins] icinga.org: icinga-api/master: * added result column for host status

Christian Doebler christian.doebler at netways.de
Sat Oct 3 21:37:29 CEST 2009


Module: icinga-api
Branch: master
Commit: 50f52af5e36ec53f078642f089d36f493fbe7d16
URL:    https://git.icinga.org/?p=icinga-api.git;a=commit;h=50f52af5e36ec53f078642f089d36f493fbe7d16

Author: Christian Doebler <christian.doebler at netways.de>
Date:   Sat Oct  3 20:03:55 2009 +0200

* added result column for host status
* modified query limit for use of default values

---

 objects/search/IcingaApiSearchColumnsIdo.php |    1 +
 objects/search/IcingaApiSearchIdo.php        |   40 +++++++++++++++----------
 2 files changed, 25 insertions(+), 16 deletions(-)

diff --git a/objects/search/IcingaApiSearchColumnsIdo.php b/objects/search/IcingaApiSearchColumnsIdo.php
index d30680c..6026323 100644
--- a/objects/search/IcingaApiSearchColumnsIdo.php
+++ b/objects/search/IcingaApiSearchColumnsIdo.php
@@ -240,6 +240,7 @@ class IcingaApiSearchColumnsIdo {
 		'HOST_PROBLEM_HAS_BEEN_ACKNOWLEDGED' => 'hs.problem_has_been_acknowledged',
 		'HOST_SCHEDULED_DOWNTIME_DEPTH' => 'hs.scheduled_downtime_depth',
 		'HOST_STATUS_UPDATE_TIME' => 'hs.status_update_time',
+		'HOST_STATE' => 'hs.current_state',
 
 		// Service data
 		'SERVICE_ID' => 's.service_id',
diff --git a/objects/search/IcingaApiSearchIdo.php b/objects/search/IcingaApiSearchIdo.php
index 71afd89..56da92c 100644
--- a/objects/search/IcingaApiSearchIdo.php
+++ b/objects/search/IcingaApiSearchIdo.php
@@ -195,8 +195,7 @@ class IcingaApiSearchIdo
 				${FILTER}
 			group by
 				ss.current_state
-			order by
-				ss.current_state',
+			${ORDERBY:ss.current_state}',
 		self::TARGET_HOST_STATUS_HISTORY => 
 			'select
 				${FIELDS}
@@ -236,10 +235,8 @@ class IcingaApiSearchIdo
 				and ohp.objecttype_id = 1
 				${FILTER}
 				${GROUPBY}
-			order by
-				parent_name asc,
-				child_name asc
-			${LIMIT}',
+				${ORDERBY:parent_name asc, child_name asc}
+				${LIMIT}',
 	);
 
 	/*
@@ -352,12 +349,13 @@ class IcingaApiSearchIdo
 	 */
 	private function replaceQueryVariables ($query, $variableName) {
 
+		$variableValuesPrefix = null;
 		$variableValues = false;
 		$variableNamePrefix = '${' . $variableName;
 
 		switch ($variableName) {
 			case 'FIELDS':
-				if (!empty($this->resultColumns)) {
+				if (count($this->resultColumns)) {
 					$variableValues = $this->applySearchType(implode(',', $this->resultColumns));
 				}
 				break;
@@ -366,25 +364,35 @@ class IcingaApiSearchIdo
 			case 'GROUPBY':
 				break;
 			case 'ORDERBY':
+				$variableValuesPrefix = ' order by ';
+				if (count($this->searchOrder)) {
+					$variableValues =  implode(',', $this->searchOrder);
+				}
 				break;
 			case 'LIMIT':
 				break;
 		}
 
-		if ($variableValues === false && strpos($query, $variableNamePrefix . ':') !== false) {
+		if (strpos($query, $variableNamePrefix . ':') !== false) {
 			$fieldPattern = '/\\' . $variableNamePrefix . ':([^}]+)}/';
 			preg_match_all($fieldPattern, $query, $fieldDefaultMatches);
-			if (count($fieldDefaultMatches) == 2) {
-				$variableName = $fieldDefaultMatches[0][0];
+		}
+
+		if ($variableValues === false) {
+			if (count($fieldDefaultMatches)) {
 				$variableValues = $fieldDefaultMatches[1][0];
 			} else {
 				$variableValues = null;				
 			}
-		} else {
-			$variableName = variableNamePrefix . '}';
 		}
 
-		$query = str_replace($variableName, $variableValues, $query);
+		$variableName = (count($fieldDefaultMatches)) ? $fieldDefaultMatches[0][0] : $variableNamePrefix . '}';
+
+		if (empty($variableValues)) {
+			$variableValuesPrefix = null;
+		}
+
+		$query = str_replace($variableName, $variableValuesPrefix . $variableValues, $query);
 
 		return $query;
 
@@ -405,9 +413,10 @@ class IcingaApiSearchIdo
 
 			// create base for query
 			$query = $this->queryMap[$this->searchTarget];
+			$query = str_replace('${TABLE_PREFIX}', $this->tablePrefix, $query);
 
+			// add query fields
 			$query = $this->replaceQueryVariables($query, 'FIELDS');
-			$query = str_replace('${TABLE_PREFIX}', $this->tablePrefix, $query);
 
 			// add filter
 			if (count($this->searchFilter)) {
@@ -425,8 +434,7 @@ class IcingaApiSearchIdo
 			$query = str_replace('${GROUPBY}', $groupBy, $query);
 
 			// add order
-			$orderBy = (count($this->searchOrder)) ? (' order by ' . implode(',', $this->searchOrder) . ' ') : null;
-			$query = str_replace('${ORDERBY}', $orderBy, $query);
+			$query = $this->replaceQueryVariables($query, 'ORDERBY');
 
 			// add limit
 			$limit = ($this->searchLimit !== false) ? (' limit ' . $this->searchLimit . ' ') : null;





More information about the icinga-checkins mailing list