Skip to content

Commit 1dc4398

Browse files
author
Till Brehm
committed
Merge branch 'dovecot_quota_calc' into 'stable-3.1'
Dovecot quota calc See merge request ispconfig/ispconfig3!1041
2 parents bb62e4d + a4ae330 commit 1dc4398

File tree

2 files changed

+24
-11
lines changed

2 files changed

+24
-11
lines changed

server/lib/classes/cron.d/100-monitor_email_quota.inc.php

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -79,37 +79,48 @@ public function onRunJob() {
7979
if(is_array($mailboxes)) {
8080

8181
//* with dovecot we can use doveadm instead of 'du -s'
82-
$dovecot = false;
83-
if (isset($mail_config['pop3_imap_daemon']) && $mail_config ['pop3_imap_daemon'] = 'dovecot' && is_executable('doveadm')) {
84-
exec('doveadm quota 2>&1', $tmp_output, $tmp_retval); // with dovecot 2.2.x 'doveadm quota' is unuseable
85-
if ($retval = 64) $dovecot = true;
82+
$dovecotQuotaUsage = array();
83+
if (isset($mail_config['pop3_imap_daemon']) && $mail_config ['pop3_imap_daemon'] = 'dovecot') {
84+
exec("doveadm quota get -A 2>&1", $res, $retval);
85+
if ($retval = 64) {
86+
foreach ($res as $v) {
87+
$s = preg_split('/\s+/', $v);
88+
if ($s[2] == 'STORAGE') {
89+
$dovecotQuotaUsage[$s[0]] = $s[3] * 1024; // doveadm output is in kB
90+
} elseif ($s[3] == 'STORAGE') {
91+
$dovecotQuotaUsage[$s[0]] = $s[4] * 1024; // doveadm output is in kB
92+
}
93+
}
94+
}
8695
}
8796

8897
foreach($mailboxes as $mb) {
8998
$email = $mb['email'];
9099
$email_parts = explode('@', $mb['email']);
91100
$filename = $mb['maildir'].'/.quotausage';
92-
if(!file_exists($filename) && $dovecot) {
93-
$app->system->exec_safe('doveadm quota recalc -u ?', $email);
94-
}
95-
if(file_exists($filename) && !is_link($filename)) {
101+
if(count($dovecotQuotaUsage) > 0 && isset($dovecotQuotaUsage[$email])) {
102+
$data[$email]['used'] = $dovecotQuotaUsage[$email];
103+
$app->log("Mail storage $email: " . $data[$email]['used'], LOGLEVEL_DEBUG);
104+
} elseif(file_exists($filename) && !is_link($filename)) {
96105
$quotafile = file($filename);
97106
preg_match('/storage.*?([0-9]+)/s', implode('',$quotafile), $storage_value);
98107
$data[$email]['used'] = $storage_value[1];
99-
$app->log("Mail storage $email: " . $storage_value[1], LOGLEVEL_DEBUG);
108+
$app->log("Mail storage $email: " . $data[$email]['used'], LOGLEVEL_DEBUG);
100109
unset($quotafile);
101110
} else {
102111
$app->system->exec_safe('du -s ?', $mb['maildir']);
103112
$out = $app->system->last_exec_out();
104113
$parts = explode(' ', $out[0]);
105114
$data[$email]['used'] = intval($parts[0])*1024;
115+
$app->log("Mail storage $email: " . $data[$email]['used'], LOGLEVEL_DEBUG);
106116
unset($out);
107117
unset($parts);
108118
}
109119
}
110120
}
111121

112122
unset($mailboxes);
123+
unset($dovecotQuotaUsage);
113124

114125
//* Dovecot quota check Courier in progress lathama@gmail.com
115126
/*
@@ -132,8 +143,8 @@ public function onRunJob() {
132143
$res['state'] = $state;
133144

134145
/*
135-
* Insert the data into the database
136-
*/
146+
* Insert the data into the database
147+
*/
137148
$sql = 'REPLACE INTO monitor_data (server_id, type, created, data, state) ' .
138149
'VALUES (?, ?, UNIX_TIMESTAMP(), ?, ?)';
139150
$app->dbmaster->query($sql, $res['server_id'], $res['type'], serialize($res['data']), $res['state']);

server/lib/classes/cron.d/500-clean_mailboxes.inc.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ public function onRunJob() {
5757
$junk_names=array('Junk', 'Junk Email', 'SPAM', 'INBOX.SPAM');
5858

5959
$purge_cmd = 'doveadm expunge -u ? mailbox ? sentbefore ';
60+
$recalc_cmd = 'doveadm quota recalc -u ?';
6061

6162
$server_id = intval($conf['server_id']);
6263
$records = $app->db->queryAllRecords("SELECT email, maildir, purge_trash_days, purge_junk_days FROM mail_user WHERE maildir_format = 'maildir' AND disableimap = 'n' AND server_id = ? AND (purge_trash_days > 0 OR purge_junk_days > 0)", $server_id);
@@ -77,6 +78,7 @@ public function onRunJob() {
7778
}
7879
}
7980
}
81+
$app->system->exec_safe($recalc_cmd, $email['email']);
8082
}
8183
}
8284

0 commit comments

Comments
 (0)