监视磁盘分区的bash脚本';用法

监视磁盘分区的bash脚本';用法,bash,Bash,测向显示 -bash-4.1# df Filesystem 1K-blocks Used Available Use% Mounted on /dev/sda3 1918217320 1783986384 36791092 98% / tmpfs 16417312 0 16417312 0% /dev/shm /dev/sda1 482214 14853

测向显示

-bash-4.1# df Filesystem 1K-blocks Used Available 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 -bash-4.1#df 已使用的文件系统1K块可用使用%已安装在 /dev/sda3 1918217320 1783986384 36791092 98%/ tmpfs 16417312 0 16417312 0%/dev/shm /dev/sda1 482214 148531 308784 33%/开机 /dev/sdb1 1922858352 1373513440 451669312 76%/disk2 我需要用bash脚本编写一个函数,如果分区100%满了,该函数将返回1。
如何做到这一点?我可以使用什么命令来解析df的输出?

类似于:

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}'
这应该做到:

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

换句话说,检查POSIX
df
输出的第五列中是否有任何行包含准确的字符串“100%”.

百分比的问题是,如果它是一个terrabyte磁盘,那么95%的磁盘空间可能仍然有大量的空闲空间-请参阅底部脚本了解实际磁盘空间-示例末尾的格式100在分区上剩余的空间低于100MB时显示警报

diskspace.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\u远程\u磁盘\u自动

#!/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

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

#!/bin/sh

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

if [ "$CHECK" == "100%"]
then
    echo 1
else
    echo 0
fi

试试这个:
df-Ph | grep-v“Use%”| sed的///g'| awk'$5>限制{print$1、$2、$3、$4、$5”;}'| column-t'

它将返回超过
限制的所有
df-Ph
条目

例如,在我的工作站上,
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的///g'| awk'$5>20{print$1、$2、$3、$4、$5“%”}列-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

这里的
列-t
部分纯粹是为了让输出可读。

不太喜欢过多的grep和awk,因为随着时间的推移,它确实会带来错误

我只想得到重要文件夹的信息。下面是一个使用stat的示例,它将为您提供文件夹中的可用字节,然后将其转换为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))     

可能是
df | grep 100%
df | awk'$5==“100%”{print$1}
它将打印文件系统是100%。还可以使用一个有用的限制:
df-h | tail-n+2;awk'$5>=“90%”{print”警告:“$1”在“$5}”
记住,
df
中的百分比和可用性不包括非root用户的系统保留(通常为5%)。awk后接grep?你一定是在开玩笑。我想表达的意思是,通常这是不必要的,但我失败了:
!df-P | awk'$5==“100%”{exit 1}'
我相信它也比magic grep选项更具可读性和自我记录能力。今天保存一个进程和一个分支:-)我通常会将其表示为可读性,但并非每个
grep
的化身都支持长选项。您的命令非常清晰,但通常
sed
awk
perl
命令都会很快变成行噪声。
stat
的唯一问题是它没有反映非root用户不可用的
5%
保留。
    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))