三里屯摇滚 发表于 2011-05-17 21:25

Linux下监控用户磁盘使用量并在超值后发邮件给相应报警

Linux下监控用户磁盘使用量并在超值后发邮件给相应报警


公司虽有磁盘配额设置,但是有些用户经常一个程序跑下来就要吃掉几十个GB的空间,如果使用配额,那在程序运行中,用户空间达到限制时,程序就不能写了,也就崩溃了,因此只能使用软限制,即在发现用户空间达到一定值后给他发邮件让其清理空间

有问题者请加群QQ: 29215534


#!/bin/bash

User_Dir=`ls /cidev/fmnp`

#for i in /cidev/fmnp/*
for i in Algo Backoffice CashEquity Data FICC IBF ITI   #用户组目录
do
      #du -s /cidev/fmnp/$i/*
      if [ -d /cidev/fmnp/$i ];then

                du -s /cidev/fmnp/$i/home/* 2>/dev/dull |while read line
                do
                  echo $line | awk '$1 >25000000{print $1,$2}' >> /root/scripts/num.txt   #超过25GB报警
                done

      else
                echo $i "is not a directory"
      fi

done

mailname=`more /root/scripts/num.txt |awk -F/ '{print $6 }'`#提取用户列表

for mail in $mailname
do
      size=`cat /root/scripts/num.txt |awk "/${mail}/{ print $1 }" `
      #size2=`cat /root/scripts/num.txt |awk '/${mail}/{ print $2 }' `
      content="\n\n Dear ${mail}: \n\n Your Home directory's volume has crossed the waring limit of 25GB,please removeyour unimportant data to local disk
ASAP!!! \n\n
                The current size of your home directory is : ${size} \n
                To locate the big file, you can enter you home directory and then use commandfind . -size +100M -exec ls -sh {} \; \n "
      echo -e $content > /root/scripts/content.txt
      echo $size
      if [ $mail == "ibf" ];then #如果ibf超值 了,那就把邮件发给 lijie2@ci.com.cn
                mail="np_iti_architecture"
                mail $mail@ci.com.cn -c lijie2@ci.com.cn -s "Diks Waring" < /root/scripts/content.txt
      elif [ $mail == "npbo" ];then
                mail=np-trading-backoffice
                mail $mail@ci.com.cn -c lijie2@ci.com.cn -s "Diks Waring" < /root/scripts/content.txt
      else
      mail $mail@ci.com.cn -c lijie2@ci.com.cn -s "Diks Waring" < /root/scripts/content.txt
      #echo -e $content > content.txt
      echo -e "############################################\n Script running at `date +%F__%H:%M`" >>/var/log/check-disk.log
      echo -e "${size} \n sent mail to ${mail}@ci.com.cn " >> /var/log/check-disk.log
      fi
done

rm -rf /root/scripts/num.txt



发给用户的邮件内容如下:
Dear maxh:Your Home directory's volume has crossed thewaring limit of 25GB,please remove your unimportant data to local disk ASAP!!!The current size of your home directory is :68506896 /ciccdev/fmnp/Backoffice/home/maxh To locate the big file, you can enter you homedirectory and then use command find . -size +100M -exec ls -sh {} \;

hianxu 发表于 2011-05-17 21:42

不错!

surpass_li 发表于 2011-05-18 09:48

好东西
页: [1]
查看完整版本: Linux下监控用户磁盘使用量并在超值后发邮件给相应报警