对 Linux 服务器可用空间进行监控
手头上有几台 Linux 服务器,硬盘空间比较有限。但因为系统日志、数据备份等各种原因,经常会导致空间不足。所以写了个脚本去监控硬盘可用空间,并使用 Bark 进行告警通知。
#!/bin/sh # 设定一个阈值,如 80% intFreePercentThresholds=80 # 获取指定挂载目录可用百分比。如 sda2 intFreePercent=`df -TH | grep sda2 | awk '{print $6}' | rev | cut -c2- | rev` # 服务器名称或 IP stringCurrentServerName='server01.mydomain.com' # 当前时间 stringCurrentTime=`date '+%Y-%m-%d %H:%M:%S'` # 通知标题 stringTitle='Disk space monitoring' # 把通知标题进行 urlencode stringURLEncodeTitle=`echo -n $stringTitle | tr -d '\n' | od -An -tx1 | tr ' ' % | tr -d '\n'` # 通知内容 stringContent="Current time is $stringCurrentTime, current server name is [$stringCurrentServerName], the server data partition used space percent is $intFreePercent%. It is very ugrent, Plase note that." # 把通知内容进行 urlencode stringURLEncodeContent=`echo -n $stringContent | tr -d '\n' | od -An -tx1 | tr ' ' % | tr -d '\n'` if [ $intFreePercent -gt $intFreePercentThresholds ]; then # 通过 Bark 发出消息 stringSendMessageURL=`/usr/bin/curl https://bark.mydomain.com/123456789abcdefghijkl/$stringURLEncodeTitle/$stringURLEncodeContent` fi