2015-12-29 28 views
1

我需要在某些變量中獲取Cassandra節點狀態以便在bash腳本中進一步使用它。如何以最有效的方式製造?爲bash腳本獲取Cassandra節點狀態

我知道,我可以從

# 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 

但也許存在更好的方法獲取狀態?

回答

1

我覺得這是更好的辦法:

mode=$(nodetool netstats | grep 'Mode') 
if [[ $mode != *"NORMAL"* ]]; then 
    echo "Aborting backup!" 
    exit 1 
fi 
2

我發現最簡單的方法是將nodetool命令包裝在Bash腳本中,並使用grepawk的組合來提取所需的特定字段。如果有更好的方法,那麼我不知道它。如果這就是你要去的方式,那麼你可能知道如何做到這一切。

但我會提供一個例子。下面是一個腳本,我寫的地方,我需要在我的羣集的節點的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" 

基本上,我處理nodetool status的結果,發送錯誤輸出/ dev/null,grep for「UN」(因爲我只關心檢查正常/正常的節點),並將第二個字段(IP地址)保存在文件中。然後我讀取該文件以處理每個IP地址,從nodetool getcompactionthroughputnodetool compactionstats的輸出中獲取特定值並顯示它們。

就效率而言,將nodetool status的輸出結果保存到文件中,使得該輸出易於重複使用。對於nodetool netstats,您必須爲集羣中的每個節點運行一次,而只需要調用一次nodetool status

就你的情況而言,由於「狀態」是你所追求的字段,所以你需要通過(爲了更容易忽略來自nodetool status的額外輸出行)而找到grep的其他內容。令牌計數(「256」)或子網(「10.131.75。」)可能適用於您?