3

I'm creating some scripts for some Cassandra clusters I operate. Some of these scripts need a list of nodes that are part of the cluster.

nodetool status will print a list of the nodes and their current state, but it prints out more information than I need. For example:

$ nodetool status mykeyspace
Datacenter: datacenter1
=======================
Status=Up/Down
|/ State=Normal/Leaving/Joining/Moving
--  Address      Load       Tokens  Owns    Host ID                               Rack
UN  10.11.12.13  47.66 KB   1       33.3%   aaa1b7c1-6049-4a08-ad3e-3697a0e30e10  rack1
UN  10.11.12.14  47.67 KB   1       33.3%   1848c369-4306-4874-afdf-5c1e95b8732e  rack1
UN  10.11.12.15  47.67 KB   1       33.3%   49578bf1-728f-438d-b1c1-d8dd644b6f7f  rack1

The list of the nodes should be IPs only ( hostnames aren't necessary) and I'd like clean output like the following:

10.11.12.13
10.11.12.14
10.11.12.15

What is the best/simplest way to do this?

I'll provide my current method below for doing this, but if there's a better way I'd love to see it. Even if it doesn't use the nodetool command.

Gene
  • 3,633
  • 19
  • 39

1 Answers1

7

You can use the nodetool status command with awk:

nodetool status | awk '/^(U|D)(N|L|J|M)/{print $2}'

Each line that contains information about a node will either start with the Status of the node being Up or Down ( ^(U|N) ) followed by the State of the node being Normal, Leaving, Joining, or Moving ( (N|L|J|M) ).

Just so long as the Apache Cassandra developers don't change the output of the nodetool status command this should work fine.

Gene
  • 3,633
  • 19
  • 39