VI-DB cluster-detail.php v1

From vwiki
Revision as of 10:12, 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 (cluster)</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!");

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

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

$query = "SELECT vc.name AS vc, vc.site, cluster.name AS name, cluster.exist, ha, drs, cluster.stamp ";
$query .= " FROM cluster JOIN vc USING (vcid) ";
$query .= "WHERE clid=". $_REQUEST["clid"] .";";

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

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

if ($row['exist'] == 0){
	print "<b>This cluster no longer exists...!!!</b>\n";
}

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

// ------------------- Basic Cluster info ------------------------

print "<table class='BasicTable' align='left'>\n<tr><th>Site:<td>". $row['site']."<th>vCentre:<td>".$row['vc']."<tr><th>HA:<td colspan=3>";
if ($row['ha']) {
	print "Enabled";
} else {
	print "Disabled";
}
print "<tr><th>DRS:<td colspan=3>";
if ($row['drs']) {
	print $row['drs'];
} else {
	print "Disabled";
}
$esxs = mysql_fetch_assoc(mysql_query("SELECT COUNT(*) AS total, SUM(IF(state='Connected',1,0)) AS connected FROM esx WHERE clid=". $_REQUEST["clid"] ." AND exist=1;"));
print "<tr><th>ESXs:<td>".$esxs['total']."<th>Connected:<td>".$esxs['connected'];
$vms = mysql_fetch_assoc(mysql_query("SELECT COUNT(*) AS total, SUM(IF(is_on=1,1,0)) AS is_on FROM vm WHERE clid=". $_REQUEST["clid"] ." AND exist=1;"));
print "<tr><th>VMs:<td>".$vms['total']."<th>Powered On:<td>".$vms['is_on'];
print "</table>";

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

// ------------------- Cluster ESX's --------------------------------------

$esxs = mysql_query("SELECT name, esx_state.state, esx_state.status, cpu_pc, mem_pc FROM esx JOIN esx_state USING (esxid) WHERE clid=".$_REQUEST["clid"]." AND exist=1 ORDER BY name;");
if (mysql_num_rows($result) == 0) {
	print ("Cluster ESX info not found....");
} else {
	print "<table class='BasicTable' align='left'>\n<tr><th>State<th>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 ("CPU", $esx['cpu_pc'], $cpu_pc_warn, $cpu_pc_alert);
		bar_graph ("Memory", $esx['mem_pc'], $mem_pc_warn, $mem_pc_alert);
	}
	print "</table>\n";
}

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

// ----------------- Cluster networks ------------------------------------

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

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

// ----------------- Cluster Datastore info ------------------------------------

$result = mysql_query("SELECT DISTINCT ds.lun, ds.name, size, used FROM ds JOIN esx2ds USING (dsid) JOIN esx USING (esxid) WHERE clid=". $_REQUEST["clid"] ." AND ds.exist=1 ORDER BY lun, name;");
if (mysql_num_rows($result) == 0) {
	print ("Cluster datastore info not found....");
} else {
	print "<table class='BasicTable' align='center'>\n<tr><th>LUN<th>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>".$row['lun']."<td>".$row['name']."<td>". round($row['size']/1024) ."<td class='bar'><div style='width: ".$used_pcent."%' ";
		if ($used_pcent >=$disk_pc_alert) {
			print "class='bar_alert'";
		} elseif ($used_pcent >= $disk_pc_warn) {
			print "class='bar_warn'";
		} else {
			print "class='bar_good'";
		}
		print ">".$used_pcent."</div>\n";
	}
	print "</table>\n";
}

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

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 ($name, $data_percent, $warn_threshold, $alert_threshold) {
	print "<td class='bar' title='".$name."'><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";
}
	
?>