[icinga-checkins] icinga.org: icinga-web/master: * Implicit match_types for Static content cronk ( match_type in datasource xml)

git at icinga.org git at icinga.org
Mon Apr 26 18:42:34 CEST 2010


Module: icinga-web
Branch: master
Commit: 3ba76bd7f4c2508d2e25c5635d03a5b47818fe3f
URL:    https://git.icinga.org/?p=icinga-web.git;a=commit;h=3ba76bd7f4c2508d2e25c5635d03a5b47818fe3f

Author: Marius Hein <marius.hein at netways.de>
Date:   Thu Apr 22 17:56:00 2010 +0200

* Implicit match_types for Static content cronk (match_type in datasource xml)

---

 .../xml/icinga-tactical-overview-template-cv.xml   |   14 ++
 .../models/System/StaticContentModel.class.php     |  158 ++++++++++++--------
 2 files changed, 111 insertions(+), 61 deletions(-)

diff --git a/app/modules/Cronks/data/xml/icinga-tactical-overview-template-cv.xml b/app/modules/Cronks/data/xml/icinga-tactical-overview-template-cv.xml
index fbd5e41..dbabaaf 100644
--- a/app/modules/Cronks/data/xml/icinga-tactical-overview-template-cv.xml
+++ b/app/modules/Cronks/data/xml/icinga-tactical-overview-template-cv.xml
@@ -32,10 +32,24 @@
 					<value>0</value>
 				</columns>
 			</filter>
+			
+			<!-- 
+				* Maps variables against real columns
+				This is needed to query subvalues from other columns names
+			-->
 			<varmap>
 				<map name="CUSTOMVARIABLE_NAME">HOST_CUSTOMVARIABLE_NAME</map>
 				<map name="CUSTOMVARIABLE_VALUE">HOST_CUSTOMVARIABLE_VALUE</map>
 			</varmap>
+			
+			<!-- 
+				* Set match types for colums implicit
+				For template query values where you can't set this through the 
+				filter			
+			-->
+			<!-- <match_type>
+				<type name="SERVICE_CUSTOMVARIABLE_VALUE">IcingaApi::MATCH_LIKE</type>
+			</match_type> -->
 		</datasource>
 		
 		<datasource id="HOST_STATUS_DOWN_CV">
diff --git a/app/modules/Cronks/models/System/StaticContentModel.class.php b/app/modules/Cronks/models/System/StaticContentModel.class.php
index 465b683..0f35632 100644
--- a/app/modules/Cronks/models/System/StaticContentModel.class.php
+++ b/app/modules/Cronks/models/System/StaticContentModel.class.php
@@ -34,7 +34,7 @@ class Cronks_System_StaticContentModel extends ICINGACronksBaseModel
 	 */
 	public function __construct () {
 		$this->api = AppKitFactories::getInstance()->getFactory('IcingaData');
-	}	
+	}
 
 	/**
 	 * main function to generate content
@@ -138,7 +138,7 @@ class Cronks_System_StaticContentModel extends ICINGACronksBaseModel
 	/**
 	 * converts XML into an associative array
 	 * @param	DOMElement		$element			XML node to convert into associative array
-	 * @return	array								converted XML data								
+	 * @return	array								converted XML data
 	 * @author	Christian Doebler <christian.doebler at netways.de>
 	 */
 	private function convertDom (DOMElement &$element) {
@@ -205,19 +205,59 @@ class Cronks_System_StaticContentModel extends ICINGACronksBaseModel
 		return $success;
 	}
 
-	private function filterColumnArray(array $columns, array $mapArray=array(), $debug=false) {
-
-		if (count($mapArray) && $columns[0]) {
-			$columns[0] = $this->filterColumn($columns[0], $mapArray);
-		}
+	/**
+	 * Test for column modification in datasource elements and
+	 * converts into IcingaApi searchfilter arrays
+	 * @param array $columns
+	 * @param array $dataSource
+	 * @return array
+	 */
+	private function buildApiSearchfilter(array $columns, array $dataSource=array()) {
+		list($column, $value) = $columns;
+		
+		// Default match if something fails
+		$match = IcingaApi::MATCH_EXACT;
+		foreach ( array('varmap', 'match_type') as $opType) {
 			
-		return $columns;
-	}
-	
-	private function filterColumn($columnName, array $mapArray=array(), $debug=false) {
-		return ((array_key_exists($columnName, $mapArray)) ? $mapArray[$columnName] : $columnName);
+			// Don't reassing values in every operation 
+			if (array_key_exists($opType, $dataSource) && is_array($dataSource[$opType])) {
+				$opArray = $dataSource[$opType];
+				$opData = false;
+				
+				// Reassing value to work in operation
+				if (array_key_exists($column, $opArray) && $opArray[$column]) {
+					$opData = $opArray[$column];
+				}
+				else {
+					continue;
+				}
+				
+				// Just a list of short modificatons
+				switch ($opType) {
+					
+					/*
+					 * Remap the column
+					 */
+					case 'varmap':
+						$column = $opData;
+						
+					break;
+					
+					/*
+					 * Implicit matchtype
+					 */
+					case 'match_type':
+						$match = (@constant($opData)) ? constant($opData) : $opData;  
+					break;
+					
+				}
+			}
+		}
+		// Safe return value
+		return array($column, $value, $match);
 	}
-	
+
+
 	/**
 	 * fetches data via IcingaApi
 	 * @param	string			$dataSourceId		source id to query settings
@@ -230,11 +270,6 @@ class Cronks_System_StaticContentModel extends ICINGACronksBaseModel
 		$success = true;
 
 		$dataSource = $this->xmlData['datasources'][$dataSourceId];
-
-		$columnMap = array();
-		if (array_key_exists('varmap', $dataSource)) {
-			$columnMap = $dataSource['varmap'];
-		}
 		
 		$apiSearch = $this->api->API()->createSearch()->setResultType(IcingaApi::RESULT_ARRAY);
 		if (!array_key_exists('target', $dataSource)) {
@@ -243,7 +278,7 @@ class Cronks_System_StaticContentModel extends ICINGACronksBaseModel
 			$success = false;
 
 		} else {
-		
+
 			// set search target
 			$apiSearch->setSearchTarget(constant($dataSource['target']));
 
@@ -275,27 +310,27 @@ class Cronks_System_StaticContentModel extends ICINGACronksBaseModel
 
 					if ($success) {
 						$filterData = array(
-							trim($filter['column']),
-							trim($filter['value'])
+						trim($filter['column']),
+						trim($filter['value'])
 						);
 
 						if (array_key_exists('match_type', $filter)) {
 							array_push($filterData, trim($filter['match_type']));
 						}
-
+						
 						$apiSearch->setSearchFilter(array($filterData));
 					}
 				}
 			}
-
+				
 			// set additional search filter
 			foreach ($this->globalFilter as $filterData) {
-				$apiSearch->setSearchFilter(array($this->filterColumnArray($filterData, $columnMap)));
+				$apiSearch->setSearchFilter(array($this->buildApiSearchfilter($filterData, $dataSource)));
 			}
 
 			// set additional search filter
 			foreach ($additionalFilter as $filterData) {
-				$apiSearch->setSearchFilter(array($this->filterColumnArray($filterData, $columnMap)));
+				$apiSearch->setSearchFilter(array($this->buildApiSearchfilter($filterData, $dataSource)));
 			}
 
 			// execute query and fetch result
@@ -306,39 +341,39 @@ class Cronks_System_StaticContentModel extends ICINGACronksBaseModel
 					'IcingaServicegroup',
 					'IcingaHostCustomVariablePair',
 					'IcingaServiceCustomVariablePair'
-				);
-				IcingaPrincipalTargetTool::applyApiSecurityPrincipals(
+					);
+					IcingaPrincipalTargetTool::applyApiSecurityPrincipals(
 					$secureSearchModels,
 					$apiSearch
-				);
+					);
 
-				// fetch data
-				$apiRes = $apiSearch->fetch()->getAll();
+					// fetch data
+					$apiRes = $apiSearch->fetch()->getAll();
 
-				// set function
-				if (array_key_exists('function', $dataSource)) {
-					$function = $dataSource['function'];
-				} else {
-					$function = false;
-				}
+					// set function
+					if (array_key_exists('function', $dataSource)) {
+						$function = $dataSource['function'];
+					} else {
+						$function = false;
+					}
 
-				// set result data
-				$numResults = count($apiRes);
-				$offset = ($numResults > 0) ? 0 : -1;
+					// set result data
+					$numResults = count($apiRes);
+					$offset = ($numResults > 0) ? 0 : -1;
 
-				$resultData = array(
+					$resultData = array(
 					'data'			=> $apiRes,
 					'function'		=> $function,
-				);
+					);
 
-				if ($templateId !== false) {
-					if (!array_key_exists($templateId, $this->content)) {
-						$this->content[$templateId] = array('data' => array());
-					} elseif (!array_key_exists('data', $this->content[$templateId])) {
-						$this->content[$templateId]['data'] = array();
+					if ($templateId !== false) {
+						if (!array_key_exists($templateId, $this->content)) {
+							$this->content[$templateId] = array('data' => array());
+						} elseif (!array_key_exists('data', $this->content[$templateId])) {
+							$this->content[$templateId]['data'] = array();
+						}
+						$this->content[$templateId]['data'][$dataSourceId] = $resultData;
 					}
-					$this->content[$templateId]['data'][$dataSourceId] = $resultData;
-				}
 			}
 
 		}
@@ -450,9 +485,9 @@ class Cronks_System_StaticContentModel extends ICINGACronksBaseModel
 
 			$this->createTemplateContent($templateVariables[1][$x], $currentFilter);
 			$content = $this->substituteTemplateVariablesByProcessedContent(
-				$templateVariables[0][$x],
-				$templateVariables[1][$x],
-				$content
+			$templateVariables[0][$x],
+			$templateVariables[1][$x],
+			$content
 			);
 		}
 
@@ -494,11 +529,12 @@ class Cronks_System_StaticContentModel extends ICINGACronksBaseModel
 				// substitute data of statement
 				$variablePattern = '/\${([A-Z0-9_\-]+):([A-Z_]+)(:[^}]+)?}/s';
 				preg_match_all($variablePattern, $content, $templateVariables);
+				
 				$ifStmtStr = $this->substituteTemplateVariables($tplId, $templateVariables, $ifStmtStr, $filter);
-
+				
 				// determine statement value
 				$stmtValue = false;
-				eval("\$stmtValue = ($ifStmtStr);");
+				@eval("\$stmtValue = ($ifStmtStr);");
 
 				// create new content
 				$newStr = ($stmtValue) ? substr($workStr, $pos) : null;
@@ -543,9 +579,9 @@ class Cronks_System_StaticContentModel extends ICINGACronksBaseModel
 		}
 
 		$content = str_replace(
-			$tplVar,
-			$this->content[$tplId]['content'],
-			$content
+		$tplVar,
+		$this->content[$tplId]['content'],
+		$content
 		);
 
 		return $content;
@@ -599,8 +635,8 @@ class Cronks_System_StaticContentModel extends ICINGACronksBaseModel
 				// apply function
 				if ($templateData['function'] !== false) {
 					$substitution = $this->applyFunction(
-						$substitution,
-						$templateData['function']
+					$substitution,
+					$templateData['function']
 					);
 				}
 			}
@@ -615,9 +651,9 @@ class Cronks_System_StaticContentModel extends ICINGACronksBaseModel
 			}
 
 			$content = str_replace(
-				$templateVariables[0][$x],
-				$substitution,
-				$content
+			$templateVariables[0][$x],
+			$substitution,
+			$content
 			);
 		}
 





More information about the icinga-checkins mailing list