#!/usr/bin/perl # Written by Andrii Grytsenko 2010 (c) # PP: http://andriigrytsenko.net use DBI; require Mail::Send; sub GetConf { my $conf_file = shift; my %conf_hash; open(CONF,"<$conf_file"); while (){ chomp(); my ($key,$value) = split(/=/,$_); $conf_hash{$key} = $value; } return(\%conf_hash); } sub GetUsers { my ($dbh,$sql,$thre) = @_; my %ret_hash; my $sth = $dbh->prepare($sql); $sth->execute($thre); while ( my @row = $sth->fetchrow_array() ) { my $user = @row[0]; my $size = @row[2]/@row[1]*100; $size = sprintf("%.1f",$size); $ret_hash{$user} = $size; } return(\%ret_hash); } sub SentMailToRoot { my ($users,$conf) = @_; $msg = Mail::Send->new; $msg->to("$conf->{admin_mail}"); $msg->subject('mail quota exceeding'); $fh = $msg->open('sendmail'); print $fh "There are users which exceeding mail quota($conf->{max_thre}%)\n"; foreach my $user (sort keys %$users){ print "Sent to root, user = $user \n"; print $fh "$user = $users->{$user} % \n"; } $fh->close; return 1; } sub SentUserMail { my ($users,$conf) = @_; foreach my $user (sort keys %$users){ print "send mail to user $user\n"; $msg = Mail::Send->new; my $u_mail_box=$user."".$conf->{domain}; $msg->to($u_mail_box); $msg->subject('mail quota exceeding'); $fh = $msg->open('sendmail'); print $fh "You mailbox is $users->{$user} % . You reached quota exceeding. Please clean up you mailbox as soon as possible\n"; $fh->close; } return 1; } sub PrintHelp { print "$0 [path_to_config]\n"; exit 0; } PrintHelp unless ($#ARGV =! 0 ); my $conf = GetConf("$ARGV[0]"); $dbh = DBI->connect("dbi:Pg:dbname=$conf->{dbname};host=$conf->{host};port=$conf->{port}",$conf->{user}, $conf->{password}, {AutoCommit => 0, RaiseError => 1, PrintError => 1, pg_server_prepare => 1 } ); my $sql="select userid,maxmail_size,curmail_size from dbmail_users where maxmail_size*?/100 < curmail_size and maxmail_size != 0;"; my $max_users = GetUsers($dbh,$sql,$conf->{max_thre}); my $min_users = GetUsers($dbh,$sql,$conf->{min_thre}); SentMailToRoot($max_users, $conf); SentUserMail($min_users, $conf);