-
Notifications
You must be signed in to change notification settings - Fork 0
/
mailspool2dog
executable file
·66 lines (54 loc) · 1.75 KB
/
mailspool2dog
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
#!/usr/bin/perl
use strict;
use warnings;
use DataDog::DogStatsd;
use File::Slurp;
use Data::Dumper;
#TODO: handle options
my $debug = 0;
my $dogstatsd = DataDog::DogStatsd->new;
#$dogstatsd->increment( 'mail.script_runs') unless $debug;
#my ($logfile) = @ARGV;
my $spooldir = '/var/spool/mail';
chdir($spooldir) or die "couldn't chdir($spooldir): $!";
# read file names
opendir(SPOOL,$spooldir) or die "couldn't opendir($spooldir): $!";
my @spool_files = readdir(SPOOL);
closedir(SPOOL) or die "couldn't closedir($spooldir): $!";
my %cache;
while (1) {
foreach my $file (@spool_files) {
next if $file =~ /^[.]/; # skip hiddens
next if $file =~ /[.]/; # skip files with any dots in them
next unless -r $file;
calculate_stats($file);
}
my $now_pretty = `date`;
chomp($now_pretty);
print "about to snooze ($now_pretty)\n";
sleep(60); # TODO: scale to how long it took to do an iteration
}
sub calculate_stats {
my($filename) = @_;
my $mtime = (stat($filename))[9];
my ($size,$message_count);
if (defined($cache{$filename}->{mtime}) and $mtime == $cache{$filename}->{mtime}) {
print "using cache\n";
$size = $cache{$filename}->{bytes};
$message_count = $cache{$filename}->{messages};
} else {
$size = -s $filename;
$message_count = `grep '^From ' $filename | wc -l`;
chomp($message_count);
$cache{$filename}->{bytes} = $size;
$cache{$filename}->{messages} = $message_count;
$cache{$filename}->{mtime} = $mtime;
my $now_pretty = `date`;
chomp($now_pretty);
print "$filename ($now_pretty) $size $message_count $mtime\n";
}
#print "$filename $size $message_count $mtime\n";
# TODO: tag user
$dogstatsd->gauge( "mail.${filename}.bytes", $size ) unless $debug;
$dogstatsd->gauge( "mail.${filename}.messages", $message_count ) unless $debug;
}