From 94c75daa81323046f02eaf4ade2e985afc133e59 Mon Sep 17 00:00:00 2001 From: intsimoa Date: Sat, 7 Jul 2012 01:00:10 +0200 Subject: [PATCH 1/3] [NEW] wfsolr_ derived from solr-stats, with some additions: * Shorter pattern solr-[name of the core]-alias * Suggest implementation * Support for solr_(host|port|webapp) environment variables * Default core handling * Error handling --- plugins/solr/wfsolr_ | 182 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 182 insertions(+) create mode 100755 plugins/solr/wfsolr_ diff --git a/plugins/solr/wfsolr_ b/plugins/solr/wfsolr_ new file mode 100755 index 00000000..1473979b --- /dev/null +++ b/plugins/solr/wfsolr_ @@ -0,0 +1,182 @@ +#!/usr/bin/php + array("CORE", "searcher", "numDocs"), + "avgTimePerRequest" => array("QUERYHANDLER", "/select", "avgTimePerRequest"), + "avgRequestsPerSecond" => array("QUERYHANDLER", "/select", "avgRequestsPerSecond"), + "errors" => array("QUERYHANDLER", "/select", "errors"), + "timeouts" => array("QUERYHANDLER", "/select", "timeouts"), + "indexSize" => array("QUERYHANDLER", "/replication", "indexSize"), + "queryResultCacheSize" => array("CACHE", "queryResultCache", "size"), + "queryResultCacheHitRatio" => array("CACHE", "queryResultCache", "hitratio"), + "queryResultCacheLookups" => array("CACHE", "queryResultCache", "lookups"), + "queryResultCacheWarmupTime" => array("CACHE", "queryResultCache", "warmupTime"), + "documentCacheSize" => array("CACHE", "documentCache", "size"), + "documentCacheHitRatio" => array("CACHE", "documentCache", "hitratio"), + "documentCacheLookups" => array("CACHE", "documentCache", "lookups"), + "documentCacheWarmupTime" => array("CACHE", "documentCache", "warmupTime"), + "filterCacheSize" => array("CACHE", "filterCache", "size"), + "filterCacheHitRatio" => array("CACHE", "filterCache", "hitratio"), + "filterCacheLookups" => array("CACHE", "filterCache", "lookups"), + "filterCacheWarmupTime" => array("CACHE", "filterCache", "warmupTime")); + +if ($tabParamsCount == 5) +{ + $core = $tabParams[1]; + $category = $tabParams[2]; + $item = $tabParams[3]; + $property = $tabParams[4]; +} +elseif ($tabParamsCount == 3) +{ + $core = $tabParams[1]; + $pathAlias = $tabParams[2]; +} +elseif ($tabParamsCount == 2) +{ + $pathAlias = $tabParams[1]; +} + +if (isset($pathAlias)) +{ + if (isset($pathAliases[$pathAlias])) + { + list($category, $item, $property) = $pathAliases[$pathAlias]; + } + else + { + echo "Unknown alias: $pathAlias\n"; + exit(1); + } +} + +function getenvdef($name, $defaultValue) +{ + $val = getenv($name); + if ($val === false) + { + return $defaultValue; + } + return $val; +} + +function getSolrAdminUrl($core = null) +{ + $solrHost = getenvdef("solr_host", "127.0.0.1"); + $solrPort = getenvdef("solr_port", "8080"); + $solrWebappName = getenvdef("solr_webapp", "solr"); + $url = "http://$solrHost:$solrPort/$solrWebappName/"; + if ($core !== null) + { + $url .= "$core/"; + } + $url .= "/admin"; + return $url; +} + +if ("config" == $action) +{ + echo 'graph_category Solr ' . $core . "\n"; + echo 'graph_title ' . $item . ' ' . $property . "\n"; + echo 'graph_vlabel ' . $property . "\n"; + if ($core !== null) + { + echo $core; + } + else + { + echo "Default_core"; + } + echo $item . $property . 'solr.label ' . $property . "\n"; +} +elseif ("suggest" == $action) +{ + $url = getSolrAdminUrl()."/cores?action=STATUS"; + $doc = new DOMDocument(); + if (!$doc->load($url)) + { + echo "Could not load $url as XML\n"; + exit(4); + } + $xpath = new DOMXpath($doc); + $names = $xpath->query("/response/lst[@name='status']/lst/str[@name='name']"); + $aliases = array_keys($pathAliases); + foreach ($names as $nameAttr) + { + $coreName = trim($nameAttr->textContent); + foreach ($aliases as $alias) + { + if ($coreName) + { + echo "$coreName-"; + } + echo "$alias\n"; + } + } +} +else +{ + if ($category === null) + { + echo "No core defined\n"; + exit(5); + } + $url = getSolrAdminUrl($core)."/stats.jsp"; + $doc = new DOMDocument(); + if (!$doc->load($url)) + { + echo "Could not load $url as XML\n"; + exit(6); + } + + $xpath = new DOMXpath($doc); + $elements = $xpath->query('/solr/solr-info/' . $category . '/entry'); + + foreach($elements as $element) + { + if($item == trim($element->getElementsByTagName('name')->item(0)->textContent)) + { + $stats = $element->getElementsByTagName('stat'); + foreach($stats as $stat) + { + if($property == trim($stat->getAttribute('name'))) + { + echo $core . $item . $property . 'solr.value ' . floatval(trim($stat->textContent)) . "\n"; + exit(0); + } + } + } + } + echo "Bad path: $category | $item | $property\n"; + exit(7); +} From 5c77c1a93b85f791c03a9760fcb2c2c1684755a5 Mon Sep 17 00:00:00 2001 From: intsimoa Date: Sat, 7 Jul 2012 11:50:44 +0200 Subject: [PATCH 2/3] [FIX] wfsolr_indexSize: always output values in the same unit (MB) --- plugins/solr/wfsolr_ | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/plugins/solr/wfsolr_ b/plugins/solr/wfsolr_ index 1473979b..01408f39 100755 --- a/plugins/solr/wfsolr_ +++ b/plugins/solr/wfsolr_ @@ -104,6 +104,24 @@ function getSolrAdminUrl($core = null) return $url; } +/** + * Assure some conversions. KB and GB converted to MB + */ +function wffloatval($val) +{ + $fVal = floatval(str_replace(",", ".", $val)); + $valEnd = substr($val, -2); + if ($valEnd == "KB") + { + $fVal = $fVal / 1024; + } + elseif ($valEnd == "GB") + { + $fVal = $fVal * 1024; + } + return $fVal; +} + if ("config" == $action) { echo 'graph_category Solr ' . $core . "\n"; @@ -171,7 +189,7 @@ else { if($property == trim($stat->getAttribute('name'))) { - echo $core . $item . $property . 'solr.value ' . floatval(trim($stat->textContent)) . "\n"; + echo $core . $item . $property . 'solr.value ' . wffloatval(trim($stat->textContent)) . "\n"; exit(0); } } From de967ad499070d11c46d29135630ed47e06d2332 Mon Sep 17 00:00:00 2001 From: intsimoa Date: Mon, 9 Jul 2012 09:42:22 +0200 Subject: [PATCH 3/3] [NEW] use curl instead of relying on allow_url_fopen if extension exists [FIX] indexSize now returns values in Bytes and graph is 1024 based [NEW] fieldValueCache aliases declaration [FIX] (minor) admin path contains two '/' --- plugins/solr/wfsolr_ | 183 ++++++++++++++++++++++++++++--------------- 1 file changed, 118 insertions(+), 65 deletions(-) diff --git a/plugins/solr/wfsolr_ b/plugins/solr/wfsolr_ index 01408f39..5f71706a 100755 --- a/plugins/solr/wfsolr_ +++ b/plugins/solr/wfsolr_ @@ -1,27 +1,31 @@ #!/usr/bin/php -alias ; use suggest to get the list of available aliases * - suggest implementation * - support for solr_(host|port|webapp) environment variables * - default core handling * - error handling - * - TODO: use curl to make requests + * - unit conversion + * - use curl to get URL contents instead of relying on allow_url_fopen */ $action = isset($argv[1]) ? $argv[1] : ''; @@ -45,6 +49,10 @@ $pathAliases = array("numDocs" => array("CORE", "searcher", "numDocs"), "documentCacheHitRatio" => array("CACHE", "documentCache", "hitratio"), "documentCacheLookups" => array("CACHE", "documentCache", "lookups"), "documentCacheWarmupTime" => array("CACHE", "documentCache", "warmupTime"), + "fieldValueCacheSize" => array("CACHE", "fieldValueCache", "size"), + "fieldValueCacheHitRatio" => array("CACHE", "fieldValueCache", "hitratio"), + "fieldValueCacheLookups" => array("CACHE", "fieldValueCache", "lookups"), + "fieldValueCacheWarmupTime" => array("CACHE", "filterCache", "warmupTime"), "filterCacheSize" => array("CACHE", "filterCache", "size"), "filterCacheHitRatio" => array("CACHE", "filterCache", "hitratio"), "filterCacheLookups" => array("CACHE", "filterCache", "lookups"), @@ -100,12 +108,12 @@ function getSolrAdminUrl($core = null) { $url .= "$core/"; } - $url .= "/admin"; + $url .= "admin"; return $url; } /** - * Assure some conversions. KB and GB converted to MB + * Assure some conversions. KB, MB and GB are converted to Bytes */ function wffloatval($val) { @@ -113,88 +121,133 @@ function wffloatval($val) $valEnd = substr($val, -2); if ($valEnd == "KB") { - $fVal = $fVal / 1024; + $fVal = $fVal * 1024; + } + elseif ($valEnd == "MB") + { + $fVal = $fVal * 1048576; } elseif ($valEnd == "GB") { - $fVal = $fVal * 1024; + $fVal = $fVal * 1073741824; } return $fVal; } -if ("config" == $action) +function wfGetUrl($url) { - echo 'graph_category Solr ' . $core . "\n"; - echo 'graph_title ' . $item . ' ' . $property . "\n"; - echo 'graph_vlabel ' . $property . "\n"; - if ($core !== null) + if (extension_loaded("curl")) { - echo $core; + $ch = curl_init(); + + $options = array(CURLOPT_URL => $url); + $options[CURLOPT_TIMEOUT] = 5; + $options[CURLOPT_CONNECTTIMEOUT] = 5; + $options[CURLOPT_RETURNTRANSFER] = true; + + curl_setopt_array($ch, $options); + + $content = curl_exec($ch); + curl_close($ch); } else { - echo "Default_core"; + $content = file_get_contents($url); } - echo $item . $property . 'solr.label ' . $property . "\n"; + if ($content === false) + { + throw new Exception("Could not get $url", 8); + } + + return $content; } -elseif ("suggest" == $action) + +try { - $url = getSolrAdminUrl()."/cores?action=STATUS"; - $doc = new DOMDocument(); - if (!$doc->load($url)) + if ("config" == $action) { - echo "Could not load $url as XML\n"; - exit(4); - } - $xpath = new DOMXpath($doc); - $names = $xpath->query("/response/lst[@name='status']/lst/str[@name='name']"); - $aliases = array_keys($pathAliases); - foreach ($names as $nameAttr) - { - $coreName = trim($nameAttr->textContent); - foreach ($aliases as $alias) + if ($property == "indexSize") { - if ($coreName) + echo "graph_args --base 1024 -l 0\n"; + } + echo "graph_category Solr $core\n"; + echo "graph_title $item $property\n"; + echo "graph_vlabel $property\n"; + if ($core !== null) + { + echo $core; + } + else + { + echo "Default_core"; + } + echo $item . $property . 'solr.label ' . $property . "\n"; + } + elseif ("suggest" == $action) + { + $url = getSolrAdminUrl()."/cores?action=STATUS"; + $doc = new DOMDocument(); + if (!$doc->loadXML(wfGetUrl($url))) + { + echo "Could not load $url as XML\n"; + exit(4); + } + $xpath = new DOMXpath($doc); + $names = $xpath->query("/response/lst[@name='status']/lst/str[@name='name']"); + $aliases = array_keys($pathAliases); + foreach ($names as $nameAttr) + { + $coreName = trim($nameAttr->textContent); + foreach ($aliases as $alias) { - echo "$coreName-"; + if ($coreName) + { + echo "$coreName-"; + } + echo "$alias\n"; } - echo "$alias\n"; } } -} -else -{ - if ($category === null) + else { - echo "No core defined\n"; - exit(5); - } - $url = getSolrAdminUrl($core)."/stats.jsp"; - $doc = new DOMDocument(); - if (!$doc->load($url)) - { - echo "Could not load $url as XML\n"; - exit(6); - } - - $xpath = new DOMXpath($doc); - $elements = $xpath->query('/solr/solr-info/' . $category . '/entry'); - - foreach($elements as $element) - { - if($item == trim($element->getElementsByTagName('name')->item(0)->textContent)) + if ($category === null) { - $stats = $element->getElementsByTagName('stat'); - foreach($stats as $stat) + echo "No core defined\n"; + exit(5); + } + $url = getSolrAdminUrl($core)."/stats.jsp"; + $doc = new DOMDocument(); + if (!$doc->loadXML(wfGetUrl($url))) + { + echo "Could not load $url as XML\n"; + exit(6); + } + + $xpath = new DOMXpath($doc); + $elements = $xpath->query('/solr/solr-info/' . $category . '/entry'); + + foreach($elements as $element) + { + if($item == trim($element->getElementsByTagName('name')->item(0)->textContent)) { - if($property == trim($stat->getAttribute('name'))) + $stats = $element->getElementsByTagName('stat'); + foreach($stats as $stat) { - echo $core . $item . $property . 'solr.value ' . wffloatval(trim($stat->textContent)) . "\n"; - exit(0); + if($property == trim($stat->getAttribute('name'))) + { + echo $core . $item . $property . 'solr.value ' . wffloatval(trim($stat->textContent)) . "\n"; + exit(0); + } } } } + echo "Bad path: $category | $item | $property\n"; + exit(7); } - echo "Bad path: $category | $item | $property\n"; - exit(7); +} +catch (Exception $e) +{ + echo "ERROR: ".$e->getMessage()."\n"; + $exitCode = ($e->getCode() != 0) ? $e->getCode() : 1; + exit($exitCode); }