https://vwiki.co.uk/index.php?title=VI-DB_vc-detail.php_v2.2&feed=atom&action=history
VI-DB vc-detail.php v2.2 - Revision history
2024-03-29T10:33:35Z
Revision history for this page on the wiki
MediaWiki 1.37.2
https://vwiki.co.uk/index.php?title=VI-DB_vc-detail.php_v2.2&diff=1592&oldid=prev
Sstrutt: Initial creation
2011-12-19T10:37:45Z
<p>Initial creation</p>
<p><b>New page</b></p><div><source lang="php"><br />
<br />
<html xmlns='http://www.w3.org/TR/REC-html40'><br />
<br />
<!-- Version 2.2 --><br />
<br />
<html><br />
<head><br />
<title>vi-db (vc)</title><br />
<link rel='SHORTCUT ICON' href='favicon.ico'><br />
<link rel='stylesheet' type='text/css' href='alpha.css' /><br />
<meta name='Author' content='Simon Strutt'><br />
<meta name='Description' content='VI Info'><br />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" ><br />
<meta http-equiv="Content-Type" content="image/jpeg" ><br />
<meta http-equiv='Pragma' content='no-cache'><br />
<meta http-equiv='Expires' content='900'><br />
</head><br />
<body><br />
<br />
<?php<br />
$time_start = microtime(true);<br />
<br />
//Local variables etc<br />
require 'config.php';<br />
$self = $_SERVER['PHP_SELF'];<br />
<br />
// open connection and select database<br />
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect to database!");<br />
mysql_select_db($db) or die ("Unable to select database!");<br />
<br />
//Set debug on if required<br />
if (array_key_exists('debug', $_REQUEST) and ($_REQUEST["debug"] == 1)) {<br />
$debug = 1;<br />
}<br />
<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";<br />
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 ";<br />
<br />
if (!array_key_exists('vcid', $_REQUEST)) {<br />
die ("Error - required vcid parameter does not exist!");<br />
}<br />
$vcid = $_REQUEST["vcid"];<br />
$data_link = "vcid=$vcid";<br />
<br />
// show parameters and errors<br />
if ($debug) {<br />
print "<br>";<br />
error_reporting(-1);<br />
print_r($_REQUEST);<br />
print "<br>";<br />
$data_link .= "&debug=1";<br />
}<br />
<br />
if (!array_key_exists('view', $_REQUEST)) {<br />
$view = "resource";<br />
} else {<br />
if ($_REQUEST["view"] == "vm") {<br />
$view = "vm";<br />
$data_link .= "&view=vm";<br />
} elseif ($_REQUEST["view"] == "resource") {<br />
$view = "resource";<br />
$data_link .= "&view=resource";<br />
} else {<br />
die ("Error - view parameter error!");<br />
}<br />
}<br />
<br />
if (array_key_exists('clid', $_REQUEST)) {<br />
$clid=$_REQUEST["clid"];<br />
$data_link .= "&clid=$clid";<br />
$cl_link="&clid=$clid";<br />
} else {<br />
$clid=0;<br />
$cl_link="";<br />
}<br />
<br />
if ($debug) {<br />
print "$data_link<br>";<br />
}<br />
<br />
// Initial query (need VC name for page title)<br />
<br />
$query = "SELECT name, site, ver, build FROM vc WHERE vcid=$vcid;";<br />
<br />
$result = mysql_query($query);<br />
if ($debug) {print $query;}<br />
if (mysql_num_rows($result) == 0) {<br />
die ("vCentre not found....");<br />
}<br />
$row = mysql_fetch_assoc($result);<br />
<br />
print "<h1>".$row['name'] ."</h1>\n";<br />
<br />
// ----------------- Top Layout table (left) ----------------------------<br />
print "<table align='center' cellspacing=10><tr><td valign='top'>\n";<br />
<br />
// ------------------ vCentre info -----------------------------------------<br />
print "<table class='BasicTable' align='left'>\n<tr><th>Site:<td>".$row['site']."\n";<br />
print "<tr><th>Version:<td>".$row['ver']."\n";<br />
print "<tr><th>Build:<td>".$row['build']."</table>\n";<br />
<br />
// ------------------ Top Layout table cell (middle)--------------------------<br />
print "\n<td valign='top'>\n";<br />
<br />
// ------------------- vCentre Cluster info ------------------------------------<br />
$query = "SELECT clid, name, ha, drs FROM cluster WHERE vcid=$vcid AND exist=1;";<br />
if ($debug) {print ($query);}<br />
$clusters = mysql_query($query);<br />
if (mysql_num_rows($result) == 0) {<br />
print ("Cluster info not found....");<br />
} else {<br />
print "<table class='BasicTable' align='left'>\n<tr><th>Name<th>HA<th>DRS<th>ESXs<th>VMs<br><small>(all)</small><th>VMs<br><small>(on)</small>\n";<br />
while ($cluster = mysql_fetch_assoc($clusters)) {<br />
if ($clid == $cluster['clid']) {<br />
print "<tr><td><b><a href='$self?vcid=$vcid&clid=".$cluster['clid']."&view=$view'>".$cluster['name']."</a></b><td>";<br />
} else {<br />
print "<tr><td><a href='$self?vcid=$vcid&clid=".$cluster['clid']."&view=$view'>".$cluster['name']."</a><td>";<br />
}<br />
if ($cluster['ha']) {<br />
print "Enabled";<br />
} else {<br />
print "Disabled";<br />
}<br />
if ($cluster['drs']) {<br />
print "<td>".$cluster['drs'];<br />
} else {<br />
print "<td>Disabled";<br />
}<br />
$esxs = mysql_fetch_assoc(mysql_query("SELECT COUNT(*) AS total FROM esx WHERE clid=".$cluster['clid']." AND exist=1;"));<br />
print "<td class='num'>".$esxs['total'];<br />
$vms = mysql_fetch_assoc(mysql_query("SELECT COUNT(*) AS total, SUM(IF(is_on=1,1,0)) AS is_on FROM vm WHERE clid=".$cluster['clid']." AND exist=1;"));<br />
print "<td class='num'>".$vms['total']."<td class='num'>".$vms['is_on'];<br />
}<br />
print "</table>";<br />
}<br />
// ------------------- Top Layout table cell (right) ----------------------<br />
print "\n<td valign='top'>\n";<br />
<br />
// ----------------- View Selector -----------------------------------------<br />
print "<table class='BasicTable' align='left'>\n<tr><th width=180 colspan=2>View Selector\n";<br />
if (array_key_exists('clid', $_REQUEST)) {<br />
print "<tr><td align='center' colspan=2><a href=$self?vcid=$vcid&view=$view>For All Clusters</a>\n";<br />
} else {<br />
print "<tr><td align='center' colspan=2><b>&lt &lt</b> Filter On Cluster <b>&lt &lt</b>\n";<br />
}<br />
if ($view == "vm") {<br />
print "<tr><th rowspan=2 width=40>Show:<td align='center'><b>Virtual Machines</b>\n<tr><td align='center'><a href=$self?vcid=$vcid$cl_link&view=resource>Cluster Resources</a>\n";<br />
} elseif ($view == "resource") {<br />
print "<tr><th rowspan=2 width=40>Show:<td align='center'><a href=$self?vcid=$vcid$cl_link&view=vm>Virtual Machines\n<tr><td align='center'><b>Cluster Resources</b>\n";<br />
} else {<br />
print "<tr>ERROR...!!!\n";<br />
}<br />
<br />
print "</table>";<br />
<br />
// ------------------- Bottom layout table cell (left) ----------------------<br />
print "\n</table>\n";<br />
<br />
if ($view == "vm") {<br />
show_vms();<br />
} elseif ($view == "resource") {<br />
print "<table align='center'><tr><td valign='top'>\n";<br />
show_esxs();<br />
print "<td valign='top'>\n";<br />
show_nws();<br />
print "<td valign='top'>\n";<br />
show_dss();<br />
print "</table>\n";<br />
} else {<br />
print "ERROR...!!!";<br />
}<br />
<br />
print "<br><a href='index.php'>Search page</a> &nbsp <a href='status.php'>Status page</a><br>\n";<br />
<br />
$time = round((microtime(true) - $time_start)*1000);<br />
print "Page took $time msecs to execute on server";<br />
<br />
// ------------------- End of HTML ---------------------------------<br />
print "</body><html>";<br />
<br />
// ================ FUNCTIONS ============================<br />
<br />
function search_char ($text) {<br />
<br />
return (str_replace(array("*", "?"), array("%", "_"), trim($text)));<br />
}<br />
<br />
function bar_graph ($data_percent, $warn_threshold, $alert_threshold) {<br />
print "<td class='bar' title='$data_percent'><div style='width: $data_percent%' ";<br />
if ($data_percent >= $alert_threshold) {<br />
print "class='bar_alert'";<br />
} elseif ($data_percent >= $warn_threshold) {<br />
print "class='bar_warn'";<br />
} else {<br />
print "class='bar_good'";<br />
}<br />
print ">".$data_percent."</div>\n";<br />
}<br />
<br />
function show_esxs() {<br />
global $debug, $vcid, $clid;<br />
global $cpu_pc_warn, $cpu_pc_alert,$mem_pc_warn, $mem_pc_alert;<br />
$query = "SELECT name, esx_state.state, esx_state.status, cpu_pc, mem_pc FROM esx JOIN esx_state USING (esxid) WHERE exist=1 ";<br />
if ($clid > 0) {<br />
$query .= "AND clid=$clid ORDER BY name;";<br />
} else {<br />
$query .= "AND vcid=$vcid ORDER BY name;";<br />
}<br />
if ($debug) {print ($query);}<br />
$esxs = mysql_query($query);<br />
if (mysql_num_rows($esxs) == 0) {<br />
print ("Cluster ESX info not found....");<br />
} else {<br />
print "<table class='BasicTable' align='left'>\n<tr><th width=35>State<th width=165>ESX<th>CPU<th>Memory\n";<br />
while ($esx = mysql_fetch_assoc($esxs)) {<br />
if ($esx['state'] == "Connected") {<br />
print "<tr><td>Live";<br />
} elseif ($esx['state'] == "NotResponding") {<br />
print "<tr><td class='alert'>Down";<br />
} elseif ($esx['state'] == "Maintenance") {<br />
print "<tr><td class='warn'>Maint";<br />
} elseif ($esx['state'] == "Disconnected") {<br />
print "<tr><td class='alert'>disc";<br />
} else {<br />
print "<tr><td>UNEX";<br />
}<br />
<br />
print "<td>".$esx['name'];<br />
if ($esx['status'] == "yellow") {<br />
print " <span class='status_warn'> has warning</span>";<br />
} elseif ($esx['status'] == "red") {<br />
print " <span class='status_alert'> has alert</span>";<br />
}<br />
<br />
bar_graph ($esx['cpu_pc'], $cpu_pc_warn, $cpu_pc_alert);<br />
bar_graph ($esx['mem_pc'], $mem_pc_warn, $mem_pc_alert);<br />
}<br />
print "</table>\n";<br />
}<br />
}<br />
<br />
function show_nws() {<br />
global $debug, $vcid, $clid;<br />
$query = "SELECT DISTINCT nw.vlan, nw.name FROM nw JOIN esx2nw USING (nwid) JOIN esx USING (esxid) WHERE nw.exist=1 ";<br />
if ($clid > 0) {<br />
$query .= "AND clid=$clid ORDER BY vlan, name;";<br />
} else {<br />
$query .= "AND nw.vcid=$vcid ORDER BY vlan, name;";<br />
}<br />
if ($debug) {print ($query);}<br />
$result = mysql_query($query);<br />
if (mysql_num_rows($result) == 0) {<br />
print ("vCentre networks info not found....");<br />
} else {<br />
print "<table class='BasicTable' align='left'>\n<tr><th>VLAN<th width=300>Network name\n";<br />
while ($row = mysql_fetch_assoc($result)) {<br />
print "<tr><td class='num'>".$row['vlan']."<td>".$row['name']."\n";<br />
}<br />
print "</table>\n";<br />
}<br />
}<br />
<br />
function show_dss() {<br />
global $debug, $vcid, $clid;<br />
global $disk_pc_warn, $disk_pc_alert;<br />
$query = "SELECT DISTINCT ds.lun, ds.name, ds.dsid, size, used FROM ds JOIN esx2ds USING (dsid) JOIN esx USING (esxid) WHERE ds.exist=1 ";<br />
if ($debug) {print ($query);}<br />
if ($clid > 0) {<br />
$query .= "AND clid=$clid ORDER BY lun, name;";<br />
} else {<br />
$query .= "AND ds.vcid=$vcid ORDER BY lun, name;";<br />
}<br />
if ($debug) {print ($query);}<br />
$result = mysql_query($query);<br />
if (mysql_num_rows($result) == 0) {<br />
print ("vCentre datastores info not found....");<br />
} else {<br />
print "<table class='BasicTable' align='center'>\n<tr><th>LUN<th width=200>Datastore name<th>Size (GB)<th width=50>Used (%)\n";<br />
while ($row = mysql_fetch_assoc($result)) {<br />
$used_pcent = round($row['used']/$row['size']*100);<br />
print "<tr><td class='num'>".$row['lun']."<td><a href='ds-info.php?dsid=".$row['dsid']."'>".$row['name']."</a><td>". round($row['size']/1024);<br />
print bar_graph($used_pcent, $disk_pc_warn, $disk_pc_alert);<br />
}<br />
print "</table>\n";<br />
}<br />
}<br />
<br />
function show_vms() {<br />
global $debug, $vcid, $clid, $data_link, $self, $_REQUEST;<br />
<br />
$query = "SELECT vm.name AS vm, INET_NTOA(vm2ip.ip) AS ip, cluster.name AS cluster, cluster.clid, esx.name AS esx, vm.vmid, vm.is_on";<br />
$query .= ", vm_ext.cpu, vm_ext.mem, vm_ext.vc_path FROM vm JOIN vm_ext USING (vmid) ";<br />
$query .= "LEFT JOIN vm2ip USING (vmid) JOIN cluster USING (clid) JOIN esx USING (esxid) WHERE vm.exist=1 ";<br />
if ($clid > 0) {<br />
$query .= "AND cluster.clid=$clid GROUP BY vm.vmid ";<br />
} else {<br />
$query .= "AND vm.vcid=$vcid GROUP BY vm.vmid ";<br />
}<br />
<br />
$th_ESX = "ESX";<br />
$th_Folder = "Folder";<br />
$th_Name = "Name";<br />
$th_IP = "IP";<br />
<br />
if (array_key_exists('sort1', $_REQUEST)) {<br />
$sort1 = $_REQUEST["sort1"];<br />
$sort_link = "&sort2=$sort1";<br />
<br />
switch ($sort1) {<br />
case "esx":<br />
$query .= "ORDER BY esx.name";<br />
$th_ESX = "ESX<br><small>&uarr;1&uarr;</small>";<br />
break;<br />
case "dir":<br />
$query .= "ORDER BY vm_ext.vc_path";<br />
$th_Folder = "Folder<br><small>&uarr;1&uarr;</small>";<br />
break;<br />
case "vm":<br />
$query .= "ORDER BY vm.name";<br />
$th_Name = "Name<br><small>&uarr;1&uarr;</small>";<br />
break;<br />
case "ip";<br />
$query .= "ORDER BY vm2ip.ip";<br />
$th_IP = "IP<br><small>&uarr;1&uarr;</small>";<br />
break;<br />
default;<br />
$query .= "ORDER BY vm.name";<br />
$th_Name = "Name<br><small>&uarr;1&uarr;</small>";<br />
}<br />
<br />
if (array_key_exists('sort2', $_REQUEST)) {<br />
switch ($_REQUEST["sort2"]) {<br />
case "esx":<br />
$query .= ", esx.name;";<br />
$th_ESX = "ESX<br><small>&uarr;2&uarr;</small>";<br />
break;<br />
case "dir":<br />
$query .= ", vm_ext.vc_path;";<br />
$th_Folder = "Folder<br><small>&uarr;2&uarr;</small>";<br />
break;<br />
case "vm":<br />
$query .= ", vm.name;";<br />
$th_Name = "Name<br><small>&uarr;2&uarr;</small>";<br />
break;<br />
case "ip";<br />
$query .= ", vm2ip.ip;";<br />
$th_IP = "IP<br><small>&uarr;2&uarr;</small>";<br />
break;<br />
default;<br />
$query .= ", vm.name;";<br />
}<br />
} else {<br />
$query .= ";";<br />
}<br />
<br />
} else {<br />
$sort_link = "";<br />
$query .= "ORDER BY cluster.name, vm.name;";<br />
}<br />
<br />
if ($debug) {print ($query);}<br />
$result = mysql_query($query);<br />
if (!$result) {<br />
print ("<b>Query failed !!!!<br>Query:</b> $query<br><b>Error:</b> ". mysql_error());<br />
}<br />
if (mysql_num_rows($result) == 0) {<br />
print ("No VMs found....");<br />
return;<br />
}<br />
<br />
print "<table class='BasicTable' align='center' valign='top'><tr><th><a href='$self?$data_link&sort1=esx$sort_link'>$th_ESX</a><th><a href='$self?$data_link&sort1=dir$sort_link'>$th_Folder</a>";<br />
print "<th width=170><a href='$self?$data_link&sort1=vm$sort_link'>$th_Name</a><th>State<th><a href='$self?$data_link&sort1=ip$sort_link'>$th_IP</a><th>vCPU<th>Mem<br><small>MB</small>\n";<br />
$current_vmid = 0;<br />
$odd_row = false;<br />
while ($row = mysql_fetch_assoc($result)) {<br />
$odd_row = !$odd_row;<br />
if ($odd_row) {<br />
print "<tr class='odd'>";<br />
} else {<br />
print "<tr>";<br />
}<br />
if ($row['is_on']) {<br />
$vm_state = "ON";<br />
} else {<br />
$vm_state = "OFF";<br />
}<br />
print "<td>".$row['esx']."<td>".$row['vc_path']."<td><a href='vm-detail.php?vmid=".$row['vmid']."'><b>".$row['vm']."</b></a>";<br />
print "<td class='num'>".$vm_state."<td class='num'>".$row['ip']."<td class='num'>".$row['cpu']."<td class='num'>".$row['mem']."\n";<br />
}<br />
print "</table>\n";<br />
}<br />
<br />
?><br />
<br />
<br />
<br />
<br />
</source><br />
<br />
[[Category:PHP Script]]</div>
Sstrutt