VI-DB snaps.php v2

From vwiki
Revision as of 10:13, 19 December 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.0 -->

<html>
<head>
<title>vi-status</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='Pragma' content='no-cache'>
<meta http-equiv="Refresh" content="150"> 
<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!");

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

print "<h1>Virtual Infrastructure Status Summary</h1>\n";

// ----------------- Layout table --------------------------------
print "<table align='center' cellspacing=10><tr>\n";

// ----------------- vCentre table -------------------------------

$vcs = mysql_query("SELECT vcid, name, site, last_esx_check FROM vc ORDER BY site;");

if (mysql_num_rows($vcs) == 0) {
	die ("vCentre info not found....!");
}
$vcsite = "";
while ($vc = mysql_fetch_assoc($vcs)) {
	//Layout table handling
	if ($vcsite <> $vc['site']) {
		print "<tr><td valign='top'>";
		$vcsite = $vc['site'];
	} else {
		print "<td valign='top'>";
	}
	
	// Virtual Centre
	print "<table class='BasicTable' align='center'>\n<tr><th width=35>".$vc['site']."<th colspan=3>". strtoupper($vc['name']);
	$clusters = mysql_query("SELECT clid, name FROM cluster WHERE vcid=".$vc['vcid']." AND exist=1;");
	
	// Cluster
	while ($cluster = mysql_fetch_assoc($clusters)) {
		$query = "SELECT COUNT(*) AS num_esx, SUM(cpu_pc) AS sum_cpu, SUM(mem_pc) AS sum_mem FROM esx JOIN esx_state USING (esxid) WHERE clid=".$cluster['clid']." AND exist=1 AND esx_state.state='Connected';";
		$capacity = mysql_fetch_assoc(mysql_query($query));
		$spare_esx_cpu = floor( ($capacity['num_esx'] * $cpu_pc_alert - $capacity['sum_cpu']) / $cpu_pc_alert);
		$spare_esx_mem = floor( ($capacity['num_esx'] * $mem_pc_alert - $capacity['sum_mem']) / $mem_pc_alert);
		if ($spare_esx_cpu > $spare_esx_mem) {
			$n = $spare_esx_mem;
		} else {
			$n = $spare_esx_cpu;
		}
		print "<tr><td colspan=2 width=225><b><a href='cluster-detail.php?clid=".$cluster['clid']."'>".$cluster['name']."</a></b>";
		if ($n >= 2) {
			print "<td colspan=2><b>n + ". $n."</b>";
		} elseif ($n == 1) {
			print "<td colspan=2><span class='status_warn'><b>n + ". $n."</b></span>";
		} else {
			print "<td colspan=2><span class='status_alert'><b>n + ". $n."</b></span>";
		}
		
		$esx_states = mysql_query("SELECT name, esx_state.state, esx_state.status, cpu_pc, mem_pc FROM esx JOIN esx_state USING (esxid) WHERE clid=".$cluster['clid']." AND exist=1 ORDER BY name;");
		list_esx_states($esx_states);
		
	}
	
	// Standalone ESX's
	$esx_states = mysql_query("SELECT name, esx_state.state, esx_state.status, cpu_pc, mem_pc FROM esx JOIN esx_state USING (esxid) WHERE ISNULL(clid) AND vcid=".$vc['vcid']." AND exist=1 ORDER BY name;");
	if (mysql_num_rows($esx_states) > 0) {
		print "<tr><td colspan=4><b>Standalone</b>";
		list_esx_states($esx_states);
	}
	
	if (is_null($vc['last_esx_check']) or (strtotime($vc['last_esx_check']) < (time() - 900))) {
		print "<tr><td colspan=4 class='time_warn'>Updated: " .$vc['last_esx_check']." hrs";
	} else {
		print "<tr><td colspan=4><span class='time_good'>Updated: " .$vc['last_esx_check']." hrs</span>";
	}
	print "</table>\n";
}

// ----------------- End of layout table ------------------------------
print "</table>\n";


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

print "<br><a href='index.php'>VM 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 "<table class='NavTable' align='center' valign='top'><th>Main Pages:<td><a href='index.php'>Search page</a><td><a href='status.php'>Status page</a></table>";
print "<table class='NavTableSub' align='center' valign='top'><th>Reports:<td><a href='snaps.php'>Snapshots</a></table>";
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_small' 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";
}

function list_esx_states($esxs) {
	global $cpu_pc_warn, $cpu_pc_alert,$mem_pc_warn, $mem_pc_alert;
	while ($esx = mysql_fetch_assoc($esxs)) {
		if (strcasecmp($esx['state'], "Connected") == 0) {
			print "<tr><td>Live";
		} elseif (strcasecmp($esx['state'], "NotResponding") == 0) {
			print "<tr><td class='alert'>Down";
		} elseif (strcasecmp($esx['state'], "Maintenance") == 0) {
			print "<tr><td class='warn'>Maint";
		} elseif (strcasecmp($esx['state'], "Disconnected") == 0) {
			print "<tr><td class='alert'>discon.";
		} elseif (strcasecmp($esx['state'], "Gone") == 0) {
			print "<tr class='unknown'><td>Gone";
		} else {
			print "<tr><td class='unknown'>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);
	}
}	

?>