-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmailstats.pl
executable file
·209 lines (181 loc) · 4.32 KB
/
mailstats.pl
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
#!/usr/bin/perl
use List::MoreUtils qw/ uniq /;
use Sort::Fields;
use Term::ANSIColor;
use Getopt::Std;
use PerlIO::gzip;
use File::ReadBackwards;
my %options=();
getopts("he", \%options);
if ($options{h})
{
do_help();
}
sub do_help {
print "\nMailStats.pl - By Chris Ice\n\n";
print "Run with no flags to parse /var/log/exim_mainlog\n\n";
print "Usage: \n\n";
print "-h : Show this help text\n";
print "-e : Check extended logs - Does not check /var/log/exim_mainlog, just the gzipped files\n";
print "\n\n\n";
die "\n";
}
if ($options{e}) {
my @files = </var/log/exim_mainlog*.gz>;
foreach (@files) {
open FILE, "<:gzip", $_ or die $!;
}
}
else {
open FILE, "/var/log/exim_mainlog";
}
## section for system users
print color 'red';
print "\nEmails by user: " . color 'reset';
print "\n\n";
our @system_users = "";
while ( $lines_users = <FILE> ){
if ( $lines_users=~/(U\=)(.+?)(\sP\=)/i ) {
my $line_users = $2;
push (@system_users, $line_users)
}
}
my %count;
$count{$_}++ foreach @system_users;
while (my ($key, $value) = each(%count)) {
if ($key =~ /^$/ ) {
delete($count{$key});
}
}
foreach my $value (reverse sort { $count{$a} <=> $count{$b} } keys %count) {
print " " . $count{$value} . " : " . $value . "\n";
}
print "\n\n";
print colored ['red on_blue'], "Total: " . scalar (@system_users - 1);
print "\n";
## Section for email accounts
print color 'red';
print "\nEmail accounts sending out mail:\n\n";
print color 'reset';
if ($options{e}) {
my @files = </var/log/exim_mainlog*.gz>;
foreach (@files) {
open FILE, "<:gzip", $_ or die $!;
}
}
else {
open FILE, "/var/log/exim_mainlog";
}
@email_users = "";
while ( $lines_email = <FILE>) {
if ( $lines_email=~/(_login:|_plain:)(.+?)(\sS=)/i) {
my $lines_emails = $2;
push (@email_users, $lines_emails);
}
}
my %email_count;
$email_count{$_}++ foreach @email_users;
while (my ($key, $value) = each(%email_count)) {
if ($key =~ /^$/) {
delete($email_count{$key});
}
}
foreach my $value (reverse sort { $email_count{$a} <=> $email_count{$b} } keys %email_count) {
print " " . $email_count{$value} . " : " . $value . "\n";
}
print "\n";
print colored ['red on_blue'], "Total: " . scalar (@email_users - 1);
print "\n";
## Section for current working directories
print color 'red';
print "\nCurrent working directories:\n\n\n";
print color 'reset';
if ($options{e}) {
my @files = </var/log/exim_mainlog*.gz>;
foreach (@files) {
open FILE, "<:gzip", $_ or die $!;
}
}
else {
open FILE, "/var/log/exim_mainlog";
}
@dirs = "";
while ($dirs = <FILE>) {
if ( $dirs=~/(cwd=)(.+?)(\s)/i) {
my $dir = $2;
push (@dirs, $dir);
}
}
my %dirs;
$dirs{$_}++ foreach @dirs;
while (my ($key, $value) = each(%dirs)) {
if ($key =~ /^$/ ) {
delete($dirs[$key]);
}
}
while (my ($key, $value) = each(%dirs)) {
if ($key =~ /^$/) {
delete($dirs{$key});
}
}
foreach my $value (reverse sort { $dirs{$a} <=> $dirs{$b} } keys %dirs) {
print " " . $dirs{$value} . " : " . $value . "\n";
}
print "\n";
print colored ['red on_blue'], "Total: " . scalar (@dirs - 1);
print "\n";
## Section for titles
print color 'red';
print "\nTop 50 Email Titles:\n\n\n";
print color 'reset';
if ($options{e}) {
my @files = </var/log/exim_mainlog*.gz>;
foreach (@files) {
open FILE, "<:gzip", $_ or die $!;
}
}
else {
open FILE, "/var/log/exim_mainlog";
}
@titles = "";
while ($titles = <FILE>) {
if ( $titles=~/((U=|_login:).+)((?<=T=\").+?(?=\"))(.+$)/i) {
my $title = $3;
push (@titles, $title);
}
}
my %titlecount;
$titlecount{$_}++ foreach @titles;
while (my ($key, $value) = each(%titlecount)) {
if ($key =~ /^$/ ) {
delete($titlecount[$key]);
}
}
my $limit = 50;
my $loops = 0;
foreach my $value (reverse sort { $titlecount{$a} <=> $titlecount{$b} } keys %titlecount) {
print " " . $titlecount{$value} . " : " . $value . "\n";
$loops++;
if ($loops >= $limit) {
last;
}
}
print "\n\n";
print colored ['red on_blue'], "Total: " . scalar (@titles - 1);
print "\n\n";
close FILE;
if (!$options{e}) {
open (my $file, "/var/log/exim_mainlog") or die "Couldn't open file : $!";
my $first = <$file>;
close $file;
if ( $first =~ /(^.+?\:[0-9][0-9])\s/i ) {
$foutput = $1;
print "Log dates and times are: \n\nStart : " . $foutput;
}
my $last = File::ReadBackwards->new("/var/log/exim_mainlog")->readline;
if ( $last =~ /(^.+?\:[0-9][0-9])\s/i ) {
$lastoutput = $1;
print "\nEnd : " . $lastoutput;
print "\n\n";
}
}