数组项之间的比较

时间:2013-04-20 14:52:12

标签: bash shell

我编写了一个脚本来计算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中的数字,并且它们的增加幅度超过了阈值,但没有发生任何事情。

我添加了一些echo语句来尝试找出问题所在,似乎这个比较返回false:

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

然后我尝试了以下内容,没有打印出任何内容:

echo ${bytes[9-i]}

有人能指出我正确的方向吗?我认为剧本几乎已经完成,可能非常简单。

1 个答案:

答案 0 :(得分:0)

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

阅读this并相应修正: - )