Internet, UNIX, Video, Leisure…
Archive for mars, 2012
Monitor SolR document count and Cache Hitrate
8/03/12
A quick note with the python scipt I made to parse the Solr stats page and provide cache usage to Nagios.
It also return the number of documents indexed in SolR but without any check on it.
use -h option to get some help.
Copy this to your script folder of nagios and create a check like :
-
define command{
-
command_name check_solr_cache
-
command_line $USER1$/check_solr_cache.py -H $HOSTADDRESS$ -p $ARG1$ -w $ARG2$ -c $ARG3$ -P $ARG4$
-
;command_example !8080!30!10!/solr!
-
;$ARG1$ port – 8080
-
;$ARG2$ warning % – 30
-
;$ARG3$ critical % – 10
-
;$ARG4$ path to reach solr admin
-
}
If your are using a clustered solR, change the path to the admin using the -P option. The latest solR for Typo3, for example, is installed under /solr/core_fr for French language.
It seems the script hereunder have some parts changed : ‘ ‘ ‘ are replaced by <<
Check that when you copy…
Again, this is a quick and dirty. No check is done on the stats URL response…
-
#!/usr/bin/python
-
#
-
»‘
-
Project : Apache Solr Cache Check
-
Version : 0.1
-
Author : prune
-
Summary : This program is a nagios plugin that checks Apache Solr cache usage
-
Dependency : Linux/nagios/Python-2.x
-
-
Usage :
-
« « « `
-
shell> python check_solr_cache.py
-
‘ »
-
-
#———————–|
-
# Import Python Modules |
-
#———————–|
-
import os, sys, urllib
-
from xml.dom import minidom
-
from optparse import OptionParser
-
-
#————————–|
-
# Main Program Starts Here |
-
#————————–|
-
# Command Line Arguments Parser
-
cmd_parser = OptionParser(version="%prog 0.1")
-
cmd_parser.add_option("-H", "–host", type="string", action="store", dest="solr_server", help="SOLR Server IPADDRESS")
-
cmd_parser.add_option("-p", "–port", type="string", action="store", dest="solr_server_port", help="SOLR Server port")
-
cmd_parser.add_option("-w", "–warning", type="int", action="store", dest="warning_per", help="Exit with WARNING status if higher than the PERCENT of cache Usage", metavar="Warning Percentage")
-
cmd_parser.add_option("-c", "–critical", type="int", action="store", dest="critical_per", help="Exit with CRITICAL status if higher than the PERCENT of cache Usage", metavar="Critical Percentage")
-
cmd_parser.add_option("-P", "–path", type="string", action="store", dest="solr_server_path", help="SOLR Server path. Change it if using multicore SolR. Ex : /solr/core_fr", default="/solr")
-
(cmd_options, cmd_args) = cmd_parser.parse_args()
-
# Check the Command syntax
-
if not (cmd_options.warning_per and cmd_options.critical_per and cmd_options.solr_server and cmd_options.solr_server_port):
-
cmd_parser.print_help()
-
sys.exit(3)
-
-
# set the locals
-
STATE=0 # the nagios return code, 0=OK, 1=WARN, 2=CRIT, 3=unknown
-
STATENAME=dict()
-
STATENAME[0] = "OK"
-
STATENAME[1] = "WARNING"
-
STATENAME[2] = "CRITICAL"
-
STATENAME[3] = "UNKNOWN"
-
perfData = » # performance data container
-
-
# Collect Solr Statistics Object
-
class CollectStat:
-
»‘ Obejct to Collect the Statistics from the ‘n‘th Element of the XML Data’ »
-
def __init__(self):
-
self.stats = {}
-
solr_all_stat = minidom.parseString(urllib.urlopen(‘http://’ + cmd_options.solr_server + ‘:’ + cmd_options.solr_server_port + cmd_options.solr_server_path + ‘/admin/stats.jsp’).read())
-
entries = solr_all_stat.getElementsByTagName(‘entry’)
-
for stat in entries:
-
name = stat.getElementsByTagName(‘name’)[0]
-
if name.childNodes[0].data.strip() == ‘queryResultCache’ or name.childNodes[0].data.strip() == ‘documentCache’ or name.childNodes[0].data.strip() == ‘searcher’ :
-
for statdata in stat.getElementsByTagName(‘stat’):
-
if statdata.getAttribute(‘name’) == ‘hitratio’:
-
self.stats[name.childNodes[0].data.strip()] = float(statdata.childNodes[0].data.strip())*100
-
elif statdata.getAttribute(‘name’) == ‘numDocs’:
-
self.stats[name.childNodes[0].data.strip()] = int(statdata.childNodes[0].data.strip())
-
-
# get the data
-
solr_qps_stats = CollectStat()
-
-
# parse state and fill the perfdata
-
for item in solr_qps_stats.stats:
-
perfData += "%s=%d%%;%d;%d " % (item, solr_qps_stats.stats[item],cmd_options.warning_per,cmd_options.critical_per)
-
if solr_qps_stats.stats[item] < cmd_options.critical_per and STATE < 2:
-
STATE = 2
-
elif solr_qps_stats.stats[item] < cmd_options.warning_per and STATE < 1:
-
STATE = 1
-
-
# return the state
-
print "Solr Cache state is "+ STATENAME[STATE] +": " + perfData + " |"+perfData
-
sys.exit(STATE)
