postfix でメールデータ使用量を確認する
postfixでクォータ情報を書き出すスクリプトが、日本語でなかったのでメモ程度に(maildirsizeを利用するため、postfixではなくても動くと思います)。
条件
maildirsizeファイルが生成されていること。
スクリプト
今回はperlです(似たプログラムがあっただけです)。
quota.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 |
#!/usr/bin/perl use strict; use warnings; # 引数 my $username=$ARGV[0]; exit if (!defined $username); # 開くファイルを指定 my $home = "/home/spool"; my $file=$home . "/" . $username . "/maildirsize"; # ファイルを開く open(FILE, "< $file") or die "OK $file does not exist."; my @lines=<FILE>; close(FILE); # どの行が何のデータかを指定 my $quota = $lines[0]; $quota = substr($quota, 0, -2); my $line=my $size=my $msgs=0; my $rate; do { $line++; (my $msgsize, my $msgcount) = split(" ", $lines[$line]); $size+=$msgsize; $msgs+=$msgcount; } while ($line < $#lines); if ($size > $quota) { print "OVERQUOTA user: $username size: $size quota: $quota msgs: $msgs\n"; } else { |
引数にアカウント名を入れて、コマンドを実行します。
1 2 |
[root@localhost quota]# perl quota.pl accountname [root@localhost quota]# user: accountname size: 51275793 quota: 1048576000 rate: 0.0489004068374634 msgs: 2184 |
応用
ユーザ一覧をテキストファイルで用意し、それを引数にして30%以上使用しているユーザ一覧を作成します。
作成するファイルは3つ。
- quota.pl (先ほどのものをベースにちょっとだけ改造)
- username.txt (アカウントの一覧)
- quota.sh (quota.plをループして一覧にするためのシェルスクリプト)
quota.pl
1234567891011121314151617181920212223242526272829303132333435363738394041 #!/usr/bin/perluse strict;use warnings;# 引数my $username=$ARGV[0];exit if (!defined $username);# 開くファイルを指定my $home = "/home/spool";my $file=$home . "/" . $username . "/maildirsize";#my $file="/home/spool/user.name\@domain.com/maildirsize";# ファイルを開くopen(FILE, "< $file") or die "OK $file does not exist.";my @lines=<FILE>;close(FILE);# どの行が何のデータかを指定my $quota = $lines[0];$quota = substr($quota, 0, -2);my $line=my $size=my $msgs=0;my $rate;do {$line++;(my $msgsize, my $msgcount) = split(" ", $lines[$line]);$size+=$msgsize; $msgs+=$msgcount;# 使用率を計算$rate = $size/$quota;} while ($line < $#lines); if ($rate > 0.3) {# 使用率を計算#$rate = $size/$quota;# 表示print "user: $username size: $size quota: $quota rate: $rate msgs: $msgs\n";} else {print "";}username.txt
aaaa@xxx.com bbbb@xxx.com cccc@xxx.com dddd@xxx.com eeee@xxx.com※メールアドレスがアカウント名のため、@xxx.comになっています。
quota.sh
1234 while read linedoperl quota.pl $line;done < ./username.txtquota.shを実行します
1 sh quota.sh結果
12345 user: aaaa@xxx.com size: 51275793 quota: 1048576000 rate: 0.3489004068374634 msgs: 2184user: bbbb@xxx.com size: 23674050 quota: 1048576000 rate: 0.4225773334503174 msgs: 3145user: cccc@xxx.com size: 20873492 quota: 1048576000 rate: 0.6199065132141113 msgs: 919user: dddd@xxx.com size: 34607792 quota: 1048576000 rate: 0.3330045623779297 msgs: 3774user: eeee@xxx.com size: 47967183 quota: 1048576000 rate: 0.8457450704574585 msgs: 2797参考
- Check Disk Quota Usage By Parsing Maildirsize (KutuKupret)
ほぼここのスクリプトそのまま使用していますが、ほんの一部修正が必要でした。
Wordでメイリオや小塚フォントを使用すると、通常よりも行間が大きく空く 暗号化PDFの暗号解除方法