VI-DB vc-detail.php v2.2

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

<!-- Version 2.2 -->

<html>
<head>
<title>vi-db (vc)</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';
$self = $_SERVER['PHP_SELF'];

// 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;
}

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('vcid', $_REQUEST)) {
	die ("Error - required vcid parameter does not exist!");
}
$vcid = $_REQUEST["vcid"];
$data_link = "vcid=$vcid";

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

if (!array_key_exists('view', $_REQUEST)) {
	$view = "resource";
} else {
	if ($_REQUEST["view"] == "vm") {
		$view = "vm";
		$data_link .= "&view=vm";
	} elseif ($_REQUEST["view"] == "resource") {
		$view = "resource";
		$data_link .= "&view=resource";
	} else {
		die ("Error - view parameter error!");
	}
}

if (array_key_exists('clid', $_REQUEST)) {
	$clid=$_REQUEST["clid"];
	$data_link .= "&clid=$clid";
	$cl_link="&clid=$clid";
} else {
	$clid=0;
	$cl_link="";
}

if ($debug) {
	print "$data_link<br>";
}

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

$query = "SELECT name, site, ver, build FROM vc WHERE vcid=$vcid;";

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

print "<h1>".$row['name'] ."</h1>\n";

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

// ------------------ vCentre info -----------------------------------------
print "<table class='BasicTable' align='left'>\n<tr><th>Site:<td>".$row['site']."\n";
print "<tr><th>Version:<td>".$row['ver']."\n";
print "<tr><th>Build:<td>".$row['build']."</table>\n";

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

// ------------------- vCentre Cluster info ------------------------------------
$query = "SELECT clid, name, ha, drs FROM cluster WHERE vcid=$vcid AND exist=1;";
if ($debug) {print ($query);}
$clusters = mysql_query($query);
if (mysql_num_rows($result) == 0) {
	print ("Cluster info not found....");
} else {
	print "<table class='BasicTable' align='left'>\n<tr><th>Name<th>HA<th>DRS<th>ESXs<th>VMs<br><small>(all)</small><th>VMs<br><small>(on)</small>\n";
	while ($cluster = mysql_fetch_assoc($clusters)) {
		if ($clid == $cluster['clid']) {
			print "<tr><td><b><a href='$self?vcid=$vcid&clid=".$cluster['clid']."&view=$view'>".$cluster['name']."</a></b><td>";
		} else {
			print "<tr><td><a href='$self?vcid=$vcid&clid=".$cluster['clid']."&view=$view'>".$cluster['name']."</a><td>";
		}
		if ($cluster['ha']) {
			print "Enabled";
		} else {
			print "Disabled";
		}
		if ($cluster['drs']) {
			print "<td>".$cluster['drs'];
		} else {
			print "<td>Disabled";
		}
		$esxs = mysql_fetch_assoc(mysql_query("SELECT COUNT(*) AS total FROM esx WHERE clid=".$cluster['clid']." AND exist=1;"));
		print "<td class='num'>".$esxs['total'];
		$vms = mysql_fetch_assoc(mysql_query("SELECT COUNT(*) AS total, SUM(IF(is_on=1,1,0)) AS is_on FROM vm WHERE clid=".$cluster['clid']." AND exist=1;"));
		print "<td class='num'>".$vms['total']."<td class='num'>".$vms['is_on'];
	}
	print "</table>";
}
// ------------------- Top Layout table cell (right) ----------------------
print "\n<td valign='top'>\n";

// ----------------- View Selector -----------------------------------------
print "<table class='BasicTable' align='left'>\n<tr><th width=180 colspan=2>View Selector\n";
if (array_key_exists('clid', $_REQUEST)) {
	print "<tr><td align='center' colspan=2><a href=$self?vcid=$vcid&view=$view>For All Clusters</a>\n";
} else {
	print "<tr><td align='center' colspan=2><b>&lt &lt</b> Filter On Cluster <b>&lt &lt</b>\n";
}
if ($view == "vm") {
	print "<tr><th rowspan=2 width=40>Show:<td align='center'><b>Virtual Machines</b>\n<tr><td align='center'><a href=$self?vcid=$vcid$cl_link&view=resource>Cluster Resources</a>\n";
} elseif ($view == "resource") {
	print "<tr><th rowspan=2 width=40>Show:<td align='center'><a href=$self?vcid=$vcid$cl_link&view=vm>Virtual Machines\n<tr><td align='center'><b>Cluster Resources</b>\n";
} else {
	print "<tr>ERROR...!!!\n";
}

print "</table>";

// -------------------  Bottom layout table cell (left) ----------------------
print "\n</table>\n";

if ($view == "vm") {
	show_vms();
} elseif ($view == "resource") {
	print "<table align='center'><tr><td valign='top'>\n";
	show_esxs();
	print "<td valign='top'>\n";
	show_nws();
	print "<td valign='top'>\n";
	show_dss();
	print "</table>\n";
} else {
	print "ERROR...!!!";
}

print "<br><a href='index.php'>Search page</a> &nbsp <a href='status.php'>Status 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 show_esxs() {
	global $debug, $vcid, $clid;
	global $cpu_pc_warn, $cpu_pc_alert,$mem_pc_warn, $mem_pc_alert;
	$query = "SELECT name, esx_state.state, esx_state.status, cpu_pc, mem_pc FROM esx JOIN esx_state USING (esxid) WHERE exist=1 ";
	if ($clid > 0) {
		$query .= "AND clid=$clid ORDER BY name;";
	} else {
		$query .= "AND vcid=$vcid ORDER BY name;";
	}
	if ($debug) {print ($query);}
	$esxs = mysql_query($query);
	if (mysql_num_rows($esxs) == 0) {
		print ("Cluster ESX info not found....");
	} else {
		print "<table class='BasicTable' align='left'>\n<tr><th width=35>State<th width=165>ESX<th>CPU<th>Memory\n";
		while ($esx = mysql_fetch_assoc($esxs)) {
			if ($esx['state'] == "Connected") {
				print "<tr><td>Live";
			} elseif ($esx['state'] == "NotResponding") {
				print "<tr><td class='alert'>Down";
			} elseif ($esx['state'] == "Maintenance") {
				print "<tr><td class='warn'>Maint";
			} elseif ($esx['state'] == "Disconnected") {
				print "<tr><td class='alert'>disc";
			} else {
				print "<tr><td>UNEX";
			}
			
			print "<td>".$esx['name'];
			if ($esx['status'] == "yellow") {
				print " <span class='status_warn'> has warning</span>";
			} elseif ($esx['status'] == "red") {
				print " <span class='status_alert'> has alert</span>";
			}
			
			bar_graph ($esx['cpu_pc'], $cpu_pc_warn, $cpu_pc_alert);
			bar_graph ($esx['mem_pc'], $mem_pc_warn, $mem_pc_alert);
		}
		print "</table>\n";
	}
}

function show_nws() {
	global $debug, $vcid, $clid;
	$query = "SELECT DISTINCT nw.vlan, nw.name FROM nw JOIN esx2nw USING (nwid) JOIN esx USING (esxid) WHERE nw.exist=1 ";
	if ($clid > 0) {
		$query .= "AND clid=$clid ORDER BY vlan, name;";
	} else {
		$query .= "AND nw.vcid=$vcid ORDER BY vlan, name;";
	}
	if ($debug) {print ($query);}
	$result = mysql_query($query);
	if (mysql_num_rows($result) == 0) {
		print ("vCentre networks info not found....");
	} else {
		print "<table class='BasicTable' align='left'>\n<tr><th>VLAN<th width=300>Network name\n";
		while ($row = mysql_fetch_assoc($result)) {
			print "<tr><td class='num'>".$row['vlan']."<td>".$row['name']."\n";
		}
		print "</table>\n";
	}
}

function show_dss() {
	global $debug, $vcid, $clid;
	global $disk_pc_warn, $disk_pc_alert;
	$query = "SELECT DISTINCT ds.lun, ds.name, ds.dsid, size, used FROM ds JOIN esx2ds USING (dsid) JOIN esx USING (esxid) WHERE ds.exist=1 ";
	if ($debug) {print ($query);}
	if ($clid > 0) {
		$query .= "AND clid=$clid ORDER BY lun, name;";
	} else {
		$query .= "AND ds.vcid=$vcid ORDER BY lun, name;";
	}
	if ($debug) {print ($query);}
	$result = mysql_query($query);
	if (mysql_num_rows($result) == 0) {
		print ("vCentre datastores info not found....");
	} else {
		print "<table class='BasicTable' align='center'>\n<tr><th>LUN<th width=200>Datastore name<th>Size (GB)<th width=50>Used (%)\n";
		while ($row = mysql_fetch_assoc($result)) {
			$used_pcent = round($row['used']/$row['size']*100);
			print "<tr><td class='num'>".$row['lun']."<td><a href='ds-info.php?dsid=".$row['dsid']."'>".$row['name']."</a><td>". round($row['size']/1024);
			print bar_graph($used_pcent, $disk_pc_warn, $disk_pc_alert);
		}
		print "</table>\n";
	}
}

function show_vms() {
	global $debug, $vcid, $clid, $data_link, $self, $_REQUEST;
	
	$query = "SELECT vm.name AS vm, INET_NTOA(vm2ip.ip) AS ip, cluster.name AS cluster, cluster.clid, esx.name AS esx, vm.vmid, vm.is_on";
	$query .= ", vm_ext.cpu, vm_ext.mem, vm_ext.vc_path FROM vm JOIN vm_ext USING (vmid) ";
	$query .= "LEFT JOIN vm2ip USING (vmid) JOIN cluster USING (clid) JOIN esx USING (esxid) WHERE vm.exist=1 ";
	if ($clid > 0) {
		$query .= "AND cluster.clid=$clid GROUP BY vm.vmid ";
	} else {
		$query .= "AND vm.vcid=$vcid GROUP BY vm.vmid ";
	}
	
	$th_ESX = "ESX";
	$th_Folder = "Folder";
	$th_Name = "Name";
	$th_IP = "IP";
	
	if (array_key_exists('sort1', $_REQUEST)) {
		$sort1 = $_REQUEST["sort1"];
		$sort_link = "&sort2=$sort1";
		
		switch ($sort1) {
			case "esx":
				$query .= "ORDER BY esx.name";
				$th_ESX = "ESX<br><small>&uarr;1&uarr;</small>";
				break;
			case "dir":
				$query .= "ORDER BY vm_ext.vc_path";
				$th_Folder = "Folder<br><small>&uarr;1&uarr;</small>";
				break;
			case "vm":
				$query .= "ORDER BY vm.name";
				$th_Name = "Name<br><small>&uarr;1&uarr;</small>";
				break;
			case "ip";
				$query .= "ORDER BY vm2ip.ip";
				$th_IP = "IP<br><small>&uarr;1&uarr;</small>";
				break;
			default;
				$query .= "ORDER BY vm.name";
				$th_Name = "Name<br><small>&uarr;1&uarr;</small>";
		}
		
		if (array_key_exists('sort2', $_REQUEST)) {
			switch ($_REQUEST["sort2"]) {
				case "esx":
					$query .= ", esx.name;";
					$th_ESX = "ESX<br><small>&uarr;2&uarr;</small>";
					break;
				case "dir":
					$query .= ", vm_ext.vc_path;";
					$th_Folder = "Folder<br><small>&uarr;2&uarr;</small>";
					break;
				case "vm":
					$query .= ", vm.name;";
					$th_Name = "Name<br><small>&uarr;2&uarr;</small>";
					break;
				case "ip";
					$query .= ", vm2ip.ip;";
					$th_IP = "IP<br><small>&uarr;2&uarr;</small>";
					break;
				default;
					$query .= ", vm.name;";
			}
		} else {
			$query .= ";";
		}

	} else {
		$sort_link = "";
		$query .= "ORDER BY cluster.name, vm.name;";
	}
	
	if ($debug) {print ($query);}
	$result = mysql_query($query);
	if (!$result) {
		print ("<b>Query failed !!!!<br>Query:</b> $query<br><b>Error:</b> ". mysql_error());
	}
	if (mysql_num_rows($result) == 0) {
		print ("No VMs found....");
		return;
	}
	
	print "<table class='BasicTable' align='center' valign='top'><tr><th><a href='$self?$data_link&sort1=esx$sort_link'>$th_ESX</a><th><a href='$self?$data_link&sort1=dir$sort_link'>$th_Folder</a>";
	print "<th width=170><a href='$self?$data_link&sort1=vm$sort_link'>$th_Name</a><th>State<th><a href='$self?$data_link&sort1=ip$sort_link'>$th_IP</a><th>vCPU<th>Mem<br><small>MB</small>\n";
	$current_vmid = 0;
	$odd_row = false;
	while ($row = mysql_fetch_assoc($result)) {
		$odd_row = !$odd_row;
		if ($odd_row) {
			print "<tr class='odd'>";
		} else {
			print "<tr>";
		}
		if ($row['is_on']) {
			$vm_state = "ON";
		} else {
			$vm_state = "OFF";
		}
		print "<td>".$row['esx']."<td>".$row['vc_path']."<td><a href='vm-detail.php?vmid=".$row['vmid']."'><b>".$row['vm']."</b></a>";
		print "<td class='num'>".$vm_state."<td class='num'>".$row['ip']."<td class='num'>".$row['cpu']."<td class='num'>".$row['mem']."\n";
	}
	print "</table>\n";
}
	
?>