Difference between revisions of "VI-DB snaps.php v2"

From vwiki
Jump to navigation Jump to search
(Initial creation)
 
(Incorrect content - corrected)
 
Line 7: Line 7:
<html>
<html>
<head>
<head>
<title>vi-status</title>
<title>vi-db (snaps)</title>
<link rel='SHORTCUT ICON' href='favicon.ico'>
<link rel='SHORTCUT ICON' href='favicon.ico'>
<link rel='stylesheet' type='text/css' href='alpha.css' />
<link rel='stylesheet' type='text/css' href='alpha.css' />
Line 13: Line 13:
<meta name='Description' content='VI Info'>
<meta name='Description' content='VI Info'>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" >
<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='Pragma' content='no-cache'>
<meta http-equiv="Refresh" content="150">
<meta http-equiv='Expires' content='900'>
<meta http-equiv='Expires' content='900'>
</head>
</head>
Line 24: Line 24:
//Local variables etc
//Local variables etc
require 'config.php';
require 'config.php';
$self = $_SERVER['PHP_SELF'];
$debug = 0;
// Import from config.php
global $snap_size, $snap_age;


// open connection and select database
// open connection and select database
Line 29: Line 34:
mysql_select_db($db) or die ("Unable to select 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";
//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 ";
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>Active Snapshots</h1>\n";
if (array_key_exists('vcid', $_REQUEST)) {
$vc_link = "&vcid=".$_REQUEST["vcid"];
$vcid=$_REQUEST["vcid"];
} else {
$vc_link = "";
$vcid=0;
}
// Initial query
$query = "SELECT vcid, vc.name AS vc, COUNT(sid) AS count, SUM(size) AS size FROM snap RIGHT JOIN vc USING (vcid) GROUP BY vc ORDER BY vc;";


print "<h1>Virtual Infrastructure Status Summary</h1>\n";
$result = mysql_query($query);
//print $query;
if (mysql_num_rows($result) == 0) {
die ("No snapshots found....");
}
$row = mysql_fetch_assoc($result);


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


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


$vcs = mysql_query("SELECT vcid, name, site, last_esx_check FROM vc ORDER BY site;");
print "<table class='BasicTable' align='center'>\n<tr><th>vCentre<th>Snapshots<th>Total Usage<br><small>MB</size>\n";
$snap_tot = 0;
while ($row = mysql_fetch_assoc($result)) {
print "<tr><td><a href='$self?vcid=".$row['vcid']."'>".$row['vc']."<td class='num'>".$row['count']."<td class='num'>".number_format($row['size']);
$snap_tot += $row['size'];
}
if (array_key_exists('vcid', $_REQUEST)) {
print "<tr><td align='center' colspan=2><a href=$self>For All vCentres</a><td class='num'>".number_format($snap_tot)."\n";
} else {
print "<tr><td align='center' colspan=2><b>&uarr; &uarr;</b> Filter On vCentre<td class='num'>".number_format($snap_tot)."\n";
}
print "</table>\n";


if (mysql_num_rows($vcs) == 0) {
// ------------------ Layout table cell (bottom)--------------------------
die ("vCentre info not found....!");
print "\n<tr><td valign='top'>\n";
 
// ----------------- Detail table ---------------------------------------
 
$query = "SELECT vc.name AS vc, vm.name AS vm, vc_path, vmid, snap.name, descr, UNIX_TIMESTAMP(created) AS created, quiesced, vm_on, size ";
$query .= "FROM snap JOIN vc USING (vcid) JOIN vm USING (vmid) JOIN vm_ext USING (vmid)";
if ($vcid > 0) {
$query .= "WHERE vc.vcid=$vcid ORDER BY";
} else {
$query .= "ORDER BY vc,";
}
}
$vcsite = "";
if (array_key_exists('sort', $_REQUEST)) {
while ($vc = mysql_fetch_assoc($vcs)) {
if ($_REQUEST["sort"] == 'folder') {
//Layout table handling
$query .= " vc_path, vm, created;";
if ($vcsite <> $vc['site']) {
} elseif ($_REQUEST["sort"] == 'vm') {
print "<tr><td valign='top'>";
$query .= " vm, created;";
$vcsite = $vc['site'];
} elseif ($_REQUEST["sort"] == 'snap') {
} else {
$query .= " snap.name, vm, created;";
print "<td valign='top'>";
}
}
} else {
// Virtual Centre
$query .= " vm, created;";
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;");
 
if ($debug == 1) {print ($query);}
// Cluster
$result = mysql_query($query);
while ($cluster = mysql_fetch_assoc($clusters)) {
//$date_old = date_sub(date_create(), date_interval_create_from_date_string('3 days'));
$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';";
$time_old = time() - ((24*60*60) * $snap_age);
$capacity = mysql_fetch_assoc(mysql_query($query));
print "<table class='BasicTable' align='center'>\n<tr><th>vCentre<th><a href='$self?sort=vm$vc_link'>VM Name</a><th><a href='$self?sort=folder$vc_link'>Folder</a><th><a href='$self?sort=snap$vc_link'>Snapshot Name</a><th>Size<br><small>MB</size><th width=380>Description<th>Created<th>Create<br>Email\n";
$spare_esx_cpu = floor( ($capacity['num_esx'] * $cpu_pc_alert - $capacity['sum_cpu']) / $cpu_pc_alert);
while ($snap = mysql_fetch_assoc($result)) {
$spare_esx_mem = floor( ($capacity['num_esx'] * $mem_pc_alert - $capacity['sum_mem']) / $mem_pc_alert);
if (($snap['size'] > $snap_size) or ($snap['created'] < $time_old)) {
if ($spare_esx_cpu > $spare_esx_mem) {
print "<tr class='warn'>";
$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 {
} else {
print "<tr><td colspan=4><span class='time_good'>Updated: " .$vc['last_esx_check']." hrs</span>";
print "<tr>";
}
}
print "</table>\n";
print "<td>".$snap['vc']."<td><a href='vm-detail.php?vmid=".$snap['vmid']."'>".$snap['vm']."</a><td>".$snap['vc_path']."<td>".$snap['name']."<td class='num'>".number_format($snap['size'])."<td>".$snap['descr']."<td>".date("d-m-y H:i", $snap['created']);
print "<td><a href='mailto:someone@michaelpage.com?subject=".$snap['vm']." snapshot...&body=Can the following snapshot for the above server be deleted?%0A";
print "%0AName:%09%09".$snap['name']."%0ADescrip.:%09".$snap['descr']."%0ACreated:%09".date("d-m-Y H:i", $snap['created'])."%0ASize:%09%09".number_format($snap['size'])." MB'>Mail</a>\n";
}
}
// ----------------- End of layout table ------------------------------
print "</table>\n";
print "</table>\n";


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


print "<br><a href='index.php'>VM Search page</a><br>\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);
$time = round((microtime(true) - $time_start)*1000);
Line 111: Line 137:


// ------------------- End of HTML ---------------------------------
// ------------------- 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>";
print "</body><html>";


Line 121: Line 145:
return (str_replace(array("*", "?"), array("%", "_"), trim($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);
}
}
?>
?>





Latest revision as of 10:15, 19 December 2011

<html xmlns='http://www.w3.org/TR/REC-html40'>

<!--Version 2.0 -->

<html>
<head>
<title>vi-db (snaps)</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'];
$debug = 0;

// Import from config.php
global $snap_size, $snap_age;

// 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 ";
print "<h1>Active Snapshots</h1>\n";

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

// Initial query

$query = "SELECT vcid, vc.name AS vc, COUNT(sid) AS count, SUM(size) AS size FROM snap RIGHT JOIN vc USING (vcid) GROUP BY vc ORDER BY vc;";

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

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

// ------------------- Summary table ------------------------

print "<table class='BasicTable' align='center'>\n<tr><th>vCentre<th>Snapshots<th>Total Usage<br><small>MB</size>\n";
$snap_tot = 0;
while ($row = mysql_fetch_assoc($result)) {
	print "<tr><td><a href='$self?vcid=".$row['vcid']."'>".$row['vc']."<td class='num'>".$row['count']."<td class='num'>".number_format($row['size']);
	$snap_tot += $row['size'];
}
if (array_key_exists('vcid', $_REQUEST)) {
	print "<tr><td align='center' colspan=2><a href=$self>For All vCentres</a><td class='num'>".number_format($snap_tot)."\n";
} else {
	print "<tr><td align='center' colspan=2><b>&uarr; &uarr;</b> Filter On vCentre<td class='num'>".number_format($snap_tot)."\n";
}
print "</table>\n";

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

// ----------------- Detail table ---------------------------------------

$query = "SELECT vc.name AS vc, vm.name AS vm, vc_path, vmid, snap.name, descr, UNIX_TIMESTAMP(created) AS created, quiesced, vm_on, size ";
$query .= "FROM snap JOIN vc USING (vcid) JOIN vm USING (vmid) JOIN vm_ext USING (vmid)";
if ($vcid > 0) {
	$query .= "WHERE vc.vcid=$vcid ORDER BY";
} else {
	$query .= "ORDER BY vc,";
}
if (array_key_exists('sort', $_REQUEST)) {
	if ($_REQUEST["sort"] == 'folder') {
		$query .= " vc_path, vm, created;";
	} elseif ($_REQUEST["sort"] == 'vm') {
		$query .= " vm, created;";
	} elseif ($_REQUEST["sort"] == 'snap') {
		$query .= " snap.name, vm, created;";
	}
} else {
	$query .= " vm, created;";
}

if ($debug == 1) {print ($query);}
$result = mysql_query($query);
//$date_old = date_sub(date_create(), date_interval_create_from_date_string('3 days'));
$time_old = time() - ((24*60*60) * $snap_age); 		
print "<table class='BasicTable' align='center'>\n<tr><th>vCentre<th><a href='$self?sort=vm$vc_link'>VM Name</a><th><a href='$self?sort=folder$vc_link'>Folder</a><th><a href='$self?sort=snap$vc_link'>Snapshot Name</a><th>Size<br><small>MB</size><th width=380>Description<th>Created<th>Create<br>Email\n";
while ($snap = mysql_fetch_assoc($result)) {
	if (($snap['size'] > $snap_size) or ($snap['created'] < $time_old)) {
		print "<tr class='warn'>";
	} else {
		print "<tr>";
	}
	print "<td>".$snap['vc']."<td><a href='vm-detail.php?vmid=".$snap['vmid']."'>".$snap['vm']."</a><td>".$snap['vc_path']."<td>".$snap['name']."<td class='num'>".number_format($snap['size'])."<td>".$snap['descr']."<td>".date("d-m-y H:i", $snap['created']);
	print "<td><a href='mailto:someone@michaelpage.com?subject=".$snap['vm']." snapshot...&body=Can the following snapshot for the above server be deleted?%0A";
	print "%0AName:%09%09".$snap['name']."%0ADescrip.:%09".$snap['descr']."%0ACreated:%09".date("d-m-Y H:i", $snap['created'])."%0ASize:%09%09".number_format($snap['size'])." MB'>Mail</a>\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)));
}
	
?>