1

我需要在某个变量中获取 Cassandra 节点状态,以便在 bash 脚本中进一步使用它。如何以最有效的方式制作?

我知道我可以从

# nodetool status
Datacenter: DC1
===============
Status=Up/Down
|/ State=Normal/Leaving/Joining/Moving
--  Address        Load       Tokens       Owns    Host ID                               Rack
UJ  10.131.75.142  698.74 KB  256          ?       d032b36b-ffb6-496a-b814-bab399ce8a1f  RAC2
UN  10.131.75.141  729.76 KB  256          ?       739c1e5f-2ff4-4bfa-9ae8-4f64ff061ce9  RAC1
Datacenter: DC2
===============
Status=Up/Down
|/ State=Normal/Leaving/Joining/Moving
--  Address        Load       Tokens       Owns    Host ID                               Rack
UN  10.131.75.144  19.12 MB   256          ?       47430976-dee6-40bb-bce2-2a9f8d401aba  RAC2
UN  10.131.75.143  28.98 MB   256          ?       7b3faef4-ba62-4d1d-87f8-9b0b082a0011  RAC1

或(众数)

# nodetool netstats
Mode: NORMAL
Not sending any streams.
Read Repair Statistics:
Attempted: 0
Mismatch (Blocking): 0
Mismatch (Background): 0
Pool Name                    Active   Pending      Completed
Large messages                  n/a         0              0
Small messages                  n/a         0              7
Gossip messages                 n/a         0          12199

但也许存在更好的方式?

4

2 回答 2

2

我发现最直接的方法是将 nodetool 命令包装在 Bash 脚本中,并使用 和 的组合提取您需要的特定grep字段awk。如果有更好的方法,那我就不知道了。如果这就是你要走的路,那么你可能知道如何做到这一切。

但无论如何我都会提供一个例子。这是我编写的脚本的摘录,我需要集群中节点的 IP 地址来检查它们的压缩吞吐量/统计信息:

#!/bin/bash
STATUS_FILE="nodetool_status.txt"
#get IP addresses, store in file
(~/local/$CASS_VERSION/bin/nodetool status 2> /dev/null | grep "UN " | awk '{print $2}' > $STATUS_FILE)

printf "%15s: %3s %4s\n" "IP" "MB/s" "Pending"

while read -r LINE
do
  COMPACTION_THROUGHPUT=$(~/local/$CASS_VERSION/bin/nodetool getcompactionthroughput -h $LINE 2> /dev/null | awk '{print $4}')
  PENDING_COMPACTIONS=$(~/local/$CASS_VERSION/bin/nodetool compactionstats -h $LINE 2> /dev/null | grep pending | awk '{print $3}')
  printf "%15s: %3s %4s\n" $LINE $COMPACTION_THROUGHPUT $PENDING_COMPACTIONS
done < "$STATUS_FILE"

基本上,我处理 a 的结果nodetool status,将错误输出发送到 /dev/null,grep 为“UN”(因为我只关心检查启动/正常的节点),并且我保存了第二个字段(IP 地址)在一个文件中。nodetool getcompactionthroughput然后我读取该文件以处理每个 IP 地址,从and的输出中获取某些值nodetool compactionstats,并显示它们。

在效率方面,将输出保存nodetool status到文件中,使输出易于重用。因为nodetool netstats您必须为集群中的每个节点运行一次,而nodetool status只需要调用一次。

在您的情况下,由于“状态”是您所追求的字段,因此您需要找到其他内容grep(以便更容易忽略来自 的额外输出行nodetool status)。也许令牌计数(“256”)或子网(“10.131.75.”)对您有用?

于 2015-12-29T17:35:28.917 回答
1

我认为这是更好的方法:

mode=$(nodetool netstats | grep 'Mode')
if [[ $mode != *"NORMAL"* ]]; then
  echo "Aborting backup!"
  exit 1
fi
于 2016-01-07T23:23:37.770 回答