Quantcast
Channel: Zimbra Forums
Viewing all articles
Browse latest Browse all 1145

Administrators • Re: Calculate average size of stored email?

$
0
0

Anyone got such a script/tool?
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:

Code:

use mboxgroup1;SELECT AVG(size / 1024) AS average_email_size_kbFROM mail_itemWHERE type = 5;
so inside a bash script, it is generating this as one loops through the databases.

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
Where:

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
I suspect it could be almost instantaneous if the db logic was in a stored procedure. Is this what you were looking for?

Jim

Statistics: Posted by JDunphy — Fri Feb 28, 2025 10:30 pm



Viewing all articles
Browse latest Browse all 1145

Trending Articles