28 Ekim 2009 Çarşamba

kullanıcı kota hesapla, klasore yaz mail at

Mail ve ftp hizmeti verilen ve kullanıcıların sistem kullanıcısı olduğu sunucuda, mailler /home/vmail/s/ad.soyad/Maildir, ftp hesapları /home/vmail/s/ad.soyad/ftp altında duruyor.
Amcaların kotalarını hesaplayıp ftp klasörlerine yazıp, kotası dolmaya yakın olanlara mail atalım.


########################################################################################
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see .
#
# serkan.capkan@gmail.com 21.10.2009
#
########################################################################################

#!/bin/bash


#sabitler
tmp_passwd="/tmp/passwd_gecici_$0"
tmp_passwd_tmp="/tmp/passwd_gecici_tmp_$0"
user_quota="/tmp/user_quota_$0"
user_quota2="/tmp/user_quota2_$0"
byte_katsayi=1024
uyari_siniri=80
uyari_konu="KOTA UYARISI (beta)"
klasor="/home/vmail"
sistem_users="root bin daemon adm lp sync shutdown halt mail news uucp operator games gopher ftp nobody nscd vcsa pcap rpc mailnull smmsp rpcuser nfsnobody sshd dbus avahi haldaemon avahi-autoipd apache ntp mysql dovecot postfix clamav distcache named mailman amavis xfs"
date=`date +%d.%m.%Y\ %H:%M`
date; echo "BASLADI ------------------------------------------------------"

#passwd dosyasini al
cp /etc/passwd $tmp_passwd

#kullanicinin sadece adi kalsin
sed -e s_:x:.*__g $tmp_passwd>$tmp_passwd_tmp ; mv $tmp_passwd_tmp $tmp_passwd

#sistem kulanicilerini yok et
for i in $sistem_users
do
sed -e s/^$i$//g $tmp_passwd>$tmp_passwd_tmp ; mv $tmp_passwd_tmp $tmp_passwd
done

#mailnull adındaki kullanici kaliyor, onu da boyle silelim silelim
sed -e s/^mailnull$//g $tmp_passwd>$tmp_passwd_tmp ; mv $tmp_passwd_tmp $tmp_passwd

#bos satirlari sil
sed '/^$/d' $tmp_passwd>$tmp_passwd_tmp ; mv $tmp_passwd_tmp $tmp_passwd

#islemleri yapmak uzere kullanicilari sirala
for username in `cat $tmp_passwd`
do

quota $username>$user_quota

#kotası olmayan bir kullanıcı ise donguyu kes,
quota_hata_verdimi=`cat $user_quota | grep 'none'`

if [ "$quota_hata_verdimi" ]
then
echo $username: Hata verdi; $quota_hata_verdi
continue;
fi

tail -n 1 $user_quota > $user_quota2 ; mv $user_quota2 $user_quota
kullanim=`cat $user_quota | awk '{print $1}'`
kota=`cat $user_quota | awk '{print $2}'`
let "kota_mb=$kota/$byte_katsayi"
let "kullanim_mb=$kullanim/$byte_katsayi"

yuzde=`echo "$kullanim*100/$kota" | bc`

#orani asti ise mail at
uyari_mesaj="$username Kullanici Hesabi kota kullaniminiz % $yuzde degerine ulasmistir.\r\ne-Posta ve FTP Dosya Alani kullaniminda sorun yasamamak icin lutfen e-Posta kutunuzu veya FTP Dosya Alaninizi bosaltiniz.\r\n\r\n Mevcut kotaniz: $kota_mb MB\r\n Kota kullaniminiz: $kullanim_mb MB ( %$yuzde )\r\n\r\nBu mesaj otomatik olarak gonderilmektedir, lutfen bu e-Posta'ya yanit yazmayiniz.\r\n\r\n"


#siniri gecti ise mail at
if [ $yuzde -gt $uyari_siniri ]
then
echo -e $uyari_mesaj | mail -s "$uyari_konu" $username bim@domainim.com -- -r noreply@domainim.com
fi

#ftp alanina kota kullanimini yaz
nokta_varmi=`echo $username |grep "\."`
if [ "$nokta_varmi" ]
then
harf=`echo $username | sed -e 's/.*\.//' | cut -c1`
else
harf=`echo $username | cut -c1`
fi
#gidip klasorlerine yaz
echo "KOTA:$kota_mb MB
KULLANIM:$kullanim_mb MB
KULANIM ORANI:%$yuzde


Not:Bu dosya $date tarihinde olusturulmustur." > $klasor/$harf/$username/ftp/KOTA_KULLANIMI.txt

done

#olusturulan gecici dosyalari sil
rm -f $tmp_passwd $passwd_tmp $user_quota $user_quota2


date; echo "BITTI ------------------------------------------------------"