VI-DB vnics.php v2

From vwiki
Revision as of 14:07, 31 August 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-db (vnics)</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 ";

print "<h1>VM's with Non Ideal vNICs Drives</h1>\n";

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

//Initial summary query
$query = "SELECT vc.name AS vc, cluster.clid, cluster.name AS cluster, COUNT(vmid) AS VMs FROM vm JOIN vc USING (vcid) JOIN cluster USING (clid) JOIN vm_ext USING (vmid) JOIN vm_nic USING (vmid) ";
$query .= "WHERE vm.exist=1 AND type!='Network adapter' AND type!='Labs NAT' AND (type!='vmxnet3' AND vm_ver=7 OR type!='EnhancedVmxnet' AND vm_ver=4) ";
$query .= "GROUP BY clid ORDER BY vc, cluster.name;";

$result = mysql_query($query);
if ($debug == 1) {print ($query);}
if (mysql_num_rows($result) == 0) {
	die ("No machines found...!");
}

print "<table class='BasicTable' align='center'>\n<tr><th>vCentre<th>Cluster<th>VM's\n";
while ($row = mysql_fetch_assoc($result)) {
	print "<tr><td>".$row['vc']."<td><a href='$self?clid=".$row['clid']."'>".$row['cluster']."<td class='num'>".$row['VMs'];
}
if (array_key_exists('clid', $_REQUEST)) {
	print "<tr><td align='center' colspan=3><a href=$self>For All Clusters</a>\n";
} else {
	print "<tr><td align='center' colspan=3><b>&uarr; &uarr;</b> Filter On Cluster\n";
}
print "</table><br>\n";

// Detail query
$query = "SELECT vc.name AS vc, cluster.name AS cl, vm.vmid, vm.name AS vm, vm_ext.vc_path, vm_nic.num, vm_nic.type FROM vm JOIN vc USING (vcid) JOIN cluster USING (clid) JOIN vm_ext USING (vmid) JOIN vm_nic USING (vmid) ";
$query .= "WHERE vm.exist=1 AND type!='Network adapter' AND type!='Labs NAT' AND (type!='vmxnet3' AND vm_ver=7 OR type!='EnhancedVmxnet' AND vm_ver=4) ";
if ($clid > 0) {
	$query .= "AND cluster.clid=$clid ORDER BY cl, vc_path, vm, num;";
} else {
	$query .= "ORDER BY cl, vc_path, vm, num;";
}
if ($debug == 1) {print ($query);}
$result = mysql_query($query);

if (mysql_num_rows($result) == 0) {
	die ("None...!");
}
print "<table class='BasicTable' align='center'>\n<tr><th>vCentre<th>Cluster<th>Folder<th>VM Name<th>vNIC<th>Type\n";

$vm_nics = mysql_fetch_all($result, MYSQL_ASSOC);
$odd_row = false;
foreach ($vm_nics as $vnic) {
	$odd_row = !$odd_row;
	if ($odd_row) {
		print "<tr class='odd'>";
	} else {
		print "<tr>";
	}
	print "<td>".$vnic['vc']."<td>".$vnic['cl']."<td>".$vnic['vc_path']."<td><a href=vm-detail.php?vmid=".$vnic['vmid'].">".$vnic['vm']."</a><td class='num'>".$vnic['num']."<td>".$vnic['type']."\n";
}
print "</table><br>";


print "<br><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><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 mysql_fetch_all($result) {
   while($row=mysql_fetch_array($result, MYSQL_ASSOC)) {
       $return[] = $row;
   }
   return $return;
}
	
?>