ディレクトリサイズの変更を監視するスクリプト

ディレクトリサイズの変更を監視するスクリプト

ディレクトリサイズの変更を監視するには、bashスクリプトが必要です。ディレクトリは、サブディレクトリのサイズが非常に異なる複数のユーザー(500〜600)を持つNFSファイルシステムです。たとえば、/home/nfs/xxx/ccc などです。ディレクトリサイズが10 GBまたは20 GBを超えるか、実際にこのサイズを超える特定のユーザーを監視、記録、および報告できる必要があります。これを文書化し、そのユーザーに「おなじみの」Eメールを送信する必要があります。私はこれを行うためにユーティリティやツールを使用したくなく、むしろbashスクリプトを使用したいと思います。スクリプトが一時的に実行されます。

どんな助けでも大変感謝します。

これまでの努力 -

#!/bin/bash

set -x
DISK="/cluster/vvvvvvv1/nfs-home/zzz" # Verzeichnis
CURRENT=$(df -h | grep ${DISK} | awk {'print $4'}) # get disk usage from monitored disk
MAX="70%" # max nn% disk usage
DOMAIN="naz.ch"


# Max Exceeded now find the largest offender
cd $DISK
for i in `ls` ; do du -sh $i ; done > /tmp/mail.1
sort -gk 1 /tmp/mail.1 | tail -1 | awk -F " " '{print $2}' > /tmp/mail.offender
OFFENDER=`cat /tmp/mail.offender`
echo $OFFENDER
du -sh $DISK/$OFFENDER > /tmp/mail.over70
mail -s "$HOSTNAME $DISK Alert!" "$OFFENDER@$DOMAIN"  < /tmp/mail.over70

# check if current disk usage is greater than or equal to max usage.
if [ ${CURRENT} ]; then
  if [ ${CURRENT%?} -ge ${MAX%?} ]; then
    # if it is greater than or equal to max usage we call our max_exceeded function and send mail
    echo "Max usage (${MAX}) exceeded. The /home disk usage is it at ${CURRENT}. Sending email."
     max_exceeded
  fi
fi

# init #
# main

#CLEANUP 

ベストアンサー1

#assuming that your users are subfolders to same parent

disk=/home/disk # which contains users dirs user-{1..999}
limit='75%' 
current=$(df -k ${disk} | tail -1| awk '{print $5}')
max=10000000000 #in kilobytes (10G)

if [[ ${current//%/} -gt ${limit//%/} ]];then
    echo disk limit has been exceeded ${disk}
    # do your magick here
fi

find ${disk} -type f -exec du -k {} + | sort -nr | while read s f;do
    if test ${s} -gt ${max};then
        fsplt=(${f//\// }) # '/x/y/z' to 'x y z' as array
        echo "file size exceeded limits | user:${fsplt[2]} file:${f}";
        # do whatever here with file and user;
    else
        break; #cuz the list is sorted all remainning are smaller files
    fi
done

# you can customize find option to find only bigger than X size (optional)

おすすめ記事