Bash 监控磁盘使用情况 [英] Bash monitor disk usage

查看:23
本文介绍了Bash 监控磁盘使用情况的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

前几天空间用完了,我没有意识到.我基本上是想写一个 bash 脚本,当磁盘超过 90% 满时会提醒我.

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?

推荐答案

#!/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 让它运行,但是你想要一个警报的频率

Crontab this to run however often you want an alert

对于多个磁盘

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

这篇关于Bash 监控磁盘使用情况的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆