监控磁盘空间大小并自动报警脚本

#!/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

 

 

 

 

#!/bin/bash
#取得每个分区的使用百分比(不要百分号) 既然有换行 我们只要有
%号的域 #下面2句只用1句 你想用哪句 就把另外一句注释掉 percent=`df -k | grep -v Filesystem | awk '{for(i=1;i<=NF;i++){if($i~"%") print int($i)}}'`
percent
=`df -k | grep -v Filesystem | grep "%" | awk '{print int($4)}'`
#循环判断分区使用率是否超过90
%
for each_one in $percent
do 
    #判断使用率是否超过90
% 注意比较的数字不带%
    echo $each_one
   
if [ $each_one -ge 90 ];then
        #如果超过90 则把使用情况发给mail_address mail_address换成你的邮件地址
        df
| mail -s "Disk Critical" mail_address
        echo
""
    fi
done

转载于:https://www.cnblogs.com/b028/archive/2010/07/02/1867318.html

你可能感兴趣的:(监控磁盘空间大小并自动报警脚本)