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

Last change on this file since 2657 was 2657, checked in by quentin, 9 years ago
Add features to prune-mailq show_rand
  • Property svn:executable set to *
File size: 3.8 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|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}
18clean_locker() {
19    echo "${1%%@scripts.mit.edu}"
20}
21
22list_from() {
23    echo "Top twenty senders by number of queued messages:"
24    mailq | tail -n +2 | grep -v '^ *(' | awk 'BEGIN { RS = "" } { print $7 }' | sort | uniq -c | sort -n | tail -n 20
25}
26
27list_to() {
28    echo "Top twenty recipients by number of queued messages:"
29    mailq | tail -n +2 | grep -v '^ *(' | awk 'BEGIN { RS = "" } { print $8 }' | sort | uniq -c | sort -n | tail -n 20
30}
31
32show_rand() {
33    if [[ $# -eq 0 ]]; then
34        files=$(ls /var/spool/postfix/deferred/?/* | shuf | head -n 3)
35    else
36        match=$2
37        case "$1" in
38            from) dir=7;;
39            to) dir=8;;
40            *) usage;;
41        esac
42        msgids=$(mailq | tail -n +2 | grep -v '^ *(' | awk "BEGIN { RS = \"\" } (\$$dir ~ /$match/) { print \$1 }" | shuf | head -n 3)
43        files=$(for msgid in $msgids; do echo /var/spool/postfix/deferred/${msgid:0:1}/$msgid; done)
44    fi
45    for file in $files; do
46        echo ">>>> $file";
47        strings "$file"
48        echo;
49    done
50}
51
52tmpl_email() {
53    sender=${SSH_GSSAPI_NAME%%/*}
54    if [[ $# -eq 0 ]]; then
55        echo "Please specify a locker to generate template for." >&2
56        exit 1
57    fi
58    for locker in "$@"; do
59        locker=$(clean_locker "$locker")
60        echo "fs la /mit/$locker/"
61        fs la "/mit/$locker"
62        echo
63        cat <<-EOF
64The 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.
65
66You 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.
67
68Frequently, 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.
69
70If you have any questions, feel free to contact us.
71
72Thanks,
73scripts.mit.edu team
74scripts@mit.edu --- semi-private
75scripts-root@mit.edu --- service maintainers only
76EOF
77        echo;echo
78    done
79}
80
81purge_from() {
82    if [[ $# -eq 0 ]]; then
83        echo "Please specify a locker to purge emails from" >&2
84        exit 1
85    fi
86    for locker in "$@"; do
87        locker=$(clean_locker "$locker")
88        echo "$locker..."
89        mailq | tail -n +2 | grep -v '^ *(' | awk "BEGIN { RS = \"\" } (\$7 == \"$locker@scripts.mit.edu\") { print \$1 }" | tr -d '*!' | postsuper -d -
90        echo
91    done
92}
93
94purge_to() {
95    if [[ $# -eq 0 ]]; then
96        echo "Please specify a locker to purge emails to" >&2
97        exit 1
98    fi
99    for locker in "$@"; do
100        locker=$(clean_locker "$locker")
101        echo "$locker..."
102        mailq | tail -n +2 | grep -v '^ *(' | awk "BEGIN { RS = \"\" } (\$8 == \"$locker@scripts.mit.edu\" && \$9 == \"\") { print \$1 }" | tr -d '*!' | postsuper -d -
103        echo
104    done
105}
106
107op=${1:-}
108
109# We want to go ahead and show the usage message if there are no args, so
110# don't let the shift fail and end the script because of "set -e"
111shift || :
112
113case "$op" in
114    list-from) list_from;;
115    list-to) list_to;;
116    show-rand) show_rand "$@";;
117    email) tmpl_email "$@";;
118    purge-from) purge_from "$@";;
119    purge-to) purge_to "$@";;
120    *)
121        usage
122        ;;
123esac
124
125# vim: set sts=4 sw=4 et:
Note: See TracBrowser for help on using the repository browser.