Skip to content

Instantly share code, notes, and snippets.

@stefanpejcic
Last active March 7, 2024 21:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stefanpejcic/ce9f0c43030d5ed246162d2117609ba7 to your computer and use it in GitHub Desktop.
Save stefanpejcic/ce9f0c43030d5ed246162d2117609ba7 to your computer and use it in GitHub Desktop.
WHM sent summary - check and email if more than 100 dail/deff emails in last 1 hour
#!/bin/bash
WORKDIR="/var/tmp"
TIMESTAMP_FILE="$WORKDIR/last_email_timestamp"
LIMIT="100"
LOAD="20"
starttime=$(date +%s -d '1 hour ago') # 1 hour ago
currenttime=$(date +%s)
send_email() {
# Get the server's hostname
hostname=$(hostname)
email="email@here.com"
message="$highest_deferfailcount fail/deff mejlova na serveru $hostname u zadnjih 1h."
echo "$message" | mail -s "$highest_deferfailcount fail/deff mejlova na $hostname" $email
# Update the timestamp file with the current time
echo "$currenttime" > "$TIMESTAMP_FILE"
}
# Fetch the 1-minute load average
load=$(cat /proc/loadavg | awk '{print $1}')
# Convert to a number with no decimal for comparison
load_int=${load%.*}
# Check if load is less than the limit
if [ "$load_int" -lt $LOAD ]; then
highest_deferfailcount=$(whmapi1 emailtrack_user_stats starttime=$starttime --output=yaml | grep -s DEFERFAILCOUNT | cut -d":" -f2 | cut -d" " -f2 | sort -n -r | head -n1)
if [ -f "$TIMESTAMP_FILE" ]; then
last_email_time=$(cat "$TIMESTAMP_FILE")
else
last_email_time=0
fi
# Calculate the time difference since the last email
time_diff=$((currenttime - last_email_time))
# Check if the highest DEFERFAILCOUNT is greater than $LIMIT and if 1 hour has passed since the last email
if [ "$highest_deferfailcount" -gt $LIMIT ] && [ "$time_diff" -ge 3600 ]; then
echo "fail/deff je trenutno $highest_deferfailcount (iznad limita od: $LIMIT) i šalje se mejl."
send_email
elif [ "$highest_deferfailcount" -le $LIMIT ]; then
echo "fail/deff je trenutno $highest_deferfailcount - ispod limita od $LIMIT i ne šalje se mejl."
else
minutes_passed=$(($time_diff / 60))
echo "Limit je dostignut, trenutno je: $highest_deferfailcount fail/deff mejlova ali nije prošlo 60 minuta od prošlog mejla, prošlo je tek $minutes_passed min kako je mejl poslat."
fi
else
echo "Server load is too high, not checking.."
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment