VI-DB vm-detail.php v2.3

From vwiki
Revision as of 07:47, 20 December 2011 by Sstrutt (talk | contribs) (Corrections)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search
<html xmlns='http://www.w3.org/TR/REC-html40'>

<!--Version 2.3 -->

<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, vc.vcid, cluster.name AS cluster, cluster.clid, esx.esxid, esx.name AS esx, vm.vmid, vm.exist, vm.is_on, ";
$query .= "vapp.vappid, vapp.name AS vapp, vm_ext.tools, vm.hostname, guest_name AS os, vm.stamp ";
$query .= " FROM vm LEFT JOIN vc USING (vcid) JOIN vm_ext USING (vmid) JOIN cluster USING (clid) JOIN esx USING (esxid) JOIN os USING (osid) LEFT JOIN vapp ON vm_ext.vapp=vapp.name ";
$query .= "WHERE vmid=". $_REQUEST["vmid"] .";";

$result = mysql_query($query);
if ($debug) {
	print $query;
}
if (mysql_num_rows($result) == 0) {
	die ("<br>VM not found....");
}
$row = mysql_fetch_assoc($result);
$esxid = $row['esxid'];

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><b>". $row['hostname']."</b>\n";
print "<tr><th>OS:<td>".$row['os'];
print "\n<tr><th>Powered:<td>";
if ($row['exist']) {
	if ($row['is_on']) {
		print "<span class='good'>ON</span>";
	} else {
		print "<span class='warn'>OFF</span>";
	}
} else {
	print "gone";
}
print "\n<tr><th>vCentre<td><a href=vc-detail.php?vcid=".$row['vcid'].">". $row['vc']."</a>";
print "\n<tr><th>Cluster<td><b><a href=cluster-detail.php?clid=".$row['clid'].">". $row['cluster']."</a></b>";
print "\n<tr><th>vApp<td><a href=vapps.php?vappid=".$row['vappid'].">". $row['vapp']."</a>";
print "\n<tr><th>ESX<td>". $row['esx'];
print "\n<tr><th>VM Tools";

switch ($row['tools']) {
	case 'toolsOk':
		print "<td>Tools OK";
		break;
	case 'toolsOld':
		print "<td><span class='warn'>Out of Date</span>";
		break;
	case 'toolsNotRunning':
		if ($row['is_on']) {
			print "<td><span class='warn'>Not Running</span>";
		} else {
			print "<td>Not Running";
		}
		break;
	case 'toolsNotInstalled':
		print "<td><span class='alert'>Not Installed</span>";
		break;
	default:
		print "<td>".$row['tools'];
}

print "\n<tr><td colspan=2>Last updated: ". $row['stamp'] ."</table><br>\n";



// ----------------- CPU/Mem resources and Extended VM info ---------------------------------------
$query = "SELECT cpu, mem, cpu_resrv, cpu_limit, cpu_level, cpu_shares, mem_resrv, mem_limit, mem_level, mem_shares, vc_path, vmx_path, vm_ver, notes, uuid ";
$query .= "FROM vm_ext JOIN vm USING (vmid) JOIN vm_res USING (vmid) WHERE vmid=". $_REQUEST["vmid"] .";";
if ($debug) {print ($query);}
$result = mysql_query($query);
if (mysql_num_rows($result) == 0) {
	print ("VM extended info not found....");
} else {
	$vm = mysql_fetch_assoc($result);
	print "<table class='BasicTable' align='center'><th><th>Allocation<th>Reservation<th>Limit<th>Shares<th>Sh. Value\n";
	print "<tr><th>vCPU's:<td class='num'><b>". $vm['cpu'] ."</b>";
	if ($vm['cpu_resrv'] == 0) {
		print "<td class='num'>none";
	} else {
		print "<td class='num'>".$vm['cpu_resrv'];
	}
	if ($vm['cpu_limit'] == -1) {
		print "<td class='num'>none";
	} else {
		print "<td class='num'>".$vm['cpu_limit'];
	}
	print "<td class='num'>". $vm['cpu_level']."<td class='num'>". $vm['cpu_shares'];
	print "<tr><th>Memory (MB):<td class='num'><b>". $vm['mem']."</b>";
	if ($vm['mem_resrv'] == 0) {
		print "<td class='num'>none";
	} else {
		print "<td class='num'>".$vm['mem_resrv'];
	}
	if ($vm['mem_limit'] == -1) {
		print "<td class='num'>none";
	} else {
		print "<td class='num'>".$vm['mem_limit'];
	}
	print "<td class='num'>". $vm['mem_level']."<td class='num'>". $vm['mem_shares']."</table>";
	
	// ------------------ Layout table cell (top right)--------------------------
	print "\n<td valign='top'>\n";
	
	print "<table class='BasicTable' align='center'><tr><th>Folder:<td><b>".$vm['vc_path']."</b>\n";
	print "<tr><th>VMX:<td>".$vm['vmx_path'];
	$query = "SELECT name, data FROM vm_attrib WHERE vmid=". $_REQUEST["vmid"] .";";
	$result = mysql_query($query);
	if (mysql_num_rows($result) > 0) {
		while ($vm_attrib = mysql_fetch_assoc($result)) {
			print "\n<tr><th>".$vm_attrib['name']."<td>".$vm_attrib['data'];
		}
	}
	print "\n<tr><th>Notes:<td>". str_replace("\\n", "<br>", $vm['notes']);
	print "\n<tr><th>UUID:<td><span style='font-family:courier;'>".$vm['uuid']."</span></table>\n";
}

// ------------------- Layout table cell (mid 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 ($nic = mysql_fetch_assoc($result)) {
		print "<table class='BasicTable' align='center' width=300>\n<tr><th width=50>NIC:<td width=30> &nbsp ". $nic['num']."<th width=50>IP:<td>".$nic['ip'];
		print "\n<tr><th>Name:<td colspan=3>". nl2br($nic['name']);
		print "\n<tr><th>VLAN:<td>".$nic['vlan']."<th>Type:";
		if (($nic['type'] != 'Vmxnet3' and $vm['vm_ver'] == 7) or ($nic['type'] != 'EnhancedVmxnet' and $vm['vm_ver'] == 4)) {
			if (strpos($nic['type'], 'NAT')) {
				print "<td><span class='highlight'>".$nic['type']."</span></table>\n";
			} else {
				print "<td><span class='warn'>".$nic['type']."</span></table>\n";
			}
		} else {
			print "<td>".$nic['type']."</table>\n";
		}
	}
}

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

// ----------------- VM Disk info ------------------------------------
$query = "SELECT num, size, thin, rdm, 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<br><small>GB</small><th>Type<th>Path";
	while ($row = mysql_fetch_assoc($result)) {
		print "\n<tr><td class='num'>".$row['num']."<td class='num'>". round($row['size']/1024, 0)."<td>";
		if ($row['thin'] == 1) {
			print "Thin";
		} elseif ($row['rdm'] == 1) {
			print "RDM";
		} else {
			print "Thick";
		}
		print "<td>".$row['path'];
	}
	print "</table>\n";
}

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

$query = "SELECT san_model, lun, ds.dsid, ds.name, ds.size, ds.used, SUM(IF(vmdk.rdm=1,0,vmdk.size)) AS vm_used, mpath FROM ds JOIN vmdk USING (dsid) JOIN esx2ds USING (dsid) WHERE vmid=". $_REQUEST["vmid"]." AND esxid=$esxid GROUP BY lun;";
if ($debug) {print ($query);}
$result = mysql_query($query);
if (mysql_num_rows($result) == 0) {
	print ("VM datastore info not found....");
} else {
	print "<table class='BasicTable' align='center'>\n<tr><th>SAN<th>LUN<th width=200>Datastore name<th>Size<br><small>GB</small><th width=50>Used (%)<br><small>By VM | In DS</small><th width=30>Multipath\n";
	while ($row = mysql_fetch_assoc($result)) {
		$used_pcent = round($row['used']/$row['size']*100);
		$vm_used_pcent = round($row['vm_used']/$row['size']*100);
		print "<tr><td>".$row['san_model']."<td class='num'>".$row['lun']."<td><a href='ds-info.php?dsid=".$row['dsid']."'>".$row['name']."</a><td class='num'>". round($row['size']/1024);
		print bar_graph_two($vm_used_pcent, $used_pcent, $disk_pc_warn, $disk_pc_alert);
		print "<td>". $row['mpath'] ."\n";
	}
	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)));
}

function bar_graph ($data_percent, $warn_threshold, $alert_threshold) {
	print "<td class='bar' title='$data_percent%'><div style='width: $data_percent%' ";
	if ($data_percent >= $alert_threshold) {
		print "class='bar_alert'";
	} elseif ($data_percent >= $warn_threshold) {
		print "class='bar_warn'";
	} else {
		print "class='bar_good'";
	}
	print ">".$data_percent."</div>\n";
}

function bar_graph_two ($data1_percent, $data2_percent, $warn_threshold, $alert_threshold) {
	// data1_pcent MUST BE LESS THAN data2_pcent !!!
	print "<td class='bar' title='$data1_percent%'><div style='width: $data1_percent%' class='bar_norm'>".$data1_percent."</div>";
	print "<div style='width: ". ($data2_percent - $data1_percent) ."'";
	if ($data2_percent >= $alert_threshold) {
		print "class='bar_alert'";
	} elseif ($data2_percent >= $warn_threshold) {
		print "class='bar_warn'";
	} else {
		print "class='bar_good'";
	}
	print ">".$data2_percent."</div>\n";
}
	
?>