I don't but was interested in how one might do this with a direct db query so with a little help from Deepseek on this one, it looks like:
Anyone got such a script/tool?
Code:
use mboxgroup1;SELECT AVG(size / 1024) AS average_email_size_kbFROM mail_itemWHERE type = 5;
Code:
# su - zimbra% /tmp/zmAveSizeMsg.sh Processing database: mboxgroup1 (this may take a moment...)Processing database: mboxgroup10 (this may take a moment...)Processing database: mboxgroup11 (this may take a moment...)Processing database: mboxgroup12 (this may take a moment...)....Processing database: mboxgroup9 (this may take a moment...)Overall average email size for the entire system: 281.80 KB
Code:
% cat /tmp/zmAveSizeMsg.sh#!/bin/bash# Zimbra MySQL credentialsZMYSQL_USER="zimbra"ZMYSQL_PASSWORD="your_mysql_password" # Replace with your Zimbra MySQL passwordZMYSQL_HOST="localhost"# Function to calculate average using awkcalculate_average() { local total_size=$1 local total_emails=$2 echo "$total_size $total_emails" | awk '{printf "%.2f", ($1 / $2) / 1024}'}# Get the list of all mboxgroup databases#MBOXGROUPS=$(mysql -h $ZMYSQL_HOST -u $ZMYSQL_USER -p$ZMYSQL_PASSWORD -e "SHOW DATABASES LIKE 'mboxgroup%';" -s -N)MBOXGROUPS=$(mysql -u $ZMYSQL_USER -e "SHOW DATABASES LIKE 'mboxgroup%';" -s -N)# Check if mboxgroup databases existif [ -z "$MBOXGROUPS" ]; then echo "No mboxgroup databases found!" exit 1fi# Initialize variables for total size and total email countTOTAL_SIZE=0TOTAL_EMAILS=0# Loop through each mboxgroup databasefor DB in $MBOXGROUPS; do echo "Processing database: $DB (this may take a moment...)" # Query the total size and email count for the current mboxgroup #RESULTS=$(mysql -h $ZMYSQL_HOST -u $ZMYSQL_USER -p$ZMYSQL_PASSWORD -D $DB -e "SELECT SUM(size), COUNT(*) FROM mail_item WHERE type = 5;" -s -N) RESULTS=$(mysql -u $ZMYSQL_USER -D $DB -e "SELECT SUM(size), COUNT(*) FROM mail_item WHERE type = 5;" -s -N) # Extract the total size and email count from the results SIZE=$(echo "$RESULTS" | awk '{print $1}') COUNT=$(echo "$RESULTS" | awk '{print $2}') # Accumulate total size and email count TOTAL_SIZE=$((TOTAL_SIZE + SIZE)) TOTAL_EMAILS=$((TOTAL_EMAILS + COUNT))done# Calculate the overall average email size in KBif [ $TOTAL_EMAILS -gt 0 ]; then OVERALL_AVG=$(calculate_average $TOTAL_SIZE $TOTAL_EMAILS) echo "Overall average email size for the entire system: $OVERALL_AVG KB"else echo "No emails found in any mboxgroup database!"fi
Jim
Statistics: Posted by JDunphy — Fri Feb 28, 2025 10:30 pm