Skip to content

Commit

Permalink
Implement resource object counters and API to print the memory sizes …
Browse files Browse the repository at this point in the history
…and fix several memory leaks

Also, version in configure.ac has been changed to 0.4.2.

Signed-off-by: Michal Novotny <minovotn@redhat.com>
  • Loading branch information
Michal Novotny committed Jun 21, 2011
1 parent 31bca38 commit 3b9d910
Show file tree
Hide file tree
Showing 6 changed files with 528 additions and 66 deletions.
2 changes: 1 addition & 1 deletion configure.ac
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
AC_INIT([libvirt-php], [0.4.1], [http://libvirt.org])
AC_INIT([libvirt-php], [0.4.2], [http://libvirt.org])
AM_INIT_AUTOMAKE([-Wall -Werror])
AC_CONFIG_FILES([Makefile tools/Makefile src/Makefile tests/Makefile docs/Makefile])
AC_CHECK_LIB([virt], [virConnectOpen], [], AC_MSG_ERROR([You need libvirt to compile this module]))
Expand Down
10 changes: 8 additions & 2 deletions docs/downloads.html.in
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,16 @@
<h2>Official Releases</h2>

<p>
You can download the first official release, release 0.4.1, from:
You can download the second official release, release 0.4.2, from:
</p>

<pre><a href="http://libvirt.org/sources/php/libvirt-php-0.4.1.tar.gz">http://libvirt.org/sources/php/libvirt-php-0.4.1.tar.gz</a></pre>
<pre><a href="http://libvirt.org/sources/php/libvirt-php-0.4.2.tar.gz">http://libvirt.org/sources/php/libvirt-php-0.4.2.tar.gz</a></pre>

<p>
However if you prefer to download the first official release, release 0.4.1, please click here:
</p>

<a href="http://libvirt.org/sources/php/libvirt-php-0.4.1.tar.gz">http://libvirt.org/sources/php/libvirt-php-0.4.1.tar.gz</a>

<h2>GIT source repository</h2>

Expand Down
43 changes: 21 additions & 22 deletions examples/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -500,14 +500,14 @@
}
}

$dom = $lv->domain_get_info($domName);
$mem = number_format($dom[$domName]['memory'] / 1024, 2, '.', ' ').' MB';
$cpu = $dom[$domName]['nrVirtCpu'];
$state = $lv->domain_state_translate($dom[$domName]['state']);
$domr = $lv->get_domain_object($domName);
$id = $lv->domain_get_id($domr);
$arch = $lv->domain_get_arch($domr);
$vnc = $lv->domain_get_vnc_port($domr);
$res = $lv->get_domain_object($domName);
$dom = $lv->domain_get_info($res);
$mem = number_format($dom['memory'] / 1024, 2, '.', ' ').' MB';
$cpu = $dom['nrVirtCpu'];
$state = $lv->domain_state_translate($dom['state']);
$id = $lv->domain_get_id($res);
$arch = $lv->domain_get_arch($res);
$vnc = $lv->domain_get_vnc_port($res);

if (!$id)
$id = 'N/A';
Expand Down Expand Up @@ -610,7 +610,7 @@
else
echo 'Domain doesn\'t have any network devices';

if ( $dom[$domName]['state'] == 1 ) {
if ( $dom['state'] == 1 ) {
echo "<h3>Screenshot</h3><img src=\"?action=get-screenshot&uuid={$_GET['uuid']}&width=640\">";
}
}
Expand Down Expand Up @@ -674,21 +674,20 @@

$active = $tmp['active'];
for ($i = 0; $i < sizeof($doms); $i++) {
$res = $lv->get_domain_by_name($doms[$i]);
$uuid = libvirt_domain_get_uuid_string($res);
$name = $doms[$i];
$domr= $lv->get_domain_object($name);
$res = $lv->get_domain_by_name($name);
$uuid = libvirt_domain_get_uuid_string($res);
$dom = $lv->domain_get_info($name);
$mem = number_format($dom[$name]['memory'] / 1024, 2, '.', ' ').' MB';
$cpu = $dom[$name]['nrVirtCpu'];
$state = $lv->domain_state_translate($dom[$name]['state']);
$id = $lv->domain_get_id($domr);
$arch = $lv->domain_get_arch($domr);
$vnc = $lv->domain_get_vnc_port($domr);
$nics = $lv->get_network_cards($domr);
if (($diskcnt = $lv->get_disk_count($domr)) > 0) {
$disks = $diskcnt.' / '.$lv->get_disk_capacity($domr);
$diskdesc = 'Current physical size: '.$lv->get_disk_capacity($domr, true);
$mem = number_format($dom['memory'] / 1024, 2, '.', ' ').' MB';
$cpu = $dom['nrVirtCpu'];
$state = $lv->domain_state_translate($dom['state']);
$id = $lv->domain_get_id($res);
$arch = $lv->domain_get_arch($res);
$vnc = $lv->domain_get_vnc_port($res);
$nics = $lv->get_network_cards($res);
if (($diskcnt = $lv->get_disk_count($res)) > 0) {
$disks = $diskcnt.' / '.$lv->get_disk_capacity($res);
$diskdesc = 'Current physical size: '.$lv->get_disk_capacity($res, true);
}
else {
$disks = '-';
Expand Down
58 changes: 44 additions & 14 deletions examples/libvirt.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
class Libvirt {
private $conn;
private $last_error;
private $allow_cached = true;
private $dominfos = array();

function Libvirt($uri = false, $debug=false) {
if ($debug)
Expand All @@ -24,6 +26,10 @@ function set_logfile($filename)
return true;
}

function print_resources() {
return libvirt_print_binding_resources();
}

function connect($uri = 'null') {
$this->conn=libvirt_connect($uri, false);
if ($this->conn==false)
Expand Down Expand Up @@ -757,28 +763,50 @@ function get_node_device_information($dev) {
return ($tmp) ? $tmp : $this->_set_last_error();
}

function domain_get_info($name = false) {
function domain_get_name($res) {
return libvirt_domain_get_name($res);
}

function domain_get_info_call($name = false, $name_override = false) {
$ret = array();

if ($name != false) {
$dom=libvirt_domain_lookup_by_name($this->conn, $name);
$dom = $this->get_domain_object($name);
if (!$dom)
return false;

if ($name_override)
$name = $name_override;

$ret[$name] = libvirt_domain_get_info($dom);
return $ret;
}

$doms = libvirt_list_domains($this->conn);
foreach ($doms as $dom) {
$tmp = libvirt_domain_get_name($dom);
$ret[$tmp] = libvirt_domain_get_info($dom);
else {
$doms = libvirt_list_domains($this->conn);
foreach ($doms as $dom) {
$tmp = $this->domain_get_name($dom);
$ret[$tmp] = libvirt_domain_get_info($dom);
}
}

ksort($ret);
return $ret;
}

function domain_get_info($name = false, $name_override = false) {
if (!$this->allow_cached)
return $this->domain_get_info_call($name, $name_override);

$domname = $name_override ? $name_override : $name;
$domkey = $name_override ? $name_override : $this->domain_get_name($name);
if (!array_key_exists($domkey, $this->dominfos)) {
$tmp = $this->domain_get_info_call($name, $name_override);
$this->dominfos[$domkey] = $tmp[$domname];
}

return $this->dominfos[$domkey];
}

function get_last_error() {
return $this->last_error;
}
Expand All @@ -801,9 +829,9 @@ function network_get_xml($network) {
return ($tmp) ? $tmp : $this->_set_last_error();;
}

function domain_get_id($domain) {
function domain_get_id($domain, $name = false) {
$dom = $this->get_domain_object($domain);
if ((!$dom) || (!$this->domain_is_running($dom)))
if ((!$dom) || (!$this->domain_is_running($dom, $name)))
return false;

$tmp = libvirt_domain_get_id($dom);
Expand All @@ -828,15 +856,15 @@ function domain_get_memory_stats($domain) {
return ($tmp) ? $tmp : $this->_set_last_error();
}

function domain_start($nameOrXml) {
$dom=libvirt_domain_lookup_by_name($this->conn, $nameOrXml);
function domain_start($dom) {
$dom=$this->get_domain_object($dom);
if ($dom) {
$ret = libvirt_domain_create($dom);
$this->last_error = libvirt_get_last_error();
return $ret;
}

$ret = libvirt_domain_create_xml($this->conn, $nameOrXml);
$ret = libvirt_domain_create_xml($this->conn, $dom);
$this->last_error = libvirt_get_last_error();
return $ret;
}
Expand Down Expand Up @@ -908,12 +936,12 @@ function domain_undefine($domain) {
return ($tmp) ? $tmp : $this->_set_last_error();
}

function domain_is_running($domain) {
function domain_is_running($domain, $name = false) {
$dom = $this->get_domain_object($domain);
if (!$dom)
return false;

$tmp = libvirt_domain_get_info($dom);
$tmp = $this->domain_get_info( $domain, $name );
if (!$tmp)
return $this->_set_last_error();
$ret = ( ($tmp['state'] == VIR_DOMAIN_RUNNING) || ($tmp['state'] == VIR_DOMAIN_BLOCKED) );
Expand Down Expand Up @@ -944,6 +972,8 @@ function domain_get_vnc_port($domain) {
}

function domain_get_arch($domain) {
$domain = $this->get_domain_object($domain);

$tmp = $this->get_xpath($domain, '//domain/os/type/@arch', false);
$var = $tmp[0];
unset($tmp);
Expand Down
Loading

0 comments on commit 3b9d910

Please sign in to comment.