VI-DB vc-detail.php v1

From vwiki
Revision as of 10:09, 14 March 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'>

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

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

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

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

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

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

$result = mysql_query($query);
//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, 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>".$row['name']."<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;
	$query = "SELECT vm.name AS vm, INET_NTOA(vm2ip.ip) AS ip, cluster.name AS cluster, cluster.clid, 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) WHERE vm.exist=1 ";
	if ($clid > 0) {
		$query .= "AND clid=$clid GROUP BY vm.vmid ORDER BY cluster.name, vm.name;";
	} else {
		$query .= "AND vm.vcid=$vcid GROUP BY vm.vmid 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 width=300>Name<th>IP<th>State<th>vCPU<th>Mem(MB)<th>Folder\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><a href='vm-detail.php?vmid=".$row['vmid']."'>".$row['vm']."</a><td>".$row['ip']."<td>$vm_state";
		print "<td>".$row['cpu']."<td>".$row['mem']."<td>".$row['vc_path']."\n";
	}
	print "</table>\n";
}
	
?>