若是你坚持每天写一个shell脚本,一年下来那么你就会写300多个,假如每个脚本10行代码,那么一年下来大概有3000多行,3年下来就会将近10000行。当然,这里面会有好多重复的东西。其实,我们仔细想想shell无非也就是一个框架(判断或者循环)而已。若是你的逻辑思维够清晰,写个脚本有什么难的。如果你觉得难,那是因为你练习的不够,遇到问题没有仔细去想思路,有了思路,shell脚本就已经完成了80%,剩下的20%是把你想要的使用逻辑判断或者循环再加上若干命令堆在一起完成的。所以,从今天起,我要你练习的不仅仅是写出shell脚本,还要把你的思路写出来。
写一个shell脚本,检测所有磁盘分区使用率和inode使用率并记录到以当天日期为命名的日志文件里,当发现某个分区容量或者inode使用量大于85%时,发邮件通知你自己。
思路:就是先df -h 然后过滤出已使用的那一列,然后再想办法过滤出百分比的整数部分,然后和85去比较,同理,inode也是一样的思路。发邮件通知你自己,需要你的系统有smtp服务,可以安装 sendmail或者postfix,安装好后不用修改配置,启动服务就可以运行,发邮件使用命令:mail -s "主题" mailer < file.txt (这个文件就是邮件内容)。mail这个命令是安装mailx包得到的。
写一个shell脚本,检测所有磁盘分区使用率和inode使用率并记录到以当天日期为命名的日志文件里,当发现某个分区容量或者inode使用量大于85%时,发邮件通知你自己。
思路:就是先df -h 然后过滤出已使用的那一列,然后再想办法过滤出百分比的整数部分,然后和85去比较,同理,inode也是一样的思路。发邮件通知你自己,需要你的系统有smtp服务,可以安装 sendmail或者postfix,安装好后不用修改配置,启动服务就可以运行,发邮件使用命令:mail -s "主题" mailer < file.txt (这个文件就是邮件内容)。mail这个命令是安装mailx包得到的。
0
本帖最后由 齐天大圣 于 2014-9-10 17:23 编辑
#!/bin/bash
##df inode
date=`date +%F`
mail='lichao@jiangmin.com'
while :;do
df -h |awk '{print $1,$5}' >>$date.disk_and_inode.txt
echo "++++++++++++++++++++" >>$date.disk_and_inode.txt
df -i |awk '{print $1,$5}' >>$date.disk_and_inode.txt
a=`df -h |awk '{print $5}' |grep -v 'Use'|awk -F'%' '{print $1}'`
for i in $a;do
if [ $i -gt '85' ];then
echo "disk full!" |mail -s "disk" $mail
fi
done
b=`df -i |awk '{print $5}' |grep -v 'IUse'|awk -F'%' '{print $1}'`
for j in $b;do
if [ $j -gt '85' ];then
echo "inode full!" |mail -s "inone" $mail
fi
done
sleep 10
done
#!/bin/bash
##df inode
date=`date +%F`
mail='lichao@jiangmin.com'
while :;do
df -h |awk '{print $1,$5}' >>$date.disk_and_inode.txt
echo "++++++++++++++++++++" >>$date.disk_and_inode.txt
df -i |awk '{print $1,$5}' >>$date.disk_and_inode.txt
a=`df -h |awk '{print $5}' |grep -v 'Use'|awk -F'%' '{print $1}'`
for i in $a;do
if [ $i -gt '85' ];then
echo "disk full!" |mail -s "disk" $mail
fi
done
b=`df -i |awk '{print $5}' |grep -v 'IUse'|awk -F'%' '{print $1}'`
for j in $b;do
if [ $j -gt '85' ];then
echo "inode full!" |mail -s "inone" $mail
fi
done
sleep 10
done
0
本帖最后由 陈沛 于 2014-9-10 18:13 编辑
#!/bin/bash
# 2014-09-10
# chenpei
# The disk check
fileName=`date +%F`.log
`echo "磁盘使用情况:" >> $fileName`
` df -h >> $fileName`
`echo "磁盘 Inode使用情况:" >> $fileName`
`df -ih >> $fileName`
date=` df -h | grep ^/ | sed 's/%//g' | awk -F' ' '{print $5}' | awk '$1>85 {print $1}'; df -ih |grep ^/ | awk -F' ' '{print $5}' | sed 's/%//g' | awk '$1>85 {print $1}'`
if [ -n "$date" ]; then
`echo "Disk space is greater than 85%." | mail -s "Disk warning Email" 865907653@qq.com`
fi
#!/bin/bash
# 2014-09-10
# chenpei
# The disk check
fileName=`date +%F`.log
`echo "磁盘使用情况:" >> $fileName`
` df -h >> $fileName`
`echo "磁盘 Inode使用情况:" >> $fileName`
`df -ih >> $fileName`
date=` df -h | grep ^/ | sed 's/%//g' | awk -F' ' '{print $5}' | awk '$1>85 {print $1}'; df -ih |grep ^/ | awk -F' ' '{print $5}' | sed 's/%//g' | awk '$1>85 {print $1}'`
if [ -n "$date" ]; then
`echo "Disk space is greater than 85%." | mail -s "Disk warning Email" 865907653@qq.com`
fi
0
本帖最后由 wyatt88 于 2014-9-10 19:58 编辑
1、如何获得信息磁盘分区df -h inode使用率df -i
2、观察、加工获得的信息,以满足需求df -h | awk '{print $5}'|grep -v Use | cut -d '%' -f1 && df -ih | awk '{print $5}'|grep -v Use | cut -d '%' -f1
3、完成脚本
#!/bin/bash
filename=`date +%F`.log
while :;do
for dusage in `df -h | awk '{print $5}'|grep -v Use | cut -d '%' -f1`
do
if [ $dusage -gt 85 ]
then
cmail=`df -h | grep $dusage% | awk '{print $1}'`
echo "disk "$cmail >> $filename
cat $filename | sendmail wyatthaha@126.com
fi
done
for iusage in `df -ih | awk '{print $5}'|grep -v IUse | cut -d '%' -f1`
do
if [ $iusage -gt 85 ]
then
cmail=`df -h | grep $iusage% | awk '{print $1}'`
echo "inode "$cmail >> $filename
cat $filename | sendmail wyatthaha@126.com
fi
done
sleep 10
done
~
1、如何获得信息磁盘分区df -h inode使用率df -i
2、观察、加工获得的信息,以满足需求df -h | awk '{print $5}'|grep -v Use | cut -d '%' -f1 && df -ih | awk '{print $5}'|grep -v Use | cut -d '%' -f1
3、完成脚本
#!/bin/bash
filename=`date +%F`.log
while :;do
for dusage in `df -h | awk '{print $5}'|grep -v Use | cut -d '%' -f1`
do
if [ $dusage -gt 85 ]
then
cmail=`df -h | grep $dusage% | awk '{print $1}'`
echo "disk "$cmail >> $filename
cat $filename | sendmail wyatthaha@126.com
fi
done
for iusage in `df -ih | awk '{print $5}'|grep -v IUse | cut -d '%' -f1`
do
if [ $iusage -gt 85 ]
then
cmail=`df -h | grep $iusage% | awk '{print $1}'`
echo "inode "$cmail >> $filename
cat $filename | sendmail wyatthaha@126.com
fi
done
sleep 10
done
~
0
#!/bin/bash
T=`date +%Y-%m-%d`
df -h > /chengcheng/$T.log
df -i >>/chengcheng/$T.log
a=`df -h |awk '{print $5}'|grep -v 'Use%'`
b=`df -i |awk '{print $5}'|grep -v 'IUse%'`
for i in `$a $b`;do
if [ $i -gt 0.85 ];then
echo "Waring,the User% or IUse% greater then 85,you should deal with it!" | mail -s "Warining!" 1908364505@qq.com
fi
done
T=`date +%Y-%m-%d`
df -h > /chengcheng/$T.log
df -i >>/chengcheng/$T.log
a=`df -h |awk '{print $5}'|grep -v 'Use%'`
b=`df -i |awk '{print $5}'|grep -v 'IUse%'`
for i in `$a $b`;do
if [ $i -gt 0.85 ];then
echo "Waring,the User% or IUse% greater then 85,you should deal with it!" | mail -s "Warining!" 1908364505@qq.com
fi
done
0
本帖最后由 ocean 于 2014-9-10 20:17 编辑
# cat check.sh
#!/bin/bash
# Desc: this scripts is to check host disk and load , if host has problem then send mail to the user;
# Author: Jeffery.Su
# Date: 9/10/2014
# version: V1
# Definite variable
IP_ADDR=`ifconfig |grep 'inet addr:'|awk '{print $2}'|cut -d : -f 2|head -1`
LOG_FILE=/tmp/daily_check_$IP_ADDR.log
MAIL_ADDR=781303205@qq.com
echo "" > $LOG_FILE
# Definite method
function check_disk() {
echo "check disk" >>$LOG_FILE
ROW=`df -h|wc -l`
for i in `seq 2 $ROW`
do
DISK_FREE=`df -h| sed -n "$i"p|awk '{print $4}'`
DISK_NAME=`df -h |sed -n "$i"p| awk '{print $6}' `
USED_PERC=`df -h| sed -n "$i"p|sed -n 's#%##'p|awk '{print $5}'`
FREE_PREC=`expr 100 - $USED_PERC`
if [[ "$FREE_PREC" -lt "15" ]];then
echo -e "$DISK_NAME IS WARNING Used ${USED_PERC}% Avail $DISK_FREE \n " >>$LOG_FILE
elif [[ "$FREE_PREC" -lt "10" ]];then
echo -e "$DISK_NAME IS CRITICAL Used ${USED_PERC}% Avail $DISK_FREE \n " >>$LOG_FILE
else
echo -e "$DISK_NAME IS OK Used ${USED_PERC}% Avail $DISK_FREE \n" >>$LOG_FILE
fi
done
}
function check_inode() {
echo "check inode" >>$LOG_FILE
ROW=`df -h|wc -l`
for i in `seq 2 $ROW`
do
INODE_FREE=`df -ih| sed -n "$i"p|awk '{print $4}'`
INODE_NAME=`df -ih |sed -n "$i"p| awk '{print $6}' `
INODE_USED_PERC=`df -ih| sed -n "$i"p|sed -n 's#%##'p|awk '{print $5}'`
#INODE_FREE_PERC=`expr 100 - $INODE_USED_PERC`
INODE_FREE_PREC=`expr 100 - $INODE_USED_PERC`
#echo $INODE_FREE_PREC
if [[ "$INODE_FREE_PREC" -lt "15" ]];then
echo -e "$INODE_NAME IS WARNING Used ${INODE_USED_PERC}% Avail $INODE_FREE \n " >>$LOG_FILE
elif [[ "$INODE_FREE_PREC" -lt "10" ]];then
echo -e "$INODE_NAME IS CRITICAL Used ${INODE_USED_PERC}% Avail $INODE_FREE \n " >>$LOG_FILE
else
echo -e "$INODE_NAME IS OK Used ${INODE_USED_PERC}% Avail $INODE_FREE \n " >>$LOG_FILE
fi
done
}
# Call method
check_disk
check_inode
EVE_SUM=`grep "CRI\|WAR" $LOG_FILE|wc -l`
if [[ $EVE_SUM -gt 0 ]];then
# Send the log throught the mail
mail -s "$IP_ADDR check_daily with problem" $MAIL_ADDR < $LOG_FILE
else
mail -s "$IP_ADDR check_daily with ok" $MAIL_ADDR < $LOG_FILE
fi
# cat check.sh
#!/bin/bash
# Desc: this scripts is to check host disk and load , if host has problem then send mail to the user;
# Author: Jeffery.Su
# Date: 9/10/2014
# version: V1
# Definite variable
IP_ADDR=`ifconfig |grep 'inet addr:'|awk '{print $2}'|cut -d : -f 2|head -1`
LOG_FILE=/tmp/daily_check_$IP_ADDR.log
MAIL_ADDR=781303205@qq.com
echo "" > $LOG_FILE
# Definite method
function check_disk() {
echo "check disk" >>$LOG_FILE
ROW=`df -h|wc -l`
for i in `seq 2 $ROW`
do
DISK_FREE=`df -h| sed -n "$i"p|awk '{print $4}'`
DISK_NAME=`df -h |sed -n "$i"p| awk '{print $6}' `
USED_PERC=`df -h| sed -n "$i"p|sed -n 's#%##'p|awk '{print $5}'`
FREE_PREC=`expr 100 - $USED_PERC`
if [[ "$FREE_PREC" -lt "15" ]];then
echo -e "$DISK_NAME IS WARNING Used ${USED_PERC}% Avail $DISK_FREE \n " >>$LOG_FILE
elif [[ "$FREE_PREC" -lt "10" ]];then
echo -e "$DISK_NAME IS CRITICAL Used ${USED_PERC}% Avail $DISK_FREE \n " >>$LOG_FILE
else
echo -e "$DISK_NAME IS OK Used ${USED_PERC}% Avail $DISK_FREE \n" >>$LOG_FILE
fi
done
}
function check_inode() {
echo "check inode" >>$LOG_FILE
ROW=`df -h|wc -l`
for i in `seq 2 $ROW`
do
INODE_FREE=`df -ih| sed -n "$i"p|awk '{print $4}'`
INODE_NAME=`df -ih |sed -n "$i"p| awk '{print $6}' `
INODE_USED_PERC=`df -ih| sed -n "$i"p|sed -n 's#%##'p|awk '{print $5}'`
#INODE_FREE_PERC=`expr 100 - $INODE_USED_PERC`
INODE_FREE_PREC=`expr 100 - $INODE_USED_PERC`
#echo $INODE_FREE_PREC
if [[ "$INODE_FREE_PREC" -lt "15" ]];then
echo -e "$INODE_NAME IS WARNING Used ${INODE_USED_PERC}% Avail $INODE_FREE \n " >>$LOG_FILE
elif [[ "$INODE_FREE_PREC" -lt "10" ]];then
echo -e "$INODE_NAME IS CRITICAL Used ${INODE_USED_PERC}% Avail $INODE_FREE \n " >>$LOG_FILE
else
echo -e "$INODE_NAME IS OK Used ${INODE_USED_PERC}% Avail $INODE_FREE \n " >>$LOG_FILE
fi
done
}
# Call method
check_disk
check_inode
EVE_SUM=`grep "CRI\|WAR" $LOG_FILE|wc -l`
if [[ $EVE_SUM -gt 0 ]];then
# Send the log throught the mail
mail -s "$IP_ADDR check_daily with problem" $MAIL_ADDR < $LOG_FILE
else
mail -s "$IP_ADDR check_daily with ok" $MAIL_ADDR < $LOG_FILE
fi
0
#!/bin/bash
DATE=`date +%F`
size_use=${DATE}.df.txt
inode_use=${DATE}.inode.txt
mail='619884311@qq.com'
echo $mail
df_use(){
df -h | sed 's/%//' | awk ' NR >2 {print $1,$5}' > ${size_use}
for line in `awk '$2>85 {print $1}' ${size_use}`
do
echo "the disk of $line is overuse" | mail -s 'disk warning' $mail
done
}
df_inode(){
df -i | sed 's/%//' | awk 'NR>2{print $1,$5}'>${inode_use}
for line in `awk '$2>85{print $1}' ${inode_use}`
do
echo "the inode of $line is overuse " | mail -s 'inode warning' $mail
done
}
df_use
df_inode
DATE=`date +%F`
size_use=${DATE}.df.txt
inode_use=${DATE}.inode.txt
mail='619884311@qq.com'
echo $mail
df_use(){
df -h | sed 's/%//' | awk ' NR >2 {print $1,$5}' > ${size_use}
for line in `awk '$2>85 {print $1}' ${size_use}`
do
echo "the disk of $line is overuse" | mail -s 'disk warning' $mail
done
}
df_inode(){
df -i | sed 's/%//' | awk 'NR>2{print $1,$5}'>${inode_use}
for line in `awk '$2>85{print $1}' ${inode_use}`
do
echo "the inode of $line is overuse " | mail -s 'inode warning' $mail
done
}
df_use
df_inode
0
#!/bin/bash
## This script is for record Filesystem Use%,IUse% everyday and send alert mail when % is more than 85%.
## Writed by Louis on 2014/09/11 11:15
log=/var/log/disk/`date +%F`.log
date +'%F %T' > $log
df -h >> $log
echo >> $log
df -i >> $log
for i in `df -h|grep -v 'Use%'|sed 's/%//'|awk '{print $5}'`; do
if [ $i -gt 85 ]; then
use=`df -h|grep -v 'Use%'|sed 's/%//'|awk '$5=='$i' {print $1,$5}'`
echo "$use" >> use
fi
done
if [ -e use ]; then
mail -s "Filesystem Use% check" root@localhost < use
rm -rf use
fi
for j in `df -i|grep -v 'IUse%'|sed 's/%//'|awk '{print $5}'`; do
if [ $j -gt 85 ]; then
iuse=`df -i|grep -v 'IUse%'|sed 's/%//'|awk '$5=='$j' {print $1,$5}'`
echo "$iuse" >> iuse
fi
done
if [ -e iuse ]; then
mail -s "Filesystem IUse% check" root@localhost < iuse
rm -rf iuse
fi
思路:
1、df -h、df -i 记录磁盘分区使用率和inode使用率,date +%F 日志名格式
2、取出使用率(第5列)百分比序列,for循环逐一与85比较,大于85则记录到新文件里,当for循环结束后,汇总超过85的一并发送邮件(邮箱服务因未搭建,发送本地root账户)。
此脚本正确运行前提:
该系统没有逻辑卷的情况下使用,因为逻辑卷df -h、df -i 时,使用率百分比是在第4列,而不是第5列。如有逻辑卷,则会漏统计逻辑卷使用情况。
## This script is for record Filesystem Use%,IUse% everyday and send alert mail when % is more than 85%.
## Writed by Louis on 2014/09/11 11:15
log=/var/log/disk/`date +%F`.log
date +'%F %T' > $log
df -h >> $log
echo >> $log
df -i >> $log
for i in `df -h|grep -v 'Use%'|sed 's/%//'|awk '{print $5}'`; do
if [ $i -gt 85 ]; then
use=`df -h|grep -v 'Use%'|sed 's/%//'|awk '$5=='$i' {print $1,$5}'`
echo "$use" >> use
fi
done
if [ -e use ]; then
mail -s "Filesystem Use% check" root@localhost < use
rm -rf use
fi
for j in `df -i|grep -v 'IUse%'|sed 's/%//'|awk '{print $5}'`; do
if [ $j -gt 85 ]; then
iuse=`df -i|grep -v 'IUse%'|sed 's/%//'|awk '$5=='$j' {print $1,$5}'`
echo "$iuse" >> iuse
fi
done
if [ -e iuse ]; then
mail -s "Filesystem IUse% check" root@localhost < iuse
rm -rf iuse
fi
思路:
1、df -h、df -i 记录磁盘分区使用率和inode使用率,date +%F 日志名格式
2、取出使用率(第5列)百分比序列,for循环逐一与85比较,大于85则记录到新文件里,当for循环结束后,汇总超过85的一并发送邮件(邮箱服务因未搭建,发送本地root账户)。
此脚本正确运行前提:
该系统没有逻辑卷的情况下使用,因为逻辑卷df -h、df -i 时,使用率百分比是在第4列,而不是第5列。如有逻辑卷,则会漏统计逻辑卷使用情况。
0
代码好少,可以运行,老师检查下符合要求吗?
#!/bin/bash
date=`date +%Y%m%d`
n=$date.log
m=$date.ilog
df -h >$n
df -i >$m
a=`awk '{print $5}' $n |grep -v "Use%"|awk -F '%' '{print $1}'`
b=`awk '{print $5}' $m |grep -v "Use%"|awk -F '%' '{print $1}'`
for i in $a $b
do
if [ $i -ge 85 ]
then
echo "the disk's space more then 85%" |mail -s "you must deal with it" 376486397@qq.com
fi
done
#!/bin/bash
date=`date +%Y%m%d`
n=$date.log
m=$date.ilog
df -h >$n
df -i >$m
a=`awk '{print $5}' $n |grep -v "Use%"|awk -F '%' '{print $1}'`
b=`awk '{print $5}' $m |grep -v "Use%"|awk -F '%' '{print $1}'`
for i in $a $b
do
if [ $i -ge 85 ]
then
echo "the disk's space more then 85%" |mail -s "you must deal with it" 376486397@qq.com
fi
done
0
这两天有点忙,脚本也是边工作边写的,中间也不知道被打断多少次了。。。 哎。。 脚本运行没问题 但是自己测试了邮件发送,提示错误
echo "ssssss" |mail -s "dddd" 1791364472@qq.com
-bash: mail: command not found
sendmail 服务是开启的。。
service sendmail status
sendmail (pid 1086) 正在运行...
sm-client (pid 1094) 正在运行...
原因还没查出来。。。
#bin/bash
#9.10
#liuyuntao
DISK_USIZE=`df -h | awk 'NR>1'|awk '{print $5}'|sed s#%##`
INODE_USIZE=`df -i | awk 'NR>1'|awk '{print $5}'|sed s#%##`
DATE=`date +%F`
LOG=${DATE}.log
df -h > $LOG
df -i >>$LOG
for i in $DISK_USIZE $INODE_USIZE; do
if [ $i -ge 85 ]; then
echo "space more than 85%" |mail -s " warnning" 1791364472@qq.com
else
echo ok
fi
done
echo "ssssss" |mail -s "dddd" 1791364472@qq.com
-bash: mail: command not found
sendmail 服务是开启的。。
service sendmail status
sendmail (pid 1086) 正在运行...
sm-client (pid 1094) 正在运行...
原因还没查出来。。。
#bin/bash
#9.10
#liuyuntao
DISK_USIZE=`df -h | awk 'NR>1'|awk '{print $5}'|sed s#%##`
INODE_USIZE=`df -i | awk 'NR>1'|awk '{print $5}'|sed s#%##`
DATE=`date +%F`
LOG=${DATE}.log
df -h > $LOG
df -i >>$LOG
for i in $DISK_USIZE $INODE_USIZE; do
if [ $i -ge 85 ]; then
echo "space more than 85%" |mail -s " warnning" 1791364472@qq.com
else
echo ok
fi
done
0
本帖最后由 游夜 于 2014-9-11 22:38 编辑
#!/bin/bash
file=`date +%F`.log #定义一个以当前日期命名的文件
echo "DISK storage Used Info:">>$file #注释用,说名以下内容为磁盘空间的使用信息
df -h >> $file #将磁盘使用信息追加至该文件内
echo "DISK Inode Used Info:">>$file #注释用,说名以下内容为磁盘inode的使用信息
df -i >> $file #将磁盘inode使用信息追加至该文件内
sed 's/%//g' $file|awk '$5~/^[0-9]/{print $5}' >tmp.txt #取文件Use及Iuse这一列的内容,去除%号,匹配以数字开头的项(去除其他不必要的字符)并写入tmp.txt文件
for check in `cat tmp.txt`#检查tmp.txt的数值是否有大于85的,只要有一个满足就发送邮件并终断循环
do
if [ $check -gt 85 ]
then
echo "Your disk space or inode is used more than 85%. "|mail -s 'disk check' 275235245@qq.com
break;
fi
done
#!/bin/bash
file=`date +%F`.log #定义一个以当前日期命名的文件
echo "DISK storage Used Info:">>$file #注释用,说名以下内容为磁盘空间的使用信息
df -h >> $file #将磁盘使用信息追加至该文件内
echo "DISK Inode Used Info:">>$file #注释用,说名以下内容为磁盘inode的使用信息
df -i >> $file #将磁盘inode使用信息追加至该文件内
sed 's/%//g' $file|awk '$5~/^[0-9]/{print $5}' >tmp.txt #取文件Use及Iuse这一列的内容,去除%号,匹配以数字开头的项(去除其他不必要的字符)并写入tmp.txt文件
for check in `cat tmp.txt`#检查tmp.txt的数值是否有大于85的,只要有一个满足就发送邮件并终断循环
do
if [ $check -gt 85 ]
then
echo "Your disk space or inode is used more than 85%. "|mail -s 'disk check' 275235245@qq.com
break;
fi
done
0
8错,很清晰
游夜 发表于 2014-9-11 22:34
#!/bin/bash
file=`date +%F`.log #定义一个以当前日期命名的文件
echo "DISK storage Used Info:">>$fi ...
8错,很清晰
0
#! /bin/bash
date=`date +%F`
df -h >> $date.log
df -i >> $date.log
a=`awk '{print $5}' $date.log|grep -v 'Use%'|grep -v 'IUse%'|sed 's/\%//g'|xargs`
while :; do
for i in $a; do
if [ $i -ge 85 ];
then
echo "Disk usage than 85%!"
mail -s "Disk usage than 85%!" 550935424@qq.com < $date.log
fi
done
sleep 10
done
date=`date +%F`
df -h >> $date.log
df -i >> $date.log
a=`awk '{print $5}' $date.log|grep -v 'Use%'|grep -v 'IUse%'|sed 's/\%//g'|xargs`
while :; do
for i in $a; do
if [ $i -ge 85 ];
then
echo "Disk usage than 85%!"
mail -s "Disk usage than 85%!" 550935424@qq.com < $date.log
fi
done
sleep 10
done
0
#! /bin/bash
n=`df -h|grep "sda2"|awk '{print $5}'|sed 's/%//g'`
if [ $n -gt 85 ];then
echo "warming!!!your haed disk useage is too high"
fi
n=`df -h|grep "sda2"|awk '{print $5}'|sed 's/%//g'`
if [ $n -gt 85 ];then
echo "warming!!!your haed disk useage is too high"
fi
0
#! /bin/bash
n=`df -h|grep "sda2"|awk '{print $5}'|sed 's/%//g'`
i=`df -i|grep "sda2"|awk '{print $5}'|sed 's/%//g'`
if [ $n -gt 85 ];then
echo "warming!!!your hard disk useage is too high"
fi
if [ $i -gt 85 ];then
echo "Warming!!! your inode useage is too high "
fi
n=`df -h|grep "sda2"|awk '{print $5}'|sed 's/%//g'`
i=`df -i|grep "sda2"|awk '{print $5}'|sed 's/%//g'`
if [ $n -gt 85 ];then
echo "warming!!!your hard disk useage is too high"
fi
if [ $i -gt 85 ];then
echo "Warming!!! your inode useage is too high "
fi
0
#! /bin/bash
n=`df -h|grep "sda2"|awk '{print $5}'|sed 's/%//g'`
i=`df -i|grep "sda2"|awk '{print $5}'|sed 's/%//g'`
d=`date +%Y%m%d`
if [ $n -gt 85 ];then
echo "warming!!!your hard disk useage is too high"
else
echo "your hard disk useage is $n" >/var/$d.log
fi
if [ $i -gt 85 ];then
echo "Warming!!! your inode useage is too high "
else
echo "your hard inode useage is $i" >/var/$d.log
fi
好了。下次不刷经验了
n=`df -h|grep "sda2"|awk '{print $5}'|sed 's/%//g'`
i=`df -i|grep "sda2"|awk '{print $5}'|sed 's/%//g'`
d=`date +%Y%m%d`
if [ $n -gt 85 ];then
echo "warming!!!your hard disk useage is too high"
else
echo "your hard disk useage is $n" >/var/$d.log
fi
if [ $i -gt 85 ];then
echo "Warming!!! your inode useage is too high "
else
echo "your hard inode useage is $i" >/var/$d.log
fi
好了。下次不刷经验了
0
本帖最后由 cmzsteven 于 2015-2-8 17:03 编辑
#!/bin/bash
logfile="df_"`date +%F`".log"
df -h > $logfile
df -i >> $logfile
test_error=0
for i in `cat $logfile |awk '{print $5}'|grep -v 'Use%'|sed '1,$s/%//g'`;do
if [ $i -gt 85 ] ; then
test_error=1
fi
done
if [ "$test_error" == "1" ] ; then
mail -s "Hard disc willout enough space!" abc@139.com < $logfile
fi
#!/bin/bash
logfile="df_"`date +%F`".log"
df -h > $logfile
df -i >> $logfile
test_error=0
for i in `cat $logfile |awk '{print $5}'|grep -v 'Use%'|sed '1,$s/%//g'`;do
if [ $i -gt 85 ] ; then
test_error=1
fi
done
if [ "$test_error" == "1" ] ; then
mail -s "Hard disc willout enough space!" abc@139.com < $logfile
fi
0
- #!/bin/bash
- #Detect disk usage
- #2015/05/14
- disk=/data/disk/`date +%F`.log
- df -h >> $disk
- echo >> $disk
- df -i >> $disk
- sed 's/%//g' $disk | awk '$5~/^[0-9]/{print $5}' > ls.txt
- for i in `cat ls.txt`
- do
- if [ $i -gt 85 ]
- then
- echo " Your disk space usage exceeds %85 " | mail -s disk xxxxxxxxx@qq.com
- fi
- done
0
本帖最后由 Armani 于 2015-8-24 17:08 编辑
aa=`df -h | awk '{print $5}' | grep '[0-9]' | sed 's/%//g'`
bb=`df -i | awk '{print $5}' | grep '[0-9]' | sed 's/%//g'`
cc=`date +%F`
df -h >> /scripts/shell/"$cc".log
df -i >> /scripts/shell/"$cc".log
if [ $aa -gt 85 ];then
mail -s "Warning" xxxx@163.com
fi
if [ $bb -gt 85 ];then
mail -s "Warning" xxxx@163.com
fi
aa=`df -h | awk '{print $5}' | grep '[0-9]' | sed 's/%//g'`
bb=`df -i | awk '{print $5}' | grep '[0-9]' | sed 's/%//g'`
cc=`date +%F`
df -h >> /scripts/shell/"$cc".log
df -i >> /scripts/shell/"$cc".log
if [ $aa -gt 85 ];then
mail -s "Warning" xxxx@163.com
fi
if [ $bb -gt 85 ];then
mail -s "Warning" xxxx@163.com
fi
0
#!/bin/bash k=`df -h|wc -l` m=$[$k - 3] #echo "m is $m" a=`df -h|tail -$m|awk -F ' ' '{print $5}'|sed 's/%//g'` b=`df -h|head -3|tail -1|awk -F ' ' '{print $4}'|sed 's/%//g' ` c=`df -h|tail -$m|awk -F ' ' '{print $1}'` v=`df -h &> 1.txt` if [ $b -gt 85 ];then w=`df -h|head -3|tail -1| grep $i%|awk -F' ' '{print $1}'` #mail -s "$w 容量有问题" 18856066058@139.com < 1.txt echo "邮件内容" |mail -s "主题" "$w 容量有问题" 18856066058@139.com < 1.txt fi for i in $a do if [ $i -ge 85 ];then d=`df -h|tail -$m |grep $i%|awk -F' ' '{print $1}'` #mail -s "$d 容量有问题" 18856066058@139.com < 1.txt echo "邮件内容" |mail -s "$d 容量有问题" 18856066058@139.com < 1.txt fi done
0
#!/bin/bash
df | grep -v '/mnt/' | awk '{print $5}' | grep -o '[0-9]\+' > /tmp/1.txt
df -i | grep -v '/mnt/' | awk '{print $5}' | grep -o '[0-9]\+' >> /tmp/1.txt
echo "分区" > /tmp/2.txt
df -h >> /tmp/2.txt
echo "inode" >> /tmp/2.txt
df -i >> /tmp/2.txt
for i in `cat /tmp/1.txt`
do
if [ "$i" -gt "80" ];then
mailx -s "aliyun" yu-yan-good@163.com < /tmp/2.txt
fi
done
date=`date "+%F %T"`
dump -0j -f /root/bak/disk/"$date"_disk_info.bz2 /tmp/2.txt
rm -rf /tmp/[12].txt
cd /root/bak/disk
a=`ls | wc -l`
if [ "$a" -le "5" ];then
exit
else
ls | head -1 | xargs -n1 -i{} rm -rf {}
fi
df | grep -v '/mnt/' | awk '{print $5}' | grep -o '[0-9]\+' > /tmp/1.txt
df -i | grep -v '/mnt/' | awk '{print $5}' | grep -o '[0-9]\+' >> /tmp/1.txt
echo "分区" > /tmp/2.txt
df -h >> /tmp/2.txt
echo "inode" >> /tmp/2.txt
df -i >> /tmp/2.txt
for i in `cat /tmp/1.txt`
do
if [ "$i" -gt "80" ];then
mailx -s "aliyun" yu-yan-good@163.com < /tmp/2.txt
fi
done
date=`date "+%F %T"`
dump -0j -f /root/bak/disk/"$date"_disk_info.bz2 /tmp/2.txt
rm -rf /tmp/[12].txt
cd /root/bak/disk
a=`ls | wc -l`
if [ "$a" -le "5" ];then
exit
else
ls | head -1 | xargs -n1 -i{} rm -rf {}
fi
0
- #!/bin/bash
- ## This is fdisk free total
- ## gxp 2016-03-22
- a=`df -h`;echo -e "`date +%F-%T` \n$a" >fs_du.log
- b=`sed s/%//g fs_du.log |awk '$5>85 {print $6}'|sed s/Mounted//g`
- if [ -n "$b" ]
- then
- cd $b;find . -size +100M >bigsize.file
- mail -s "Host Pattion Use 85%" lz_gxp@126.com
- else
- exit
- fi
0
#! /bin/bash
df_log=`date +%F`.log
disk_use_max=`df -h |sed -n '2,$'p |sed 's/%//g' |awk '{print $5}' |sort -n |sed -n '$'p`
inode_use_max=`df -i |sed -n '2,$'p |sed 's/%//g' |awk '{print $5}' |sort -n |sed -n '$'p`
while [ `date +%H:%M:%S`=="00:00:00" ]
do
touch $df_log
echo `df -h` >> $df_log
echo `df -i` >> $df_log
if [ disk_use_max -gt '85' ]
then
mail -s "disk_use overload" mailer < file.txt
fi
if [ disk_inode_max -gt '85' ]
then
mail -s "inode_use overload" mailer < file.txt
fi
done
df_log=`date +%F`.log
disk_use_max=`df -h |sed -n '2,$'p |sed 's/%//g' |awk '{print $5}' |sort -n |sed -n '$'p`
inode_use_max=`df -i |sed -n '2,$'p |sed 's/%//g' |awk '{print $5}' |sort -n |sed -n '$'p`
while [ `date +%H:%M:%S`=="00:00:00" ]
do
touch $df_log
echo `df -h` >> $df_log
echo `df -i` >> $df_log
if [ disk_use_max -gt '85' ]
then
mail -s "disk_use overload" mailer < file.txt
fi
if [ disk_inode_max -gt '85' ]
then
mail -s "inode_use overload" mailer < file.txt
fi
done
0
#!/bin/bash
/usr/bin/yum remove -y postfix >/dev/null 2>&1
c=`which sendmail`
d=`netstat -lnp |grep ':25'`
if [ -z "$c" ]
then
/usr/bin/yum install -y sendmail
service sendmail start
elif [ -z "$d" ] && [ -n "$c" ]
then
service sendmail start
fi
df -h -P >>/tmp/disk_usage.txt
df -i -P >>/tmp/inode_usage.txt
disk_u=`df -h -P |sed 's/\%//g' |awk '{print $5}'|grep -iv use`
inode_u=`df -i -P |sed 's/\%//g' |awk '{print $5}'|grep -iv use`
for a in $disk_u
do
if [ $a -gt 30 ]
then
/usr/sbin/sendmail -s "check disk usage at /tmp/" 327357283@qq.com </tmp/disk_usage.txt
fi
done
for b in $inode_u
do
if [ $b -gt 10 ]
then
/usr/sbin/sendmail -s "check inode usage at /tmp/" 327357283@qq.com </tmp/inode_usage.txt
fi
done
/usr/bin/yum remove -y postfix >/dev/null 2>&1
c=`which sendmail`
d=`netstat -lnp |grep ':25'`
if [ -z "$c" ]
then
/usr/bin/yum install -y sendmail
service sendmail start
elif [ -z "$d" ] && [ -n "$c" ]
then
service sendmail start
fi
df -h -P >>/tmp/disk_usage.txt
df -i -P >>/tmp/inode_usage.txt
disk_u=`df -h -P |sed 's/\%//g' |awk '{print $5}'|grep -iv use`
inode_u=`df -i -P |sed 's/\%//g' |awk '{print $5}'|grep -iv use`
for a in $disk_u
do
if [ $a -gt 30 ]
then
/usr/sbin/sendmail -s "check disk usage at /tmp/" 327357283@qq.com </tmp/disk_usage.txt
fi
done
for b in $inode_u
do
if [ $b -gt 10 ]
then
/usr/sbin/sendmail -s "check inode usage at /tmp/" 327357283@qq.com </tmp/inode_usage.txt
fi
done
0
!/bin/bash
Description: This script is to check system disk and inode usage.
Author: Jiazhi Yang
Date: 20/11/2016
Script Name: diskusagerate.sh
#vol=df -h | awk -F'[ %]+' '{print $5":"$6}'|grep "/"
vol=df -h |awk '{print $5":"$6}'|sed -n 's/\%//g'p |sed -n '2,$'p
curip=/sbin/ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr -d "addr:"
curdir=$(pwd)
curtime=date +%F-%s
for i in $voldo
count=echo $i |cut -d : -f1
mountPoint=echo $i |cut -d : -f2
if [ $count -gt 10 ];then
echo "$count"% "$mountPoint" >$curdir/systemdiskinfo.log
fi
done
echo "Now,we will send you on system disk info.please check."
sleep 3
/bin/mail -s "system disk info" 2067562440@qq.com < $curdir/systemdiskinfo.log
if [ $? -eq 0 ]; then
sleep 3
echo "OK, mail is already sent at: $curtime!"
fi
0
#! /bin/bash
d=`date +%F`
disklog=$d.disk.log
inodelog=$d.inode.log
for i in `df -h | awk '{print $5}'| grep -v [a-Z] | awk -F '%' '{print $1}'`
do
echo $i >> /tmp/$disklog
if [ $i -gt 16 ]
then
mail -s "title" test@163.com < /tmp/$disklog
fi
done
d=`date +%F`
disklog=$d.disk.log
inodelog=$d.inode.log
for i in `df -h | awk '{print $5}'| grep -v [a-Z] | awk -F '%' '{print $1}'`
do
echo $i >> /tmp/$disklog
if [ $i -gt 16 ]
then
mail -s "title" test@163.com < /tmp/$disklog
fi
done
0
#! /bin/bash
d=`date +%F`
disklog=$d.disk.log
inodelog=$d.inode.log
for i in `df -h | awk '{print $5}'| grep -v [a-Z] | awk -F '%' '{print $1}'`
do
echo `df -h` >> /tmp/$disklog
if [ $i -gt 16 ]
then
mail -s "title" test@163.com < /tmp/$disklog
fi
done
for u in `df -i | awk '{print $5}'| grep -v [a-Z] | awk -F '%' '{print $1}'`
do
echo `df -i` >> /tmp/$inodelog
if [ $u -gt 80 ]
then
mail -s "title" test@163.com < /tmp/$inodelog
fi
done
0
#!/bin/bash
d=`date +%F`.log
s=`df -h|sed -n '2,$'p|awk '{print $5}'|sed 's/%//g'`
i=`df -i|sed -n '2,$'p|awk '{print $5}'|sed 's/%//g'`
#检查磁盘分区使用率,记录以当天日期为命名的日志文件
df -h >>$d
df -ih>>$d
#分区或inode使用量大于85%,发邮件通知自己
if [ $s -ge 85 -o $i -ge 85 ]
then
echo "size or inode use ge 85%"|mail -s "inode or size" 123@.163.com
fi
编辑回复