public function authenticate() { $resource = libvirt_connect("qemu+tcp://{$this->hostname}/system", $this->readonly, [VIR_CRED_AUTHNAME => $this->username, VIR_CRED_PASSPHRASE => $this->password]); if (!is_resource($resource)) { throw new \Exception("Falha na authenticação: " . libvirt_get_last_error()); } return $resource; }
/** * write last libvirt error to log file */ function ace_kvm_log_last_error() { $str = libvirt_get_last_error(); if ($str !== NULL) { $str = date("[Y/m/d] H:i:s : ") . $str; file_put_contents(_ACE_LIBVIRT_LOG_FILE_, $str . "\n", FILE_APPEND | LOCK_EX); } }
/** * Domain Resource * * @param $domain * @return resource * @throws \Exception */ public function getDomainResource($domain) { $domain = libvirt_domain_lookup_by_name($this->resource, $domain); if (is_resource($domain)) { return $domain; } $domain = libvirt_domain_lookup_by_uuid_string($this->resource, $domain); if (is_resource($domain)) { return $domain; } throw new \Exception("Dominio informado não existe: " . libvirt_get_last_error()); }
$list = libvirt_list_domains($libvirt->conn); $size = sizeof($list); for ($i = 0; $i < $size; $i++) { if (strlen(strstr($list[$i], $courseid)) > 0) { echo "<div id=\"uppervm\">"; echo "<div id=\"vmname\">" . $list[$i] . "</div>"; echo "<div id=\"vmscreen\">"; if ($libvirt->is_active($list[$i])) { echo "<img src=\"images/running.jpg\" height=95% style=\"border:2px solid gray;border-radius:10px;\">"; } else { echo "<img src=\"images/vm.png\" height=95% style=\"border:2px solid gray;border-radius:10px;\">"; } echo "</div>"; echo "<div id=\"vminfo\">"; $res = libvirt_domain_lookup_by_name($libvirt->conn, $list[$i]) or die(libvirt_get_last_error()); $info = libvirt_domain_get_info($res) or die(libvirt_get_last_error()); $result = mysql_query("select template_info.os from template_info,course_info where template_info.templatename=course_info.template && course_info.courseid in \t\t\t\t\t\t\t(SELECT courseid from vm_info where vmname='{$list[$i]}')"); $row = mysql_fetch_array($result); echo "Max memory:\t" . $info['maxMem'] . " KB<br>"; echo "Used Memory:\t" . $info['memory'] . " KB<br>"; if (!$libvirt->is_active($list[$i])) { echo "State:\t Shutdown" . "<br>"; } else { echo "State:\t Running" . "<br>"; } echo "CPU(s):\t" . $info['nrVirtCpu'] . "<br>"; echo "</div><div id=\"vmop\">"; echo "<button type=\"submit\" name=\"ON\" value=\"{$list[$i]}\"><img src=\"images/poweron.png\" alt=\"\" style=\"width:11%;height:10%;\"></button>"; echo "<button type=\"submit\" name=\"OFF\" value=\"{$list[$i]}\"><img src=\"images/poweroff.png\" alt=\"\" style=\"width:11%;height:21%\";></button>"; echo "</div></div>"; }
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, $dom); $this->last_error = libvirt_get_last_error(); return $ret; }
$vmname = $_POST['start']; $vmname = trim($vmname); $user = trim($user); if (strlen(strstr($vmname, $user)) > 0) { $password = ""; $username = ""; $ip = ""; $result = mysql_query("select template_info.username,template_info.password from template_info,course_info where template_info.templatename=course_info.template && course_info.courseid in (SELECT courseid from vm_info where vmname='{$vmname}')"); if (mysql_num_rows($result) >= 0) { $row = mysql_fetch_array($result); $username = $row['username']; $password = $row['password']; } $output = $libvirt->domain_start($vmname); if ($output == "0") { $output = libvirt_get_last_error(); } if (strpos($output, 'started') !== false || strpos($output, 'already') !== false) { $time_start = microtime(true); $ip = $libvirt->domipaddr($vmname); $time_end = microtime(true); $output .= "<br>Use '<b>ssh {$username}@{$ip}</b>' to access your Virtual Machine<br>"; $output .= "Default Password for VM was/is <b>\"{$password}\"</b>. Please change your password."; if (strpos($output, 'started') !== false) { $boottime = sprintf('%.2f', $time_end - $time_start); $output .= "<br>VM took <b>{$boottime}</b> seconds to boot."; $log = "[" . date(DATE_RFC2822) . "] BOOT TIME: {$vmname} took {$boottime} seconds to boot." . PHP_EOL; error_log("{$log}", 3, "stats.log"); $log = "[" . date(DATE_RFC2822) . "] IP Address: {$vmname} IP Address is {$ip}." . PHP_EOL; error_log("{$log}", 3, "stats.log"); }
if ($redis->ping()) { $num = $redis->get("cluster:num-nodes"); $res = null; for ($i = 0; $i < $num; $i++) { $ip = $redis->get("cluster:node" . $i); echo "<div id=\"widget\" style=\"float:left;font-size:100%;width:30%;margin-left:2.2%;\">"; echo "<div id=\"strip\">"; echo "NODE :" . $ip; echo "<button type=\"button\" class=\"primary\">-</button>"; echo "</div>"; echo "<div id=\"widget-main\">"; echo "<table style=\"font:serif;font-size:100%;text-align:left;\">"; $uri = "qemu+tcp://" . $ip . "/system"; $con = libvirt_connect($uri, false); if (!$con) { echo libvirt_get_last_error(); } $info = libvirt_connect_get_information($con); echo "<tr><th>Hostname</th><td>: " . $info['hostname'] . "</td></tr>"; echo "<tr><th>Hypervisor</th><td>: " . $info['hypervisor_string'] . "</td></tr>"; echo "<tr><th>Max VCPU(s) per VM</th><td>: " . $info['hypervisor_maxvcpus'] . "</td></tr>"; $info1 = libvirt_node_get_info($con); echo "<tr><th>CPU(s)</th><td>: " . $info1['cpus'] . "</td></tr>"; echo "<tr><th>CPU frequency</th><td>: " . $info1['mhz'] . "</td></tr>"; echo "<tr><th>Architecture</th><td>: " . $info1['model'] . "</td></tr>"; echo "<tr><th>Memory</th><td>: " . $info1['memory'] . "</td></tr>"; echo "<tr><th>Total Number of VM(s)</th><td>: " . $info['num_total_domains'] . "</td></tr>"; echo "<tr><th>Running VM(s)</th><td>: " . $info['num_active_domains'] . "</td></tr>"; echo "<tr><th>Stopped VM(s)</th><td>: " . $info['num_inactive_domains'] . "</td></tr>"; echo "</table>"; echo "</div>";
while ($row = mysql_fetch_array($result)) { $course = $row['courseid']; echo "<div id=\"topwidget\" style=\"float:left;\">"; echo "<div id=\"topstrip\">"; echo "VM(s) for {$course}"; echo "<button type=\"button\" class=\"primary\">-</button>"; echo "</div>"; echo "<div id=\"topwidget-main\">"; echo "<table style=\"font:serif;font-size:80%;text-align:left;\">"; $result1 = mysql_query("select * from vm_info where courseid='{$course}'"); if (mysql_num_rows($result1) == 0) { echo "No student is enrolled in this course<br>"; } while ($row = mysql_fetch_array($result1)) { $vm = $row['vmname']; $a = $libvirt->is_active($vm) or libvirt_get_last_error(); if ($a) { echo "<tr><th>VM " . $vm . "</th><td>: Running</td></tr>"; } else { echo "<tr><th>VM " . $vm . "</th><td>: Shutdown</td></tr>"; } } echo "</table>"; echo "</div>"; echo "</div>"; } } ?> </div> <?php if ($option == 3) {