Script to display disk usage by mailbox

Discuss your pilot or production implementation with other Zimbra admins or our engineers.
User avatar
JDunphy
Outstanding Member
Outstanding Member
Posts: 513
Joined: Fri Sep 12, 2014 11:18 pm
Location: Victoria, BC
ZCS/ZD Version: 8.7.11_P14 RHEL6 Network Edition
Contact:

Script to display disk usage by mailbox

Postby JDunphy » Sat Jun 29, 2019 2:37 pm

Comments are bigger than the script but I was looking at a system this morning that was filling up and wanted to know my disk hogs. In case it helps someone else. Enjoy.

Code: Select all

#!/bin/bash
#
# Printout email disk usage for all accounts in human readable form
#
# usage: zmdu.sh
#
# Ouptut: user size(human readable)
#     0 ... admin@example.com
#   15K ... wiki@example.com
#   75M ... sam@example.com
#   17K ... ham.lub2tukfun@example.com
#    3G ... jim@example.com
#
# Caveat: zmprov is slow... So slow you won't believe it. :-)
#
# Required: NEEDS to RUN as THE ZIMBRA USER
#
# Author: Jim Dunphy
#
# Sort by highest users
# % zmdu.sh | sort -rh 
#
# is slow with no output until done so this might work better or use tee
# % zmdu.sh > zmdu.out
# % sort -rh zmdu.out
#    3G ... jim@example.com
#    3G ... betsy@example.com
#  696M ... anna@example.com
#   72M ... sam@example.com
#   67M ... helen@example.com
#   12M ... noc@example.com
#  460K ... spam.trivkbr1q@example.com
#   27K ... wiki@mail.example.com
#   17K ... ham.lub2tukfun@example.com
#   15K ... wiki@example.com
#    54 ... virus-quarantine.t9l1ltaadf8ih@example.com
#     0 ... admin@example.com
#

for account in `zmprov -l gaa`
do
  result=$(zmprov gmi "$account" | awk '{/mailboxId/} { print $2 }' | tail -1)
  size=$(echo $result | awk '{ suffix=" KMGT"; for(i=1; $1>1024 && i < length(suffix); i++) $1/=1024; print int($1) substr(suffix, i, 1), $3; }')
  printf "%6s ... %s\n" $size  $account
done

exit 0


Klug
Elite member
Elite member
Posts: 2365
Joined: Mon Dec 16, 2013 11:35 am
Contact:

Re: Script to display disk usage by mailbox

Postby Klug » Sat Jun 29, 2019 2:43 pm

If you're on a single server this gives you the size (and quota defined) per mailbox:

Code: Select all

zmprov gqu `zmhostname`


You just need to deal with the unit to get it "human readable".
It should be much faster than the loop.

If you have a multi-mailstore setup, you'll just have to loop once per server (not account).
User avatar
JDunphy
Outstanding Member
Outstanding Member
Posts: 513
Joined: Fri Sep 12, 2014 11:18 pm
Location: Victoria, BC
ZCS/ZD Version: 8.7.11_P14 RHEL6 Network Edition
Contact:

Re: Script to display disk usage by mailbox

Postby JDunphy » Sat Jun 29, 2019 2:44 pm

I should have guessed they had something... That is awesome. I'll use that as the base. Thanks
User avatar
JDunphy
Outstanding Member
Outstanding Member
Posts: 513
Joined: Fri Sep 12, 2014 11:18 pm
Location: Victoria, BC
ZCS/ZD Version: 8.7.11_P14 RHEL6 Network Edition
Contact:

Re: Script to display disk usage by mailbox

Postby JDunphy » Sat Jun 29, 2019 5:21 pm

So it becomes a 1-liner and fast enough to sort with klug's recommendation... but more importantly, documentation is much reduced! Now if I can remember I have this for next time and not rewrite something from scratch is my real challenge here! :-)

Code: Select all

# su - zimbra
%  zmprov gqu $(zmhostname) | awk '{ $2="";suffix=" KMGT"; for(i=1; $3>1024 && i < length(suffix); i++) $3/=1024; printf "%6s%c ...%s\n", int($3),substr(suffix, i, 1), $1; }' | sort -rh
    3G ... jim@example.com
    3G ... betsy@example.com
  696M ... anna@example.com
   72M ... sam@example.com
   ...
   ...

updated script:

Code: Select all

#!/bin/bash
#
# Printout email disk usage for all accounts in human readable form
#
# usage: zmdu.sh
#
# Required: NEEDS to RUN as THE ZIMBRA USER
#
# Author: Jim Dunphy, assist: Klug from Zimbra forums.
#
# Sorted by highest users
#
# su - zimbra
# % zmdu.sh
#    3G ... jim@example.com
#    3G ... betsy@example.com
#  696M ... anna@example.com
#   72M ... sam@example.com
#   67M ... helen@example.com
#   12M ... noc@example.com
#  460K ... spam.trivkbr1q@example.com
#   27K ... wiki@mail.example.com
#   17K ... ham.lub2tukfun@example.com
#   15K ... wiki@example.com
#    54 ... virus-quarantine.t98l1ltk1a@example.com
#     0 ... admin@example.com
#

zmprov gqu $(zmhostname) | awk '{ $2="";suffix=" KMGT"; for(i=1; $3>1024 && i < length(suffix); i++) $3/=1024; printf "%6s%c ... %s\n", int($3),substr(suffix, i, 1), $1; }' | sort -rh

exit 0

Return to “Administrators”

Who is online

Users browsing this forum: No registered users and 21 guests