3

df 显示

-bash-4.1# df
文件系统 1K-blocks Used 可用 Use% Mounted on
/dev/sda3 1918217320 1783986384 36791092 98% /
tmpfs 16417312 0 16417312 0% /dev/shm
/dev/sda1 482214 148531 308784 33% /boot
/dev/sdb1 1922858352 1373513440 451669312 76% /disk2

如果分区已满 100%,我需要 bash 编写一个返回 1 的函数。如何才能做到这一点?我可以使用哪些命令来解析 df 的输出?

4

6 回答 6

1

这应该这样做:

disks_space() {
    ! df -P | awk '{print $5}' | grep -Fqx '100%'
}

换句话说,检查 POSIXdf输出的第五列中的任何行是否包含确切的字符串“100%”。

于 2013-05-23T08:10:37.567 回答
1

百分比问题是,如果它是一个 TB 磁盘,其中 95% 可能仍然有很多可用的 gig - 请参阅底部脚本以了解实际磁盘空间 - 示例末尾的格式 100 在剩余空间低于 100MB 时显示警报分割

磁盘空间.sh

#!/bin/sh
# set -x
# Shell script to monitor or watch the disk space
# It will send an email to $ADMIN, if the (free available) percentage of space is >= 90%.
# -------------------------------------------------------------------------
# Set admin email so that you can get email.
ADMIN="root"
# set alert level 90% is default
ALERT=90
# Exclude list of unwanted monitoring, if several partions then use "|" to separate the partitions.
# An example: EXCLUDE_LIST="/dev/hdd1|/dev/hdc5"
EXCLUDE_LIST="/auto/ripper"
#
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
#
function main_prog() {
while read output;
do
echo $output
  usep=$(echo $output | awk '{ print $1}' | cut -d'%' -f1)

  partition=$(echo $output | awk '{print $2}')

  if [ $usep -ge $ALERT ] ; then
    if [ "$partition" == "/var" ]; then
             # echo "Running out of space \"$partition ($usep%)\" on server $(hostname), $(date)"
             echo "Running out of space \"$partition ($usep%)\" on server $(hostname), $(date)" |  mail -s "Alert: Almost out of disk space $usep%" $ADMIN

        # Extra bits  you may wish to do -    
        #for FILE in `find $partition -size +1G -print`
        #do
        #    echo $FILE
        #    DATE=`date  +%Y-%m-%d_%H%M`
        #    filename=`echo ${FILE##*/}`
        #    mkdir /mnt/san/$hostname
        #    echo cp $FILE /mnt/san/$(hostname)/$filename-$DATE
        #    #echo > $FILE
        #done
    fi
  fi
done
}
if [ "$EXCLUDE_LIST" != "" ] ; then
  df -hP |  grep -vE "^[^/]|tmpfs|cdrom|${EXCLUDE_LIST}" | awk '{print $5 " " $6}' | main_prog
else
  df -hP |  grep -vE "^[^/]|tmpfs|cdrom"| awk '{print $5 " " $6}' | main_prog
fi

或者您可以使用我为 nagios 设置的这种检查方式(使用 snmp 连接到远程主机)

snmp_remote_disk_auto

#!/bin/bash

# This script takes:
# <host> <community> <megs>

snmpwalk="/usr/bin/snmpwalk"
snmpget="/usr/bin/snmpget"

function usage() { 
echo "$0 localhost public 100"
echo "where localhost is server"
echo "public is snmp pass"
echo "100 is when it reaches below a 100Mb"
echo "-----------------------------------"
echo "define threshold below limit specific for partitions i.e. boot can be 50mb where as /var I guess we want to catch it at around 1 gig so"
echo "$0 localhost public  1024"

}


server=$1;
pass=$2
limit=$3;

errors_found="";
partitions_found="";
lower_limit=10;
graphtext="|"

if [ $# -lt 3 ]; then
    usage;
    exit 1;
fi

# takes <size> <used> <allocation>
calc_free() {
    echo "$1 $2 - $3 * 1024 / 1024 / p" | dc
}

    for partitions in $($snmpwalk -v2c -c $pass -Oq $server  hrStorageDescr|grep /|egrep -v "(/mnt|/home|/proc|/sys)"|awk '{print $NF}'); do
        if [[ $partitions =~ /boot ]]; then
            limit=$lower_limit;
        fi
        if result=$($snmpwalk -v2c -c $pass -Oq $server hrStorageDescr | grep "$partitions$"); then
            index=$(echo $result | sed 's/.*hrStorageDescr//' | sed 's/ .*//')
            args=$($snmpget -v2c -c $pass -Oqv $server hrStorageSize$index hrStorageUsed$index hrStorageAllocationUnits$index | while read oid j ; do printf " $oid" ; done)
            free=$(calc_free$args)


            back_count=$(echo $partitions|grep -o "/"|wc -l)
            if [[ $back_count -ge 2 ]]; then
                gpartition=$(echo "/"${partitions##*/})
            else
                gpartition=$partitions;
            fi

            if [ "$free" -gt "$limit" ]
            then

                graphtext=$graphtext$gpartition"="$free"MB;;;0 "
                #graphtext=$graphtext$partitions"="$free"MB;;;0 "
                partitions_found=$partitions_found" $partitions ($free MB)"
            else
                graphtext=$graphtext$gpartition"="$free"MB;;;0 "
                #graphtext=$graphtext$partitions"="$free"MB;;;0 "
                errors_found=$errors_found" $partitions ($free MB)"

            fi

        else
                graphtext=$graphtext$gpartition"="0"MB;;;0 "
                #graphtext=$graphtext$partitions"="0"MB;;;0 "
             errors_found=$errors_found" $paritions does_not_exist_or_snmp_is_not_responding"
        fi
    done

    if [ "$errors_found" == "" ]; then
        echo "OK: $partitions_found$graphtext"
        exit 0
    else
        echo "CRITICAL: $errors_found$graphtext";
        exit 2;
    fi

./snmp_remote_disk_auto localhost public 100

OK:  / (1879 MB) /var (2281 MB) /tmp (947 MB) /boot (175 MB)|/=1879MB;;;0 /var=2281MB;;;0 /tmp=947MB;;;0 /boot=175MB;;;0 
于 2013-05-23T08:28:20.977 回答
1

不是过度 greps 和 awks 的忠实拥护者,因为随着时间的推移它确实会带来错误。

我只会获取重要文件夹的信息。下面是使用 stat 的示例,它将为您提供文件夹中可用的 BYTES,然后将其转换为 MB (10**6)。我在我的 RHEL6.x 系统上对此进行了粗略的测试。

    folder_x_mb=$(($(stat -f --format="%a*%s" /folder_x)/10**6))    
    folder_y_mb=$(($(stat -f --format="%a*%s" /folder_y)/10**6))   
    folder_z_mb=$(($(stat -f --format="%a*%s" /folder_z)/10**6))     
于 2014-06-02T17:03:26.100 回答
0

怎么样:

df | perl -wne 'if(/(\d+)%\s+(.*)/){print "$2 at $1%\n" if $1>90}'

您可以更改阈值,而不是打印,您可以退出:

df | perl -wne 'if(/(\d+)%\s+(.*)/){exit 1 if $1>99}'
于 2013-05-23T07:50:58.340 回答
0

尝试这个:df -Ph | grep -v "Use%" | sed 's/%//g' | awk '$5 > LIMIT {print $1,$2,$3,$4,$5"%";}' | column -t'

它将返回所有df -Ph超过LIMIT

例如,在我的工作站上,df -Ph返回:

Filesystem            Size  Used Avail Use% Mounted on
/dev/cciss/c0d0p1      92G   32G   56G  37% /
shmfs                  98G  304K   98G   1% /dev/shm
192.168.1.1:/apache_cache  2.7T  851G  1.9T  32% /media/backup
/dev/dm-4              50G   49G  1.1G  98% /lun1
/dev/dm-7              247G  30G  218G  12% /lun2

假设我想列出超过容量 20% 的挂载点。

我使用df -Ph | grep -v "Use%" | sed 's/%//g' | awk '$5 > 20 {print $1,$2,$3,$4,$5"%";}' | column -t,它返回以下内容:

/dev/cciss/c0d0p1      92G   32G   56G  37% /
192.168.1.1:/apache_cache  2.7T  851G  1.9T  32% /media/backup
/dev/dm-4              50G   49G  1.1G  98% /lun1

column -t部分纯粹是为了输出可读性。

于 2014-04-21T00:38:06.440 回答
0

这是一个简单的脚本,用于检查是否已经有磁盘达到了最大容量,如果有,它将返回/输出 1。

#!/bin/sh

CHECK=$(df -Ph | grep '100%' | xargs echo | cut -d' ' -f5)

if [ "$CHECK" == "100%"]
then
    echo 1
else
    echo 0
fi
于 2013-05-25T08:46:01.577 回答