Skip to content
Snippets Groups Projects
Commit b3de32ed authored by ale's avatar ale
Browse files

Make Postfix queue manipulation tools more accurate

Replace awk-based regular expressions with 'jq' parsing the JSON
postqueue output, which is more accurate as it does not depend on the
exact syntax of the queue IDs.
parent 3268f00d
Branches
No related tags found
No related merge requests found
...@@ -3,6 +3,17 @@ ...@@ -3,6 +3,17 @@
# Filter out messages matching a specific criteria. # Filter out messages matching a specific criteria.
# #
usage() {
cat >&2 <<EOF
Usage: pqgrep [<options>] <sender>
Known options:
-i INSTANCE Limit to a specific Postfix instance
-d Delete the selected messages from the queue
EOF
exit 2
}
instances=postfix-out instances=postfix-out
do_delete=0 do_delete=0
sender= sender=
...@@ -15,21 +26,25 @@ while [ $# -gt 0 ]; do ...@@ -15,21 +26,25 @@ while [ $# -gt 0 ]; do
-d) -d)
do_delete=1 do_delete=1
;; ;;
-h|--help)
usage
;;
-*) -*)
echo "Unknown option '$1'" >&2 echo "Unknown option '$1'" >&2
exit 2 usage
;; ;;
*) *)
if [ -n "$sender" ]; then
echo "Too many arguments" >&2
usage
fi
sender="$1" sender="$1"
;; ;;
esac esac
shift shift
done done
if [ -z "$sender" ]; then [ -n "$sender" ] || usage
echo "Usage: pqgrep [<options>] <sender>" >&2
exit 2
fi
if [ "$instances" = "all" ]; then if [ "$instances" = "all" ]; then
instances=$(postmulti -l | awk '$1 != "-" {print $1}') instances=$(postmulti -l | awk '$1 != "-" {print $1}')
...@@ -40,7 +55,7 @@ for instance in $instances; do ...@@ -40,7 +55,7 @@ for instance in $instances; do
if [ $do_delete -eq 1 ]; then if [ $do_delete -eq 1 ]; then
cmd="xargs -n 1 postmulti -i $instance -x postsuper -d" cmd="xargs -n 1 postmulti -i $instance -x postsuper -d"
fi fi
postmulti -i $instance -x mailq \ postmulti -i $instance -x postqueue -j \
| awk "\$7 == \"${sender}\" {print \$1}" \ | jq -r "select(.sender == \"${sender}\") | .queue_id" \
| $cmd | $cmd
done done
...@@ -3,20 +3,50 @@ ...@@ -3,20 +3,50 @@
# Print a summary of a Postfix queue. # Print a summary of a Postfix queue.
# #
usage() {
cat >&2 <<EOF
Usage: pqsummary [<options>]
Known options:
-i INSTANCE Limit to a specific Postfix instance
-n N Show the top N results (default: 20)
-s Aggregate by sender (default)
-r Aggregate by recipient
EOF
exit 2
}
instances=postfix-out instances=postfix-out
num_results=20
jq_query_sender=".sender"
jq_query_recipient=".recipients[0].address"
jq_query="$jq_query_sender"
while [ $# -gt 0 ]; do while [ $# -gt 0 ]; do
case "$1" in case "$1" in
-i) -i)
instances="$instances $2" instances="$instances $2"
shift shift
;; ;;
-s)
jq_query="$jq_query_sender"
;;
-r)
jq_query="$jq_query_recipient"
;;
-n)
num_results="$2"
shift
;;
-h|--help)
usage
;;
-*) -*)
echo "Unknown option '$1'" >&2 echo "Unknown option '$1'" >&2
exit 2 usage
;; ;;
*) *)
echo "Too many arguments" >&2 echo "Too many arguments" >&2
exit 2 usage
;; ;;
esac esac
shift shift
...@@ -29,8 +59,11 @@ fi ...@@ -29,8 +59,11 @@ fi
for instance in $instances; do for instance in $instances; do
echo echo
echo "* $instance:" echo "* $instance:"
postmulti -i $instance -x mailq \ postmulti -i $instance -x postqueue -j \
| awk '/^[a-zA-Z0-9]+/ {count[$7]++} END {for(i in count){ print i, count[i] }}' \ | jq -r "$jq_query" \
| sort -n -k2 \ | sort \
| column -t | uniq -c \
| sort -n -r \
| column -t \
| head -${num_results}
done done
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment