Originally published November 1, 2016 @ 11:42 pm

The Cloud tab of the Solr Web UI is convenient, but not for monitoring purposes. Just like you, I hate XML and parsing it from shell is more convoluted that it needed to be. But there are some helpful tools. Here’s a quick example of monitoring Solr cores with a shell script.

solr_url="http://solr01.krazyworks.local:8983/solr/admin/collections?action=CLUSTERSTATUS"

getinfo() {
	xmlstarlet sel -T -t -m response`awk 'BEGIN{for(c=0;c<7;c++) printf "/lst"}'` \
	-v "concat(str[@name='core'],' ',@name,' ',str[@name='node_name'],' ',str[@name='state'],' ',str[@name='leader'], '@')" \
	<<<$(wget -q -O - "${solr_url}") | \
	sed -e 's/@/\n/g' -e 's/\.krazyworks\.local:8983_solr//g' | \
	awk '{if(NF < 5) print $0, "false"; else print $0}' | \
	(echo "REPLICA CORE NODE STATE LEADER" && cat) | column -t
}

setarray() {
	unset a
	IFS=$'\n' ; a=($(getinfo)) ; unset IFS
}

printarray() {
	for ((i = 0; i < ${#a[@]}; i++)) ; do echo "${a[$i]}" ; done
}

setarray
printarray

This will give a shell array containing the status of your Solr cores. You can use the printarray function to recall this data for further processing without having to use temp files or repeatedly query Solr servers. Sample output:

REPLICA                      CORE         NODE    STATE   LEADER
test01_core_shard1_replica1  core_node1   solr03  active  true
test01_core_shard2_replica1  core_node2   solr05  active  true
test01_core_shard3_replica1  core_node3   solr02  active  true
test01_core_shard4_replica1  core_node4   solr04  active  true
test01_core_shard5_replica1  core_node5   solr01  active  true
test02_core_shard1_replica1  core_node15  solr06  active  false
test02_core_shard1_replica2  core_node3   solr10  active  true
test02_core_shard2_replica1  core_node19  solr08  active  true
test02_core_shard2_replica2  core_node12  solr03  active  false
test02_core_shard3_replica1  core_node17  solr07  active  false
test02_core_shard3_replica2  core_node21  solr02  active  true
test02_core_shard4_replica2  core_node4   solr05  active  false