Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

quota_usage.pl 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #!/usr/bin/perl
  2. # vim:ts=4:sw=4:et
  3. # Virtual quota_usage 0.3
  4. # Contributed to Postfixadmin by Jose Nilton <jniltinho@gmail.com>
  5. #
  6. # See also : http://www.russelldare.net/media/perl/dirsizeSource.pdf
  7. # License: GPL v2.
  8. # Usage:
  9. # perl quota_usage.pl --list
  10. # perl quota_usage.pl --list --addmysql
  11. # for add mysql database postfix
  12. #
  13. # Requirements - the following perl modules are required:
  14. # DBD::Pg or DBD::mysql; perl perl-DBD-mysql perl-DBD (may be named differently depending on your platform).
  15. # and the 'du' binary in $ENV{'PATH'} (see below).
  16. #
  17. # You will need to modify the postfix DATABASE to add a quota_usage column.
  18. # Mysql:
  19. # ALTER TABLE mailbox ADD quota_usage INT(11) NOT NULL DEFAULT '0' AFTER modified,
  20. # ADD quota_usage_date DATE NOT NULL DEFAULT '0000-00-00' AFTER quota_usage;
  21. # PostgreSQL:
  22. # ALTER TABLE mailbox ADD COLUMN quota_usage INTEGER NOT NULL DEFAULT 0;
  23. # ALTER TABLE mailbox ADD COLUMN quota_usage_date DATE NOT NULL DEFAULT current_date;
  24. #
  25. use strict;
  26. use warnings;
  27. use File::Path;
  28. use DBI;
  29. use Getopt::Long;
  30. ##EDIT##
  31. my $db_host = 'localhost';
  32. my $db_database = 'postfix';
  33. my $db_user = 'postfix';
  34. my $db_password = '123456';
  35. my $root_path = '/home/vmail';
  36. # Pg or mysql
  37. my $db_type = 'mysql';
  38. ##END EDIT##
  39. (help()) if (!$ARGV[0]);
  40. $ENV{'PATH'} = "/sbin:/bin:/usr/sbin:/usr/bin";
  41. my($domain_dir, $full_domain_dir, $user_dir, $usage, $email, $sql, $dbh);
  42. my $list = 0;
  43. my $insert_db = 0;
  44. my $total_mailbox = 0;
  45. my $total_domain = 0;
  46. GetOptions ('l|list' => \$list, 'i|addmysql' => \$insert_db, 'help|h|man' => \&help) or (help());
  47. (list_quota_usage()) if ($list == 1 || $insert_db == 1 );
  48. sub list_quota_usage {
  49. opendir(DOMAINDIR, $root_path) or die ("Unable to access directory '$root_path' ($!)");
  50. if($insert_db == 1){
  51. $dbh = DBI->connect("DBI:$db_type:database=$db_database;host=$db_host", $db_user, $db_password) or die ("cannot connect the database");
  52. execSql("UPDATE mailbox set quota_usage = 0");
  53. }
  54. foreach $domain_dir (sort readdir DOMAINDIR) {
  55. next if $domain_dir =~ /^\./; # skip dotted dirs
  56. $full_domain_dir = "$root_path/$domain_dir"; #print "$full_domain_dir\n";
  57. $total_domain++;
  58. opendir(USERDIR, $full_domain_dir) or die ("Unable to access directory '$full_domain_dir' ($!)");
  59. foreach $user_dir (sort readdir USERDIR) {
  60. next if $user_dir =~ /^\./; # skip dotted dirs
  61. $email = "$user_dir\@$domain_dir";
  62. $total_mailbox++;
  63. my $i = `du -0 --summarize $full_domain_dir/$user_dir`;
  64. ($usage) = split(" ", $i);
  65. if ($usage < 100) {
  66. $usage = 0;
  67. } elsif ($usage < 1000) {
  68. $usage = 1;
  69. } else {
  70. $usage = $usage + 500;
  71. $usage = int $usage / 1000;
  72. }
  73. if($insert_db == 1){execSql("UPDATE mailbox set quota_usage = $usage, quota_usage_date = CAST(NOW() AS DATE) WHERE username = '$email'");}
  74. print_list() if ($list == 1);
  75. }
  76. }
  77. close(DOMAINDIR);
  78. close(USERDIR);
  79. (print_total()) if ($list == 1);
  80. }
  81. sub execSql {
  82. my $sql = shift;
  83. my $ex;
  84. $ex = $dbh->do($sql) or die ("error when running $sql");
  85. }
  86. sub print_total{
  87. print "---------------------------------------------------------\n";
  88. print "TOTAL DOMAIN\t\t\t\tTOTAL MAILBOX\n";
  89. print "---------------------------------------------------------\n";
  90. print "$total_domain\t\t\t\t\t\t$total_mailbox\n";
  91. }
  92. sub print_list {
  93. format STDOUT_TOP =
  94. Report of Quota Used
  95. ---------------------------------------------------------
  96. EMAIL QUOTA USED
  97. ---------------------------------------------------------
  98. .
  99. format =
  100. @<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< @<<<<<<<<<<
  101. $email, "$usage MB"
  102. .
  103. write;
  104. }
  105. sub help {
  106. print "$0 [options...]\n";
  107. print "-l|--list List quota used\n";
  108. print "-i|--addmysql For insert quota used in database mysql\n";
  109. }