From 04ae4a6346d23aabfdcdb52e9792255322e4621d Mon Sep 17 00:00:00 2001 From: Leo Nishio Date: Thu, 24 Jul 2014 10:44:19 -0700 Subject: [PATCH 1/3] Saigon Search Feature --- controllers/CoreController.class.php | 15 ++ include/SaigonSearch.class.php | 213 +++++++++++++++++++++++++++ lib/class_mapping.ini | 1 + views/nrpe_cfg_stage.php | 4 +- views/search.php | 177 ++++++++++++++++++++++ views/site_input.php | 22 ++- www/index.php | 4 +- 7 files changed, 431 insertions(+), 5 deletions(-) create mode 100644 include/SaigonSearch.class.php create mode 100644 views/search.php diff --git a/controllers/CoreController.class.php b/controllers/CoreController.class.php index 944e059..7955805 100644 --- a/controllers/CoreController.class.php +++ b/controllers/CoreController.class.php @@ -84,5 +84,20 @@ public function getheader() $this->sendResponse('site_header', $viewData); } + /** + * search - Search Saigon Data + * + * @access public + * @return void + */ + public function search() + { + $viewData = new ViewData(); + $viewData->search = trim($this->getParam('search')); + $viewData->deployment = $this->getParam('deployment'); + $viewData->searchResults = SaigonSearch::search($viewData->deployment, $viewData->search); + $this->sendResponse('search', $viewData); + } + } diff --git a/include/SaigonSearch.class.php b/include/SaigonSearch.class.php new file mode 100644 index 0000000..0b4059a --- /dev/null +++ b/include/SaigonSearch.class.php @@ -0,0 +1,213 @@ +checkAuth($deployment) === true) { + array_push($viewDeployments, $deployment); + } + } + } elseif (in_array($deployment, $deployments)) { + if ($authmodule->checkAuth($deployment) === true) { + array_push($viewDeployments, $deployment); + } + } + asort($viewDeployments); + return $viewDeployments; + + } + + /* + * searchDeployment - search the component keys of a deployment + */ + private static function searchDeployment($deployment, $search) { + $results = array(); + $match = false; + + // Get the deployment version + $results['version'] = RevDeploy::getDeploymentRev($deployment); + + // See if deployment name matches + if (preg_match("/$search/", $deployment)) { + $results['deployment_name'] = $deployment; + $match = true; + } + + // Get the key hash + list($dKey) = array_shift(NagRedis::keys(md5('deployment:'.$deployment), false)); + $results['deployment_hash'] = $dKey; + + // Look through the deployment hash + list($match_deployment_info, $match_flag) = self::searchHash($dKey, $search); + if ($match_flag) { + $results['deployment_info'] = $match_deployment_info; + $match = true; + } + + // Other non-versioned keys + $nvKeys = array_shift(NagRedis::keys(md5('deployment:'.$deployment).":hostsearch*", true)); + sort($nvKeys); + foreach ($nvKeys as $nvKey) { + if (preg_match("/$search/", $nvKey)) { + if (isset($results['nonversioned']['key_name'])) { + array_push($results['nonversioned']['key_name'], $nvKey); + } else { + $results['nonversioned']['key_name'] = array($nvKey); + } + $match = true; + } + list($match_nv_info, $match_flag) = self::searchRedisKey($nvKey, $search); + if ($match_flag) { + if (isset($results['nonversioned']['key_value'][$nvKey])) { + array_push($results['nonversioned']['key_value'][$nvKey], $match_nv_info); + } else { + $results['nonversioned']['key_value'][$nvKey] = $match_nv_info; + } + $match = true; + } + } + + // Other versioned keys + $vKeys = array_shift(NagRedis::keys(md5('deployment:'.$deployment).":".$results['version'].":*", true)); + sort($vKeys); + foreach ($vKeys as $vKey) { + if (preg_match("/$search/", $vKey)) { + if (isset($results['versioned']['key_name'])) { + array_push($results['versioned']['key_name'], $vKey); + } else { + $results['versioned']['key_name'] = array($vKey); + } + $match = true; + } + list($match_v_info, $match_flag) = self::searchRedisKey($vKey, $search); + if ($match_flag) { + if (isset($results['versioned']['key_value'][$vKey])) { + array_push($results['versioned']['key_value'][$vKey], $match_v_info); + } else { + $results['versioned']['key_value'][$vKey] = $match_v_info; + } + $match = true; + } + } + + $results['match'] = $match; + return $results; + } + + /* + * searchHash - search a hash type key + */ + private static function searchHash ($hashKey, $search) { + $results = array(); + $match = false; + + //echo var_dump($hashKey,true); exit; + foreach (NagRedis::hKeys($hashKey) as $hashKeyKey) { + if (preg_match("/$search/", $hashKeyKey)) { + if (isset($results['key_name'])) { + array_push($results['key_name'], "$hashKeyKey"); + } else { + $results['key_name'] = array("$hashKeyKey"); + } + $match = true; + } + $keyValue = NagRedis::hget($hashKey, $hashKeyKey); + if (preg_match("/$search/", $keyValue)) { + if (isset($results['key_value'])) { + array_push($results['key_value'], "$hashKeyKey = $keyValue"); + } else { + $results['key_value'] = array("$hashKeyKey = $keyValue"); + } + $match = true; + } + } + return array($results, $match); + } + + /* + * searchSet - search a set type key + */ + private static function searchSet ($key, $search) { + + $results = array(); + $match = false; + + $smembers = NagRedis::sMembers($key); + + foreach ($smembers as $smember) { + if (preg_match("/$search/", $smember)) { + if (isset($results['key_value'])) { + array_push($results['key_value'], $smember); + } else { + $results['key_value'] = array($smember); + } + $match = true; + } + } + return array($results, $match); + } + + /* + * searchRedisKey - given a key, determine it's type and look at it's data + */ + private static function searchRedisKey ($key, $search) { + /* Redis Types + * string: Redis::REDIS_STRING + * set: Redis::REDIS_SET + * list: Redis::REDIS_LIST + * zset: Redis::REDIS_ZSET + * hash: Redis::REDIS_HASH + * other: Redis::REDIS_NOT_FOUND + */ + $type = NagRedis::type($key); + switch ($type) { + case Redis::REDIS_HASH: + return self::searchHash($key, $search); + break; + case Redis::REDIS_SET: + return self::searchSet($key, $search); + break; + default: + error_log("ERROR: searchRedisKey: Unknown type: $key"); + } + return array(array(), false); + } + +} diff --git a/lib/class_mapping.ini b/lib/class_mapping.ini index 132e2ec..e9aaae3 100644 --- a/lib/class_mapping.ini +++ b/lib/class_mapping.ini @@ -23,6 +23,7 @@ NagTester = include/NagTester.class.php NRPECreate = include/NRPECreate.class.php NRPERpm = include/NRPERpm.class.php RevDeploy = include/RevDeploy.class.php +SaigonSearch = include/SaigonSearch.class.php VarnishCache = include/VarnishCache.class.php ;;;; diff --git a/views/nrpe_cfg_stage.php b/views/nrpe_cfg_stage.php index 5641d08..ef4308d 100644 --- a/views/nrpe_cfg_stage.php +++ b/views/nrpe_cfg_stage.php @@ -140,11 +140,11 @@ function( input ) { Command Timeout: - + Connection Timeout: - + diff --git a/views/search.php b/views/search.php new file mode 100644 index 0000000..3d406d9 --- /dev/null +++ b/views/search.php @@ -0,0 +1,177 @@ + + + + + + + + + + 'command', + 'hostgroup' => 'hostgrp', + 'hostgroups' => 'hostgrp', + 'hosttemplate' => 'hosttemp', + 'hosttemplates' => 'hosttemp', + 'nodetemplate' => 'ngnt', + 'nodetemplates' => 'ngnt', + 'nrpecmds' => 'nrpecmd', + 'nrpeplugins' => 'nrpeplugin', + 'svcs' => 'svc', + 'timeperiods' => 'timeperiod', + 'contacttemplate' => 'contacttemp', + 'contacttemplates' => 'contacttemp', + 'contacts' => 'contact', + ); + + $cmdActionMap = array( 'ngnt' => 'manage' ); + + foreach ($viewData->searchResults as $deployment => $matches) { + if (isset($matches['match']) && $matches['match']) { + + echo "

Match in Deployment: $deployment

\n"; + + // Deployment Info Section + if (isset($matches['deployment_info']) || isset($matches['nonversioned'])) { + echo "Deployment Info\n"; + if (isset($matches['deployment_info']['key_name']) && count($matches['deployment_info']['key_name'])) { + echo "\n"; + } + if (isset($matches['deployment_info']['key_value']) && count($matches['deployment_info']['key_value'])) { + echo "\n"; + } + } + + // hostsearch* keys + $replaceMe = "/^".$matches['deployment_hash'].":/"; + if (isset($matches['nonversioned'])) { + if (isset($matches['nonversioned']['key_name']) && count($matches['nonversioned']['key_name'])) { + echo "\n"; + } + if (isset($matches['nonversioned']['key_value']) && count($matches['nonversioned']['key_value'])) { + echo "\n"; + } + } + + // All Versioned Keys + if (isset($matches['versioned'])) { + echo "

Versioned Info (version: ".$matches['version'].")

"; + $replaceMe = "/^".$matches['deployment_hash'].":".$matches['version'].":/"; + $lastLink = ''; + + if (isset($matches['versioned']['key_name']) && count($matches['versioned']['key_name'])) { + echo "$link\n"; + } + if (isset($matches['versioned']['key_value']) && count($matches['versioned']['key_value'])) { + echo "$link\n"; + } + } + + + } + } +?> + +
+

Search Data Dump:

+
+search =  search; ?>
+
+deployment =  deployment; ?>
+
+--------------------------------------------------------------------------------
+searchResults, true); ?>
+
+--------------------------------------------------------------------------------
+
+
+
+ + + + + + +
@@ -51,8 +65,14 @@ function getDeploymentMenu() {
+ +
+
+ Search: +
+
-
+
diff --git a/www/index.php b/www/index.php index 8d0f4ae..e5b22e2 100644 --- a/www/index.php +++ b/www/index.php @@ -20,7 +20,7 @@ - - + + From 206b149aa48c4937df1e32c6b59eaef2a4804aa0 Mon Sep 17 00:00:00 2001 From: Leo Nishio Date: Thu, 24 Jul 2014 13:08:00 -0700 Subject: [PATCH 2/3] Update repo --- include/SaigonSearch.class.php | 2 +- views/search.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/include/SaigonSearch.class.php b/include/SaigonSearch.class.php index 0b4059a..a6e635d 100644 --- a/include/SaigonSearch.class.php +++ b/include/SaigonSearch.class.php @@ -1,7 +1,7 @@ Date: Mon, 4 Aug 2014 11:45:54 -0700 Subject: [PATCH 3/3] Search Feature fixes --- include/SaigonSearch.class.php | 6 ++++++ views/search.php | 28 ++++++++++++++++++++++++++-- views/site_input.php | 9 ++++++++- 3 files changed, 40 insertions(+), 3 deletions(-) diff --git a/include/SaigonSearch.class.php b/include/SaigonSearch.class.php index a6e635d..4fee1d0 100644 --- a/include/SaigonSearch.class.php +++ b/include/SaigonSearch.class.php @@ -108,6 +108,8 @@ private static function searchDeployment($deployment, $search) { $vKeys = array_shift(NagRedis::keys(md5('deployment:'.$deployment).":".$results['version'].":*", true)); sort($vKeys); foreach ($vKeys as $vKey) { + // skip buildoutput key + if (preg_match('/buildoutput/', $vKey)) continue; if (preg_match("/$search/", $vKey)) { if (isset($results['versioned']['key_name'])) { array_push($results['versioned']['key_name'], $vKey); @@ -149,6 +151,10 @@ private static function searchHash ($hashKey, $search) { $match = true; } $keyValue = NagRedis::hget($hashKey, $hashKeyKey); + // decode base64 encoded key values + if ($hashKeyKey == 'command_line' || $hashKeyKey == 'cmd_line' || $hashKeyKey == 'location' || $hashKeyKey == 'file' || preg_match('/^USER|^ARG/', $hashKeyKey)) { + $keyValue = '
'.base64_decode($keyValue).'
'; + } if (preg_match("/$search/", $keyValue)) { if (isset($results['key_value'])) { array_push($results['key_value'], "$hashKeyKey = $keyValue"); diff --git a/views/search.php b/views/search.php index 558af85..72745c0 100644 --- a/views/search.php +++ b/views/search.php @@ -92,8 +92,15 @@ function goto (deployment) { if (isset($matches['nonversioned']['key_value']) && count($matches['nonversioned']['key_value'])) { echo "
    \n"; foreach ($matches['nonversioned']['key_value'] as $nvKey => $nvVal) { - foreach ($nvVal['key_value'] as $parmVal) { - echo "
  • Value match in ".preg_replace($replaceMe, '', $nvKey).": $parmVal
  • \n"; + if (isset($nvVal['key_name'])) { + foreach ($nvVal['key_name'] as $parmVal) { + echo "
  • Key Name match in ".preg_replace($replaceMe, '', $nvKey).": $parmVal
  • \n"; + } + } + if (isset($nvVal['key_value'])) { + foreach ($nvVal['key_value'] as $parmVal) { + echo "
  • Value match in ".preg_replace($replaceMe, '', $nvKey).": $parmVal
  • \n"; + } } } echo "
\n"; @@ -128,6 +135,23 @@ function goto (deployment) { if (isset($matches['versioned']['key_value']) && count($matches['versioned']['key_value'])) { echo "
    \n"; foreach ($matches['versioned']['key_value'] as $vKey => $vVal) { + if (isset($vVal['key_name'])) { + $stripped = preg_replace($replaceMe, '', $vKey); + list($cmd) = explode(':', $stripped); + if (isset($cmdControllerMap[$cmd])) $cmd = $cmdControllerMap[$cmd]; + if ($cmd != 'testoutput') { + $action = 'stage'; + if (isset($cmdActionMap[$cmd])) $action = $cmdActionMap[$cmd]; + $link = "$cmd"; + if ($link != $lastLink) { + echo "
$link
    \n"; + } + foreach ($vVal['key_name'] as $parmVal) { + echo "
  • Key Name match in ".$stripped.": $parmVal
  • \n"; + } + $lastLink = $link; + } + } if (isset($vVal['key_value'])) { $stripped = preg_replace($replaceMe, '', $vKey); list($cmd) = explode(':', $stripped); diff --git a/views/site_input.php b/views/site_input.php index bc7d3e2..134f507 100644 --- a/views/site_input.php +++ b/views/site_input.php @@ -44,6 +44,13 @@ function runSearch() { alert("A search term must be entered"); } } +function inputKeyUp(e) { + e.which = e.which || e.keyCode; + if(e.which == 13) { + runSearch(); + return false; + } +} @@ -68,7 +75,7 @@ function runSearch() {
    - Search: + Search: