VI-DB vm-detail.php v1

From vwiki
Jump to navigation Jump to search
<html xmlns='http://www.w3.org/TR/REC-html40'>

<html>
<head>
<title>vi-db (vm)</title>
<link rel='SHORTCUT ICON' href='favicon.ico'>
<link rel='stylesheet' type='text/css' href='alpha.css' />
<meta name='Author' content='Simon Strutt'>
<meta name='Description' content='VI Info'>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" >
<meta http-equiv="Content-Type" content="image/jpeg" >
<meta http-equiv='Pragma' content='no-cache'>
<meta http-equiv='Expires' content='900'>
</head>
<body>

<?php
$time_start = microtime(true);

//Local variables etc
require 'config.php';

// open connection and select database
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect to database!");
mysql_select_db($db) or die ("Unable to select database!");

//Set debug on if required
if (array_key_exists('debug', $_REQUEST) and ($_REQUEST["debug"] == 1)) {
	$debug = 1;
}

// show parameters and errors
if ($debug) {
	error_reporting(-1);
	print_r($_REQUEST);
	print "<br>";
}

print "DISCLAIMER: Please use all information gained from here with caution - I haven't performed exhaustive validation and testing of any of the information that may be presented.<br>\n";
print "If you're making changes or decisions, please double check with the reality of what is displayed by the vCentre servers.  You retain full responsibility for your actions...!\n ";

if (!array_key_exists('vmid', $_REQUEST)) {
	die ("Error - required parameter does not exist!");
}

// Initial query (need VM name for page title)

$query = "SELECT vm.name AS vm, vc.name AS vc, cluster.name AS cluster, esx.name AS esx, vm.vmid, vm.exist, vm.is_on, ";
$query .= "vm.hostname, vm.stamp ";
$query .= " FROM vm JOIN vc USING (vcid) JOIN cluster USING (clid) JOIN esx USING (esxid) ";
$query .= "WHERE vmid=". $_REQUEST["vmid"] .";";

$result = mysql_query($query);
//print $query;
if (mysql_num_rows($result) == 0) {
	die ("VM not found....");
}
$row = mysql_fetch_assoc($result);

print "<h1>". $row['vm'] ."</h1>";

// ----------------- Layout table (top left) ----------------------------
print "<table align='center' cellspacing=10><tr><td valign='top'>\n";

// ------------------- Basic VM info ------------------------

print "<table class='BasicTable' align='center'>\n<tr><th>Hostname:<td>". $row['hostname'];
print "\n<tr><th>Powered:<td>";
if ($row['exist']) {
	if ($row['is_on']) {
		print "ON";
	} else {
		print "OFF";
	}
} else {
	print "gone";
}
print "\n<tr><th>vCentre<td>". $row['vc'];
print "\n<tr><th>Cluster<td>". $row['cluster'];
print "\n<tr><th>ESX<td>". $row['esx'];
print "\n<tr><td colspan=2>Last changed: ". $row['stamp'] ."</table>\n";

// ------------------ Layout table cell (top right)--------------------------
print "\n<td valign='top'>\n";

// ----------------- Extended VM info ---------------------------------------
$query = "SELECT cpu, mem, vc_path, vmx_path, guest_name, notes FROM vm_ext JOIN os USING (osid)WHERE vmid=". $_REQUEST["vmid"] .";";
if ($debug) {print ($query);}
$result = mysql_query($query);
//print $query;
if (mysql_num_rows($result) == 0) {
	print ("VM extended info not found....");
} else {
	$row = mysql_fetch_assoc($result);
	print "<table class='BasicTable' align='center'>\n<tr><th>vCPU's:<td> &nbsp ". $row['cpu'] ."<th>Memory (MB):<td> &nbsp ". $row['mem'];
	print "<th>OS Type:<td>".$row['guest_name'];
	print "\n<tr><th>Folder:<td colspan=5>".$row['vc_path'];
	print "\n<tr><th>VMX:<td colspan=5>".$row['vmx_path'];
	print "\n<tr><th>Notes:<td colspan=5>". str_replace("\\n", "<br>", $row['notes'])."</table>\n";
}

// ------------------- Layout table cell (bottom left) ----------------------
print "\n<tr><td valign='top'>\n";

// ----------------- VM Network info ------------------------------------
$query = "SELECT num, name, vlan, INET_NTOA(ip) AS ip, type FROM vm2ip JOIN vm_nic USING (vnicid) LEFT JOIN nw USING (nwid) WHERE vm2ip.vmid=". $_REQUEST["vmid"];
$query .= " ORDER BY num;";
if ($debug) {print ($query);}
$result = mysql_query($query);
//print $query;
if (mysql_num_rows($result) == 0) {
	print ("VM network info not found....");
} else {
	while ($row = mysql_fetch_assoc($result)) {
		print "<table class='BasicTable' align='center' width=300>\n<tr><th width=50>NIC:<td width=30> &nbsp ". $row['num']."<th width=50>IP:<td>".$row['ip'];
		print "\n<tr><th>Name:<td colspan=3>". nl2br($row['name']);
		print "\n<tr><th>VLAN:<td>".$row['vlan']."<th>Type:<td>".$row['type']."</table>\n";
	}
}

// ------------------- Layout table  (bottom right) ------------------------
print "\n<td valign='top'>\n";

// ----------------- VM Disk info ------------------------------------
$query = "SELECT num, size, thin, path FROM vmdk WHERE vmid=". $_REQUEST["vmid"] .";";
if ($debug) {print ($query);}
$result = mysql_query($query);
//print $query;
if (mysql_num_rows($result) == 0) {
	print ("VM disk info not found....");
} else {
	print "<table class='BasicTable' align='center'>\n<tr><th>No<th>Size (GB)<th>Type<th>Path";
	while ($row = mysql_fetch_assoc($result)) {
		print "\n<tr><td>".$row['num']."<td>". round($row['size']/1024, 0)."<td>";
		if ($row['thin'] == 1) {
			print "Thin";
		} else {
			print "Thick";
		}
		print "<td>".$row['path'];
	}
	print "</table>\n";
}

// ------------------- Layout table end ------------------------------
print "</table>\n";

print "<br><a href='index.php'>Back to search page</a><br>\n";
	
$time = round((microtime(true) - $time_start)*1000);
print "Page took $time msecs to execute on server";

// ------------------- End of HTML ---------------------------------
print "</body><html>";

// ================ FUNCTIONS ============================

function search_char ($text) {
	
	return (str_replace(array("*", "?"), array("%", "_"), trim($text)));
}
	
?>