Add script for calculation diskspace usage of e-mails.
This commit is contained in:
parent
dd758254b3
commit
939dcf5a7c
@ -28,7 +28,7 @@ address="$1"
|
|||||||
|
|
||||||
domain=`echo $address | cut -d'@' -f2`
|
domain=`echo $address | cut -d'@' -f2`
|
||||||
|
|
||||||
mysql=true
|
mysql=false
|
||||||
# - mysql_credential_args
|
# - mysql_credential_args
|
||||||
# -
|
# -
|
||||||
# - MySQL / MariaDB credentials
|
# - MySQL / MariaDB credentials
|
||||||
|
@ -19,7 +19,7 @@ db_user=postfix
|
|||||||
quota=536870912
|
quota=536870912
|
||||||
#_passwd='$E%R&T/Z(U'
|
#_passwd='$E%R&T/Z(U'
|
||||||
|
|
||||||
in_file=/root/mailboxes_ak.lst
|
in_file=/root/mailboxes_new.lst
|
||||||
|
|
||||||
log_file=/tmp/postfix_add_mailboxes.log
|
log_file=/tmp/postfix_add_mailboxes.log
|
||||||
|
|
||||||
|
255
postfix_diskspace_usage.sh
Executable file
255
postfix_diskspace_usage.sh
Executable file
@ -0,0 +1,255 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
|
|
||||||
|
# -------------
|
||||||
|
# - Settings
|
||||||
|
# -------------
|
||||||
|
|
||||||
|
declare -i sum=0
|
||||||
|
declare -i sum_over_all=0
|
||||||
|
declare -i pos
|
||||||
|
declare -i pos_begin=58
|
||||||
|
all_domains=false
|
||||||
|
|
||||||
|
info_file="/var/vmail/DISKSPACE_USAGE"
|
||||||
|
|
||||||
|
|
||||||
|
# -------------
|
||||||
|
# - Some functions
|
||||||
|
# -------------
|
||||||
|
|
||||||
|
usage() {
|
||||||
|
|
||||||
|
echo
|
||||||
|
|
||||||
|
[[ -n "$1" ]] && echo -e " [ \033[31mError\033[m ]: $1\n"
|
||||||
|
|
||||||
|
echo -e "$(cat<<EOF
|
||||||
|
|
||||||
|
\033[1mUsage\033[m: $(basename $0) <-m|-p> <-A|-d email-domain>
|
||||||
|
|
||||||
|
Prints out the diskspace used by all e-mail addresses for the given domain.
|
||||||
|
If \033[1m-A\033[m is given, diskspace usage for all domains will be printed out.
|
||||||
|
|
||||||
|
\033[1mRequires\033[m on of the options \033[1m-m\033[m or\033[m \033[1m-p\033[m for database type.
|
||||||
|
|
||||||
|
\033[1mRequires\033[m one of the options \033[1m-A\033[m or \033[1m-d\033[m email-domain.
|
||||||
|
|
||||||
|
Options:
|
||||||
|
|
||||||
|
\033[1m-A\033[m
|
||||||
|
Prints diskspace usage for all mail domains.
|
||||||
|
|
||||||
|
\033[1m-d <email-domain>\033[m
|
||||||
|
Prints out diskspace usage for the given domain.
|
||||||
|
|
||||||
|
\033[1m-h\033[m
|
||||||
|
Prints this help.
|
||||||
|
|
||||||
|
\033[1m-m\033[m
|
||||||
|
Database type is Mysql.
|
||||||
|
|
||||||
|
\033[1m-p\033[m
|
||||||
|
Database type is PostgreSQL.
|
||||||
|
\n
|
||||||
|
EOF
|
||||||
|
)"
|
||||||
|
|
||||||
|
exit 1
|
||||||
|
}
|
||||||
|
|
||||||
|
is_valid_domain() {
|
||||||
|
|
||||||
|
|
||||||
|
if [[ "@$1" =~ ^@(([[:alpha:]](-?[[:alnum:]])*)\.)*[[:alpha:]](-?[[:alnum:]])+\.[[:alpha:]]{2,}$ ]]; then
|
||||||
|
return 0
|
||||||
|
else
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
|
# - Check if a given array (parameter 2) contains a given string (parameter 1)
|
||||||
|
# -
|
||||||
|
containsElement () {
|
||||||
|
local e
|
||||||
|
for e in "${@:2}"; do [[ "$e" == "$1" ]] && return 0; done
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# -------------
|
||||||
|
# - Read in Commandline Arguments
|
||||||
|
# -------------
|
||||||
|
|
||||||
|
option_m=false
|
||||||
|
option_p=false
|
||||||
|
while getopts Ad:hmp opt ; do
|
||||||
|
case $opt in
|
||||||
|
A)
|
||||||
|
all_domains=true
|
||||||
|
;;
|
||||||
|
d)
|
||||||
|
domain="$OPTARG"
|
||||||
|
;;
|
||||||
|
h)
|
||||||
|
usage
|
||||||
|
;;
|
||||||
|
m)
|
||||||
|
db_type="mysql"
|
||||||
|
option_m=true
|
||||||
|
;;
|
||||||
|
p)
|
||||||
|
db_type="psql"
|
||||||
|
option_p=true
|
||||||
|
;;
|
||||||
|
\?) usage
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
|
||||||
|
shift "$((OPTIND - 1))"
|
||||||
|
|
||||||
|
|
||||||
|
[[ $# -ne "0" ]] && usage "Wrong number of arguments."
|
||||||
|
|
||||||
|
[[ -z "$db_type" ]] && usage "\033[1mMissing\033[m Option \033[1m-m\033[m or \033[1m-p\033[m"
|
||||||
|
|
||||||
|
if $option_m && $option_p ; then
|
||||||
|
usage "\033[1mEither\033[m option \033[1m-m OR \033[moption \033[1m-p\033[m must be given."
|
||||||
|
fi
|
||||||
|
|
||||||
|
if ! $all_domains && [[ -z "$domain" ]] ; then
|
||||||
|
usage "\033[1mMissing\033[m Option \033[1m-A\033[m or \033[1m-d\033[m"
|
||||||
|
fi
|
||||||
|
|
||||||
|
if $all_domains && [[ -n "$domain" ]] ; then
|
||||||
|
usage "\033[1mEither\033[m option \033[1m-A OR \033[moption \033[1m-d\033[m must be given."
|
||||||
|
fi
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if [[ -n $domain ]] && ! is_valid_domain $domain ; then
|
||||||
|
usage "Given Domain \033[1m$domain\033[m is invalid"
|
||||||
|
fi
|
||||||
|
|
||||||
|
|
||||||
|
# -------------
|
||||||
|
# - Do the stuff
|
||||||
|
# -------------
|
||||||
|
|
||||||
|
if [[ -t 1 ]] ; then
|
||||||
|
terminal=true
|
||||||
|
else
|
||||||
|
terminal=false
|
||||||
|
fi
|
||||||
|
|
||||||
|
|
||||||
|
if $all_domains ; then
|
||||||
|
if [[ $db_type == 'psql' ]]; then
|
||||||
|
domains=$(su - postgres -c "psql -q -t postfix -c 'SELECT domain FROM domain'" | sort)
|
||||||
|
alias_domains=$(su - postgres -c "psql -q -t postfix -c 'SELECT alias_domain FROM alias_domain'" | sort)
|
||||||
|
elif [[ $db_type == 'mysql' ]]; then
|
||||||
|
|
||||||
|
if [[ -f "/usr/local/mysql/sys-maint.cnf" ]] ; then
|
||||||
|
mysql_credential_args="--defaults-file=/usr/local/mysql/sys-maint.cnf"
|
||||||
|
elif [[ -f "/etc/mysql/debian.cnf" ]] ; then
|
||||||
|
mysql_credential_args="--defaults-file=/etc/mysql/debian.cnf"
|
||||||
|
fi
|
||||||
|
|
||||||
|
domains=$(mysql $mysql_credential_args -N -s postfix -e "SELECT domain FROM domain")
|
||||||
|
alias_domains=$(mysql $mysql_credential_args -N -s postfix -e "SELECT alias_domain FROM alias_domain")
|
||||||
|
else
|
||||||
|
usage "Database Type \033[1m${db_type}\033[m not known."
|
||||||
|
fi
|
||||||
|
|
||||||
|
declare -a alias_domain_arr
|
||||||
|
for _domain in $alias_domains ; do
|
||||||
|
alias_domain_arr+=("$_domain")
|
||||||
|
done
|
||||||
|
|
||||||
|
echo "# --------------------" > $info_file
|
||||||
|
echo "# - Diskpace usage of e-mail domains at $(hostname -f)" >> $info_file
|
||||||
|
echo "# --------------------" >> $info_file
|
||||||
|
echo "" >> $info_file
|
||||||
|
|
||||||
|
else
|
||||||
|
domains=$domain
|
||||||
|
fi
|
||||||
|
|
||||||
|
if $terminal ; then
|
||||||
|
echo ""
|
||||||
|
fi
|
||||||
|
|
||||||
|
for _domain in $domains ; do
|
||||||
|
[[ "$_domain" == "ALL" ]] && continue
|
||||||
|
if containsElement "$_domain" "${alias_domain_arr[@]}" ; then
|
||||||
|
continue
|
||||||
|
fi
|
||||||
|
|
||||||
|
sum=0
|
||||||
|
while read address used fake ; do
|
||||||
|
[[ $used =~ ^-?[0-9]+$ ]] || continue
|
||||||
|
if [[ $used -gt 0 ]] ; then
|
||||||
|
sum=$(( $sum + $used))
|
||||||
|
fi
|
||||||
|
done < <( doveadm quota get -A 2> /dev/null | grep STORAGE | grep $_domain | awk '{print$1" "$5}' )
|
||||||
|
|
||||||
|
sum_over_all=$(( $sum_over_all + $sum))
|
||||||
|
pos=$(( $pos_begin - ${#sum}))
|
||||||
|
if [[ $sum -gt 999999 ]]; then
|
||||||
|
pos=$(( $pos - 2 ))
|
||||||
|
elif [[ $sum -gt 999 ]]; then
|
||||||
|
pos=$(( $pos - 1 ))
|
||||||
|
fi
|
||||||
|
|
||||||
|
if $terminal ; then
|
||||||
|
echo -e " $_domain: \033[${pos}G$(printf "%'d" $sum) KB"
|
||||||
|
fi
|
||||||
|
|
||||||
|
if $all_domains ; then
|
||||||
|
echo -n " $_domain:" >> $info_file
|
||||||
|
j=1
|
||||||
|
pos=$(( $pos - ${#_domain} ))
|
||||||
|
while [[ $j -lt $pos ]]; do
|
||||||
|
echo -n "." >> $info_file
|
||||||
|
let "j += 1"
|
||||||
|
done
|
||||||
|
echo "$(printf "%'d" $sum) KB" >> $info_file
|
||||||
|
fi
|
||||||
|
|
||||||
|
done
|
||||||
|
|
||||||
|
|
||||||
|
if $all_domains ; then
|
||||||
|
|
||||||
|
pos=$(( $pos_begin - ${#sum_over_all}))
|
||||||
|
if [[ $sum_over_all -gt 999999 ]]; then
|
||||||
|
pos=$(( $pos - 2 ))
|
||||||
|
elif [[ $sum_over_all -gt 999 ]]; then
|
||||||
|
pos=$(( $pos - 1 ))
|
||||||
|
fi
|
||||||
|
|
||||||
|
if $terminal ; then
|
||||||
|
echo ""
|
||||||
|
echo -e " ========================================================="
|
||||||
|
echo ""
|
||||||
|
echo -e " gesamt:\033[${pos}G$(printf "%'d" $sum_over_all) KB"
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "" >> $info_file
|
||||||
|
echo -e " =============================================================" >> $info_file
|
||||||
|
echo "" >> $info_file
|
||||||
|
echo -n " gesamt:" >> $info_file
|
||||||
|
j=1
|
||||||
|
pos=$(( $pos - 6 ))
|
||||||
|
while [[ $j -lt $pos ]]; do
|
||||||
|
echo -n " " >> $info_file
|
||||||
|
let "j += 1"
|
||||||
|
done
|
||||||
|
echo "$(printf "%'d" $sum_over_all) KB" >> $info_file
|
||||||
|
echo "" >> $info_file
|
||||||
|
fi
|
||||||
|
|
||||||
|
if $terminal ; then
|
||||||
|
echo ""
|
||||||
|
fi
|
||||||
|
exit 0
|
Loading…
Reference in New Issue
Block a user