VI-DB snaps.php v1: Difference between revisions

From vwiki
Jump to navigation Jump to search
(Initial creation)
 
m (Update category)
 
Line 136: Line 136:
</source>
</source>


[[Category:PHP]]
[[Category:PHP Script]]

Latest revision as of 10:12, 19 December 2011

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

<!--Version 1.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, vmid, snap.name, descr, UNIX_TIMESTAMP(created) AS created, quiesced, vm_on, size FROM snap JOIN vc USING (vcid) JOIN vm USING (vmid) ";
if ($vcid > 0) {
	$query .= "WHERE vc.vcid=$vcid ORDER BY vm, created;";
} else {
	$query .= "ORDER BY vc, 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>VM Name<th>Snapshot Name<th>Size<br><small>MB</size><th width=380>Description<th>Created\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['name']."<td class='num'>".number_format($snap['size'])."<td>".$snap['descr']."<td>".date("d-m-y H:i", $snap['created'])."\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)));
}
	
?>