Subversion Repositories sysadmin_scripts

Rev

Rev 14 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
14 rodolico 1
#! /usr/bin/perl -w
2
 
3
# Script to calculate stats on mail accounts. Assumes virtual mail server
4
# with directories in the form of
5
# clientName/email@address
6
# where email@address is a MailDir structure
7
 
8
# returns a CSV containing sizes and number of files. NOTE: these numbers are off
9
# a little since they do not take into account the IMAP config files for each client.
10
 
11
my $CONFIG_FILE_NAME = 'mailmanage.cfg';
12
my $ROOT_DIR = '/var/vmail';  # location containing the mail files
13
my %IGGY = ( 'lost+found'=>1,'mailfilters'=>1,'quota.group'=>1,'quota.user'=>1);
14
 
15
sub getScriptLocation {
16
   use strict;
17
   use File::Spec::Functions qw(rel2abs);
18
   use File::Basename;
19
   return dirname(rel2abs($0));
20
}
21
 
22
sub readConfig {
23
   my $scriptLocation = &getScriptLocation();
24
   my $return = '';
25
   if ( -e "$scriptLocation/$CONFIG_FILE_NAME" ) {
26
      open CFG,"<$scriptLocation/$CONFIG_FILE_NAME" or die "Could not read $scriptLocation/$CONFIG_FILE_NAME: $!\n";
27
      $return = join ( '', <CFG> );
28
      close CFG;
29
   }
30
   return $return;
31
}
32
 
33
$config = &readConfig();
34
eval( $config );
35
 
36
# get all subdirectories. They are assumed to be client names/domain names
37
opendir (my $handle, $ROOT_DIR) || die "Can't open $ROOT_DIR: $!";
38
my @directoriesToProcess = grep { /^[^.]/ } readdir( $handle );
39
closedir $handle;
40
 
41
# print Header
42
print "Client\tAccount\tMessages\tSize (k)\tNew\n";
43
foreach my $domain (@directoriesToProcess) {
44
   next if $IGGY{$domain};
45
   my $workingOn = "$ROOT_DIR/$domain";
46
   opendir (my $handle, $workingOn) || die "Can't open $workingOn: $!";
47
   my @accounts = grep { /^[^.]/ } readdir( $handle );
48
   closedir $handle;
49
   # each individual account
50
   foreach $thisAccount (@accounts) {
51
      my $temp = "$workingOn/$thisAccount/Maildir";
52
      my $fileCount = `find $temp -type f | wc -l`;
53
      my $size = `du -sk $temp`;
54
      my $active = `find $temp/new -type f | wc -l`; # the new directory will be very small on an active account
55
      chomp( $fileCount );
56
      chomp( $size );
57
      chomp( $active );
58
      $size =~ m/(\d+)/;
59
      $size = $1;
60
      $active =~ m/(\d+)/;
61
      $active = $1;
62
      print "$domain\t$thisAccount\t$fileCount\t$size\t$active\n";
63
   }
64
}
65
1;