VI-DB vm-detail.php v2.2

From vwiki
Revision as of 08:55, 12 October 2011 by Sstrutt (talk | contribs) (Initial creation)
(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.2 -->

<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.name AS esx, vm.vmid, vm.exist, vm.is_on, ";
$query .= "vapp.vappid, vapp.name AS vapp, vm_ext.tools, vm.hostname, vm.stamp ";
$query .= " FROM vm LEFT JOIN vc USING (vcid) JOIN vm_ext USING (vmid) JOIN cluster USING (clid) JOIN esx USING (esxid) JOIN vapp ON vm_ext.vapp=vapp.name ";
$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 "<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><a href=cluster-detail.php?clid=".$row['clid'].">". $row['cluster']."</a>";
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>\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, vm_ver, notes, uuid FROM vm_ext JOIN vm USING (vmid) 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 {
	$vm = mysql_fetch_assoc($result);
	print "<table class='BasicTable' align='center'>\n<tr><th>vCPU's:<td> &nbsp ". $vm['cpu'] ."<th>Memory (MB):<td> &nbsp ". $vm['mem'];
	print "<th>OS Type:<td>".$vm['guest_name'];
	print "\n<tr><th>Folder:<td colspan=5>".$vm['vc_path'];
	print "\n<tr><th>VMX:<td colspan=5>".$vm['vmx_path'];
	print "\n<tr><th>Notes:<td colspan=5>". str_replace("\\n", "<br>", $vm['notes']);
	print "\n<tr><th>UUID:<td colspan=5><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)) {
			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, 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  (bottom right) ------------------------
print "\n<tr><td><td valign='top'>\n";

$query = "SELECT san_model, lun, ds.name, ds.size, used, SUM(vmdk.size) AS vm_used FROM ds JOIN vmdk USING (dsid) WHERE vmid=". $_REQUEST["vmid"]." 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>\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>".$row['name']."<td class='num'>". round($row['size']/1024);
		print bar_graph_two($vm_used_pcent, $used_pcent, $disk_pc_warn, $disk_pc_alert);
	}
	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";
}
	
?>