[icinga-checkins] icinga.org: icinga-web/master: Exclude customvars now applies to the whole HTTP Icinga Api (fixes #3183)

git at icinga.org git at icinga.org
Tue Oct 9 10:59:32 CEST 2012


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

Author: Jannis Mosshammer <jannis.mosshammer at netways.de>
Date:   Tue Oct  9 10:59:04 2012 +0200

Exclude customvars now applies to the whole HTTP Icinga Api (fixes #3183)

---

 app/modules/Api/actions/ApiSearchAction.class.php  |   34 ++++++--
 .../Api/models/Result/OutputRewriteModel.class.php |   82 +++++++++++++-------
 2 files changed, 81 insertions(+), 35 deletions(-)

diff --git a/app/modules/Api/actions/ApiSearchAction.class.php b/app/modules/Api/actions/ApiSearchAction.class.php
old mode 100644
new mode 100755
index dc9553c..569405d
--- a/app/modules/Api/actions/ApiSearchAction.class.php
+++ b/app/modules/Api/actions/ApiSearchAction.class.php
@@ -130,13 +130,14 @@ class Api_ApiSearchAction extends IcingaApiBaseAction {
         
         // Rewrite output, e.g. plugin_output
         // see #2598
-        if ($rd->getParameter('enableRewrite', false) === true) {
-            $rewrite = $this->getContext()->getModel('Result.OutputRewrite', 'Api', array(
-                    'target' => $target
-            ));
-            
-            $res = $rewrite->rewrite($res);
-        }
+        
+        $rewrite = $this->getContext()->getModel('Result.OutputRewrite', 'Api', array(
+                'target' => $target,
+                'optional' => $rd->getParameter('enableRewrite', false)
+        ));
+
+        $res = $rewrite->rewrite($res);
+        
 
         $rd->setParameter("searchResult", $res);
 
@@ -265,7 +266,23 @@ class Api_ApiSearchAction extends IcingaApiBaseAction {
             $search->setSearchOrder($order["column"],$order["dir"]);
         }
     }
-
+    // make sure cvs are always returned as value->name pairs
+    private function fillCustomvariables(array &$columns) {
+        $cv_value_field = false;
+        $cv_name_field = false;
+        foreach($columns as $column) {
+            if(preg_match("/CUSTOMVARIABLE_VALUE$/",$column)) {
+                $cv_value_field = $column;
+            } else if(preg_match("/CUSTOMVARIABLE_NAME$/",$column)) {
+                $cv_name_field = $column;
+            }
+        }
+        if($cv_value_field && !$cv_name_field)
+            $columns[] = str_replace("VALUE","NAME",$cv_value_field);
+        if($cv_name_field && !$cv_value_field)
+            $columns[] = str_replace("NAME","VALUE",$cv_name_field);
+    }
+    
     public function setColumns($search,AgaviRequestDataHolder $rd) {
         if ($search->getSearchType() == IcingaApiConstants::SEARCH_TYPE_COUNT) {
             $search->setResultColumns($rd->getParameter("countColumn"));
@@ -275,6 +292,7 @@ class Api_ApiSearchAction extends IcingaApiBaseAction {
         $columns = $rd->getParameter("columns",null);
         if(!is_array($columns))
             $columns = array($columns);
+        $this->fillCustomvariables($columns);
         $columns_result = array();
         foreach($columns as $column) {
             $column = preg_replace("/[^1-9_A-Za-z]/","",$column);
diff --git a/app/modules/Api/models/Result/OutputRewriteModel.class.php b/app/modules/Api/models/Result/OutputRewriteModel.class.php
old mode 100644
new mode 100755
index a39a214..836d8aa
--- a/app/modules/Api/models/Result/OutputRewriteModel.class.php
+++ b/app/modules/Api/models/Result/OutputRewriteModel.class.php
@@ -33,7 +33,7 @@
  * @since 1.7.0
  */
 class Api_Result_OutputRewriteModel extends IcingaApiBaseModel {
-    
+    private $excludeCVs = array();
     /**
      * Static array of rewrite strings. Used to set icinga status classes
      * to specific states
@@ -48,46 +48,55 @@ class Api_Result_OutputRewriteModel extends IcingaApiBaseModel {
      * @todo If needed allow external filling
      * @var array
      */
-    private static $rewriters = array(

-            array(

-                    'regex'    => '^(HOST|SERVICE)_(LONG_)?OUTPUT$',
-                    'targets'  => array('host', 'service'),

-                    'method'   => 'rewritePluginOutput'

-            )

-    );

+    private static $rewriters = array(
+            array(
+                'regex'    => '^(HOST|SERVICE)_(LONG_)?OUTPUT$',
+                'targets'  => array('host', 'service'),
+                'method'   => 'rewritePluginOutput',
+                'optional' => true
+            ),
+            array(
+                'regex'    => 'CUSTOMVARIABLE_VALUE$',
+                'targets'  => 'all',
+                'method'   => 'rewriteCustomvariables',
+                'optional' => false
+            )
+        );
     
     /**
      * Maps the methods to array key names
      * @param array $res
      * @return array Ready mapped rewriter description
-     */

+     */
     private function buildRewriterMatch(array $res) {
-        $out = array ();

-        if (count($res)) {

-            $record = $res[0];

-            foreach ($record as $field=>$value) {

+        $out = array ();
+        if (count($res)) {
+            $record = $res[0];
+            foreach ($record as $field=>$value) {
                 foreach (self::$rewriters as $rewriter) {
                     
-                    if (in_array(
+                    if ($rewriter['optional'] && $this->getParameter('optional', false) == false)
+                        continue;
+                    
+                    if ($rewriter['targets'] != "all" && in_array(
                             $this->getParameter('target'), 
                             $rewriter['targets']) === false) {
                         continue;
                     }
                     
-                    if (preg_match('@'. $rewriter['regex']. '@', $field)) {
-                        
+                    if (preg_match('@'. $rewriter['regex']. '@', $field)) {            
                         if (!isset($out[$field])) {
                             $out[$field] = array();
                         }
                         
                         $out[$field][] = $rewriter['method'];
                     }
-                }

-            }

+                }
+            }
         }
         
-        return $out;

-    }

+        return $out;
+    }
     
     /**
      * Rewrite the values based on predefined internal methods
@@ -100,7 +109,7 @@ class Api_Result_OutputRewriteModel extends IcingaApiBaseModel {
             foreach ($rewriters as $field=>$methods) {
                 if (isset($row[$field])) {
                     foreach ($methods as $method) {
-                        $result[$idx][$field] = $this->$method($row[$field]);
+                        $result[$idx][$field] = $this->$method($row[$field],$field,$row);
                     }
                 }
             }
@@ -115,8 +124,8 @@ class Api_Result_OutputRewriteModel extends IcingaApiBaseModel {
      * if state
      * @param string $val
      * @return string New value
-     */

-    private function rewritePluginOutput($val) {
+     */
+    private function rewritePluginOutput($val,$field,$row) {
         $new_val = str_replace('\\n', '<br />', $val);
         
         foreach(self::$states as $state) {
@@ -127,26 +136,45 @@ class Api_Result_OutputRewriteModel extends IcingaApiBaseModel {
             );
         }
         
-        return $new_val;

+        return $new_val;
+    }
+    /**
+     * Exclude customvariables defined in api.exclude_customvars  (see #3183)
+     * 
+     * @param type $val
+     * @param type $field
+     * @param type $row
+     */
+    private function rewriteCustomvariables($val,$field,$row) {
+        $namefield = str_replace("VALUE","NAME",$field);
+        if(!isset($row[$namefield])) { // Fallback case: No name given, no c
+            return "";
+        } else if(in_array($row[$namefield],$this->excludeCVs)) {
+            return "";
+        }
+        return $val;
     }
     
+    
     public function initialize(AgaviContext $context, array $parameters = array()) {
         parent::initialize($context, $parameters);
         
         if ($this->getParameter('target', false) === false) {
             throw new AppKitModelException('Parameter "target" is mandatory!');
         }
+        
+        $this->excludeCVs = AgaviConfig::get('modules.api.exclude_customvars');
     }
     
     /**
      * Interface for consuming components (e.g. ApiSearchAction)
      * @param array $result
-     */

-    public function rewrite(array $result) {

+     */
+    public function rewrite(array $result) {
         $rewriters = $this->buildRewriterMatch($result);
         if (count($rewriters)) {
             $result = $this->doRewrites($result, $rewriters);
         }
-        return $result;

+        return $result;
     }
 }
\ No newline at end of file





More information about the icinga-checkins mailing list