BASH脚本修改用于自由空间监视目的

时间:2012-11-15 12:51:31

标签: bash

无法进行修改以适应我的环境,这是check_disc功能,但在我的环境中无法正常工作,其中光盘通过/ dev / mapper /附加,所以我有 对于exmaple / dev / mapper / debian-var而不是/ var。

BASH脚本的功能:

function check_disks {

    THOLD=$1;
    DISK_ERROR="";

    # df -kl is the most portable options for df 

    MOUNTS=`df -lk | grep -v devfs | grep -v none | grep -v Filesystem | awk '{print $6}'`;
    for MOUNT in $MOUNTS; do
        PERCENT=`df -kl $MOUNT | grep -v Filesystem | awk '{print $5}' | sed 's/\%//'`;
        if [ $PERCENT -gt $THOLD ]; then
            DISK_ERROR="$DISK_ERROR    Volume '$MOUNT' is at $PERCENT% usage.\n";
        fi
    done
    if [ -n "$DISK_ERROR" ]; then
        echo -en "ERROR: The following volumes have exceeded the allowable threshold of $THOLD% usage.\n$DISK_ERROR";
        return 1;
    else
        return 0;
    fi

}

df -kl cmd的输出:

df -kl
Filesystem           1K-blocks      Used Available Use% Mounted on
/dev/mapper/debian-root
                       1919048    493616   1327948  28% /
tmpfs                 18717148        12  18717136   1% /lib/init/rw
udev                     10240       784      9456   8% /dev
tmpfs                 18717148         0  18717148   0% /dev/shm
/dev/cciss/c0d0p1       241116     23679    204989  11% /boot
/dev/mapper/debian-home
                       3842104   2681264    965668  74% /home
/dev/mapper/debian-tmp
                       4805760    366228   4195412   9% /tmp
/dev/mapper/debian-usr
                       3842104   1816200   1830732  50% /usr
/dev/mapper/debian-var
                     377857560 183429768 175233732  52% /var

我想这行应该改变:

MOUNTS=`df -lk | grep -v devfs | grep -v none | grep -v Filesystem | awk '{print $6}'`;

但不确定如何。有人可以帮我一把吗? THX

1 个答案:

答案 0 :(得分:0)

df上有一个可移植性选项,可以将输出保持在每卷一行:

   -P, --portability
          use the POSIX output format

我想改变

MOUNTS=`df -lk | grep -v devfs | grep -v none | grep -v Filesystem | awk '{print $6}'`

MOUNTS=`df -lkP | grep -v devfs | grep -v none | grep -v Filesystem | awk '{print $6}'`

会有所帮助。

仅供参考,这是输出差异的一个例子:

$ df -lk | grep -B1 /var
/dev/mapper/rootvg-var
                   4062912   1900724   1952476  50% /var
$ df -lkP | grep /var
/dev/mapper/rootvg-var   4062912   1900748   1952452      50% /var