From 5e0d4e8dc7339d41edf13002c33cc4bdc4773452 Mon Sep 17 00:00:00 2001 From: quenenni Date: Thu, 17 Aug 2017 21:32:21 +0200 Subject: [PATCH] =?UTF-8?q?syst=C3=A8me=20de=20quota=20-=20quota=20Vs=20du?= =?UTF-8?q?.pl=20+=20affichage=20des=20quotas=20pour=20les=20utilisateurs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bureau/admin/quota_show.php | 18 +++++++++++----- bureau/class/m_cron.php | 2 +- bureau/class/m_ftp.php | 2 +- bureau/class/m_mail.php | 3 ++- bureau/class/m_mysql.php | 3 ++- bureau/class/m_quota.php | 42 ++++++++++++++++++++++++++++++------- debian/alternc.cron.d | 5 ++++- src/spoolsize.php | 15 ++++++++----- src/update_quota_mail.sh | 5 +++-- 9 files changed, 70 insertions(+), 25 deletions(-) diff --git a/bureau/admin/quota_show.php b/bureau/admin/quota_show.php index 0d1be01e..ea747ddc 100644 --- a/bureau/admin/quota_show.php +++ b/bureau/admin/quota_show.php @@ -43,10 +43,11 @@ if (!is_array($q) || empty($q) ) { } echo ""; -echo ""; +echo ""; $qlist=$quota->qlist(); reset($qlist); - while (list($key,$val)=each($qlist)) { +$totalsize = 0; +while (list($key,$val)=each($qlist)) { if ( !isset($q[$key]) || !$q[$key]["t"]) continue; echo ""; echo ""; + echo " "; } else { echo " "; } - echo ""; - } + if (isset($q[$key]['s'])) { + $totalsize += $q[$key]["s"]; + echo ""; + } else { + echo ""; + } + echo ""; +} +echo ""; echo "
"._("Quota").""._("Used").""._("Total")."
"._("Quota").""._("Used").""._("Total").""._("Size on disk")."
"; @@ -55,13 +56,20 @@ reset($qlist); if ($q[$key]["u"] >= $q[$key]["t"]) echo ""; if (($key == 'web')||(isset($q[$key]['type'])&&($q[$key]['type']=='size'))) { - echo " ". format_size($q[$key]["u"] * 1024) . " ". format_size($q[$key]["t"] * 1024) ." ". format_size($q[$key]["u"] * 1024) . "  ".$q[$key]["u"]." ".$q[$key]["t"]." 
". format_size($q[$key]["s"] * 1024) . " 
"._("Total").": ".format_size($totalsize * 1024)." / ".format_size($q['web']["t"] * 1024)."
"; include_once("foot.php"); diff --git a/bureau/class/m_cron.php b/bureau/class/m_cron.php index bba8214d..ef0b1242 100644 --- a/bureau/class/m_cron.php +++ b/bureau/class/m_cron.php @@ -77,7 +77,7 @@ class m_cron { 'title' => _("Scheduled tasks"), 'ico' => 'images/schedule.png', 'link' => 'cron.php', - 'pos' => 90, + 'pos' => 120, ); return $obj; diff --git a/bureau/class/m_ftp.php b/bureau/class/m_ftp.php index ed119c67..5425ff6f 100644 --- a/bureau/class/m_ftp.php +++ b/bureau/class/m_ftp.php @@ -58,7 +58,7 @@ class m_ftp { 'title' => _("FTP accounts"), 'ico' => 'images/ftp.png', 'link' => 'toggle', - 'pos' => 60, + 'pos' => 100, 'links' => array(), ); diff --git a/bureau/class/m_mail.php b/bureau/class/m_mail.php index ccfb9c69..24dabe39 100644 --- a/bureau/class/m_mail.php +++ b/bureau/class/m_mail.php @@ -200,12 +200,13 @@ class m_mail { * or false if I'm not the one for the named quota */ function hook_quota_get() { - global $db, $msg, $cuid; + global $db, $msg, $cuid, $quota; $msg->log("mail", "getquota"); $q = Array("name" => "mail", "description" => _("Email addresses"), "used" => 0); $db->query("SELECT COUNT(*) AS cnt FROM address a, domaines d WHERE a.domain_id=d.id AND d.compte= ? AND a.type='';", array($cuid)); if ($db->next_record()) { $q['used'] = $db->f("cnt"); + $q['sizeondisk'] = $quota->get_size_mail_sum_user($cuid)/1024; } return $q; } diff --git a/bureau/class/m_mysql.php b/bureau/class/m_mysql.php index b409e5e6..cd5687d8 100644 --- a/bureau/class/m_mysql.php +++ b/bureau/class/m_mysql.php @@ -942,12 +942,13 @@ class m_mysql { * @access private */ function hook_quota_get() { - global $msg; + global $msg, $mem, $quota; $msg->log("mysql", "alternc_get_quota"); $q = Array("name" => "mysql", "description" => _("MySQL Databases"), "used" => 0); $c = $this->get_dblist(); if (is_array($c)) { $q['used'] = count($c); + $q['sizeondisk'] = $quota->get_size_db_sum_user($mem->user["login"])/1024; } return $q; } diff --git a/bureau/class/m_quota.php b/bureau/class/m_quota.php index e229a46a..e2d2139f 100644 --- a/bureau/class/m_quota.php +++ b/bureau/class/m_quota.php @@ -40,6 +40,7 @@ class m_quota { var $disk = Array(); /* disk resource for which we will manage quotas */ var $disk_quota_enable; + var $disk_quota_not_blocking; var $quotas; var $clquota; // Which class manage which quota. @@ -52,6 +53,8 @@ class m_quota { $this->disk_quota_enable = variable_get('disk_quota_enable', 1, 'Are disk quota enabled for this server', array('desc' => 'Enabled', 'type' => 'boolean')); if ($this->disk_quota_enable) { $this->disk = Array("web" => "web"); + + $this->disk_quota_not_blocking = variable_get('disk_quota_not_blocking', 1, "0 - Block data when quota are exceeded (you need a working quota system) | 1 - Just show quota but don't block anything", array('desc' => 'Enabled', 'type' => 'boolean')); } } @@ -60,11 +63,13 @@ class m_quota { } function hook_menu() { + global $cuid, $mem, $quota; + $obj = array( 'title' => _("Show my quotas"), 'ico' => 'images/quota.png', 'link' => 'toggle', - 'pos' => 110, + 'pos' => 5, 'divclass' => 'menu-quota', 'links' => array(), ); @@ -77,9 +82,10 @@ class m_quota { continue; } - $usage_percent = (int) ($q[$key]["u"] / $q[$key]["t"] * 100); + $totalsize_used = $quota->get_size_web_sum_user($cuid) + $quota->get_size_mailman_sum_user($cuid) + ($quota->get_size_db_sum_user($mem->user["login"]) + $quota->get_size_mail_sum_user($cuid))/1024; + $usage_percent = (int) ($totalsize_used / $q[$key]["t"] * 100); $obj['links'][] = array('txt' => _("quota_" . $key) . " " . sprintf(_("%s%% of %s"), $usage_percent, format_size($q[$key]["t"] * 1024)), 'url' => 'quota_show.php'); - $obj['links'][] = array('txt' => 'progressbar', 'total' => $q[$key]["t"], 'used' => $q[$key]["u"]); + $obj['links'][] = array('txt' => 'progressbar', 'total' => $q[$key]["t"], 'used' => $totalsize_used); } // do not return menu item if there is no quota @@ -186,6 +192,8 @@ class m_quota { foreach ($res as $r) { $this->quotas[$r['name']] = $r; $this->quotas[$r['name']]['u'] = $r['used']; // retrocompatibilité + if (isset($r['sizeondisk'])) + $this->quotas[$r['name']]['s'] = $r['sizeondisk']; $this->quotas[$r['name']]['t'] = 0; // Default quota = 0 } reset($this->disk); @@ -202,14 +210,20 @@ class m_quota { // If there is a cached value $a = $disk_cached[$val]; } else { - exec("/usr/lib/alternc/quota_get " . intval($cuid), $ak); - $a['u'] = intval($ak[0]); - $a['t'] = @intval($ak[1]); + if ($this->disk_quota_not_blocking) { + $a['u'] = $this->get_size_web_sum_user($cuid); + $a['t'] = $this->get_quota_user_cat($cuid, 'web'); + } else { + exec("/usr/lib/alternc/quota_get " . intval($cuid), $ak); + $a['u'] = intval($ak[0]); + $a['t'] = @intval($ak[1]); + } + $a['sizeondisk'] = $a['u']; $a['timestamp'] = time(); $a['uid'] = $cuid; $disk_cached = $mem->session_tempo_params_set('quota_cache_disk', array($val => $a)); } - $this->quotas[$val] = array("name" => "$val", 'description' => _("quota_" . $val), "t" => $a['t'], "u" => $a['u']); + $this->quotas[$val] = array("name" => "$val", 'description' => _("Web disk space"), "s" => $a['sizeondisk'], "t" => $a['t'], "u" => $a['u']); } } @@ -245,7 +259,7 @@ class m_quota { if (floatval($size) == 0) { $size = "0"; } - if (isset($this->disk[$ressource])) { + if (!$this->disk_quota_not_blocking && isset($this->disk[$ressource])) { // It's a disk resource, update it with shell command exec("sudo /usr/lib/alternc/quota_edit " . intval($cuid) . " " . intval($size) . " &> /dev/null &"); // Now we check that the value has been written properly : @@ -463,6 +477,12 @@ class m_quota { } } + /* get the quota from one user for a cat */ + + function get_quota_user_cat($uid, $name) { + return $this->_get_sum_sql("SELECT SUM(total) AS sum FROM quotas WHERE uid='$uid' AND name='$name';"); + } + /* sum of websites sizes from all users */ function get_size_web_sum_all() { @@ -488,6 +508,12 @@ class m_quota { return $mail->get_total_size_for_domain($dom); } + /* sum of mailbox size for ine user */ + + function get_size_mail_sum_user($u) { + return $this->_get_sum_sql("SELECT SUM(quota_dovecot) as sum FROM dovecot_quota WHERE user IN (SELECT CONCAT(a.address, '@', d.domaine) as mail FROM `address` as a INNER JOIN domaines as d ON a.domain_id = d.id WHERE d.compte = '$u' AND a.type ='')"); + } + /* count of mailbox sizes from all domains */ function get_size_mail_count_all() { diff --git a/debian/alternc.cron.d b/debian/alternc.cron.d index c67056f5..c14b5eda 100644 --- a/debian/alternc.cron.d +++ b/debian/alternc.cron.d @@ -21,7 +21,7 @@ # Every hour, check for slave_dns refreshes 5 * * * * root /usr/lib/alternc/slave_dns -# Every day at 2am, compute web, mail and db space usage per account. +# Every day at 2am, compute web, mailman and db space usage per account. # You may put this computing every week only or on your filer on busy services. 0 2 * * * alterncpanel /usr/lib/alternc/spoolsize.php 2>&1 > /dev/null @@ -34,3 +34,6 @@ # Every 20 minutes, do actions */20 * * * * root /usr/lib/alternc/do_actions.php +# Calculate the mail accounts size once a week beacause the dovecot plugin to do that is not precise (see ticket AlternC #168) +# Every Sunday at 4am +0 4 * * 0 root /usr/lib/alternc/update_quota_mail.sh -a diff --git a/src/spoolsize.php b/src/spoolsize.php index 1574e29d..87caa26c 100644 --- a/src/spoolsize.php +++ b/src/spoolsize.php @@ -18,9 +18,14 @@ if ($db->query("SELECT uid,login FROM membres;")) { while ($db->next_record()) { if (isset($list_quota[$db->f('uid')])) { $qu=$list_quota[$db->f('uid')]; - $db2->query("REPLACE INTO size_web SET uid=?, size=?;",array(intval($db->f('uid')),intval($qu['used']))); - echo $db->f('login')." (".$qu['used']." B)\n"; - } + $size=$qu['used']; + } else { + // Le système de quota n'étant pas actif, on doit passer par un 'du' sur chaque dossier + $login = $db->f('login'); + $size=exec("/usr/lib/alternc/du.pl /var/www/alternc/".substr($login,0,1)."/".$login); + } + $db2->query("REPLACE INTO size_web SET uid=?, size=?;",array(intval($db->f('uid')),intval($size))); + echo $db->f('login')." (".(round($size/1024, 1))." MB)\n"; } } @@ -38,7 +43,7 @@ foreach($allsrv as $c) { echo "++ Processing ".$c["name"]." ++\n"; foreach ($tab as $dbname=>$size) { $db->query("REPLACE INTO size_db SET db=?,size=?;",array($dbname,$size)); - echo " $dbname done ($size B) \n"; flush(); + echo " $dbname done (".(round(($size/1024)/1024,1))." MB) \n"; flush(); } echo "\n"; } @@ -58,7 +63,7 @@ if ($db->query("SELECT uid, name FROM mailman;")) { $size3=exec("sudo /usr/lib/alternc/du.pl ".escapeshellarg("/var/lib/mailman/archives/private/".$c["name"].".mbox")); $size=(intval($size1)+intval($size2)+intval($size3)); $db->query("REPLACE INTO size_mailman SET uid=?,list=?,size=?;",array($c["uid"],$c["name"],$size)); - echo " done ($size KB) \n"; flush(); + echo " done (".(round($size/1024, 1))." MB) \n"; flush(); } } } diff --git a/src/update_quota_mail.sh b/src/update_quota_mail.sh index c774119c..6830f536 100755 --- a/src/update_quota_mail.sh +++ b/src/update_quota_mail.sh @@ -108,8 +108,9 @@ for i in $maildirs ; do echo "dir size : "$size echo "" #update the mailbox table accordingly - mysql_query "UPDATE mailbox SET bytes=$size WHERE path='$i' ; " - mysql_query "UPDATE mailbox SET messages=$mail_count WHERE path='$i' ; " + MAILADD=`basename $i` + MAILADD=${MAILADD/_/@} + mysql_query "REPLACE INTO dovecot_quota VALUES('$MAILADD', $size, $mail_count);" done # may cause a problem, let's fix this here :)