1

我编写了一个脚本来计算 OpenVZ 容器随时间的带宽使用情况,并在它使用得太快时暂停它。这是到目前为止的脚本:

#!/bin/bash
# Thresholds are in bytes per second
LOGDIR="/var/log/outbound_ddos"
THRESHOLD1=65536
THRESHOLD2=117964

while [ 1 ]
do
        for veid in $(/usr/sbin/vzlist -o veid -H)
        do
                # Create the log file if it doesn't already exist
                if ! test -e $LOGDIR/$veid.log; then
                        touch $LOGDIR/$veid.log
                fi

                # Parse out the inbound/outbound traffic and assign them to the corresponding variables     
                eval $(/usr/sbin/vzctl exec $veid "grep venet0 /proc/net/dev"  |  \
                        awk -F: '{print $2}' | awk '{printf"CTOUT=%s\n", $9}')

                # Print the output and a timestamp to a log file
                echo $(date +%s) $CTOUT >> $LOGDIR/$veid.log

                # Read last 10 entries into arrays
                i=0
                tail $LOGDIR/$veid.log | while read time byte
                do
                        times[i]=$time
                        bytes[i]=$byte
                        let ++i
                done

                # Time checks & calculations for higher threshold
                counter=0
                for (( i=0; i<9; i++ ))
                do
                        # If we have roughly the right timestamp 
                        if (( times[9-i] < times[8-i] + 20 ))
                                then
                                # If the user has gone over the threshold
                                if (( bytes[9-i] > bytes[8-i] + THRESHOLD2 * 10 ))
                                        then let ++counter
                                fi
                        fi
                done

                # Now check counter
                if (( counter == 9 ))
                        then vzctl stop $veid
                fi

                # Same for lower threshold
                counter=0
                for (( i=0; i<3; i++ ))
                do   
                        # If we have roughly the right timestamp 
                        if (( times[3-i] < times[2-i] + 20 )) 
                                then
                                # If the user has gone over the threshold
                                if (( bytes[3-i] > bytes[2-i] + THRESHOLD1 * 10 ))
                                        then let ++counter
                                fi
                        fi
                done

                # Now check counter
                if (( counter == 2 ))
                        then vzctl stop $veid
                fi
        done
        sleep 10
done

我检查了 /var/log/outbound_ddos/vm101.log 中的数字,它们的增长超过了阈值,但什么也没发生。

我添加了一些回显语句来尝试找出问题所在,并且似乎是这个比较返回了错误:

if (( bytes[9-i] > bytes[8-i] + THRESHOLD2 * 10 ))

所以然后我尝试了以下内容,但没有打印出来:

echo ${bytes[9-i]}

谁能指出我正确的方向?我认为脚本几乎完成了,可能非常简单。

4

1 回答 1

0

您的 shellwhile read在子 shell 中运行循环(请参阅此处了解为什么它不能按预期工作),因此您的数组魔法不会传播到tail | while构造之外。

阅读相应修复:-)

于 2013-04-20T15:43:26.377 回答