don't count assigned domain quota for inactive domains

now only summing up the assigned domain quota for active domains, and not subtracting disc base occupied by mailboxes of inactive domains (inactive_bytes)
This commit is contained in:
heavygale 2019-09-29 19:04:00 +02:00
parent 471ab05423
commit f01e3e59c5
3 changed files with 34 additions and 20 deletions

View File

@ -44,8 +44,9 @@ else {
$exec_fields = array('cmd' => 'system', 'task' => 'df', 'dir' => '/var/vmail'); $exec_fields = array('cmd' => 'system', 'task' => 'df', 'dir' => '/var/vmail');
$vmail_df = explode(',', json_decode(docker('post', 'dovecot-mailcow', 'exec', $exec_fields), true)); $vmail_df = explode(',', json_decode(docker('post', 'dovecot-mailcow', 'exec', $exec_fields), true));
$domainQuota = round(domain_admin('total_quota')/1024); $domainQuota = round(domain_admin('total_quota')/1024);
$inactive_bytes = round(mailbox('get','inactive_bytes')/1024/1024/1024);
$quotaPercent1 = round(($domainQuota/substr($vmail_df[3], 0, -1))*100); $quotaPercent1 = round(($domainQuota/substr($vmail_df[3], 0, -1))*100);
$quotaPercent2 = round((($domainQuota-substr($vmail_df[2], 0, -1))/substr($vmail_df[3], 0, -1))*100); $quotaPercent2 = round((($domainQuota-substr($vmail_df[2], 0, -1)+$inactive_bytes)/substr($vmail_df[3], 0, -1))*100);
$quotaPercent2 = ($quotaPercent2+substr($vmail_df[4], 0, -1)>100) ? 100-substr($vmail_df[4], 0, -1) : $quotaPercent2; //handling overcommitment $quotaPercent2 = ($quotaPercent2+substr($vmail_df[4], 0, -1)>100) ? 100-substr($vmail_df[4], 0, -1) : $quotaPercent2; //handling overcommitment
?> ?>
<div role="tabpanel" class="tab-pane active" id="tab-containers"> <div role="tabpanel" class="tab-pane active" id="tab-containers">

View File

@ -453,7 +453,7 @@ function domain_admin($_action, $_data = null) {
); );
return false; return false;
} }
$stmt = $pdo->query("SELECT SUM(`quota`) AS `quota` FROM `domain`"); $stmt = $pdo->query("SELECT SUM(`quota`) AS `quota` FROM `domain` WHERE `active`=1");
$row = $stmt->fetch(PDO::FETCH_ASSOC); $row = $stmt->fetch(PDO::FETCH_ASSOC);
return $row['quota']; return $row['quota'];
break; break;

View File

@ -46,7 +46,7 @@ function mailbox($_action, $_type, $_data = null, $_extra = null) {
$stmt = $pdo->prepare("SELECT `domain` FROM `mailbox` WHERE `username` = :username"); $stmt = $pdo->prepare("SELECT `domain` FROM `mailbox` WHERE `username` = :username");
$stmt->execute(array(':username' => $_SESSION['mailcow_cc_username'])); $stmt->execute(array(':username' => $_SESSION['mailcow_cc_username']));
$domain = $stmt->fetch(PDO::FETCH_ASSOC)['domain']; $domain = $stmt->fetch(PDO::FETCH_ASSOC)['domain'];
$validity = strtotime("+".$_data["validity"]." hour"); $validity = strtotime("+".$_data["validity"]." hour");
$letters = 'abcefghijklmnopqrstuvwxyz1234567890'; $letters = 'abcefghijklmnopqrstuvwxyz1234567890';
$random_name = substr(str_shuffle($letters), 0, 24); $random_name = substr(str_shuffle($letters), 0, 24);
$stmt = $pdo->prepare("INSERT INTO `spamalias` (`address`, `goto`, `validity`) VALUES $stmt = $pdo->prepare("INSERT INTO `spamalias` (`address`, `goto`, `validity`) VALUES
@ -485,7 +485,7 @@ function mailbox($_action, $_type, $_data = null, $_extra = null) {
'msg' => 'comment_too_long' 'msg' => 'comment_too_long'
); );
return false; return false;
} }
if (empty($addresses[0])) { if (empty($addresses[0])) {
$_SESSION['return'][] = array( $_SESSION['return'][] = array(
'type' => 'danger', 'type' => 'danger',
@ -841,7 +841,7 @@ function mailbox($_action, $_type, $_data = null, $_extra = null) {
WHERE `domain` = :domain"); WHERE `domain` = :domain");
$stmt->execute(array(':domain' => $domain)); $stmt->execute(array(':domain' => $domain));
$DomainData = $stmt->fetch(PDO::FETCH_ASSOC); $DomainData = $stmt->fetch(PDO::FETCH_ASSOC);
$stmt = $pdo->prepare("SELECT $stmt = $pdo->prepare("SELECT
COUNT(*) as count, COUNT(*) as count,
COALESCE(ROUND(SUM(`quota`)/1048576), 0) as `quota` COALESCE(ROUND(SUM(`quota`)/1048576), 0) as `quota`
FROM `mailbox` FROM `mailbox`
@ -945,7 +945,7 @@ function mailbox($_action, $_type, $_data = null, $_extra = null) {
); );
return false; return false;
} }
$stmt = $pdo->prepare("INSERT INTO `mailbox` (`username`, `password`, `name`, `quota`, `local_part`, `domain`, `attributes`, `active`) $stmt = $pdo->prepare("INSERT INTO `mailbox` (`username`, `password`, `name`, `quota`, `local_part`, `domain`, `attributes`, `active`)
VALUES (:username, :password_hashed, :name, :quota_b, :local_part, :domain, :mailbox_attrs, :active)"); VALUES (:username, :password_hashed, :name, :quota_b, :local_part, :domain, :mailbox_attrs, :active)");
$stmt->execute(array( $stmt->execute(array(
':username' => $username, ':username' => $username,
@ -1073,7 +1073,7 @@ function mailbox($_action, $_type, $_data = null, $_extra = null) {
); );
return false; return false;
} }
$stmt = $pdo->prepare("INSERT INTO `mailbox` (`username`, `password`, `name`, `quota`, `local_part`, `domain`, `active`, `multiple_bookings`, `kind`) $stmt = $pdo->prepare("INSERT INTO `mailbox` (`username`, `password`, `name`, `quota`, `local_part`, `domain`, `active`, `multiple_bookings`, `kind`)
VALUES (:name, 'RESOURCE', :description, 0, :local_part, :domain, :active, :multiple_bookings, :kind)"); VALUES (:name, 'RESOURCE', :description, 0, :local_part, :domain, :active, :multiple_bookings, :kind)");
$stmt->execute(array( $stmt->execute(array(
':name' => $name, ':name' => $name,
@ -1249,7 +1249,7 @@ function mailbox($_action, $_type, $_data = null, $_extra = null) {
'msg' => 'access_denied' 'msg' => 'access_denied'
); );
continue; continue;
} }
$stmt = $pdo->prepare("UPDATE `mailbox` $stmt = $pdo->prepare("UPDATE `mailbox`
SET `attributes` = JSON_SET(`attributes`, '$.quarantine_notification', :quarantine_notification) SET `attributes` = JSON_SET(`attributes`, '$.quarantine_notification', :quarantine_notification)
WHERE `username` = :username"); WHERE `username` = :username");
@ -1360,7 +1360,7 @@ function mailbox($_action, $_type, $_data = null, $_extra = null) {
continue; continue;
} }
$validity = round((int)time() + ($_data['validity'] * 3600)); $validity = round((int)time() + ($_data['validity'] * 3600));
$stmt = $pdo->prepare("UPDATE `spamalias` SET `validity` = :validity WHERE $stmt = $pdo->prepare("UPDATE `spamalias` SET `validity` = :validity WHERE
`address` = :address"); `address` = :address");
$stmt->execute(array( $stmt->execute(array(
':address' => $address, ':address' => $address,
@ -1888,7 +1888,7 @@ function mailbox($_action, $_type, $_data = null, $_extra = null) {
); );
continue; continue;
} }
$stmt = $pdo->prepare("UPDATE `domain` SET $stmt = $pdo->prepare("UPDATE `domain` SET
`description` = :description, `description` = :description,
`gal` = :gal `gal` = :gal
WHERE `domain` = :domain"); WHERE `domain` = :domain");
@ -1928,7 +1928,7 @@ function mailbox($_action, $_type, $_data = null, $_extra = null) {
continue; continue;
} }
// todo: should be using api here // todo: should be using api here
$stmt = $pdo->prepare("SELECT $stmt = $pdo->prepare("SELECT
COUNT(*) AS count, COUNT(*) AS count,
MAX(COALESCE(ROUND(`quota`/1048576), 0)) AS `biggest_mailbox`, MAX(COALESCE(ROUND(`quota`/1048576), 0)) AS `biggest_mailbox`,
COALESCE(ROUND(SUM(`quota`)/1048576), 0) AS `quota_all` COALESCE(ROUND(SUM(`quota`)/1048576), 0) AS `quota_all`
@ -2009,7 +2009,7 @@ function mailbox($_action, $_type, $_data = null, $_extra = null) {
); );
continue; continue;
} }
$stmt = $pdo->prepare("UPDATE `domain` SET $stmt = $pdo->prepare("UPDATE `domain` SET
`relay_all_recipients` = :relay_all_recipients, `relay_all_recipients` = :relay_all_recipients,
`backupmx` = :backupmx, `backupmx` = :backupmx,
`gal` = :gal, `gal` = :gal,
@ -2071,7 +2071,7 @@ function mailbox($_action, $_type, $_data = null, $_extra = null) {
$domain = $is_now['domain']; $domain = $is_now['domain'];
$quota_b = $quota_m * 1048576; $quota_b = $quota_m * 1048576;
$password = (!empty($_data['password'])) ? $_data['password'] : null; $password = (!empty($_data['password'])) ? $_data['password'] : null;
$password2 = (!empty($_data['password2'])) ? $_data['password2'] : null; $password2 = (!empty($_data['password2'])) ? $_data['password2'] : null;
} }
else { else {
$_SESSION['return'][] = array( $_SESSION['return'][] = array(
@ -2517,14 +2517,14 @@ function mailbox($_action, $_type, $_data = null, $_extra = null) {
} }
$stmt = $pdo->prepare("SELECT `domain` FROM `domain` $stmt = $pdo->prepare("SELECT `domain` FROM `domain`
WHERE `domain` NOT IN ( WHERE `domain` NOT IN (
SELECT REPLACE(`send_as`, '@', '') FROM `sender_acl` SELECT REPLACE(`send_as`, '@', '') FROM `sender_acl`
WHERE `logged_in_as` = :logged_in_as1 WHERE `logged_in_as` = :logged_in_as1
AND `external` = '0' AND `external` = '0'
AND `send_as` LIKE '@%') AND `send_as` LIKE '@%')
UNION UNION
SELECT '*' FROM `domain` SELECT '*' FROM `domain`
WHERE '*' NOT IN ( WHERE '*' NOT IN (
SELECT `send_as` FROM `sender_acl` SELECT `send_as` FROM `sender_acl`
WHERE `logged_in_as` = :logged_in_as2 WHERE `logged_in_as` = :logged_in_as2
AND `external` = '0' AND `external` = '0'
)"); )");
@ -2546,7 +2546,7 @@ function mailbox($_action, $_type, $_data = null, $_extra = null) {
$stmt = $pdo->prepare("SELECT `address` FROM `alias` $stmt = $pdo->prepare("SELECT `address` FROM `alias`
WHERE `goto` != :goto WHERE `goto` != :goto
AND `address` NOT IN ( AND `address` NOT IN (
SELECT `send_as` FROM `sender_acl` SELECT `send_as` FROM `sender_acl`
WHERE `logged_in_as` = :logged_in_as WHERE `logged_in_as` = :logged_in_as
AND `external` = '0' AND `external` = '0'
AND `send_as` NOT LIKE '@%')"); AND `send_as` NOT LIKE '@%')");
@ -3074,11 +3074,11 @@ function mailbox($_action, $_type, $_data = null, $_extra = null) {
if (!empty($row)) { if (!empty($row)) {
$_data = $row['target_domain']; $_data = $row['target_domain'];
} }
$stmt = $pdo->prepare("SELECT $stmt = $pdo->prepare("SELECT
`domain`, `domain`,
`description`, `description`,
`aliases`, `aliases`,
`mailboxes`, `mailboxes`,
`defquota`, `defquota`,
`maxquota`, `maxquota`,
`quota`, `quota`,
@ -3096,7 +3096,7 @@ function mailbox($_action, $_type, $_data = null, $_extra = null) {
':domain' => $_data ':domain' => $_data
)); ));
$row = $stmt->fetch(PDO::FETCH_ASSOC); $row = $stmt->fetch(PDO::FETCH_ASSOC);
if (empty($row)) { if (empty($row)) {
return false; return false;
} }
$stmt = $pdo->prepare("SELECT COUNT(*) AS `count`, $stmt = $pdo->prepare("SELECT COUNT(*) AS `count`,
@ -3254,6 +3254,19 @@ function mailbox($_action, $_type, $_data = null, $_extra = null) {
} }
return $resourcedata; return $resourcedata;
break; break;
case 'inactive_bytes':
if ($_SESSION['mailcow_cc_role'] != "admin") {
return false;
}
$stmt = $pdo->query("SELECT SUM(`quota2`.`bytes`) AS `bytes`
FROM `mailbox`, `quota2`, `domain`
WHERE `mailbox`.`kind` NOT REGEXP 'location|thing|group'
AND `mailbox`.`username` = `quota2`.`username`
AND `domain`.`domain` = `mailbox`.`domain`
AND `domain`.`active` = 0");
$row = $stmt->fetch(PDO::FETCH_ASSOC);
return $row['bytes'];
break;
} }
break; break;
case 'delete': case 'delete':
@ -3724,7 +3737,7 @@ function mailbox($_action, $_type, $_data = null, $_extra = null) {
curl_setopt($curl, CURLOPT_HTTPHEADER,array('Content-Type: text/xml')); curl_setopt($curl, CURLOPT_HTTPHEADER,array('Content-Type: text/xml'));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, '<delete><query>user:' . $username . '</query></delete>'); curl_setopt($curl, CURLOPT_POSTFIELDS, '<delete><query>user:' . $username . '</query></delete>');
curl_setopt($curl, CURLOPT_TIMEOUT, 30); curl_setopt($curl, CURLOPT_TIMEOUT, 30);
$response = curl_exec($curl); $response = curl_exec($curl);
if ($response === false) { if ($response === false) {