|
Directory : /scripts2/ |
#!/bin/bash
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
THRESHOLD_DEFAULT=200
CONFIG_FILE="/etc/exim-limits-alert"
EMAIL_RECIPIENT="sysadmin@sitioshispanos.com"
LOCK_FILE="/var/lock/check-exim-alerts.lock"
LOG_FILE="/var/log/check-exim-alerts.log"
# Hosts that will not report.
# Examples:
# EXCLUDE="dns"
# EXCLUDE="linux{58,{91..95},97} dns"
EXCLUDE="dns"
DOMAIN_SUFFIX="webhosting-network-services.com"
# Do not include the entire queue in the email if it is huge.
QUEUE_DETAILS_LIMIT=300
log() {
echo "$(date '+%F %T') $*" >> "$LOG_FILE"
}
command_exists() {
command -v "$1" >/dev/null 2>&1
}
count_lines() {
grep -c . 2>/dev/null || true
}
send_alert() {
local subject="$1"
local body="$2"
if command_exists mail; then
printf "%s\n" "$body" | mail -s "$subject" "$EMAIL_RECIPIENT"
elif command_exists mailx; then
printf "%s\n" "$body" | mailx -s "$subject" "$EMAIL_RECIPIENT"
else
log "ERROR: mail/mailx not found. Could not send alert."
return 1
fi
}
# Prevent overlapping runs.
exec 9>"$LOCK_FILE"
if ! flock -n 9; then
log "Another instance is already running. Exiting."
exit 0
fi
HOSTNAME_SHORT="$(hostname -s 2>/dev/null || hostname)"
HOSTNAME_FQDN="$(hostname -f 2>/dev/null || hostname)"
THRESHOLD="$THRESHOLD_DEFAULT"
if [ -f "$CONFIG_FILE" ]; then
CONFIG_THRESHOLD="$(grep -E '^[[:space:]]*[0-9]+[[:space:]]*$' "$CONFIG_FILE" | head -n 1 | tr -d '[:space:]')"
if [ -n "$CONFIG_THRESHOLD" ]; then
THRESHOLD="$CONFIG_THRESHOLD"
else
log "Invalid threshold in $CONFIG_FILE. Using default: $THRESHOLD_DEFAULT"
fi
fi
if ! [[ "$THRESHOLD" =~ ^[0-9]+$ ]]; then
log "Invalid threshold value '$THRESHOLD'. Using default: $THRESHOLD_DEFAULT"
THRESHOLD="$THRESHOLD_DEFAULT"
fi
# Exclude matching short hostname or FQDN.
for excluded in $(eval echo "$EXCLUDE"); do
excluded_short="$excluded"
excluded_fqdn="$excluded.$DOMAIN_SUFFIX"
if [ "$HOSTNAME_SHORT" = "$excluded_short" ] || [ "$HOSTNAME_FQDN" = "$excluded_fqdn" ] || [ "$HOSTNAME_FQDN" = "$excluded_short" ]; then
log "Host excluded: short=$HOSTNAME_SHORT fqdn=$HOSTNAME_FQDN"
exit 0
fi
done
if ! command_exists exim; then
log "ERROR: exim command not found."
exit 1
fi
if ! command_exists exiqgrep; then
REPORT="ERROR: exiqgrep command not found on $HOSTNAME_FQDN.
This script requires exiqgrep to safely detect frozen and old Exim messages."
send_alert "Alert: Exim check failed on $HOSTNAME_SHORT" "$REPORT"
exit 1
fi
QUEUE_SIZE="$(exim -bpc 2>/dev/null | tr -d '[:space:]')"
if ! [[ "$QUEUE_SIZE" =~ ^[0-9]+$ ]]; then
log "ERROR: invalid queue size returned by exim -bpc: '$QUEUE_SIZE'"
REPORT="ERROR: Could not get a valid Exim queue size on $HOSTNAME_FQDN.
exim -bpc returned: $QUEUE_SIZE"
send_alert "Alert: Exim queue check failed on $HOSTNAME_SHORT" "$REPORT"
exit 1
fi
log "Queue size on $HOSTNAME_FQDN: $QUEUE_SIZE. Threshold: $THRESHOLD."
if [ "$QUEUE_SIZE" -le "$THRESHOLD" ]; then
exit 0
fi
QUEUE_DETAILS="$(exim -bp 2>/dev/null | head -n "$QUEUE_DETAILS_LIMIT")"
TOTAL_QUEUE_LINES="$(exim -bp 2>/dev/null | wc -l | tr -d '[:space:]')"
FROZEN_IDS="$(exiqgrep -z -i 2>/dev/null | sort -u)"
OLD_IDS="$(exiqgrep -o 86400 -i 2>/dev/null | sort -u)"
FROZEN_COUNT="$(printf "%s\n" "$FROZEN_IDS" | count_lines)"
OLD_COUNT="$(printf "%s\n" "$OLD_IDS" | count_lines)"
REMOVED_FROZEN_COUNT=0
REMOVED_OLD_COUNT=0
if [ "$FROZEN_COUNT" -gt 0 ]; then
if printf "%s\n" "$FROZEN_IDS" | xargs -r -n 100 exim -Mrm >/dev/null 2>&1; then
REMOVED_FROZEN_COUNT="$FROZEN_COUNT"
FROZEN_REMOVAL_SUMMARY="$REMOVED_FROZEN_COUNT frozen emails were removed."
log "$REMOVED_FROZEN_COUNT frozen emails removed."
else
FROZEN_REMOVAL_SUMMARY="Failed while removing frozen emails. Check $LOG_FILE."
log "ERROR: failed removing frozen emails."
fi
else
FROZEN_REMOVAL_SUMMARY="No frozen emails were found."
fi
# Recalculate old IDs after frozen removals to avoid trying to delete already removed IDs.
OLD_IDS="$(exiqgrep -o 86400 -i 2>/dev/null | sort -u)"
OLD_COUNT="$(printf "%s\n" "$OLD_IDS" | count_lines)"
if [ "$OLD_COUNT" -gt 0 ]; then
if printf "%s\n" "$OLD_IDS" | xargs -r -n 100 exim -Mrm >/dev/null 2>&1; then
REMOVED_OLD_COUNT="$OLD_COUNT"
OLD_REMOVAL_SUMMARY="$REMOVED_OLD_COUNT emails older than 24 hours were removed."
log "$REMOVED_OLD_COUNT old emails removed."
else
OLD_REMOVAL_SUMMARY="Failed while removing emails older than 24 hours. Check $LOG_FILE."
log "ERROR: failed removing old emails."
fi
else
OLD_REMOVAL_SUMMARY="No emails older than 24 hours were found."
fi
QUEUE_SIZE_AFTER="$(exim -bpc 2>/dev/null | tr -d '[:space:]')"
REPORT="ALERT: The Exim queue on $HOSTNAME_FQDN has $QUEUE_SIZE emails, exceeding the threshold of $THRESHOLD.
Queue after cleanup: $QUEUE_SIZE_AFTER
--- Removed Emails Summary ---
$FROZEN_REMOVAL_SUMMARY
$OLD_REMOVAL_SUMMARY
--- Mail Queue Details ---
Showing first $QUEUE_DETAILS_LIMIT lines out of approximately $TOTAL_QUEUE_LINES queue output lines.
$QUEUE_DETAILS
"
send_alert "Alert: Exim Queue on $HOSTNAME_SHORT" "$REPORT"
exit 0