# This Script checks /etc/userdomains for per user domain count and generates STRs if a user contains more than the threshold set in $DOMCOUNT Domains
# Debug mode (set to true for logging to file, false for sending email)
# Log file path for debug mode
LOG_FILE="/var/log/domcount_checker.log"
HOSTNAME=$(hostname | cut -d '.' -f1)
if ! [[ "$DOMCOUNT" =~ ^[0-9]+$ ]]; then
echo "Error: DOMCOUNT argument is missing or not a number."
# Check for --send flag to disable debug mode
if [[ "$2" == "--send" ]]; then
EMAIL_RECIPIENT="str@imhadmin.net"
EMAIL_SUBJECT="User with High Domain Count Detected"
# Function to check if a user is suspended
function check_suspended() {
if [ -f /var/cpanel/suspended/$1 ]; then
local AGE=$(stat --format=%Y /var/cpanel/suspended/$1)
suspended="SUSPENDED $AGE DAYS $(cat /var/cpanel/suspended/$1)"
# Log users with over $DOMCOUNT domains or send an email
local suspended_status=$(check_suspended "$user")
if [ "$debug" = true ]; then
echo "User $user with $domain_count domains detected. $suspended_status Logged for review." >> "$LOG_FILE"
echo "A user with more than $DOMCOUNT domains has been detected: $user with $domain_count domains on $HOSTNAME - $suspended_status Please review. You may run 'touch /home/$user/.imh/domcount' to exclude this user from future checks." | mail -s "$EMAIL_SUBJECT: $user" $EMAIL_RECIPIENT
# Check domain count and skip user if excluded
local exclusion_file="/home/$user/.imh/domcount"
# Skip if user is excluded
if [ -f "$exclusion_file" ]; then
echo "Skipping user $user due to exclusion file."
echo "$exclusion_file No exclusion file found for user $user. Proceeding with domain count check..."
# Act if domain count is greater than or equal to $DOMCOUNT
if [ "$domain_count" -ge $DOMCOUNT ]; then
process_user "$user" "$domain_count"
# Process /etc/userdomains to count domains per user
while IFS=: read -r domain user; do
user=$(echo "$user" | xargs)
((userdomains["$user"]++))
# Check each user and manage notification
for user in "${!userdomains[@]}"; do
check_and_notify "$user" "${userdomains[$user]}"