Bash 监控磁盘使用情况

发布于 2024-11-01 04:23:36 字数 139 浏览 4 评论 0原文

我买了一个 NAS 盒子,上面有 debian 的精简版。

前几天它空间不足,但我没有意识到。我基本上想编写一个 bash 脚本,每当磁盘已满 90% 以上时就会提醒我。

有谁知道可以执行此操作的脚本或给我一些关于编写脚本的建议吗?

I bought a NAS box which has a cut down version of debian on it.

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.

Is anyone aware of a script that will do this or give me some advice on writing one?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

把人绕傻吧 2024-11-08 04:23:36
#!/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" [email protected]
fi

crontab 可以根据您想要的频率运行警报

编辑:对于多个磁盘

#!/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" [email protected]
  fi
done
#!/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" [email protected]
fi

Crontab this to run however often you want an alert

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" [email protected]
  fi
done
乄_柒ぐ汐 2024-11-08 04:23:36

我尝试使用 Erik 的答案,但遇到了名称较长的设备的问题,这些名称包含数字并导致脚本失败,而且数学对我来说看起来是错误的,并且与 df 本身报告的百分比不匹配。

以下是他的脚本的更新:

#!/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" [email protected]
  fi
done

关键更改为 df -k 更改为 df -Pk 以避免换行,并简化 awk 以使用预先计算的百分比而不是重新计算。

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.

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" [email protected]
  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.

梦萦几度 2024-11-08 04:23:36

您还可以使用 Monit 来完成此类工作。它是一个“免费的开源实用程序,用于管理和监视 UNIX 系统上的进程、程序、文件、目录和文件系统”。

You could also use Monit for this kind of job. It's a "free open source utility for managing and monitoring, processes, programs, files, directories and filesystems on a UNIX system".

那伤。 2024-11-08 04:23:36

根据@Erik的回答,这是我的带有变量的版本:

#!/bin/bash

DEVNAMES="/ /home"
THRESHOLD=80
[email protected]

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

如果您的服务器上没有 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

Based on @Erik answer, here is my version with variables :

#!/bin/bash

DEVNAMES="/ /home"
THRESHOLD=80
[email protected]

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 :

swaks --from "$EMAIL" --to "$EMAIL" --server "TheServer" --auth LOGIN --auth-user "TheUser" --auth-password "ThePasswrd" --h-Subject "Disk space alert on $host" --body - << EOF
少钕鈤記 2024-11-08 04:23:36
#!/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

根据以前的答案,这是我的版本,进行了以下更改:

  1. 自动检查所有已安装的设备
  2. 每次检查仅发送一封邮件,无论有多少设备超过阈值
  3. 代码通常会整理
#!/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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文