脚本:Linux系统下监控用户磁盘使用量,如果超值就发邮件给该用户Email通知

脚本:Linux系统下监控用户磁盘使用量,如果超值就发邮件给该用户Email通知

* 来源: * 作者: * 发表时间: 2020-08-20 11:01:00 * 浏览: 4

今天IT外包网 就为大家分享,脚本:Linux系统下监控用户磁盘使用量,如果超值就发邮件给该用户Email通知。情况是这样的,公司虽有磁盘配额设置,但是有些用户经常一个程序跑下来就要吃掉几十个GB的空间,如果使用配额,那在程序运行中,用户空间达到限制时,程序就不能写了,也就崩溃了,因此只能使用软限制,即在发现用户空间达到一定值后给他发邮件让其清理空间。脚本如下:
#!/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=" Dear ${mail}: Your Home directory's volume has crossed the waring limit of 25GB,please remove  your unimportant data to local disk
ASAP!!!
                The current size of your home directory is : ${size}
                To locate the big file, you can enter you home directory and then use command  find . -size +100M -exec ls -sh {} \; "
        echo -e $content > /root/scripts/content.txt
        echo $size
        if [ $mail == "ibf" ];then #如果ibf超值 了,那就把邮件发给 lijie2@itwaibaow.com
                mail="np_iti_architecture"
                mail $mail@itwaibaow.com -c lijie2@itwaibaow.com -s "Diks Waring" < /root/scripts/content.txt
        elif [ $mail == "npbo" ];then
                mail=np-trading-backoffice
                mail $mail@itwaibaow.com -c lijie2@itwaibaow.com -s "Diks Waring" < /root/scripts/content.txt
        else
        mail $mail@itwaibaow.com -c lijie2@itwaibaow.com -s "Diks Waring" < /root/scripts/content.txt
        #echo -e $content > content.txt
        echo -e "############################################ Script running at `date +%F__%H:%M`" >>/var/log/check-disk.log
        echo -e "${size} sent mail to ${mail}@itwaibaow.com " >> /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 {} \;