source: trunk/server/fedora/config/etc/scripts/prune-mailq @ 2658

Last change on this file since 2658 was 2658, checked in by quentin, 8 years ago
prune-mailq usage improvements and postcat
  • Property svn:executable set to *
File size: 3.9 KB
Line 
1#!/bin/sh
2
3set -eu
4shopt -s failglob
5
6usage="Usage:
7    $0 list-from
8    $0 list-to
9    $0 show-rand [from regex|to regex]
10    $0 email lockers...
11    $0 purge-from lockers...
12    $0 purge-to lockers..."
13
14usage() {
15    echo "$usage" >&2;
16    exit 1
17}
18
19clean_locker() {
20    echo "${1%%@scripts.mit.edu}"
21}
22
23list_from() {
24    echo "Top twenty senders by number of queued messages:"
25    mailq | tail -n +2 | grep -v '^ *(' | awk 'BEGIN { RS = "" } { print $7 }' | sort | uniq -c | sort -n | tail -n 20
26}
27
28list_to() {
29    echo "Top twenty recipients by number of queued messages:"
30    mailq | tail -n +2 | grep -v '^ *(' | awk 'BEGIN { RS = "" } { print $8 }' | sort | uniq -c | sort -n | tail -n 20
31}
32
33show_rand() {
34    if [[ $# -eq 0 ]]; then
35        files=$(ls /var/spool/postfix/deferred/?/* | shuf | head -n 3)
36    elif [[ $# -eq 2 ]]; then
37        match=$2
38        case "$1" in
39            from) dir=7;;
40            to) dir=8;;
41            *) usage;;
42        esac
43        msgids=$(mailq | tail -n +2 | grep -v '^ *(' | awk "BEGIN { RS = \"\" } (\$$dir ~ /$match/) { print \$1 }" | shuf | head -n 3)
44        files=$(for msgid in $msgids; do echo /var/spool/postfix/deferred/${msgid:0:1}/$msgid; done)
45    else
46        usage
47    fi
48    for file in $files; do
49        echo ">>>> $file";
50        postcat "$file"
51        echo;
52    done
53}
54
55tmpl_email() {
56    sender=${SSH_GSSAPI_NAME%%/*}
57    if [[ $# -eq 0 ]]; then
58        echo "Please specify a locker to generate template for." >&2
59        exit 1
60    fi
61    for locker in "$@"; do
62        locker=$(clean_locker "$locker")
63        echo "fs la /mit/$locker/"
64        fs la "/mit/$locker"
65        echo
66        cat <<-EOF
67The scripts.mit.edu servers currently have a large number of email messages destined for the *$locker* account that are not being handled by your account and are being queued. Sufficiently large numbers of queued messages can cause stability issues for the servers, so we would like you to ensure that your account can handle all messages it receives by two weeks from now.
68
69You will be able to process the incoming messages if you sign up for the mail scripts service (http://scripts.mit.edu/mail/). You're welcome to simply forward all incoming mail to another address (the default is to forward it to the mit.edu address of the user who signs up); otherwise, you can configure mail scripts to process the incoming messages in some suitable fashion.
70
71Frequently, large numbers of queued messages are a sign that some wiki, blog, forum, or other site has been spammed. If this is the case, you should apply some appropriate spam-blocking mechanism.
72
73If you have any questions, feel free to contact us.
74
75Thanks,
76scripts.mit.edu team
77scripts@mit.edu --- semi-private
78scripts-root@mit.edu --- service maintainers only
79EOF
80        echo;echo
81    done
82}
83
84purge_from() {
85    if [[ $# -eq 0 ]]; then
86        echo "Please specify a locker to purge emails from" >&2
87        exit 1
88    fi
89    for locker in "$@"; do
90        locker=$(clean_locker "$locker")
91        echo "$locker..."
92        mailq | tail -n +2 | grep -v '^ *(' | awk "BEGIN { RS = \"\" } (\$7 == \"$locker@scripts.mit.edu\") { print \$1 }" | tr -d '*!' | postsuper -d -
93        echo
94    done
95}
96
97purge_to() {
98    if [[ $# -eq 0 ]]; then
99        echo "Please specify a locker to purge emails to" >&2
100        exit 1
101    fi
102    for locker in "$@"; do
103        locker=$(clean_locker "$locker")
104        echo "$locker..."
105        mailq | tail -n +2 | grep -v '^ *(' | awk "BEGIN { RS = \"\" } (\$8 == \"$locker@scripts.mit.edu\" && \$9 == \"\") { print \$1 }" | tr -d '*!' | postsuper -d -
106        echo
107    done
108}
109
110op=${1:-}
111
112# We want to go ahead and show the usage message if there are no args, so
113# don't let the shift fail and end the script because of "set -e"
114shift || :
115
116case "$op" in
117    list-from) list_from;;
118    list-to) list_to;;
119    show-rand) show_rand "$@";;
120    email) tmpl_email "$@";;
121    purge-from) purge_from "$@";;
122    purge-to) purge_to "$@";;
123    *)
124        usage
125        ;;
126esac
127
128# vim: set sts=4 sw=4 et:
Note: See TracBrowser for help on using the repository browser.