Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Saigon Search Feature #1

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions controllers/CoreController.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

}

219 changes: 219 additions & 0 deletions include/SaigonSearch.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,219 @@
<?php
//
// Copyright (c) 2014, Zynga Inc.
// https://github.com/mhwest13
// Author: Leo Nishio
// License: BSD 2-Clause
//

class SaigonSearch {

/*
* search - search each deployment based on what is stored in Redis for the current version
*/
public static function search($deploymentLimit, $search) {

$results = array();

if (! empty($search)) {

$deployments = self::getDeployments($deploymentLimit);

foreach ($deployments as $deployment) {
$results[$deployment] = self::searchDeployment($deployment, $search);
}
}

return $results;
}

/*
* getDeployments - get an authorized list of deployments
*/
private static function getDeployments($deployment = null) {

$amodule = AUTH_MODULE;
$authmodule = new $amodule();

$viewDeployments = array();
$deployments = RevDeploy::getDeployments();

if (empty($deployment)) {
foreach ($deployments as $deployment) {
if ($authmodule->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) {
// 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);
} 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);
// decode base64 encoded key values
if ($hashKeyKey == 'command_line' || $hashKeyKey == 'cmd_line' || $hashKeyKey == 'location' || $hashKeyKey == 'file' || preg_match('/^USER|^ARG/', $hashKeyKey)) {
$keyValue = '<pre>'.base64_decode($keyValue).'</pre>';
}
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);
}

}
1 change: 1 addition & 0 deletions lib/class_mapping.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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

;;;;
Expand Down
4 changes: 2 additions & 2 deletions views/nrpe_cfg_stage.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,11 @@ function( input ) {
</tr><tr>
<th style="width:25%;text-align:right;">Command Timeout:</th>
<td style="width:25%;text-align:left;">
<input type="text" value="<?php echo $cmdtimeout?>" size="2" maxlength="2" id="cmdtimeout" name="cmdtimeout" />
<input type="text" value="<?php echo $cmdtimeout?>" size="3" maxlength="3" id="cmdtimeout" name="cmdtimeout" />
</td>
<th style="width:25%;text-align:right;">Connection Timeout:</th>
<td style="width:25%;text-align:left;">
<input type="text" value="<?php echo $conntimeout?>" size="2" maxlength="2" id="conntimeout" name="conntimeout" />
<input type="text" value="<?php echo $conntimeout?>" size="3" maxlength="3" id="conntimeout" name="conntimeout" />
</td>
</tr><tr>
<th colspan="1" style="width:25%;text-align:right;">
Expand Down
Loading