Have some default values if a command fails to run

feature/node-rewrite
Sven Slootweg 12 years ago
parent 5b15b798ed
commit 6824a7db84

@ -98,23 +98,30 @@ class Container extends CPHPDatabaseRecordClass
} }
else else
{ {
$command = array("vzctl", "status", $this->sInternalId); try
$result = $this->sNode->ssh->RunCommandCached($command, false);
if($result->returncode == 0)
{ {
$values = split_whitespace($result->stdout); $command = array("vzctl", "status", $this->sInternalId);
if($values[4] == "running") $result = $this->sNode->ssh->RunCommandCached($command, false);
{
return CVM_STATUS_STARTED; if($result->returncode == 0)
}
else
{ {
return CVM_STATUS_STOPPED; $values = split_whitespace($result->stdout);
if($values[4] == "running")
{
return CVM_STATUS_STARTED;
}
else
{
return CVM_STATUS_STOPPED;
}
} }
} }
catch (SshCommandException $e)
{
return CVM_STATUS_STOPPED;
}
} }
} }
@ -154,26 +161,35 @@ class Container extends CPHPDatabaseRecordClass
public function GetRam() public function GetRam()
{ {
$result = $this->RunCommandCached("free -m", true); try
$lines = explode("\n", $result->stdout);
array_shift($lines);
$total_free = 0;
$total_used = 0;
$total_total = 0;
foreach($lines as $line)
{ {
$line = trim($line); $result = $this->RunCommandCached("free -m", true);
$values = split_whitespace($line); $lines = explode("\n", $result->stdout);
array_shift($lines);
if(trim($values[0]) == "Mem:") $total_free = 0;
$total_used = 0;
$total_total = 0;
foreach($lines as $line)
{ {
$total_total = $values[1]; $line = trim($line);
$total_used = $values[2]; $values = split_whitespace($line);
$total_free = $values[3];
if(trim($values[0]) == "Mem:")
{
$total_total = $values[1];
$total_used = $values[2];
$total_free = $values[3];
}
} }
}
catch (SshCommandException $e)
{
$total_free = 0;
$total_used = 0;
$total_total = 0;
} }
return array( return array(
@ -197,26 +213,35 @@ class Container extends CPHPDatabaseRecordClass
public function GetDisk() public function GetDisk()
{ {
$result = $this->RunCommandCached("df -l -x tmpfs", true); try
$lines = explode("\n", $result->stdout);
array_shift($lines);
$total_free = 0;
$total_used = 0;
$total_total = 0;
foreach($lines as $disk)
{ {
$disk = trim($disk); $result = $this->RunCommandCached("df -l -x tmpfs", true);
$lines = explode("\n", $result->stdout);
array_shift($lines);
$total_free = 0;
$total_used = 0;
$total_total = 0;
if(!empty($disk)) foreach($lines as $disk)
{ {
$values = split_whitespace($disk); $disk = trim($disk);
$total_free += (int)$values[3] / 1024;
$total_used += (int)$values[2] / 1024; if(!empty($disk))
$total_total += ((int)$values[2] + (int)$values[3]) / 1024; {
$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;
}
} }
} }
catch (SshCommandException $e)
{
$total_free = 0;
$total_used = 0;
$total_total = 0;
}
return array( return array(
'free' => $total_free, 'free' => $total_free,

@ -44,7 +44,7 @@ class SshConnector extends CPHPBaseClass
catch (SshConnectException $e) catch (SshConnectException $e)
{ {
$error = $e->getMessage(); $error = $e->getMessage();
throw new SshCommandException("Could not run command {$command}: Failed to connect: {$error}"); throw new SshConnectException("Could not run command {$command}: Failed to connect: {$error}");
} }
} }
@ -68,7 +68,7 @@ class SshConnector extends CPHPBaseClass
'hostkey' => $this->keytype 'hostkey' => $this->keytype
); );
if($this->connection = ssh2_connect($this->host, $this->port, $options)) if($this->connection = @ssh2_connect($this->host, $this->port, $options))
{ {
$this->connected = true; $this->connected = true;

@ -61,7 +61,7 @@ try
$sVariables = array_merge($sVariables, array( $sVariables = array_merge($sVariables, array(
'disk-used' => number_format($sContainer->sDiskUsed / 1024, 2), 'disk-used' => number_format($sContainer->sDiskUsed / 1024, 2),
'disk-total' => number_format($sContainer->sDiskTotal / 1024, 2), 'disk-total' => number_format($sContainer->sDiskTotal / 1024, 2),
'disk-percentage' => number_format(($sContainer->sDiskUsed / $sContainer->sDiskTotal) * 100, 2), 'disk-percentage' => ($sContainer->sDiskTotal == 0) ? 0 : number_format(($sContainer->sDiskUsed / $sContainer->sDiskTotal) * 100, 2),
'disk-unit' => "GB" 'disk-unit' => "GB"
)); ));
} }
@ -80,7 +80,7 @@ try
$sVariables = array_merge($sVariables, array( $sVariables = array_merge($sVariables, array(
'ram-used' => $sContainer->sRamUsed, 'ram-used' => $sContainer->sRamUsed,
'ram-total' => $sContainer->sRamTotal, 'ram-total' => $sContainer->sRamTotal,
'ram-percentage' => number_format(($sContainer->sRamUsed / $sContainer->sRamTotal) * 100, 2), 'ram-percentage' => ($sContainer->sRamTotal == 0) ? 0 : number_format(($sContainer->sRamUsed / $sContainer->sRamTotal) * 100, 2),
'ram-unit' => "MB" 'ram-unit' => "MB"
)); ));
} }

Loading…
Cancel
Save