2012-07-07 01:00:10 +02:00
#!/usr/bin/php
<?php
/**
2012-07-09 09:42:22 +02:00
* Wfsolr Plugin https://github.com/lexsimon/contrib/master/plugins/solr/wfsolr_
2012-07-07 01:00:10 +02:00
* "Wf" stands for "RBS Web Factory" (http://www.rbs.fr.fr/webfactory/)
2012-07-09 09:42:22 +02:00
* @author : alexandre.simon@rbs.fr
*
2012-07-07 01:00:10 +02:00
* Derived from nicolas.moussikian@shopbot-inc.com's plugin (https://raw.github.com/munin-monitoring/contrib/master/plugins/solr/solr-stats)
2012-07-09 09:42:22 +02:00
* This plugin allows to graph any data present in the stats report on a
* multi-core Solr instance
2012-07-07 01:00:10 +02:00
* AKA : http://127.0.0.1:8080/solr/[name of the core]/admin/stats.jsp
* Verify the server where the munin-node instance is can access that URL
*
2012-07-09 09:42:22 +02:00
* You need to have a PHP 5.2.6+ CLI installed too with curl extension or
* allow_url_fopen directive on
2018-08-02 02:03:42 +02:00
*
2012-07-07 01:00:10 +02:00
* Once the plugin is available you can symlink it with the following naming convention :
2012-07-09 09:42:22 +02:00
* wfsolr-[name of the core]-[name of the stats section - ex.: CORE]-[name of the entry in the xml - ex.: searcher]-[name of the stat to graph - ex.: numDocs]
2012-07-07 01:00:10 +02:00
*
2012-07-09 09:42:22 +02:00
* Alexandre SIMON additions:
* - wfsolr-<coreName>-alias ; use suggest to get the list of available aliases
2012-07-07 01:00:10 +02:00
* - suggest implementation
* - support for solr_(host|port|webapp) environment variables
* - default core handling
* - error handling
2012-07-09 09:42:22 +02:00
* - unit conversion
* - use curl to get URL contents instead of relying on allow_url_fopen
2012-07-07 01:00:10 +02:00
*/
$action = isset($argv[1]) ? $argv[1] : '';
$core = null;
$category = null;
$tabParams = explode('-', $argv[0]);
$tabParamsCount = count($tabParams);
$pathAliases = array("numDocs" => 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"),
2012-07-09 09:42:22 +02:00
"fieldValueCacheSize" => array("CACHE", "fieldValueCache", "size"),
"fieldValueCacheHitRatio" => array("CACHE", "fieldValueCache", "hitratio"),
"fieldValueCacheLookups" => array("CACHE", "fieldValueCache", "lookups"),
"fieldValueCacheWarmupTime" => array("CACHE", "filterCache", "warmupTime"),
2012-07-07 01:00:10 +02:00
"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/";
}
2012-07-09 09:42:22 +02:00
$url .= "admin";
2012-07-07 01:00:10 +02:00
return $url;
}
2012-07-07 11:50:44 +02:00
/**
2012-07-09 09:42:22 +02:00
* Assure some conversions. KB, MB and GB are converted to Bytes
2012-07-07 11:50:44 +02:00
*/
function wffloatval($val)
{
$fVal = floatval(str_replace(",", ".", $val));
$valEnd = substr($val, -2);
if ($valEnd == "KB")
{
2012-07-09 09:42:22 +02:00
$fVal = $fVal * 1024;
}
elseif ($valEnd == "MB")
{
$fVal = $fVal * 1048576;
2012-07-07 11:50:44 +02:00
}
elseif ($valEnd == "GB")
{
2012-07-09 09:42:22 +02:00
$fVal = $fVal * 1073741824;
2012-07-07 11:50:44 +02:00
}
return $fVal;
}
2012-07-09 09:42:22 +02:00
function wfGetUrl($url)
2012-07-07 01:00:10 +02:00
{
2012-07-09 09:42:22 +02:00
if (extension_loaded("curl"))
2012-07-07 01:00:10 +02:00
{
2012-07-09 09:42:22 +02:00
$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);
2012-07-07 01:00:10 +02:00
}
else
{
2012-07-09 09:42:22 +02:00
$content = file_get_contents($url);
2012-07-07 01:00:10 +02:00
}
2012-07-09 09:42:22 +02:00
if ($content === false)
{
throw new Exception("Could not get $url", 8);
}
return $content;
2012-07-07 01:00:10 +02:00
}
2012-07-09 09:42:22 +02:00
try
2012-07-07 01:00:10 +02:00
{
2012-07-09 09:42:22 +02:00
if ("config" == $action)
2012-07-07 01:00:10 +02:00
{
2012-07-09 09:42:22 +02:00
if ($property == "indexSize")
{
echo "graph_args --base 1024 -l 0\n";
}
2017-02-22 20:59:43 +01:00
echo "graph_category search $core\n";
2012-07-09 09:42:22 +02:00
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";
2012-07-07 01:00:10 +02:00
}
2012-07-09 09:42:22 +02:00
elseif ("suggest" == $action)
2012-07-07 01:00:10 +02:00
{
2012-07-09 09:42:22 +02:00
$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)
2012-07-07 01:00:10 +02:00
{
2012-07-09 09:42:22 +02:00
$coreName = trim($nameAttr->textContent);
foreach ($aliases as $alias)
2012-07-07 01:00:10 +02:00
{
2012-07-09 09:42:22 +02:00
if ($coreName)
{
echo "$coreName-";
}
echo "$alias\n";
2012-07-07 01:00:10 +02:00
}
}
}
2012-07-09 09:42:22 +02:00
else
2012-07-07 01:00:10 +02:00
{
2012-07-09 09:42:22 +02:00
if ($category === null)
{
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);
}
2012-07-07 01:00:10 +02:00
2012-07-09 09:42:22 +02:00
$xpath = new DOMXpath($doc);
$elements = $xpath->query('/solr/solr-info/' . $category . '/entry');
2012-07-07 01:00:10 +02:00
2012-07-09 09:42:22 +02:00
foreach($elements as $element)
2012-07-07 01:00:10 +02:00
{
2012-07-09 09:42:22 +02:00
if($item == trim($element->getElementsByTagName('name')->item(0)->textContent))
2012-07-07 01:00:10 +02:00
{
2012-07-09 09:42:22 +02:00
$stats = $element->getElementsByTagName('stat');
foreach($stats as $stat)
2012-07-07 01:00:10 +02:00
{
2012-07-09 09:42:22 +02:00
if($property == trim($stat->getAttribute('name')))
{
echo $core . $item . $property . 'solr.value ' . wffloatval(trim($stat->textContent)) . "\n";
exit(0);
}
2012-07-07 01:00:10 +02:00
}
}
}
2012-07-09 09:42:22 +02:00
echo "Bad path: $category | $item | $property\n";
exit(7);
2012-07-07 01:00:10 +02:00
}
2012-07-09 09:42:22 +02:00
}
catch (Exception $e)
{
echo "ERROR: ".$e->getMessage()."\n";
$exitCode = ($e->getCode() != 0) ? $e->getCode() : 1;
exit($exitCode);
2012-07-07 01:00:10 +02:00
}