linux自动检测磁盘空间脚本并自动报

作者:简简单单 2013-10-16


闲来蛋痛写了一代码

例1

 代码如下 复制代码

#!/bin/sh
MAX=99
LIST=$(df -h|awk '{print $5}'|sed 's/%//g'|sed '/[a-zA-Z]/d')
 
mailto="135********@139.com"
subject="$(hostname)'s partition alert"
content="$(hostname)'s used detail:n $(df -h |awk '{print$6,$5}') ntime:$(date -d today +'%Y-%m-%d %H:%M:%S')"
 
for PERCENT in $LIST; do
    if [ $PERCENT -gt $MAX ]; then
        echo -e "MAX Used:$PERCENT%n$content" | mail -s "$subject" $mailto
        exit
    fi
done
 
exit

大家可自动加入定时任务中去执行了

例2(功能:监控磁盘空间大小并自动报)

 代码如下 复制代码

 #!/bin/sh

  #make by itbit

  init()

  {

  IPADD=`ifconfig|grep 172.18|awk '{print $2}'|sed 's/addr://g'`;

  NOWTIME=`date +%F-%H:%M`;

  MAILTO="";

  MAIL_SUB=" !**Report - Disk Capacity Report $NOWTIME **!";

  tmpfile=/tmp/iamatmpfile.tmp

  }

  mhead()

  {

  echo "" >$tmpfile;

  echo "From :$IPADD" >>$tmpfile

  echo "State :Report" >>$tmpfile

  echo "ReportBy:$0" >> $tmpfile

  echo "DateTime:$NOWTIME" >> $tmpfile

  echo "Info :codfei" >> $tmpfile

  echo "" >>$tmpfile;

  }

  smail()

  {

  mail -s "$MAIL_SUB" $MAILTO < $tmpfile

  }

  init;

  for x in `df | grep /dev | awk '{print $5}' | sed 's/%//g'`

  do

  if [ $x -gt 89 ]; then

  mhead;

  df -h >>$tmpfile;

  smail;

  exit 0;

  fi

  done

相关文章

精彩推荐