获取 bash 脚本的 Cassandra 节点状态
Get Cassandra node state for bash script
我需要在某个变量中获取 Cassandra 节点状态,以便在 bash 脚本中进一步使用它。如何以最有效的方式制作thst?
我知道我可以从
获得状态
# 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
但也许存在更好的方法?
我发现 most-straightforward 方法是将 nodetool 命令包装在 Bash 脚本中,并使用 grep
和 awk
。如果有更好的方法,那我就不知道了。如果这就是你要走的路,那么你可能知道如何做到这一切。
不过我还是要举个例子。这是我编写的脚本的摘录,我需要集群中节点的 IP 地址来检查它们的压缩 throughput/stats:
#!/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 }' > $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 }')
PENDING_COMPACTIONS=$(~/local/$CASS_VERSION/bin/nodetool compactionstats -h $LINE 2> /dev/null | grep pending | awk '{print }')
printf "%15s: %3s %4s\n" $LINE $COMPACTION_THROUGHPUT $PENDING_COMPACTIONS
done < "$STATUS_FILE"
基本上,我处理 nodetool status
的结果,将错误输出发送到 /dev/null,grep "UN "(因为我只关心检查 up/normal),我 save-off 文件中的第二个字段(IP 地址)。然后我读取该文件以处理每个 IP 地址,从 nodetool getcompactionthroughput
和 nodetool compactionstats
的输出中获取某些值,并显示它们。
在效率方面,saving-off 将 nodetool status
的输出到一个文件中,使该输出变得容易 re-useable。对于 nodetool netstats
,您必须 运行 为集群中的每个节点调用一次,而 nodetool status
只需要调用一次。
在您的情况下,由于 "status" 是您之后的字段,因此您需要找到 grep
的其他内容(以便更容易忽略来自nodetool status
)。也许令牌计数(“256”)或子网(“10.131.75.”)对您有用?
我认为这是更好的方法:
mode=$(nodetool netstats | grep 'Mode')
if [[ $mode != *"NORMAL"* ]]; then
echo "Aborting backup!"
exit 1
fi
我需要在某个变量中获取 Cassandra 节点状态,以便在 bash 脚本中进一步使用它。如何以最有效的方式制作thst?
我知道我可以从
获得状态# 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
但也许存在更好的方法?
我发现 most-straightforward 方法是将 nodetool 命令包装在 Bash 脚本中,并使用 grep
和 awk
。如果有更好的方法,那我就不知道了。如果这就是你要走的路,那么你可能知道如何做到这一切。
不过我还是要举个例子。这是我编写的脚本的摘录,我需要集群中节点的 IP 地址来检查它们的压缩 throughput/stats:
#!/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 }' > $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 }')
PENDING_COMPACTIONS=$(~/local/$CASS_VERSION/bin/nodetool compactionstats -h $LINE 2> /dev/null | grep pending | awk '{print }')
printf "%15s: %3s %4s\n" $LINE $COMPACTION_THROUGHPUT $PENDING_COMPACTIONS
done < "$STATUS_FILE"
基本上,我处理 nodetool status
的结果,将错误输出发送到 /dev/null,grep "UN "(因为我只关心检查 up/normal),我 save-off 文件中的第二个字段(IP 地址)。然后我读取该文件以处理每个 IP 地址,从 nodetool getcompactionthroughput
和 nodetool compactionstats
的输出中获取某些值,并显示它们。
在效率方面,saving-off 将 nodetool status
的输出到一个文件中,使该输出变得容易 re-useable。对于 nodetool netstats
,您必须 运行 为集群中的每个节点调用一次,而 nodetool status
只需要调用一次。
在您的情况下,由于 "status" 是您之后的字段,因此您需要找到 grep
的其他内容(以便更容易忽略来自nodetool status
)。也许令牌计数(“256”)或子网(“10.131.75.”)对您有用?
我认为这是更好的方法:
mode=$(nodetool netstats | grep 'Mode')
if [[ $mode != *"NORMAL"* ]]; then
echo "Aborting backup!"
exit 1
fi