[icinga-checkins] icinga.org: icinga-api/mhein/default: * added filter and limit to livestatus

git at icinga.org git at icinga.org
Tue Jan 12 14:21:31 CET 2010


Module: icinga-api
Branch: mhein/default
Commit: 1a35dbd781ec32c7e82b6a6067959b29c1fd0489
URL:    https://git.icinga.org/?p=icinga-api.git;a=commit;h=1a35dbd781ec32c7e82b6a6067959b29c1fd0489

Author: Christian Doebler <christian.doebler at netways.de>
Date:   Tue Dec 22 13:52:43 2009 +0100

* added filter and limit to livestatus

---

 objects/search/IcingaApiSearchLivestatus.php       |  103 +++++++++++++++-----
 .../IcingaApiSearchLivestatusSocket.php            |    9 ++
 2 files changed, 87 insertions(+), 25 deletions(-)

diff --git a/objects/search/IcingaApiSearchLivestatus.php b/objects/search/IcingaApiSearchLivestatus.php
index 4c01f88..673c676 100644
--- a/objects/search/IcingaApiSearchLivestatus.php
+++ b/objects/search/IcingaApiSearchLivestatus.php
@@ -182,25 +182,75 @@ class IcingaApiSearchLivestatus
 	private function getQueryFilter () {
 		$filter = null;
 
-		var_dump($this->searchFilter);
+		$numAnd = count($this->searchFilter);
+
+		if ($numAnd) {
+			foreach ($this->searchFilter as $column => $columnData) {
+				$numOr = 0;
+
+				foreach ($columnData as $matchType => $filterStrArr) {
+					$numOr += count($filterStrArr);
+
+					$matchRegEx = ($matchType == self::MATCH_LIKE) ? true : false;
+
+					foreach ($filterStrArr as $filterStr) {
+						if ($matchRegEx) {
+							$filterStr =
+								'^' . str_replace('%', '.*' , $filterStr) . '$';
+						}
+
+						$filter .= sprintf (
+							"Filter: %s %s %s\n",
+								$column,
+								$this->ifSettings->matchMap[$matchType],
+								$filterStr
+						);
+					}
+				}
+
+				if ($numOr > 1) {
+					$filter .= 'Or: ' . $numOr . "\n";
+				}
+			}
+
+			if ($numAnd > 1) {
+				$filter .= 'And: ' . $numAnd . "\n";
+			}
+		}
 
 		return $filter;
 	}
 
 	/**
+	 * creates the limit statements for livestatus query and returns it
+	 * @param	void
+	 * @return	string							filter statements
+	 */
+	private function getQueryLimit () {
+		$limit = null;
+
+		if (count($this->searchLimit)) {
+			$limit = 'Limit: ' . $this->searchLimit[0] . "\n";
+		}
+
+		return $limit;
+	}
+
+	/**
 	 * creates query string for livestatus
 	 * @param unknown_type $queryObject
 	 * @return unknown_type
 	 */
 	private function createQuery () {
 		$query = sprintf(
-			"GET %s\n%s%sOutputFormat:json\nKeepAlive: on\nResponseHeader: fixed16\n\n",
+			"GET %s\n%s%s%sOutputFormat: json\nKeepAlive: on\nResponseHeader: fixed16\n\n",
 			$this->ifSettings->queryMap[$this->searchTarget],
 			$this->getQueryColumns(),
-			$this->getQueryFilter()
+			$this->getQueryFilter(),
+			$this->getQueryLimit()
 		);
 
-var_dump($query);
+//var_dump($query);
 		return $query;
 	}
 
@@ -225,7 +275,7 @@ var_dump($query);
 	 * @return	
 	 * @author	Christian Doebler <christian.doebler at netways.de>
 	 */
-	private function executeQuery ($queryObject) {
+	private function executeQuery () {
 		$data = null;
 
 		$query = $this->createQuery();
@@ -237,30 +287,34 @@ var_dump($query);
 			socket_write($this->socket, $query);
 			$data = $this->readSocket(16);
 
-			if ($data === false) {
-				throw new IcingaApiSearchLivestatusException('querySocket(): could not read from socket!');
-			}
+			if ($data !== false) {
 
-			// extract status code
-			$status = substr($data, 0, 3);
+				// extract status code
+				$status = substr($data, 0, 3);
 
-			// extract content length
-			$length = intval(trim(substr($data, 4, 11)));
+				// extract content length
+				$length = intval(trim(substr($data, 4, 11)));
 
-			// read remaining data
-			$data = $this->readSocket($length);
+				// read remaining data
+				$data = $this->readSocket($length);
 
-			if ($data === false) {
-				throw new IcingaApiSearchLivestatusException('querySocket(): could not read from socket!');
-			}
+				if ($data === false) {
+					throw new IcingaApiSearchLivestatusException('querySocket(): could not read from socket!');
+				}
 
-			if ($status != '200') {
-				throw new IcingaApiSearchLivestatusException('querySocket(): could not read from socket: status ' . $status . '!');
-			}
+				if ($status != '200') {
+					throw new IcingaApiSearchLivestatusException('querySocket(): could not read from socket: status ' . $status . ', message: ' . $data);
+				}
+
+				// check for 'connection reset by peer'
+				if (socket_last_error($this->socket) == 104) {
+					throw new IcingaApiSearchLivestatusException('querySocket(): could not read from socket: connection reset by peer!');
+				}
+
+			} else {
+
+				throw new IcingaApiSearchLivestatusException('querySocket(): could not read from socket!');
 
-			// check for 'connection reset by peer'
-			if (socket_last_error($this->socket) == 104) {
-				throw new IcingaApiSearchLivestatusException('querySocket(): could not read from socket: connection reset by peer!');
 			}
 
 		}
@@ -281,9 +335,8 @@ var_dump($query);
 //		$class = 'IcingaApiResultLivestatus';
 //		$object = new $class;
 
-		if (($data = $this->executeQuery('hosts')) !== false) {
+		if (($data = $this->executeQuery()) !== false) {
 //var_dump($data);
-//var_dump(json_decode($data, true));
 //			$object->setSearchObject($this->connectionObject->connectionStatement);
 //			$object->setDbType($this->connectionObject->getDbType());
 //			if ($this->resultType !== false) {
diff --git a/objects/search/data_interfaces/IcingaApiSearchLivestatusSocket.php b/objects/search/data_interfaces/IcingaApiSearchLivestatusSocket.php
index 382a797..3c7db7a 100644
--- a/objects/search/data_interfaces/IcingaApiSearchLivestatusSocket.php
+++ b/objects/search/data_interfaces/IcingaApiSearchLivestatusSocket.php
@@ -35,6 +35,15 @@ class IcingaApiSearchLivestatusSocket
 		self::TARGET_SERVICEGROUP_SUMMARY => ''
 	);
 
+	public $matchMap = array (
+		self::MATCH_EXACT => '=~',
+		self::MATCH_LIKE => '~~',
+		self::MATCH_GREATER_THAN => '>',
+		self::MATCH_GREATER_OR_EQUAL => '>=',
+		self::MATCH_LESS_THAN => '<',
+		self::MATCH_LESS_OR_EQUAL => '<=',
+	);
+
 	// COLUMNS
 	public $columns = array(
 /*





More information about the icinga-checkins mailing list