-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimapsize-multi.pl
More file actions
373 lines (297 loc) · 8.28 KB
/
imapsize-multi.pl
File metadata and controls
373 lines (297 loc) · 8.28 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
#!/usr/bin/perl
#
#
# This program is free software: you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see
# <http://www.gnu.org/licenses/>.
# imapsize-multi.pl
#
# + check email account(s) via IMAP for how many messages there are and
# compute their total size
#
# + compute "percentage full" of each account, using the specified
# size-quota, and send an informative email about usage
#
# + write the obtained information to a CSV file
#
#
# usage:
#
# + adjust settings below
#
# + create a file listing the IMAP accounts to check, for example:
#
#
# server1.example.com, user1@example.com, password_user1, quotaMB
# server2.example.com, user2@example.com, password_user2, quotaMB
#
#
# + make sure the passwords cannot be read by unauthorized users!
#
# + run the script --- you may need to install additional perl modules
#
# + you can run the script from your contab to check the accounts regularly
#
#
use strict;
use warnings;
use autodie;
use Net::IMAP::Client;
use MIME::Lite;
use Scalar::Util qw(openhandle);
use File::Slurp;
# SETTINGS
#
# list of emailaccounts
# entries are SERVER, USERNAME, PASSWORD, QUOTA LIMIT
# items must be seperated by comma; QUOTA LIMIT is in MB
#
use constant ACCOUNTLIST => '/root/acctlst';
# send email with quota information from, to
# The recipient receives the OUTFILE.
#
use constant RCPT => 'recipient@example.com';
use constant FROM => 'sender@example.com';
# file for quota information summary
#
use constant OUTFILE => '/root/imapsize.out';
# file for statistics
#
use constant STATISTICSFILE => '/root/imapsize-stats.csv';
#
# how many lines in STATISTICSFILE before it´s truncated
#
use constant STATSFILE_MAXLINES => 4096;
#
# truncate STATISTICSFILE by this many lines
# The file may grow indefinitely when more new lines are added
# each run than are being truncated.
# When STATSFILE_TRUNCLINES is greater than STATSFILE_MAXLINES,
# STATISTICSFILE will be unlinked rather than truncated.
#
use constant STATSFILE_TRUNCLINES => 5;
#
# When the STATISTICSFILE has more lines than STATSFILE_MAXLINES, a
# copy of the file is created without the first STATSFILE_MAXLINES
# lines which then replaces the STATISTICSFILE. Specify the name to
# use for this file here.
#
use constant STATSFILE_COPY => STATISTICSFILE . '_tmp';
#
# Put a nice header on top of the STATISTICSFILE, telling what each field is.
# do not change
#
use constant STATSFILE_HEADER => "unixtime,msgs,size,percent quota,quota,username,server\n";
# internal factors for kilobyte, megabyte and gigabyte
# do not change
#
use constant KB => 1024;
use constant MB => KB * 1024;
use constant GB => MB * 1024;
# give warning when quota limit is almost reached
# (like when 85% full)
#
use constant QUOTA_WARNING_PERCENT => 85.0;
# / SETTINGS
# go through each account given and check
#
sub chkacct {
my ($out, $stat, $server, $username, $pass, $quota) = @_;
# create new imap connection ...
#
my $imap = Net::IMAP::Client->new(
server => $server,
user => $username,
pass => $pass,
ssl => 1,
ssl_verify_peer => 0,
port => 993)
or die "connection to imap server ($server) failed";
# ... and log in
#
return 1 unless $imap->login;
# request all folders
#
my @folders = $imap->folders;
my $msgsize = 0;
my $nm = 0;
# go through all messages in all folders and fetch their size
#
foreach my $this (@folders) {
my $status = $imap->status($this);
my $nofmsgs = $status->{MESSAGES};
if ($nofmsgs) {
$nm += $nofmsgs;
$imap->examine($this);
foreach my $id ($imap->search('ALL')) {
foreach my $msg (@$id) {
$msgsize += $imap->fetch($msg, 'RFC822.SIZE')->{'RFC822.SIZE'};
}
}
}
}
# done with imap
#
$imap->logout;
# add info to outfile
#
my $megabyte = int($msgsize / MB + 0.5);
# $msgsize is in bytes
#
$quota *= MB;
my $percent_quota = int($msgsize * 100.0 / $quota + 0.5);
printf $out "%6d email(s), %12d byte(s) (~%6d MB), ~%3d%% used <%s>@[%s]: ", $nm, $msgsize, $megabyte, $percent_quota, $username, $server;
my $status = 'OK';
if (QUOTA_WARNING_PERCENT < $percent_quota) {
$status = 'WARNING';
}
print $out $status . "\n";
# add info to statistics file
#
print $stat join(',', time, $nm, $msgsize, $percent_quota, $quota, $username, $server) . "\n";
return 0;
}
# send info as email
#
sub file2mail {
my ($filename, $from, $rcpt, $subj) = @_;
my $file = read_file($filename);
my $email = MIME::Lite->new(
From => $from,
To => $rcpt,
Subject => $subj,
Data => $file
);
$email->send;
}
###########################################################################
open my $outfile, ">", OUTFILE;
print $outfile "Email Quota check, " . localtime(time) . "\n\n";
# check statistics file before opening
#
my $statsfile_exists = 0;
#
if (-e STATISTICSFILE) {
$statsfile_exists = 1;
open my $origin_fh, "<", STATISTICSFILE;
my $lines = 0;
ORIGIN:
while (<$origin_fh>) {
$lines++;
if (STATSFILE_MAXLINES < $lines) {
#
# unlink the file when there are more lines to truncate than the
# file is allowed to have
#
if (STATSFILE_TRUNCLINES >= $lines) {
close $origin_fh;
unlink(STATISTICSFILE);
$statsfile_exists = 0;
print $outfile STATISTICSFILE . " has been unlinked\n";
print $outfile "because it would have been truncated by more lines than it had\n\n";
# the file has been unlinked anyway
#
last ORIGIN;
} else {
#
# otherwise, truncate some lines from the top
#
# seek STATSFILE_TRUNCLINES into file
#
seek($origin_fh, 0, 0);
$lines = 0;
while (<$origin_fh>) {
$lines++;
last if(STATSFILE_TRUNCLINES < $lines);
}
# From there, create a forward copy of the remaining lines.
# What is more awkward: Reading backwards or copying forwards?
open my $copy_fh, ">", STATSFILE_COPY;
print $copy_fh STATSFILE_HEADER;
$lines = 0;
COPY:
while (my $thisline = <$origin_fh>) {
chomp $thisline;
# skip blank line at eof unless not first line
#
if (length($thisline) || $lines) {
print $copy_fh $thisline . "\n";
$lines++;
}
}
close $copy_fh;
close $origin_fh;
unlink(STATISTICSFILE);
rename(STATSFILE_COPY, STATISTICSFILE);
print $outfile STATISTICSFILE . " has been truncated forwardly because it had more than " . STATSFILE_MAXLINES . " lines\n";
print $outfile "$lines line(s) remained\n\n";
# at eof anyway
#
last ORIGIN;
}
}
}
# only close when still open
#
if (defined(openhandle($origin_fh))) {
close $origin_fh;
}
}
#
# / check statistics file before opening
# check accounts
open my $statsfile, ">>", STATISTICSFILE;
#
# what it says
#
print $statsfile STATSFILE_HEADER unless($statsfile_exists);
my $linecount = 0;
open my $fh, "<", ACCOUNTLIST;
LINE:
while (my $line = <$fh>) {
$linecount++;
unless($line =~ m/^#/)
{
chomp $line;
my @acct = split(',', $line);
if(@acct == 4) {
for (@acct) {
# trim them all
#
s/^\s+|\s+$//g;
unless(length) {
printf $outfile "syntax error in %s, line %8d (zero-length item?)\n", ACCOUNTLIST, $linecount;
next LINE;
}
}
# check an account
#
print $outfile "login for $acct[3] ($acct[2]) failed\n" if chkacct($outfile, $statsfile, @acct);
}
else
{
printf $outfile "syntax error in %s, line %8d (missing item?)\n", ACCOUNTLIST, $linecount;
}
}
}
close $fh;
close $statsfile;
if (STATSFILE_TRUNCLINES < $linecount) {
#
# be nice and give a warning
#
print $outfile "\n\n" . STATISTICSFILE . " may grow out of bounds because more lines are added\nthan may be truncated on each run unless\n" . ACCOUNTLIST . " has blank lines\n";
}
close $outfile;
file2mail(OUTFILE, FROM, RCPT, 'Email Quota Check, ' . localtime(time));
exit 0;