-
-
Save hluaces/41f6d856f359702937866003b04e34db to your computer and use it in GitHub Desktop.
Alert based in mail queue size for postfix and plesk forked from [agarzon's](https://gist.github.com/heartshare/7bdeebfab74ed436e72c163aadbb3004) and slightly tuned.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash -e | |
# Binary locations | |
readonly MAIL_BIN=$(command -v mail) | |
readonly QUEUE_SIZE=$(/usr/sbin/postqueue -p | tail -n1 | awk '{print $5}') | |
readonly QUEUE_SUMMARY=$(/usr/sbin/qshape -s deferred | head) | |
readonly CONTROL_FILE="/tmp/queueCounter" | |
# Comma-separated list of email addreses that should be notified when the | |
# queue grows above the defined threshold | |
MAILTO="[email protected],[email protected]" | |
# Queue size from which the script should start to send alerts | |
LIMIT=100 | |
# Subject for the alert email | |
EMAIL_SUBJECT="WARNING: mail queue critical on $HOSTNAME" | |
# How many seconds the queue shoud be over LIMIT in order to send an alert | |
WARNING_DELAY=300 | |
# Sends an alert email to every address specified on $MAILTO | |
send_notification_mail() | |
{ | |
IFS=','; for MAIL in $MAILTO; do | |
echo "$QUEUE_SUMMARY" | $MAIL_BIN -s "$EMAIL_SUBJECT" "$MAIL" | |
done | |
} | |
# Determines if a alert notification should be sent. | |
# | |
# Returns: | |
# - 1 no notification should be sent | |
# - 0 notification should be sent | |
should_send_notification() | |
{ | |
if [ ! -f $CONTROL_FILE ]; then | |
return 1 | |
fi | |
let next_warning_date=$(cat $CONTROL_FILE)+WARNING_DELAY | |
let now=$(date +%s) | |
if [ $now -lt $next_warning_date ]; then | |
return 1 | |
else | |
return 0 | |
fi | |
} | |
# Tries to delete the control file, if it exists | |
delete_control_file() | |
{ | |
if [ -f $CONTROL_FILE ]; then | |
rm $CONTROL_FILE | |
fi | |
} | |
# Creates a control fie, if it doesn't exist | |
update_control_file() | |
{ | |
if [ ! -f $CONTROL_FILE ]; then | |
date +%s > $CONTROL_FILE | |
fi | |
} | |
# Determines if the queue is above the defined alert threshold | |
# Returns: | |
# - 0 The queue is over the threshold | |
# - 1 The queue is not over the threshold | |
is_over_limit() | |
{ | |
if [ "$QUEUE_SIZE" -gt "$LIMIT" ]; then | |
return 0 | |
else | |
return 1 | |
fi | |
} | |
main() | |
{ | |
if is_over_limit; then | |
if should_send_notification; then | |
send_notification_mail | |
delete_control_file | |
fi | |
update_control_file | |
else | |
delete_control_file | |
fi | |
exit 0 | |
} | |
main "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment