简体   繁体   English

Bash 监控磁盘使用情况

[英]Bash monitor disk usage

I bought a NAS box which has a cut down version of debian on it.我买了一个 NAS 盒子,上面有一个精简版的 debian。

It ran out of space the other day and I did not realise.前几天空间用完了,我没有意识到。 I am basically wanting to write a bash script that will alert me whenever the disk gets over 90% full.我基本上是想写一个 bash 脚本,当磁盘超过 90% 满时会提醒我。

Is anyone aware of a script that will do this or give me some advice on writing one?有没有人知道会执行此操作的脚本或给我一些关于编写脚本的建议?

#!/bin/bash
source /etc/profile

# Device to check
devname="/dev/sdb1"

let p=`df -k $devname | grep -v ^File | awk '{printf ("%i",$3*100 / $2); }'`
if [ $p -ge 90 ]
then
  df -h $devname | mail -s "Low on space" my@email.com
fi

Crontab this to run however often you want an alert crontab 这个运行,但是你经常需要一个警报

EDIT: For multiple disks编辑:对于多个磁盘

#!/bin/bash
source /etc/profile

# Devices to check
devnames="/dev/sdb1 /dev/sda1"

for devname in $devnames
do
  let p=`df -k $devname | grep -v ^File | awk '{printf ("%i",$3*100 / $2); }'`
  if [ $p -ge 90 ]
  then
    df -h $devname | mail -s "$devname is low on space" my@email.com
  fi
done

I tried to use Erik's answer but had issues with devices having long names which wraps the numbers and causes script to fail, also the math looked wrong to me and didn't match the percentages reported by df itself.我尝试使用 Erik 的答案,但是设备名称很长,会包含数字并导致脚本失败,而且数学对我来说看起来是错误的,并且与df本身报告的百分比不匹配。

Here's an update to his script:这是他脚本的更新:

#!/bin/bash
source /etc/profile

# Devices to check
devnames="/dev/sda1 /dev/md1 /dev/mapper/vg1-mysqldisk1 /dev/mapper/vg4-ctsshare1 /dev/mapper/vg2-jbossdisk1 /dev/mapper/vg5-ctsarchive1 /dev/mapper/vg3-muledisk1"


for devname in $devnames
do
  let p=`df -Pk $devname | grep -v ^File | awk '{printf ("%i", $5) }'`
  if [ $p -ge 70 ]
  then
    df -h $devname | mail -s "$devname is low on space" my@email.com
  fi
done

Key changes are changed df -k to df -Pk to avoid line wrapping and simplified the awk to use pre-calc'd percent instead of recalcing.关键更改将df -k更改为df -Pk以避免换行并简化 awk 以使用预先计算的百分比而不是重新计算。

Based on @Erik answer, here is my version with variables :基于@Erik 的回答,这是我的带有变量的版本:

#!/bin/bash

DEVNAMES="/ /home"
THRESHOLD=80
EMAIL=you@email.com

host=$(hostname)
for devname in $DEVNAMES
do
    current=$(df $devname | grep / | awk '{ print $5}' | sed 's/%//g')
    if [ "$current" -gt "$THRESHOLD" ] ; then
        mail -s "Disk space alert on $host" "$EMAIL" << EOF
WARNING: partition $devname on $host is $current% !!

To list big files (>100Mo) :
  find $devname -xdev -type f -size +100M
EOF
    fi
done

And if you do not have the mail command on your server, you can send email via SMPT with swaks :如果您的服务器上没有mail命令,您可以使用swaks通过 SMPT 发送电子邮件:

swaks --from "$EMAIL" --to "$EMAIL" --server "TheServer" --auth LOGIN --auth-user "TheUser" --auth-password "ThePasswrd" --h-Subject "Disk space alert on $host" --body - << EOF
#!/bin/bash

DEVNAMES=$(df --output=source | grep ^/dev)
THRESHOLD=90
EMAIL=your@email
HOST=$(hostname)

for devname in $DEVNAMES
do
    current=$(df $devname | awk 'NR>1 {printf "%i",$5}')
    [ "$current" -gt "$THRESHOLD" ] && warn="WARNING: partition $devname on $HOST is $current% !! \n$warn"
done

[ "$warn" ] && echo -e "$warn" | mail -s "Disk space alert on $HOST" $EMAIL

Based on previous answers, here's my version with following changes:根据以前的答案,这是我的版本,有以下更改:

  1. Automatically checks all mounted devices自动检查所有挂载的设备
  2. Sends only one mail per check, regardless of how many devices are over the threshold无论有多少设备超过阈值,每次检查仅发送一封邮件
  3. Code generally tidied up代码一般都整理好了

You could also use Monit for this kind of job.您也可以将Monit用于此类工作。 It's a "free open source utility for managing and monitoring, processes, programs, files, directories and filesystems on a UNIX system".它是“用于管理和监视 UNIX 系统上的进程、程序、文件、目录和文件系统的免费开源实用程序”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM