public function work()
 {
     $t = new LinfoTimerStart('libvirt extension');
     if (!extension_loaded('libvirt')) {
         $this->LinfoError->add('libvirt extension', 'Libvirt PHP extension not installed');
         $this->res = false;
         return false;
     }
     if (!$this->connect()) {
         return false;
     }
     if (!($doms = libvirt_list_domains($this->connection))) {
         $this->LinfoError->add('libvirt extension', 'Failed getting domain list');
         $this->res = false;
         return false;
     }
     foreach ($doms as $name) {
         if (!($domain = libvirt_domain_lookup_by_name($this->connection, $name))) {
             continue;
         }
         if (!($info = libvirt_domain_get_info($domain)) || !is_array($info)) {
             continue;
         }
         $info['storage'] = array();
         foreach ((array) @libvirt_domain_get_disk_devices($domain) as $blockName) {
             if (!is_string($blockName)) {
                 continue;
             }
             // Sometime device exists but libvirt fails to get more docs. just settle for device name
             if (!($blockInfo = libvirt_domain_get_block_info($domain, $blockName)) || !is_array($blockInfo)) {
                 $info['storage'][] = array('device' => $blockName);
                 continue;
             }
             if (isset($blockInfo['partition']) && !isset($blockInfo['file'])) {
                 $blockInfo['file'] = $blockInfo['partition'];
             }
             $info['storage'][] = $blockInfo;
         }
         $this->VMs[$name] = $info;
     }
     $this->res = true;
 }
Example #2
0
 public function work()
 {
     $t = new Timer('libvirt extension');
     if (!extension_loaded('libvirt')) {
         Errors::add('libvirt extension', 'Libvirt PHP extension not installed');
         $this->res = false;
         return;
     }
     if (!$this->connect()) {
         Errors::add('libvirt extension', 'Failed connecting');
         return;
     }
     if (!($doms = libvirt_list_domains($this->connection))) {
         Errors::add('libvirt extension', 'Failed getting domain list');
         $this->res = false;
         return;
     }
     foreach ($doms as $name) {
         if (!($domain = libvirt_domain_lookup_by_name($this->connection, $name))) {
             continue;
         }
         if (!($info = libvirt_domain_get_info($domain)) || !is_array($info)) {
             continue;
         }
         $info['autostart'] = libvirt_domain_get_autostart($domain);
         if ($info['autostart'] == 1) {
             $info['autostart'] = 'Yes';
         } elseif ($info['autostart'] == 0) {
             $info['autostart'] = 'No';
         } else {
             $info['autostart'] = 'N/A';
         }
         $info['nets'] = array();
         $nets = @libvirt_domain_get_interface_devices($domain);
         foreach ($nets as $key => $net) {
             if (!is_numeric($key)) {
                 continue;
             }
             $info['nets'][] = $net;
         }
         $info['storage'] = array();
         foreach ((array) @libvirt_domain_get_disk_devices($domain) as $blockName) {
             if (!is_string($blockName)) {
                 continue;
             }
             // Sometime device exists but libvirt fails to get more docs. just settle for device name
             if (!($blockInfo = @libvirt_domain_get_block_info($domain, $blockName)) || !is_array($blockInfo)) {
                 $info['storage'][] = array('device' => $blockName);
                 continue;
             }
             if (isset($blockInfo['partition']) && !isset($blockInfo['file'])) {
                 $blockInfo['file'] = $blockInfo['partition'];
             }
             $info['storage'][] = $blockInfo;
         }
         $this->VMs[$name] = $info;
     }
     $this->res = true;
 }
 function get_disk_stats($domain, $sort = true)
 {
     $dom = $this->get_domain_object($domain);
     $buses = $this->get_xpath($dom, '//domain/devices/disk[@device="disk"]/target/@bus', false);
     $disks = $this->get_xpath($dom, '//domain/devices/disk[@device="disk"]/target/@dev', false);
     // create image as: qemu-img create -f qcow2 -o backing_file=RAW_IMG OUT_QCOW_IMG SIZE[K,M,G suffixed]
     $ret = array();
     for ($i = 0; $i < $disks['num']; $i++) {
         $tmp = libvirt_domain_get_block_info($dom, $disks[$i]);
         if ($tmp) {
             $tmp['bus'] = $buses[$i];
             $ret[] = $tmp;
         } else {
             $this->_set_last_error();
         }
     }
     if ($sort) {
         for ($i = 0; $i < sizeof($ret); $i++) {
             for ($ii = 0; $ii < sizeof($ret); $ii++) {
                 if (strcmp($ret[$i]['device'], $ret[$ii]['device']) < 0) {
                     $tmp = $ret[$i];
                     $ret[$i] = $ret[$ii];
                     $ret[$ii] = $tmp;
                 }
             }
         }
     }
     unset($buses);
     unset($disks);
     return $ret;
 }
Example #4
0
		function get_disk_stats($domain, $sort=true) {
			$dom = $this->get_domain_object($domain);

			$buses =  $this->get_xpath($dom, '//domain/devices/disk[@device="disk"]/target/@bus', false);
			$disks =  $this->get_xpath($dom, '//domain/devices/disk[@device="disk"]/target/@dev', false);
			$files =  $this->get_xpath($dom, '//domain/devices/disk[@device="disk"]/source/@file', false);

			$ret = array();
			for ($i = 0; $i < $disks['num']; $i++) {
				$tmp = libvirt_domain_get_block_info($dom, $disks[$i]);
				if ($tmp) {
					$tmp['bus'] = $buses[$i];

					// Libvirt reports 0 bytes for raw disk images that haven't been
					// written to yet so we just report the raw disk size for now
					if ( !empty($tmp['file']) &&
						 $tmp['type'] == 'raw' &&
						 empty($tmp['physical']) &&
						 is_file($tmp['file']) ) {

						$intSize = filesize($tmp['file']);
						$tmp['physical'] = $intSize;
						$tmp['capacity'] = $intSize;
					}

					$ret[] = $tmp;
				}
				else {
					$this->_set_last_error();

					$ret[] = array(
						'device' => $disks[$i],
						'file'   => $files[$i],
						'type'   => '-',
						'capacity' => '-',
						'allocation' => '-',
						'physical' => '-',
						'bus' => $buses[$i]
					);
				}
			}

			if ($sort) {
				for ($i = 0; $i < sizeof($ret); $i++) {
					for ($ii = 0; $ii < sizeof($ret); $ii++) {
						if (strcmp($ret[$i]['device'], $ret[$ii]['device']) < 0) {
							$tmp = $ret[$i];
							$ret[$i] = $ret[$ii];
							$ret[$ii] = $tmp;
						}
					}
				}
			}

			unset($buses);
			unset($disks);
			unset($files);

			return $ret;
		}