-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimqc
More file actions
executable file
·149 lines (137 loc) · 4.46 KB
/
Copy pathsimqc
File metadata and controls
executable file
·149 lines (137 loc) · 4.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#!/bin/bash
# simqc: does queue tricks, plays the fight song
#
# -s : prints out stats on slow queue
# -f : prints out stats on fast queue
# -# : sets number to be used by head in -s and -f, default is 10
# -d : prints out queue stats for last days ( equiv to -t 24 )
# -t hours : prints out queue stats for last [hours] hours
# -n : prints out the name of the newest queue file in simta/etc
export PATH=/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/sbin:/usr/rsug/bin
name=simqc
stats=$(mktemp -d /tmp/${name}.XXXXXXXX)
simtadir=/var/spool/simta
ignore=umich.edu
slow_opt=0
fast_opt=0
local_opt=0
dead_opt=0
ip_opt=0
authuser_opt=0
subject_opt=0
head_opt=10
day_opt=0
time_opt=0
newest_opt=0
usage() {
echo "Usage: $name [-sfldaiS] [-h num] [-D] [ -t hours ] | [-n]"
exit 1
}
error() {
echo $* 1>&2
exit 1
}
checkq(){
queue=$1
find $simtadir/$queue -maxdepth 1 -ignore_readdir_race -name E\* -type f > $stats/$queue.list
# `xargs cat` is used to avoid jq dying when a file goes away
cat $stats/$queue.list | xargs cat 2>/dev/null | jq -r .recipients[] | sort | uniq -c | sort -rn > $stats/$queue.sortedrecip
echo "Top $queue recipients"
head -n $head_opt $stats/$queue.sortedrecip
echo ""
cat $stats/$queue.list | xargs cat 2>/dev/null | jq -r .hostname | sort | uniq -c | sort -rn > $stats/$queue.sortedhosts
echo "Top $queue hosts"
head -n $head_opt $stats/$queue.sortedhosts
echo ""
cat $stats/$queue.list | xargs cat 2>/dev/null | jq -r .sender | sort | uniq -c | sort -rn > $stats/$queue.sortedfrom
echo "Top $queue senders"
head -n $head_opt $stats/$queue.sortedfrom
echo ""
if [[ $authuser_opt -eq 1 ]]; then
sed 's/E/D/' $stats/$queue.list | xargs -r fgrep -h 'auth=pass smtp.auth=' | sed -e 's/.*=//' > $stats/$queue.authusers
sort -n $stats/$queue.authusers | uniq -c | sort -rn > $stats/$queue.sortedauthusers
echo "Top $queue authusers"
head -n $head_opt $stats/$queue.sortedauthusers
echo ""
fi
if [[ $ip_opt -eq 1 ]]; then
sed 's/E/D/' $stats/$queue.list | xargs -r fgrep 'policy.iprev' | grep -v $ignore | sed -e 's/.*policy.iprev=//; s/ .*//' > $stats/$queue.ips
sort -n $stats/$queue.ips | uniq -c | sort -rn > $stats/$queue.sortedips
echo "Top $queue non-$ignore IPs"
head -n $head_opt $stats/$queue.sortedips
echo ""
cut -d. -f1-3 $stats/$queue.ips | sort -n | uniq -c | sort -rn > $stats/$queue.sortedsubnets
echo "Top $queue non-$ignore subnets"
head -n $head_opt $stats/$queue.sortedsubnets
echo ""
fi
if [[ $subject_opt -eq 1 ]]; then
sed 's/E/D/' $stats/$queue.list | xargs -r awk '/auth=pass smtp.auth=/{ auth=$NF } /^Subject:/{ print auth, $0} /^$/{ exit }' | sort -n | uniq -c | sort -rn > $stats/$queue.subjects
echo "Top $queue subjects"
head -n $head_opt $stats/$queue.subjects
echo ""
fi
}
qstats() {
minutes=$1
if [[ -d $simtadir/etc ]]; then
for x in $(find $simtadir/etc -ignore_readdir_race -name queue_schedule.\* -type f -mmin -${minutes}); do
awk -v f=$x '{ s += $2 } END { print f,s }' $x
done | sort -n -k 2 > $stats/tmpfile
adqavg=$(awk '{ s += $2 } END { print s/NR }' $stats/tmpfile)
adqmin=$(awk 'NR==1 { print $2 }' $stats/tmpfile)
adqmax=$(awk 'END { print $2 }' $stats/tmpfile)
admed=$(( $(awk 'END { print NR }' $stats/tmpfile) / 2 ))
adqmed=$(awk -v m=$admed 'NR==m { print $2 }' $stats/tmpfile)
echo -e "$(( $minutes / 60 )) hour total queue stats"
echo -e "max: $adqmax min: $adqmin avg: $adqavg med: $adqmed"
else
error $simtadir/etc does not exist
fi
}
while getopts adDfh:ilsSt: opt; do
case $opt in
a) authuser_opt=1
;;
d) dead_opt=1
;;
D) day_opt=1
;;
f) fast_opt=1
;;
h) head_opt="$OPTARG"
;;
i) ip_opt=1
;;
l) local_opt=1
;;
s) slow_opt=1
;;
S) subject_opt=1
;;
t) time_opt="$OPTARG"
;;
*) usage
;;
esac
done
if [[ $# -eq 0 ]] || [[ $slow_opt -eq 1 || $fast_opt -eq 1 || $local_opt -eq 1 ]] || [[ $dead_opt -eq 1 ]]; then
if [[ $slow_opt -eq 1 ]]; then
checkq slow
fi
if [[ $fast_opt -eq 1 ]]; then
checkq fast
fi
if [[ $local_opt -eq 1 ]]; then
checkq local
fi
if [[ $dead_opt -eq 1 ]]; then
checkq dead
fi
fi
if [[ $day_opt -eq 1 ]]; then
qstats $(( 60 * 24 ))
elif [[ $time_opt -gt 0 ]]; then
qstats $(( 60 * time_opt ))
fi
rm -rf $stats