分享一个shell脚本代码,用于监控服务器硬盘使用率,当超过90%时发邮件报警,shell脚本监控脚本学习。 用shell脚本监控服务器硬盘使用率
1、shell脚本代码监控硬盘使用率。
复制代码代码示例:
#!/bin/sh
# 用法:./scriptname.sh # SEE URL: http://www.cyberciti.biz/tips/shell-script-to-watch-the-disk-space.html # set admin email so that you can get email ADMIN="me@somewher.com" # set alert level 90% is default ALERT=90 df -H | grep -vE '^Filesystem|tmpfs|cdrom' | awk '{ print $5 " " $1 }' | while read output; do #echo $output usep=$(echo $output | awk '{ print $1}' | cut -d'%' -f1 ) partition=$(echo $output | awk '{ print $2 }' ) if [ $usep -ge $ALERT ]; then echo "Running out of space "$partition ($usep%)" on $(hostname) as on $(date)" | mail -s "Alert: Almost out of disk space $usep" $ADMIN fi done
Shell scripts 监控linux硬盘使用量(在硬盘使用量达到80%发送邮件报警)在服务器没有上nagios 监控服务器之前不可能时刻登陆服务器检查服务器硬盘使用情况。
服务器设置
#service sendmail restart
#chkconfig –level 2345 sendmail on # 在重启服务器时可以自动启动sendmail服务。
脚本内容:
复制代码代码示例:
#!/bin/bash
#This scripts used montor disk. #This script editor is written by badboy ,connect leezhenhua17@163.com e_mail=leezhenhua17@163.com Day=$(date ‘+%w’) hour=$(date ‘+H’) log=/var/log/hd.log DATE=$(date ‘+%Y-%m-%d %H:%I:%S’) Host=`hostname` echo “====================${Host}${DATE}===================” > $log hd_info=`df -h|grep -v “File” |sed ‘s/%//g’| awk ‘{if ($5 > 80) print $0}’` if [ -n "$hd_info" ] then df -h|grep -v “File” |sed ‘s/%//g’| awk ‘{if ($5 > 80) print $0}’ >> $log /bin/mail -s “$Host Disk warning” $e_mail < $log else echo “Disk space is ok” >>$log if [ $Day -eq 1 && $hour -eq 9 ] then df -h|grep -v “File” |sed ‘s/%//g’| awk ‘{ print $0}’ >>$log /bin/mail -s “$Host Dis Information” < $log else : fi fi
添加到crontab中,定时执行: 如果有硬盘使用量达到80% 就会收到邮件,现在可以使用手机邮箱,只要有新邮件就会手机短信提醒,可以使时刻知道那个服务器硬盘使用到达80%。 |