Subversion Repositories sysadmin_scripts

Rev

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

#! /usr/bin/perl -w

# Script to calculate stats on mail accounts. Assumes virtual mail server
# with directories in the form of
# clientName/email@address
# where email@address is a MailDir structure

# returns a CSV containing sizes and number of files. NOTE: these numbers are off
# a little since they do not take into account the IMAP config files for each client.

my $CONFIG_FILE_NAME = 'mailmanage.cfg';
my $ROOT_DIR = '/var/vmail';  # location containing the mail files
my %IGGY = ( 'lost+found'=>1,'mailfilters'=>1,'quota.group'=>1,'quota.user'=>1);

sub getScriptLocation {
   use strict;
   use File::Spec::Functions qw(rel2abs);
   use File::Basename;
   return dirname(rel2abs($0));
}

sub readConfig {
   my $scriptLocation = &getScriptLocation();
   my $return = '';
   if ( -e "$scriptLocation/$CONFIG_FILE_NAME" ) {
      open CFG,"<$scriptLocation/$CONFIG_FILE_NAME" or die "Could not read $scriptLocation/$CONFIG_FILE_NAME: $!\n";
      $return = join ( '', <CFG> );
      close CFG;
   }
   return $return;
}

$config = &readConfig();
eval( $config );

# get all subdirectories. They are assumed to be client names/domain names
opendir (my $handle, $ROOT_DIR) || die "Can't open $ROOT_DIR: $!";
my @directoriesToProcess = grep { /^[^.]/ } readdir( $handle );
closedir $handle;

# print Header
print "Client\tAccount\tMessages\tSize (k)\tNew\n";
foreach my $domain (@directoriesToProcess) {
   next if $IGGY{$domain};
   my $workingOn = "$ROOT_DIR/$domain";
   opendir (my $handle, $workingOn) || die "Can't open $workingOn: $!";
   my @accounts = grep { /^[^.]/ } readdir( $handle );
   closedir $handle;
   # each individual account
   foreach $thisAccount (@accounts) {
      my $temp = "$workingOn/$thisAccount/Maildir";
      my $fileCount = `find $temp -type f | wc -l`;
      my $size = `du -sk $temp`;
      my $active = `find $temp/new -type f | wc -l`; # the new directory will be very small on an active account
      chomp( $fileCount );
      chomp( $size );
      chomp( $active );
      $size =~ m/(\d+)/;
      $size = $1;
      $active =~ m/(\d+)/;
      $active = $1;
      print "$domain\t$thisAccount\t$fileCount\t$size\t$active\n";
   }
}
1;