监控磁盘分区使用情况的 bash 脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16708242/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 05:30:13  来源:igfitidea点击:

bash script that monitor a disk partition's usage

bash

提问by user121196

df shows

df 显示

-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

I need to bash script a function that returns 1 if an paritions become 100% full. how can this be done? what commands can I use to parse out the output of df?

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

回答by Mike Q

Not a huge fan of excessive greps and awks as it can really bring errors over time..

不是过度的 grep 和 awks 的忠实粉丝,因为随着时间的推移,它确实会带来错误。

I would just get the information for the folders that matter. Below is a sample of using stat which will give you the available BYTES in a folder, then converts it to MB (10**6). I roughly tested this on my RHEL6.x system.

我只会获取重要文件夹的信息。下面是使用 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))     

回答by l0b0

This should do it:

这应该这样做:

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

In other words, check if any of the lines in the fifth column of the POSIX dfoutput contains the exact string "100%".

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

回答by V H

Probelm with percentage is if its a terrabyte disk 95% of that may still be lots of free gig - refer to the bottom script for actual disk space - the format 100 at the end of the example shows alert when it is below 100MB left on a partition

百分比问题是如果它的太字节磁盘 95% 可能仍然有很多免费演出 - 请参阅底部脚本以获得实际磁盘空间 - 示例末尾的 100 格式在剩余空间低于 100MB 时显示警报划分

diskspace.sh

磁盘空间.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 }' | cut -d'%' -f1)

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

  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  " " }' | main_prog
else
  df -hP |  grep -vE "^[^/]|tmpfs|cdrom"| awk '{print  " " }' | main_prog
fi

Or you could use this style of check I put in place for nagios (using snmp to connect to a remote host)

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

snmp_remote_disk_auto

snmp_remote_disk_auto

#!/bin/bash

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

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

function usage() { 
echo "
OK:  / (1879 MB) /var (2281 MB) /tmp (947 MB) /boot (175 MB)|/=1879MB;;;0 /var=2281MB;;;0 /tmp=947MB;;;0 /boot=175MB;;;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 "
#!/bin/sh

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

if [ "$CHECK" == "100%"]
then
    echo 1
else
    echo 0
fi
localhost public 1024" } server=; pass= limit=; errors_found=""; partitions_found=""; lower_limit=10; graphtext="|" if [ $# -lt 3 ]; then usage; exit 1; fi # takes <size> <used> <allocation> calc_free() { echo " - * 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

./snmp_remote_disk_auto localhost public 100

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

回答by Edward Fitz Abucay

Here is a simple script to check if there are already disk that reached their maximum capacity and -- if there were it would return / output 1.

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

/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

回答by user3000921

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

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

It will return all df -Phentries that exceed the LIMIT

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

For example, on my workstation, df -Phreturns:

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

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

Let's say I want to list the mount points that exceed 20% of capacity.

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

I use df -Ph | grep -v "Use%" | sed 's/%//g' | awk '$5 > 20 {print $1,$2,$3,$4,$5"%";}' | column -t, and it returns the following:

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

df | perl -wne 'if(/(\d+)%\s+(.*)/){exit 1 if >99}'

The column -tpart is here purely for the output to be readable.

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

回答by michas

How about something like:

怎么样:

##代码##

You can change the threshold and instead of printing you can just exit:

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

##代码##