From aeaaddd389507f19bd3da86fe44f74c96610ab9a Mon Sep 17 00:00:00 2001 From: Sven Slootweg Date: Mon, 7 May 2012 02:23:29 +0200 Subject: [PATCH] Added disk usage stats to VPS overview --- frontend/classes/class.container.php | 45 ++++++++++++++++++++++++++++ frontend/classes/class.node.php | 5 +++- frontend/module.vps.overview.php | 7 ++++- frontend/templates/vps.overview.tpl | 4 +-- 4 files changed, 57 insertions(+), 4 deletions(-) diff --git a/frontend/classes/class.container.php b/frontend/classes/class.container.php index f8cb128..254f483 100644 --- a/frontend/classes/class.container.php +++ b/frontend/classes/class.container.php @@ -64,6 +64,9 @@ class Container extends CPHPDatabaseRecordClass case "sDiskUsed": return $this->GetDiskUsed(); break; + case "sDiskTotal": + return $this->GetDiskTotal(); + break; case "sBandwidthUsed": return $this->GetBandwidthUsed(); break; @@ -127,6 +130,48 @@ class Container extends CPHPDatabaseRecordClass } } + public function GetDiskUsed() + { + $disk = $this->GetDisk(); + return $disk['used']; + } + + public function GetDiskTotal() + { + $disk = $this->GetDisk(); + return $disk['total']; + } + + public function GetDisk() + { + $result = $this->sNode->ssh->RunCommandCached("vzctl exec {$this->sInternalId} df -l -x tmpfs", true); + $lines = explode("\n", $result->stdout); + array_shift($lines); + + $total_free = 0; + $total_used = 0; + $total_total = 0; + + foreach($lines as $disk) + { + $disk = trim($disk); + + if(!empty($disk)) + { + $values = split_whitespace($disk); + $total_free += (int)$values[3] / 1024; + $total_used += (int)$values[2] / 1024; + $total_total += ((int)$values[2] + (int)$values[3]) / 1024; + } + } + + return array( + 'free' => $total_free, + 'used' => $total_used, + 'total' => $total_total + ); + } + public function Deploy($conf = array()) { $sRootPassword = random_string(20); diff --git a/frontend/classes/class.node.php b/frontend/classes/class.node.php index 34ea79f..bb3c901 100644 --- a/frontend/classes/class.node.php +++ b/frontend/classes/class.node.php @@ -104,6 +104,7 @@ class Node extends CPHPDatabaseRecordClass $total_free = 0; $total_used = 0; + $total_total = 0; foreach($lines as $disk) { @@ -114,12 +115,14 @@ class Node extends CPHPDatabaseRecordClass $values = split_whitespace($disk); $total_free += (int)$values[3] / 1024; $total_used += (int)$values[2] / 1024; + $total_total += ((int)$values[2] + (int)$values[3]) / 1024; } } return array( 'free' => $total_free, - 'used' => $total_used + 'used' => $total_used, + 'total' => $total_total ); } } diff --git a/frontend/module.vps.overview.php b/frontend/module.vps.overview.php index 4ac245e..554fbbf 100644 --- a/frontend/module.vps.overview.php +++ b/frontend/module.vps.overview.php @@ -38,6 +38,11 @@ $sPageContents = Templater::InlineRender("vps.overview", $locale->strings, array 'disk-space' => "{$sContainer->sDiskSpace}MB", 'total-traffic-limit' => "{$sContainer->sTotalTrafficLimit} bytes", 'bandwidth-limit' => "100mbit", - 'status' => $sContainer->sStatusText + 'status' => $sContainer->sStatusText, + 'disk-used' => number_format($sContainer->sDiskUsed / 1024, 2), + 'disk-total' => number_format($sContainer->sDiskTotal / 1024, 2), + 'disk-percentage' => number_format(($sContainer->sDiskUsed / $sContainer->sDiskTotal) * 100, 2), + 'disk-unit' => "GB" + )); ?> diff --git a/frontend/templates/vps.overview.tpl b/frontend/templates/vps.overview.tpl index ccfc750..8603b0d 100644 --- a/frontend/templates/vps.overview.tpl +++ b/frontend/templates/vps.overview.tpl @@ -5,8 +5,8 @@

Disk space

-
-
55/100GB
+
+
<%?disk-used>/<%?disk-total><%?disk-unit>