Projet

Général

Profil

Paste
Télécharger au format
Statistiques
| Branche: | Révision:

root / plugins / solr / wfsolr_ @ ef960abc

Historique | Voir | Annoter | Télécharger (6,71 ko)

1
#!/usr/bin/php
2
<?php
3
/**
4
 * Wfsolr Plugin https://github.com/lexsimon/contrib/master/plugins/solr/wfsolr_
5
 * "Wf" stands for "RBS Web Factory" (http://www.rbs.fr.fr/webfactory/)
6
 * @author : alexandre.simon@rbs.fr
7
 *
8
 * Derived from nicolas.moussikian@shopbot-inc.com's plugin (https://raw.github.com/munin-monitoring/contrib/master/plugins/solr/solr-stats)
9

    
10
 * This plugin allows to graph any data present in the stats report on a
11
 * multi-core Solr instance
12
 * AKA : http://127.0.0.1:8080/solr/[name of the core]/admin/stats.jsp
13
 * Verify the server where the munin-node instance is can access that URL
14
 *
15
 * You need to have a PHP 5.2.6+ CLI installed too with curl extension or
16
 * allow_url_fopen directive on
17
 * 
18
 * Once the plugin is available you can symlink it with the following naming convention :
19
 * 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]
20
 *
21
 * Alexandre SIMON additions:
22
 * - wfsolr-<coreName>-alias ; use suggest to get the list of available aliases
23
 * - suggest implementation
24
 * - support for solr_(host|port|webapp) environment variables
25
 * - default core handling
26
 * - error handling
27
 * - unit conversion
28
 * - use curl to get URL contents instead of relying on allow_url_fopen
29
 */
30

    
31
$action = isset($argv[1]) ? $argv[1] : '';
32
$core = null;
33
$category = null;
34

    
35
$tabParams = explode('-', $argv[0]);
36
$tabParamsCount = count($tabParams);
37

    
38
$pathAliases = array("numDocs" => array("CORE", "searcher", "numDocs"),
39
		"avgTimePerRequest" => array("QUERYHANDLER", "/select", "avgTimePerRequest"),
40
		"avgRequestsPerSecond" => array("QUERYHANDLER", "/select", "avgRequestsPerSecond"),
41
		"errors" => array("QUERYHANDLER", "/select", "errors"),
42
		"timeouts" => array("QUERYHANDLER", "/select", "timeouts"),
43
		"indexSize" => array("QUERYHANDLER", "/replication", "indexSize"),
44
		"queryResultCacheSize" => array("CACHE", "queryResultCache", "size"),
45
		"queryResultCacheHitRatio" => array("CACHE", "queryResultCache", "hitratio"),
46
		"queryResultCacheLookups" => array("CACHE", "queryResultCache", "lookups"),
47
		"queryResultCacheWarmupTime" => array("CACHE", "queryResultCache", "warmupTime"),
48
		"documentCacheSize" => array("CACHE", "documentCache", "size"),
49
		"documentCacheHitRatio" => array("CACHE", "documentCache", "hitratio"),
50
		"documentCacheLookups" => array("CACHE", "documentCache", "lookups"),
51
		"documentCacheWarmupTime" => array("CACHE", "documentCache", "warmupTime"),
52
		"fieldValueCacheSize" => array("CACHE", "fieldValueCache", "size"),
53
		"fieldValueCacheHitRatio" => array("CACHE", "fieldValueCache", "hitratio"),
54
		"fieldValueCacheLookups" => array("CACHE", "fieldValueCache", "lookups"),
55
		"fieldValueCacheWarmupTime" => array("CACHE", "filterCache", "warmupTime"),
56
		"filterCacheSize" => array("CACHE", "filterCache", "size"),
57
		"filterCacheHitRatio" => array("CACHE", "filterCache", "hitratio"),
58
		"filterCacheLookups" => array("CACHE", "filterCache", "lookups"),
59
		"filterCacheWarmupTime" => array("CACHE", "filterCache", "warmupTime"));
60

    
61
if ($tabParamsCount == 5)
62
{
63
	$core = $tabParams[1];
64
	$category = $tabParams[2];
65
	$item = $tabParams[3];
66
	$property = $tabParams[4];
67
}
68
elseif ($tabParamsCount == 3)
69
{
70
	$core = $tabParams[1];
71
	$pathAlias = $tabParams[2];
72
}
73
elseif ($tabParamsCount == 2)
74
{
75
	$pathAlias = $tabParams[1];
76
}
77

    
78
if (isset($pathAlias))
79
{
80
	if (isset($pathAliases[$pathAlias]))
81
	{
82
		list($category, $item, $property) = $pathAliases[$pathAlias];
83
	}
84
	else
85
	{
86
		echo "Unknown alias: $pathAlias\n";
87
		exit(1);
88
	}
89
}
90

    
91
function getenvdef($name, $defaultValue)
92
{
93
	$val = getenv($name);
94
	if ($val === false)
95
	{
96
		return $defaultValue;
97
	}
98
	return $val;
99
}
100

    
101
function getSolrAdminUrl($core = null)
102
{
103
	$solrHost = getenvdef("solr_host", "127.0.0.1");
104
	$solrPort = getenvdef("solr_port", "8080");
105
	$solrWebappName = getenvdef("solr_webapp", "solr");
106
	$url = "http://$solrHost:$solrPort/$solrWebappName/";
107
	if ($core !== null)
108
	{
109
		$url .= "$core/";
110
	}
111
	$url .= "admin";
112
	return $url;
113
}
114

    
115
/**
116
 * Assure some conversions. KB, MB and GB are converted to Bytes
117
 */
118
function wffloatval($val)
119
{
120
	$fVal = floatval(str_replace(",", ".", $val));
121
	$valEnd = substr($val, -2);
122
	if ($valEnd == "KB")
123
	{
124
		$fVal = $fVal * 1024;
125
	}
126
	elseif ($valEnd == "MB")
127
	{
128
		$fVal = $fVal * 1048576;
129
	}
130
	elseif ($valEnd == "GB")
131
	{
132
		$fVal = $fVal * 1073741824;
133
	}
134
	return $fVal;
135
}
136

    
137
function wfGetUrl($url)
138
{
139
	if (extension_loaded("curl"))
140
	{
141
		$ch = curl_init();
142

    
143
                $options = array(CURLOPT_URL => $url);
144
                $options[CURLOPT_TIMEOUT] = 5;
145
                $options[CURLOPT_CONNECTTIMEOUT] = 5;
146
		$options[CURLOPT_RETURNTRANSFER] = true;
147

    
148
                curl_setopt_array($ch, $options);
149

    
150
                $content = curl_exec($ch);
151
                curl_close($ch);
152
	}
153
	else
154
	{
155
		$content = file_get_contents($url);
156
	}
157
	if ($content === false)
158
	{
159
		throw new Exception("Could not get $url", 8);
160
	}
161

    
162
	return $content;
163
}
164

    
165
try
166
{
167
	if ("config" == $action)
168
	{
169
		if ($property == "indexSize")
170
		{
171
			echo "graph_args --base 1024 -l 0\n";
172
		}
173
		echo "graph_category Solr $core\n";
174
		echo "graph_title $item $property\n";
175
		echo "graph_vlabel $property\n";
176
		if ($core !== null)
177
		{
178
			echo $core;
179
		}
180
		else
181
		{
182
			echo "Default_core";
183
		}
184
		echo $item . $property . 'solr.label ' . $property . "\n";
185
	}
186
	elseif ("suggest" == $action)
187
	{
188
		$url = getSolrAdminUrl()."/cores?action=STATUS";
189
		$doc = new DOMDocument();
190
		if (!$doc->loadXML(wfGetUrl($url)))
191
		{
192
			echo "Could not load $url as XML\n";
193
			exit(4);
194
		}
195
		$xpath = new DOMXpath($doc);
196
		$names = $xpath->query("/response/lst[@name='status']/lst/str[@name='name']");
197
		$aliases = array_keys($pathAliases);
198
		foreach ($names as $nameAttr)
199
		{
200
			$coreName = trim($nameAttr->textContent);
201
			foreach ($aliases as $alias)
202
			{
203
				if ($coreName)
204
				{
205
					echo "$coreName-";
206
				}
207
				echo "$alias\n";
208
			}
209
		}
210
	}
211
	else
212
	{
213
		if ($category === null)
214
		{
215
			echo "No core defined\n";
216
			exit(5);
217
		}
218
		$url = getSolrAdminUrl($core)."/stats.jsp";
219
		$doc = new DOMDocument();
220
		if (!$doc->loadXML(wfGetUrl($url)))
221
		{
222
			echo "Could not load $url as XML\n";
223
			exit(6);
224
		}
225

    
226
		$xpath = new DOMXpath($doc);
227
		$elements = $xpath->query('/solr/solr-info/' . $category . '/entry');
228

    
229
		foreach($elements as $element)
230
		{
231
			if($item == trim($element->getElementsByTagName('name')->item(0)->textContent))
232
			{
233
				$stats = $element->getElementsByTagName('stat');
234
				foreach($stats as $stat)
235
				{
236
					if($property == trim($stat->getAttribute('name')))
237
					{
238
						echo $core . $item . $property . 'solr.value ' . wffloatval(trim($stat->textContent)) . "\n";
239
						exit(0);
240
					}
241
				}
242
			}
243
		}
244
		echo "Bad path: $category | $item | $property\n";
245
		exit(7);
246
	}
247
}
248
catch (Exception $e)
249
{
250
	echo "ERROR: ".$e->getMessage()."\n";
251
	$exitCode = ($e->getCode() != 0) ? $e->getCode() : 1;
252
	exit($exitCode);
253
}