Bash 监控磁盘使用情况
我买了一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
crontab 可以根据您想要的频率运行警报
编辑:对于多个磁盘
Crontab this to run however often you want an alert
EDIT: For multiple disks
我尝试使用 Erik 的答案,但遇到了名称较长的设备的问题,这些名称包含数字并导致脚本失败,而且数学对我来说看起来是错误的,并且与 df 本身报告的百分比不匹配。
以下是他的脚本的更新:
关键更改为
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:
Key changes are changed
df -k
todf -Pk
to avoid line wrapping and simplified the awk to use pre-calc'd percent instead of recalcing.您还可以使用 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".
根据@Erik的回答,这是我的带有变量的版本:
如果您的服务器上没有
mail
命令,您可以使用swaks
通过 SMPT 发送电子邮件:Based on @Erik answer, here is my version with variables :
And if you do not have the
mail
command on your server, you can send email via SMPT withswaks
:根据以前的答案,这是我的版本,进行了以下更改:
Based on previous answers, here's my version with following changes: