如果我正在尋找特定的退出狀態,for 迴圈的第一個實例將退出

如果我正在尋找特定的退出狀態,for 迴圈的第一個實例將退出

我正在嘗試從存儲設備獲取一些磁碟監控。我可以查詢設備並輸出。

例如—vol_list

dr_prdoracle_bkup  83%
dr_test  6%
infra_backup  3%
logs  28%
oem_shared  2%
prd_backup  51%
rhev_export  24%
ss_backup  2%
ss_data  23%

在這裡,我試圖對使用的百分比發出警報。所以在這裡我試圖創建一個警告@50%和嚴重@70%

閾值在命令列上解析

WarnSpace=$2
CritSpace=$2


ST_OK=0
ST_WR=1
ST_CR=2
ST_UK=3

for line in $(cat "vol_list"); do
SpaceUsed=`echo ${line}|awk '{print $2}'|sed 's/%//'`
volume=`echo ${line}|awk '{print $1}'`

if [ -n "$WarnSpace" -a -n "$CritSpace" ]
then
    if [ "$SpaceUsed" -ge "$WarnSpace" -a "$SpaceUsed" -le "$CritSpace" ]
    then
                #echo "WARNING - ${output} | ${perfdata}"
                echo "WARNING; $volume: total $SpaceUsed"%""
        exit $ST_WR
           elif [ "$SpaceUsed" -ge "$CritSpace" ]
    then
                #echo "CRITICAL - ${output} | ${perfdata}"
                echo "CRITICAL; $volume: total $SpaceUsed"%""

        exit $ST_CR


fi
done

當我使用退出狀態時,它會跳出循環,例如

./purefs_check.sh  -w 50 -c 70

WARNING; dev_client: total 67%

如果我刪除退出程式碼,我會得到我所期望的結果

WARNING; dev_client: total 67%
CRITICAL; dev_data: total 89%
CRITICAL; dev_vendor: total 99%
WARNING; dr_client: total 54%
CRITICAL; dr_prdoracle_bkup: total 78%
WARNING; prd_backup: total 51%

我需要什麼來完成循環並給我退出狀態 1(警告)和 2(嚴重)。

最好只有 1 個退出代碼 >1 個條目。

所以在這裡我想看看

./purefs_check.sh  -w 50 -c 70 |grep WAR ;echo $?
WARNING; dev_client: total 67%
WARNING; dr_client: total 54%
WARNING; prd_backup: total 51%
1
CRITICAL; dev_data: total 89%
CRITICAL; dev_vendor: total 99%
CRITICAL; dr_prdoracle_bkup: total 78%
2

任何幫助,將不勝感激 ...

答案1

exit將退出外殼程序, 立即地。如果您想設定稍後使用的退出代碼,則需要設定一個標誌或手動儲存退出代碼。

#!/bin/bash
warns=""
crits=""
for x in ...; do
    if warning_condition; then 
        warns=1                 # or keep a count with warns=$((warns + 1))
    elif critical_condition; then
        crits=1 
    fi
done
[ "$crits" ] && exit 2
[ "$warns" ] && exit 1

甚至

#!/bin/bash
exit_code=0
set_exit_code() { 
    # save the greatest given exit code
    [ "$1" -gt "$exit_code" ] && exit_code=$1
}
for x in ...; do
    if warning_condition; then 
        set_exit_code 1
    elif critical_condition; then
        set_exit_code 2
    fi
done
exit "$exit_code"

至於這個…

$ ./purefs_check.sh  -w 50 -c 70 |grep WAR ;echo $?

它不會工作,因為$?將保存grep.這可以用來找出是否有任何包含 的輸出行WAR,但我認為這不是您想要的。

如果您想先對輸出警告進行排序,則需要將警告和嚴重警告收集到陣列中,並在最後列印它們。或只是檢查資料兩次,先找警告,然後才找批評。

答案2

你有:

WarnSpace=$2
CritSpace=$2

然後你像這樣呼叫腳本

./purefs_check.sh  -w 50 -c 70 

所以 CritSpace 值為 50。

您根本沒有處理這些選項。改為這樣做:

#!/bin/bash

ST_OK=0
ST_WR=1
ST_CR=2
ST_UK=3

while getopts :w:c: opt; do
    case $opt in
        w) WarnSpace=$OPTARG ;;
        c) CritSpace=$OPTARG ;;
        *) echo "Warning: unknown option -$opt: ignoring it" >&2 ;;
    esac
done
shift $((OPTIND - 1))

if [[ -z "$WarnSpace" ]]; then
    echo "Error: specify a warning threshold with -w" >&2
    exit $ST_UK
elif [[ -z "$CritSpace" ]]; then
    echo "Error: specify a critical threshold with -c" >&2
    exit $ST_UK
fi

warn=0
crit=0
while read -r volume SpaceUsed; do
    SpaceUsed=${SpaceUsed//[^[:digit:]]/}   # delete all non-digits

    if (( SpaceUsed >= CritSpace )); then
        echo "CRITICAL; $volume: total $SpaceUsed"
        ((crit++))
    elif (( SpaceUsed >= WarnSpace )); then
        echo "WARNING; $volume: total $SpaceUsed"
        ((warn++))
    fi
done < vol_list

((crit > 0)) && exit $ST_CR
((warn > 0)) && exit $ST_WR
exit $ST_OK

相關內容