最后 x 分钟交换输出

最后 x 分钟交换输出

是否有任何命令或脚本可以将 o/p 作为“过去 10 分钟使用的交换是 x”?我希望将该输出用于我的 nagios 监控。

我在下面的脚本中使用了 SAR,并尝试在各个服务器上运行以进行 nagios 监控。有人可以建议我如何在下面的检查中获得交换警告并交换临界值吗?我尝试在每台服务器上运行以下命令时动态计算它,但它报告错误。

请建议。

# 2 = CRITICAL (SWAP usage higher than CRITICAL)
# 3 = UNKNOWN (Wrong usage)

SWAP_WARN=
SWAP_CRIT=
SWAPOUT_ACTIVITY_TEST=

## GET SWAP Warning and Critical values from the Machine

# 2 = CRITICAL (SWAP usage higher than CRITICAL)
# 3 = UNKNOWN (Wrong usage)

SWAP_WARN=
SWAP_CRIT=
SWAPOUT_ACTIVITY_TEST=

**## GET SWAP Warning and Critical values from the Machine
temp=$(swapon -s | tail -n 1 | awk '{print $3}' )
SWAP_WARN=$(echo '$(temp)*.20' | bc)
SWAP_CRIT=$(echo '$(temp)*.40' | bc)**

## FETCH ARGUMENTS
##while getopts "h:" OPTION; do
#        case "${h}" in
#                 h)
#                        usage
#                        exit 3
#                        ;;
#                ?)
#                        usage
#                        exit 3
#                        ;;
#        esac
#done

## CHECK ARGUMENTS
if [ -z ${SWAP_WARN} ] || [ -z ${SWAP_CRIT} ] || [ ${SWAP_WARN} -gt ${SWAP_CRIT} ] ; then
        usage
        exit 3
fi

## GET SWAP INFO FROM MACHINE

cd /var/log/sa
FOR_VALUE=sa
FOR_DATE=$(date | awk '{print $3}')
SA=$FOR_VALUE$FOR_DATE
SWAPOUT_ACTIVITY_TEST=$(sar -S -f $SA | tail -n 2| head -n 1 | awk '{print $4}')


## CHECK SWAPPING ON MACHINE
if [ ${SWAPOUT_ACTIVITY_TEST} -lt ${SWAP_WARN} ]; then
        ## SWAP IS OK
        LINE="OK! Swapout size in last 10 minutes : ${SWAPOUT_ACTIVITY_TEST} | swapout_size=${SWAPOUT_ACTIVITY_TEST}B;${SWAP_WARN};${SWAP_CRIT};"
        echo $LINE
        exit 0
elif [ ${SWAPOUT_ACTIVITY_TEST} -gt ${SWAP_WARN} ] && [ ${SWAPOUT_ACTIVITY_TEST} -lt ${SWAP_CRIT} ] || [ ${SWAPOUT_ACTIVITY_TEST} -eq ${SWAP_WARN} ]; then
        ## SWAP IS IN WARNING STATE
        LINE="WARNING! Swapout size in last 10 minutes: ${SWAPOUT_ACTIVITY_TEST} | swapout_size=${SWAPOUT_ACTIVITY_TEST}B;${SWAP_WARN};${SWAP_CRIT};"
:

## FETCH ARGUMENTS
##while getopts "h:" OPTION; do
#        case "${h}" in
#                 h)
#                        usage
#                        exit 3
#                        ;;
#                ?)
#                        usage
#                        exit 3
#                        ;;
#        esac
#done

## CHECK ARGUMENTS
if [ -z ${SWAP_WARN} ] || [ -z ${SWAP_CRIT} ] || [ ${SWAP_WARN} -gt ${SWAP_CRIT} ] ; then
        usage
        exit 3
fi

## GET SWAP INFO FROM MACHINE

cd /var/log/sa
FOR_VALUE=sa
FOR_DATE=$(date | awk '{print $3}')
SA=$FOR_VALUE$FOR_DATE
SWAPOUT_ACTIVITY_TEST=$(sar -S -f $SA | tail -n 2| head -n 1 | awk '{print $4}')


## CHECK SWAPPING ON MACHINE
if [ ${SWAPOUT_ACTIVITY_TEST} -lt ${SWAP_WARN} ]; then
        ## SWAP IS OK
        LINE="OK! Swapout size in last 10 minutes : ${SWAPOUT_ACTIVITY_TEST} | swapout_size=${SWAPOUT_ACTIVITY_TEST}B;${SWAP_WARN};${SWAP_CRIT};"
        echo $LINE
        exit 0
elif [ ${SWAPOUT_ACTIVITY_TEST} -gt ${SWAP_WARN} ] && [ ${SWAPOUT_ACTIVITY_TEST} -lt ${SWAP_CRIT} ] || [ ${SWAPOUT_ACTIVITY_TEST} -eq ${SWAP_WARN} ]; then
        ## SWAP IS IN WARNING STATE
        LINE="WARNING! Swapout size in last 10 minutes: ${SWAPOUT_ACTIVITY_TEST} | swapout_size=${SWAPOUT_ACTIVITY_TEST}B;${SWAP_WARN};${SWAP_CRIT};"
:

答案1

您可以通过在 cron 中设置 sar 来收集此类信息。然后在 nagios 中创建自定义探针以读取过去 10 分钟的 sar 结果

答案2

尝试这个命令..它将每 10 分钟为您提供有关交换使用情况的信息

sar -W -f /var/log/sa/sa(日期)

样本输出

05:00:01 AM pswpin/s pswpout/s 05:10:03 AM 0.00 0.77 05:20:03 AM 10.00 45.08 05:30:02 AM 10.38 1.36 平均值:7.18 16.38

相关内容