finalizing disk usage rework

- changed dockerapi to not return human readable values for df
- all caluclations now in KiB
- moved code from domain_admin to mailbox functions
- using actual occupied bytes as used quota
- all outputs now in *iB using formatBytes()
master
heavygale 2019-10-01 20:27:06 +02:00
parent 7c2a5ba37e
commit ecda554e53
4 changed files with 28 additions and 36 deletions

View File

@ -64,11 +64,11 @@ class container_post(Resource):
print("api call: %s, container_id: %s" % (api_call_method_name, container_id)) print("api call: %s, container_id: %s" % (api_call_method_name, container_id))
return api_call_method(container_id) return api_call_method(container_id)
except Exception as e: except Exception as e:
print("error - container_post: %s" % str(e)) print("error - container_post: %s" % str(e))
return jsonify(type='danger', msg=str(e)) return jsonify(type='danger', msg=str(e))
else: else:
return jsonify(type='danger', msg='invalid container id or missing action') return jsonify(type='danger', msg='invalid container id or missing action')
# api call: container_post - post_action: stop # api call: container_post - post_action: stop
@ -107,7 +107,7 @@ class container_post(Resource):
# api call: container_post - post_action: exec - cmd: mailq - task: delete # api call: container_post - post_action: exec - cmd: mailq - task: delete
def container_post__exec__mailq__delete(self, container_id): def container_post__exec__mailq__delete(self, container_id):
if 'items' in request.json: if 'items' in request.json:
r = re.compile("^[0-9a-fA-F]+$") r = re.compile("^[0-9a-fA-F]+$")
filtered_qids = filter(r.match, request.json['items']) filtered_qids = filter(r.match, request.json['items'])
if filtered_qids: if filtered_qids:
@ -121,7 +121,7 @@ class container_post(Resource):
# api call: container_post - post_action: exec - cmd: mailq - task: hold # api call: container_post - post_action: exec - cmd: mailq - task: hold
def container_post__exec__mailq__hold(self, container_id): def container_post__exec__mailq__hold(self, container_id):
if 'items' in request.json: if 'items' in request.json:
r = re.compile("^[0-9a-fA-F]+$") r = re.compile("^[0-9a-fA-F]+$")
filtered_qids = filter(r.match, request.json['items']) filtered_qids = filter(r.match, request.json['items'])
if filtered_qids: if filtered_qids:
@ -135,7 +135,7 @@ class container_post(Resource):
# api call: container_post - post_action: exec - cmd: mailq - task: unhold # api call: container_post - post_action: exec - cmd: mailq - task: unhold
def container_post__exec__mailq__unhold(self, container_id): def container_post__exec__mailq__unhold(self, container_id):
if 'items' in request.json: if 'items' in request.json:
r = re.compile("^[0-9a-fA-F]+$") r = re.compile("^[0-9a-fA-F]+$")
filtered_qids = filter(r.match, request.json['items']) filtered_qids = filter(r.match, request.json['items'])
if filtered_qids: if filtered_qids:
@ -149,7 +149,7 @@ class container_post(Resource):
# api call: container_post - post_action: exec - cmd: mailq - task: deliver # api call: container_post - post_action: exec - cmd: mailq - task: deliver
def container_post__exec__mailq__deliver(self, container_id): def container_post__exec__mailq__deliver(self, container_id):
if 'items' in request.json: if 'items' in request.json:
r = re.compile("^[0-9a-fA-F]+$") r = re.compile("^[0-9a-fA-F]+$")
filtered_qids = filter(r.match, request.json['items']) filtered_qids = filter(r.match, request.json['items'])
if filtered_qids: if filtered_qids:
@ -206,7 +206,7 @@ class container_post(Resource):
def container_post__exec__system__df(self, container_id): def container_post__exec__system__df(self, container_id):
if 'dir' in request.json: if 'dir' in request.json:
for container in docker_client.containers.list(filters={"id": container_id}): for container in docker_client.containers.list(filters={"id": container_id}):
df_return = container.exec_run(["/bin/bash", "-c", "/bin/df -H '" + request.json['dir'].replace("'", "'\\''") + "' | /usr/bin/tail -n1 | /usr/bin/tr -s [:blank:] | /usr/bin/tr ' ' ','"], user='nobody') df_return = container.exec_run(["/bin/bash", "-c", "/bin/df '" + request.json['dir'].replace("'", "'\\''") + "' | /usr/bin/tail -n1 | /usr/bin/tr -s [:blank:] | /usr/bin/tr ' ' ','"], user='nobody')
if df_return.exit_code == 0: if df_return.exit_code == 0:
return df_return.output.decode('utf-8').rstrip() return df_return.output.decode('utf-8').rstrip()
else: else:
@ -264,7 +264,7 @@ class container_post(Resource):
def container_post__exec__sieve__print(self, container_id): def container_post__exec__sieve__print(self, container_id):
if 'username' in request.json and 'script_name' in request.json: if 'username' in request.json and 'script_name' in request.json:
for container in docker_client.containers.list(filters={"id": container_id}): for container in docker_client.containers.list(filters={"id": container_id}):
cmd = ["/bin/bash", "-c", "/usr/bin/doveadm sieve get -u '" + request.json['username'].replace("'", "'\\''") + "' '" + request.json['script_name'].replace("'", "'\\''") + "'"] cmd = ["/bin/bash", "-c", "/usr/bin/doveadm sieve get -u '" + request.json['username'].replace("'", "'\\''") + "' '" + request.json['script_name'].replace("'", "'\\''") + "'"]
sieve_return = container.exec_run(cmd) sieve_return = container.exec_run(cmd)
return exec_run_handler('utf8_text_only', sieve_return) return exec_run_handler('utf8_text_only', sieve_return)
@ -286,7 +286,7 @@ class container_post(Resource):
for container in docker_client.containers.list(filters={"id": container_id}): for container in docker_client.containers.list(filters={"id": container_id}):
cmd = "/usr/bin/rspamadm pw -e -p '" + request.json['raw'].replace("'", "'\\''") + "' 2> /dev/null" cmd = "/usr/bin/rspamadm pw -e -p '" + request.json['raw'].replace("'", "'\\''") + "' 2> /dev/null"
cmd_response = exec_cmd_container(container, cmd, user="_rspamd") cmd_response = exec_cmd_container(container, cmd, user="_rspamd")
matched = False matched = False
for line in cmd_response.split("\n"): for line in cmd_response.split("\n"):
if '$2$' in line: if '$2$' in line:
@ -306,7 +306,7 @@ class container_post(Resource):
return jsonify(type='success', msg='command completed successfully') return jsonify(type='success', msg='command completed successfully')
else: else:
return jsonify(type='danger', msg='command did not complete') return jsonify(type='danger', msg='command did not complete')
def exec_cmd_container(container, cmd, user, timeout=2, shell_cmd="/bin/bash"): def exec_cmd_container(container, cmd, user, timeout=2, shell_cmd="/bin/bash"):
@ -333,7 +333,7 @@ def exec_cmd_container(container, cmd, user, timeout=2, shell_cmd="/bin/bash"):
except: except:
pass pass
return ''.join(total_data) return ''.join(total_data)
try : try :
socket = container.exec_run([shell_cmd], stdin=True, socket=True, user=user).output._sock socket = container.exec_run([shell_cmd], stdin=True, socket=True, user=user).output._sock
if not cmd.endswith("\n"): if not cmd.endswith("\n"):

View File

@ -43,11 +43,11 @@ else {
<?php <?php
$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); $used_percent = substr($vmail_df[4], 0, -1);
$inactive_bytes = round(mailbox('get','inactive_bytes')/1024/1024/1024); $quota_stats = mailbox('get','quota_stats');
$quotaPercent1 = round(($domainQuota/substr($vmail_df[3], 0, -1))*100); $quotaPercent1 = round(($quota_stats['total_quota']/$vmail_df[3])*100);
$quotaPercent2 = round((($domainQuota-substr($vmail_df[2], 0, -1)+$inactive_bytes)/substr($vmail_df[3], 0, -1))*100); $quotaPercent2 = round((($quota_stats['total_quota']-$quota_stats['used_bytes'])/$vmail_df[3])*100);
$quotaPercent2 = ($quotaPercent2+substr($vmail_df[4], 0, -1)>100) ? 100-substr($vmail_df[4], 0, -1) : $quotaPercent2; //handling overcommitment $quotaPercent2 = ($quotaPercent2+$used_percent>100) ? 100-$used_percent : $quotaPercent2;
?> ?>
<div role="tabpanel" class="tab-pane active" id="tab-containers"> <div role="tabpanel" class="tab-pane active" id="tab-containers">
<div class="panel panel-default"> <div class="panel panel-default">
@ -58,15 +58,15 @@ else {
<div class="row"> <div class="row">
<div class="col-sm-3"> <div class="col-sm-3">
<p>/var/vmail on <?=$vmail_df[0];?></p> <p>/var/vmail on <?=$vmail_df[0];?></p>
<p class="disk_space"><?=$lang['debug']['disk_space'];?> <?=$vmail_df[3];?>B</p> <p class="disk_space"><?=$lang['debug']['disk_space'];?> <?=formatBytes($vmail_df[3]*1024);?></p>
</div> </div>
<div class="col-sm-9"> <div class="col-sm-9">
<div class="progress"> <div class="progress">
<div class="progress-bar progress-bar-info" role="progressbar" style="width:<?=$vmail_df[4];?>"></div> <div class="progress-bar progress-bar-info" role="progressbar" style="width:<?=$used_percent;?>%"></div>
<div class="progress-bar progress-bar-committed" role="progressbar" style="width:<?=$quotaPercent2;?>%"></div> <div class="progress-bar progress-bar-committed" role="progressbar" style="width:<?=$quotaPercent2;?>%"></div>
</div> </div>
<p><span class="container-indicator label usage-info progress-bar-info">&nbsp;</span> <?=$lang['debug']['disk_used'];?> <?=$vmail_df[2];?>B (<?=$vmail_df[4];?>)</p> <p><span class="container-indicator label usage-info progress-bar-info">&nbsp;</span> <?=$lang['debug']['disk_used'];?> <?=formatBytes($vmail_df[2]*1024);?> (<?=$used_percent;?>%)</p>
<p><span class="container-indicator label usage-info progress-bar-committed">&nbsp;</span> <?=$lang['debug']['total_quota'];?> <?=$domainQuota;?>GB (<?=$quotaPercent1;?>%)</p> <p><span class="container-indicator label usage-info progress-bar-committed">&nbsp;</span> <?=$lang['debug']['total_quota'];?> <?=formatBytes($quota_stats['total_quota']*1024);?> (<?=$quotaPercent1;?>%)</p>
</div> </div>
</div> </div>
</div> </div>

View File

@ -444,18 +444,5 @@ function domain_admin($_action, $_data = null) {
return $domainadmindata; return $domainadmindata;
break; break;
case 'total_quota':
if ($_SESSION['mailcow_cc_role'] != "admin") {
$_SESSION['return'][] = array(
'type' => 'danger',
'log' => array(__FUNCTION__, $_action, $_data_log),
'msg' => 'access_denied'
);
return false;
}
$stmt = $pdo->query("SELECT SUM(`quota`) AS `quota` FROM `domain` WHERE `active`=1");
$row = $stmt->fetch(PDO::FETCH_ASSOC);
return $row['quota'];
break;
} }
} }

View File

@ -3254,7 +3254,8 @@ function mailbox($_action, $_type, $_data = null, $_extra = null) {
} }
return $resourcedata; return $resourcedata;
break; break;
case 'inactive_bytes': case 'quota_stats':
$quotadata = array();
if ($_SESSION['mailcow_cc_role'] != "admin") { if ($_SESSION['mailcow_cc_role'] != "admin") {
return false; return false;
} }
@ -3263,9 +3264,13 @@ function mailbox($_action, $_type, $_data = null, $_extra = null) {
WHERE `mailbox`.`kind` NOT REGEXP 'location|thing|group' WHERE `mailbox`.`kind` NOT REGEXP 'location|thing|group'
AND `mailbox`.`username` = `quota2`.`username` AND `mailbox`.`username` = `quota2`.`username`
AND `domain`.`domain` = `mailbox`.`domain` AND `domain`.`domain` = `mailbox`.`domain`
AND `domain`.`active` = 0"); AND `domain`.`active` = 1");
$row = $stmt->fetch(PDO::FETCH_ASSOC); $row = $stmt->fetch(PDO::FETCH_ASSOC);
return $row['bytes']; $quotadata['used_bytes'] = $row['bytes']/1024;
$stmt = $pdo->query("SELECT SUM(`quota`) AS `quota` FROM `domain` WHERE `active`=1");
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$quotadata['total_quota'] = $row['quota']*1024;
return $quotadata;
break; break;
} }
break; break;