I'm trying to get the cassandra schema version into variable from output of nodetool command.
Here are some of the output of nodetool commands:
Cluster Information:
Name: Test Cluster
Snitch: org.apache.cassandra.locator.DynamicEndpointSnitch
Partitioner: org.apache.cassandra.dht.Murmur3Partitioner
Schema versions:
65e78f0e-e81e-30d8-a631-a65dff93bf82: [127.0.0.1]
When few nodes are not reachable here's the output.
Cluster Information:
Name: Production Cluster
Snitch: org.apache.cassandra.locator.DynamicEndpointSnitch
Partitioner: org.apache.cassandra.dht.Murmur3Partitioner
Schema versions:
UNREACHABLE: 1176b7ac-8993-395d-85fd-41b89ef49fbb: [10.202.205.203]
Can anyone suggest how to get schema version into variable irrespective of reachable or not?
Tried to use awk and grep commands but didn't work because of unreachable.
CodePudding user response:
Another version of an awk
script that will match only the UUID type REGEX can be written to use match()
setting the internal RSTART
and RLENGTH
variables that can then be used with substr()
.
That would be:
awk '
/Schema versions:/ {
set=1
next
}
set {
match($0,/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/)
print substr($0, RSTART, RLENGTH)
exit
}' file
Example Use/Output
$ awk '
> /Schema versions:/ {
> set=1
> next
> }
> set {
> match($0,/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/)
> print substr($0, RSTART, RLENGTH)
> exit
> }' << 'eof'
> Cluster Information:
> Name: Test Cluster
> Snitch: org.apache.cassandra.locator.DynamicEndpointSnitch
> Partitioner: org.apache.cassandra.dht.Murmur3Partitioner
> Schema versions:
> 65e78f0e-e81e-30d8-a631-a65dff93bf82: [127.0.0.1]
>
> eof
65e78f0e-e81e-30d8-a631-a65dff93bf82
and
$ awk '
> /Schema versions:/ {
> set=1
> next
> }
> set {
> match($0,/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/)
> print substr($0, RSTART, RLENGTH)
> exit
> }' << 'eof'
> Cluster Information:
> Name: Production Cluster
> Snitch: org.apache.cassandra.locator.DynamicEndpointSnitch
> Partitioner: org.apache.cassandra.dht.Murmur3Partitioner
> Schema versions:
> UNREACHABLE: 1176b7ac-8993-395d-85fd-41b89ef49fbb: [10.202.205.203]
> eof
1176b7ac-8993-395d-85fd-41b89ef49fbb
You can use the command in a command substitution in bash to capture the result in a variable.
Let me know if you have further questions.
CodePudding user response:
Using GNU sed
$ var=$(nodetool command | sed -En '/schema versions:/I{n;s/[^0-9]*([^:]*).*/\1/p}')
$ echo "$var"
65e78f0e-e81e-30d8-a631-a65dff93bf82
1176b7ac-8993-395d-85fd-41b89ef49fbb
CodePudding user response:
Awk will do the job for that:
version=$(awk '/Schema versions:/ {
getline
gsub(/:/,"")
if ($1 == "UNREACHABLE:") {
print $2
} else {
print $1
}
}' < <(nodetool_cmd)) # remplace "nodetool_cmd" by the correct command
$ echo "$version" #when reachable
65e78f0e-e81e-30d8-a631-a65dff93bf82
$ echo "$version" # when unreachable
1176b7ac-8993-395d-85fd-41b89ef49fbb
# or in single line:
version=$(awk '/version/ {getline;gsub(/:/,"");if ($1 == "UNREACHABLE") {print $2} else {print $1}}' < <(nodetool_cmd))