/[projects]/misc/xenconsole/index.php
ViewVC logotype

Diff of /misc/xenconsole/index.php

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1881 by torben, Fri Nov 30 09:09:06 2012 UTC revision 2018 by torben, Thu Jul 18 13:00:14 2013 UTC
# Line 1  Line 1 
1  <?php  <?php
2  include("config.php");  require("config.php");
3    
4  function format_memory($size) {  function format_memory($size) {
5    if (1024 > $size) {    if (1024 > $size) {
# Line 28  include('xenapi.php'); Line 28  include('xenapi.php');
28  /* Establish session with Xenserver */  /* Establish session with Xenserver */
29  $xenserver = new XenApi($url, $login, $password);  $xenserver = new XenApi($url, $login, $password);
30    
31    $hosts_array = $xenserver->host__get_all();
32    $host = $xenserver->host__get_record($hosts_array[0]);
33    $host_metrics = $xenserver->host_metrics__get_record($host["metrics"]);
34    
35    $xenversion = $host["software_version"]["product_brand"] . " " . $host["software_version"]["product_version"];
36    $xenversion .= " / Linux:" . $host["software_version"]["linux"] . " / xen: " . $host["software_version"]["xen"] . " / xapi: " . $host["software_version"]["xapi"] ;
37    
38    $expire = split('T', $host["license_params"]["expiry"]);
39    $license = "License: " . $host["license_params"]["sku_type"] . ", expires " . $expire[0] ;
40    
41  $vms_array = $xenserver->VM__get_all_records();  $vms_array = $xenserver->VM__get_all_records();
42    
43    $namelabel = $host["name_label"];
44    
45  ?>  ?>
46  <html>  <html>
47  <head>  <head>
48  <title>XenServer::SERVER</title>  <title>XenServer::<?php echo $namelabel;?></title>
49  <script type='text/javascript' src="jquery-1.8.2.min.js"></script>  
50    <link rel="stylesheet" type="text/css" href="jquery/theme/jquery-ui.css">
51    <style>
52    .ui-menu {
53            width: 200px;
54    }
55    </style>
56    <script type='text/javascript' src="jquery/jquery-1.9.1.min.js"></script>
57    <script type='text/javascript' src="jquery/jquery-ui-1.10.2.min.js"></script>
58    
59  <script type='text/javascript'>  <script type='text/javascript'>
60    var menu = 0;
61    var menu_uuid = '';
62    
63  $(document).ready( function() {  $(document).ready( function() {
64          loadServer();          $("#menu").hide();
65            setInterval(refreshData, 60000);        
66            refreshData();
67            $('.console').click( function() {
68                    var session = $(this).data('session');
69                    var conurl = $(this).data('conurl');
70                    var name = $(this).data('name');
71                    openConsole(conurl,session,name);
72            });
73    
74            $('#logo').click( function() {
75                    refreshData();
76            });
77    
78            $('.settings').click( function(event) {
79                    menu_uuid = $(this).attr('uuid');
80    
81                    $('#menu').menu( {
82                            select: function(event2,ui) {
83                                            var action = $(ui.item).attr("action");
84                                            if (action == "memory") {
85                                                    doAction("setMemory", menu_uuid, "");  
86                                            }
87                                            if (action == "cpu") {
88                                                    doAction("setCPU", menu_uuid, "");      
89                                            }
90                                    },
91                            create: function(event3,ui) {
92                                    menu = 1;
93                                    }
94                            /*position: {
95                                    my: "left", of: event
96                            }*/
97                    });
98                    $('#menu').show().position( {my: "left top", of: event} );
99                    event.stopPropagation();
100            });
101    
102            $('.cd').click( function(event) {
103                    var uuid = $(this).attr('uuid');
104                    cdSelectorDialog(uuid);
105            });
106    
107            $(document).click( function(event) {
108                    closeMenu();
109            });
110  });  });
111    
112  function doAction(action, uuid) {  function cdSelectorDialog(uuid) {
113            $('#cdselector').html('');
114            $('#cdselector').load( 'getisolist.php' );
115            var cddata;
116    
117            $.getJSON('getcdinfo.php?uuid=' + uuid, function(data) {
118                    cddata = data;
119                    if (data.ISO != '') {
120                            $('#cdcurrent').html(  data.ISO );
121                    } else {
122                            $('#cdcurrent').html('<i>No ISO currently mounted</i>');
123                    }
124            });
125            
126            $('#dialog-cd').dialog({
127                    modal: true,
128                    width: 800,
129                    height: 300,
130                    buttons: {
131                            Mount: function() {
132                                    $( this ).dialog( "close" );
133                                    var vdi = $("#cdselector").val();
134                                    cdAction("mount", cddata.VBD, vdi);
135                            },
136                            Eject: function() {
137                                    $( this ).dialog( "close" );
138                                    cdAction("eject", cddata.VBD, "");
139                            },
140                            Cancel: function() {
141                                    $( this ).dialog( "close" );
142                            }
143                            
144                    }
145            });
146    }
147    
148    function closeMenu() {
149            if (menu > 0) {
150                    $("#menu").menu("destroy").hide();
151                    menu = 0;
152            }
153    }
154    
155    function refreshData() {
156            $("#logo").hide();      
157            $.get('ajaxdata.php', function(xml) {
158                    $(xml).find('host').each(function() {
159                            var memtotal = $(this).find('memtotal').text();
160                            var memfree = $(this).find('memfree').text();  
161                            var cpuavg = $(this).find('cpuavg').text();
162    
163                            memtotal = Math.round ( memtotal / (1024*1024) );
164                            memfree = Math.round ( memfree / (1024*1024) );
165                            var memused = memtotal - memfree;
166                            var mem_percentage = Math.round( (memused/memtotal) * 100);
167    
168                            //alert(memused + ' ' + mem_percentage);
169    
170                            $('#server_memory_usage').attr('src', 'usagebar.php?usage=' + mem_percentage);
171                            $('#server_memory_usage').attr('alt', mem_percentage + '%');
172                            $('#server_memory_usage').attr('title', mem_percentage + '%');
173                            $('#server_memory_usage_txt').text( memused + '/' + memtotal + 'MB');
174    
175    
176                            $('#server_cpu_usage').attr('src', 'usagebar.php?usage=' + cpuavg);
177                            $('#server_cpu_usage').attr('alt', cpuavg + '%');
178                            $('#server_cpu_usage').attr('title', cpuavg + '%');
179                            $('#server_cpu_usage_txt').text( cpuavg+ '%');
180                    });
181                    $(xml).find('vm').each(function() {
182                            var name = $(this).find('name').text();
183                            var state = $(this).find('state').text();
184                            var network = $(this).find('network').text();
185                            var state = $(this).find('state').text();
186                            var conurl = $(this).find('conurl').text();
187                            var session = $(this).find('session').text();
188                            var os = $(this).find('os').text();
189                            var guestversion = $(this).find('guestversion').text();
190                            var cpuavg = $(this).find('cpuavg').text();
191                            var curmem = $(this).find('curmem').text();
192                            var maxmem = $(this).find('maxmem').text();
193                            var cpus = $(this).find('cpus').text();
194    
195                            var mempercent = Math.round( (curmem*100) / maxmem );
196    
197                            name = name.replace(/ /g, "_");
198                            name = name.replace(/\./g, "_");
199                            name = name.replace(/\(/g, "_");
200                            name = name.replace(/\)/g, "_");
201                            
202                            var id = "#vm_" + name;
203    
204                            var vm = $(id);                
205    
206                            if (guestversion != '') {
207                                    os += ' Guest Tools: ' + guestversion;
208                            }
209    
210                            vm.find('.vps_memory_usage_txt_UID').text( maxmem + " MB");
211                            vm.find('.vps_cpu_usage_txt_UID').text( cpus + " VCPU");
212    
213                            if (state == "Running") {
214                                    vm.find('.state').css("background-image", "url('gfx/vps_topgreen.png')");
215                                    vm.find('.network').show();
216                                    vm.find('.network').text( '(IP: ' + network + ')' );
217                                    vm.find('.console').show();
218                                    vm.find('.console').data('conurl', conurl);
219                                    vm.find('.console').data('session', session);
220                                    vm.find('.console').data('name', name);
221                                    vm.find('.settings').hide();
222                                    vm.find('.actionstop').show();
223                                    vm.find('.actionstart').hide();
224                                    vm.find('.os').text(' - ' + os);
225    
226    
227                                    vm.find('.cpu_graph').show();
228                                    vm.find('.cpu_graph').attr('src', 'usagebar.php?usage=' + cpuavg);
229                                    vm.find('.cpu_graph').attr('title',  cpuavg + '%');
230    
231                                    vm.find('.mem_graph').show();
232                                    vm.find('.mem_graph').attr('src', 'usagebar.php?usage=' + mempercent);
233                                    vm.find('.mem_graph').attr('title',  curmem + ' / ' + maxmem + ' MB' );
234                            } else {
235                                    if (state == "Halted") {
236                                            vm.find('.state').css("background-image", "url('gfx/vps_topred.png')");
237                                    } else {
238                                            vm.find('.state').css("background-image", "url('gfx/vps_topyellow.png')");
239                                    }
240                                    vm.find('.os').text('');
241    
242                                    vm.find('.network').hide();
243                                    vm.find('.console').hide();
244                                    vm.find('.settings').show();
245                                    vm.find('.actionstop').hide();
246                                    vm.find('.actionstart').show();
247                                    vm.find('.cpu_graph').hide();
248                                    vm.find('.mem_graph').hide();
249                            }
250                            
251                    });
252                    $('#logo').show();
253           });
254    }
255    
256    function doAction(action, uuid, vm) {
257            var val="";
258    
259          var key = prompt("Key");          var key = prompt("Key");
260          if (key == "" || key == null) {          if (key == "" || key == null) {
261                  return;                  return;
262          }          }
263          document.body.style.cursor = 'wait';          document.body.style.cursor = 'wait';
264            $('#vm_' + vm).find('.state').css("background-image", "url('gfx/vps_topyellow.png')");
265    
266          var url = "action.php?action=" + action + "&uuid=" + uuid + "&key=" + key;          if (action == "setMemory") {
267                    val = prompt("Set memory target");
268                    val *= (1024*1024);
269                    if (val == "" || val == null) {
270                            return;
271                    }
272            }
273            if (action == "setCPU") {
274                    val = prompt("Set CPU count");
275                    if (val == "" || val == null) {
276                            return;
277                    }
278            }
279    
280            var url = "action.php?action=" + action + "&uuid=" + uuid + "&key=" + key + "&val=" + val;;
281          var response = $.get(url, function(data) {          var response = $.get(url, function(data) {
282                  alert(data);                  if (data != 'OK') {
283                            alert(data);
284                    }
285                  document.body.style.cursor = 'default'                  document.body.style.cursor = 'default'
286                  if (data == "OK") {  
287                          window.location.reload();                  refreshData();
288            });
289    }
290    
291    function cdAction(action, vbd,vdi) {
292            var key = prompt("Key");
293            if (key == "" || key == null) {
294                    return;
295            }
296            document.body.style.cursor = 'wait';
297            var url = "cdaction.php?action=" + action + "&VBD=" + vbd + "&key=" + key + "&VDI=" + vdi;
298            var response = $.get(url, function(data) {
299                    if (data != 'OK') {
300                            alert(data);
301                  }                  }
302                    document.body.style.cursor = 'default'
303          });          });
304            
305  }  }
306    
307  function console(url, session) {  function openConsole(url, session, name) {
308          if (url == '')          if (url == '')
309                  return;                  return;
310    
# Line 67  function console(url, session) { Line 312  function console(url, session) {
312          if (key == "" || key == null) {          if (key == "" || key == null) {
313                  return;                  return;
314          }          }
315          var url = "console.php?url=" + url + "&session=" + session + "&key=" + key;          var url = "console.php?url=" + url + "&session=" + session + "&key=" + key + "&name=" + name;
316    
317          //window.location = url;          //window.location = url;
318          //$('#mainwindow').load(url);          //$('#mainwindow').load(url);
319          window.open(url);          window.open(url);
320  }  }
321    
 function loadServer() {  
         $('#mainwindow').load('server.php');  
 }  
322    
 function loadvm(uuid) {  
         $('#mainwindow').load('vm.php?uuid=' + uuid );  
 }  
323    
324  </script>  </script>
325    
# Line 122  body { Line 361  body {
361    
362  <table width='100%' background='gfx/topbg.png' cellpadding='3' cellspacing='0' border='0' class='toptable'>  <table width='100%' background='gfx/topbg.png' cellpadding='3' cellspacing='0' border='0' class='toptable'>
363  <tr>  <tr>
364          <td rowspan='2' width='160'><img src='gfx/citrix-logo.png'></td>          <td rowspan='2' width='160'><img src='gfx/citrix-logo.png' id='logo'></td>
365          <td colspan='3' class='small' align='right'>Linux:2.6.32.12-0.7.1.xs6.0.2.542.170665xen / xen: 4.1.2 / xapi: 1.3</td>          <td colspan='4' class='small' align='right'><?php echo $namelabel . " / " . $xenversion;?><br><?php echo $license;?></td>
366  </tr>  </tr>
367  <tr>  <tr>
368    <td width='150'></td>
369    <td width='150'></td>
370          <td width='150' align='right' class='small'><img src='gfx/icon-cpu.png'>          <td width='150' align='right' class='small'><img src='gfx/icon-cpu.png'>
371                  <img id='server_cpu_usage' src='usagebar.php?usage=12' width='102' height='16'><div id='server_cpu_usage_txt'>12%</div></td>                  <img id='server_cpu_usage' src='usagebar.php?usage=12' width='102' height='16' title='static dummy data'><div id='server_cpu_usage_txt'>12%</div></td>
372  <!--  
373          <td width='150' align='right' class='small'><img src='gfx/icon-memory.png'>          <td width='150' align='right' class='small'><img src='gfx/icon-memory.png'>
374                  <img id='server_memory_usage' src='usagebar.php?usage=81' width='102' height='16'><div id='server_memory_usage_txt'>4096/7912 MB</div></td>                  <img id='server_memory_usage' src='usagebar.php?usage=1' width='102' height='16' title='static dummy data'><div id='server_memory_usage_txt'>0/4 MB</div></td>
375  -->  <!--
376          <td width='150' align='right' class='small'><img src='gfx/icon-network.png'>          <td width='150' align='right' class='small'><img src='gfx/icon-network.png'>
377                  <img id='server_net_usage' src='usagebar.php?usage=41' width='102' height='16'><div id='server_net_usage_txt'>1%</div></td>                  <img id='server_net_usage' src='usagebar.php?usage=41' width='102' height='16' title='static dummy data'><div id='server_net_usage_txt'>1%</div></td>
378          <td width='150' align='right' class='small'><img src='gfx/icon-disk.png'>          <td width='150' align='right' class='small'><img src='gfx/icon-disk.png'>
379                  <img id='server_disk_usage' src='usagebar.php?usage=41' width='102' height='16'><div id='server_memory_usage_txt'>89.3/405.5 GB</div></td>                  <img id='server_disk_usage' src='usagebar.php?usage=41' width='102' height='16' title='static dummy data'><div id='server_memory_usage_txt'>89.3/405.5 GB</div></td>
   
380          </td>          </td>
381    -->
382  </tr></table>  </tr></table>
383    
384    
# Line 150  foreach($vms_array as $vm) { Line 391  foreach($vms_array as $vm) {
391          }          }
392    
393          $name = $vm["name_label"];          $name = $vm["name_label"];
394            $description = $vm["name_description"];
395          $uuid = $vm["uuid"];          $uuid = $vm["uuid"];
396          $state = $vm["power_state"] ;          $state = $vm["power_state"] ;
397          $memory = $vm['memory_target'];          $memory = $vm['memory_target'];
398          $harddrive_size = 0;          $harddrive_size = 0;
399          $cpu_count = $vm["VCPUs_max"];          $cpu_count = $vm["VCPUs_max"];
400    
401            $clean_name = str_replace(" ", "_", $name);
402            $clean_name = str_replace(".", "_", $clean_name);
403            $clean_name = str_replace("(", "_", $clean_name);
404            $clean_name = str_replace(")", "_", $clean_name);
405            
406    
407          foreach ($vm["VBDs"] as $vbds) {          foreach ($vm["VBDs"] as $vbds) {
408                  $vbd = $xenserver->VBD__get_record($vbds);                  $vbd = $xenserver->VBD__get_record($vbds);
409                  if ($vbd["type"] == 'Disk') {                  if ($vbd["type"] == 'Disk') {
# Line 164  foreach($vms_array as $vm) { Line 412  foreach($vms_array as $vm) {
412                  }                  }
413          }          }
414    
         $vps_topbg = 'vps_topyellow.png';  
   
         if ($state == "Running") {  
                 $vps_topbg = 'vps_topgreen.png';  
         } else if ($state == "Halted") {  
                 $vps_topbg = 'vps_topred.png';  
         }  
   
         $network = "";  
         if ( $vm["power_state"] == "Running" && $vm["guest_metrics"] != "OpaqueRef:NULL" ) {  
                 $guest_metrics = $xenserver->VM_guest_metrics__get_record( $vm["guest_metrics"] );  
                 $network = "(IP: " . implode( ",",  $guest_metrics["networks"]) . ")";  
         } else {  
                 $guest_metrics = Array() ;  
         }  
   
   
         $url = "";  
         $session = "";  
         foreach($vm["consoles"] as $conref) {  
                 $con = $xenserver->console__get_record( $conref );  
                 //var_dump($con);  
   
                 if ($con["protocol"] == "rfb") {  
                         $url = urlencode( $con["location"] );  
                         $session = $xenserver->get_id();  
                 }  
         }  
   
415    
416            
 //      echo "<a href='#' onclick=\"loadvm('$uuid');\">$name</a><br>\n";  
417  ?>  ?>
418          <!-- MACHINE -->          <!-- MACHINE -->
419          <table width='99%' cellpadding='3' cellspacing='0' border='0' class='vps'>          <table width='99%' cellpadding='3' cellspacing='0' border='0' class='vps' id='vm_<?php echo $clean_name;?>'>
420                  <tr background='gfx/vps_topbg.png'>                  <tr background='gfx/vps_topbg.png'  >
421                          <td width='10' background='gfx/<?php echo $vps_topbg; ?>' class='small'>&nbsp;</td>                          <td width='10' class='small state'>&nbsp;</td>
422                          <td colspan='2' class='small'><b><?php echo $name; ?></b></td>                          <td colspan='2' class='small' title='<?php echo $description;?>'><b><?php echo $name; ?></b><span class='os'></span></td>
423                          <td colspan='2' class='small' align='right'>                          <td colspan='2' class='small' align='right'>
424                                  <?php echo $network;?>                                  <span class='network'></span>
425                                  <a href="#" onclick="console('<?php echo $url;?>','<?php echo $session;?>')"><img src='gfx/icon-terminal.png' style='vertical-align: middle;'></a>                                  <a href='#' class='cd' uuid='<?php echo $uuid;?>' >
426                                            <img src='gfx/icon-cd.png' style='vertical-align: middle;'>
427                                    </a>
428                                    <a href='#' style='display:none;' class='console'>
429                                            <img src='gfx/icon-terminal.png' style='vertical-align: middle;'>
430                                    </a>
431                                    <a href='#' style='display:none;' class='settings' uuid='<?php echo $uuid;?>'>
432                                            <img src='gfx/icon-settings16.png' style='vertical-align: middle;'>
433                                    </a>
434                          </td>                          </td>
435                  </tr>                  </tr>
436    
# Line 212  foreach($vms_array as $vm) { Line 438  foreach($vms_array as $vm) {
438                  <td></td>                  <td></td>
439    
440                  <td width='150' class='small'>                  <td width='150' class='small'>
441                          <?php                          <span class="actionstop" style='display:none'>
442                          if ($state == "Running") {                                  start |
443                                  echo "start | ";                                  <a href='#' onclick="doAction('shutdown','<?php echo $uuid;?>','<?php echo $clean_name;?>')" >stop</a> |
444                                  echo "<a href='#' onclick=\"doAction('shutdown','$uuid')\">stop</a> | ";                                  <a href='#' onclick="doAction('hardshutdown','<?php echo $uuid;?>','<?php echo $clean_name;?>')" >force shutdown</a>
445                                  echo "<a href='#' onclick=\"doAction('hardshutdown','$uuid')\">force shutdown</a>";                          </span>
446                          } else {                          
447                                  echo "<a href='#' onclick=\"doAction('start','$uuid')\">start</a> | ";                          <span class="actionstart">                      
448                                  echo "stop | ";                                  <a href='#' onclick="doAction('start','<?php echo $uuid;?>','<?php echo $clean_name;?>')" >start</a> |
449                                  echo "force shutdown";                                  stop |
450                          }                                  force shutdown
451                          ?>                          </span>
452    
453                  </td>                  </td>
454    
455                  <td width='100' align='right' class='small'>                  <td width='100' align='right' class='small' valign='top'>
456                            <img src='usagebar.php?usage=1' width='102' height='16' title='static dummy data' class='cpu_graph'><br>
457                          <img src='gfx/icon-cpu.png' style='vertical-align: middle;'>                          <img src='gfx/icon-cpu.png' style='vertical-align: middle;'>
458                          <span id='vps_cpu_usage_txt_UID'><?php echo $cpu_count; ?> VCPU</span>                          <span class='vps_cpu_usage_txt_UID'><?php echo $cpu_count; ?> VCPU</span>
459                  </td>                  </td>
460                  <td width='100' align='right' class='small'>                  <td width='100' align='right' class='small'>
461                            <img src='usagebar.php?usage=1' width='102' height='16' title='static dummy data' class='mem_graph'><br>
462                          <img src='gfx/icon-memory.png' style='vertical-align: middle;'>                          <img src='gfx/icon-memory.png' style='vertical-align: middle;'>
463                          <span id='vps_memory_usage_txt_UID'><?php echo format_memory($memory); ?></span>                          <span class='vps_memory_usage_txt_UID'><?php echo format_memory($memory); ?></span>
464                  </td>                  </td>
465  <!--  <!--
466                  <td width='100' align='right' class='small'>                  <td width='100' align='right' class='small'>
# Line 240  foreach($vms_array as $vm) { Line 468  foreach($vms_array as $vm) {
468                          <span id='vps_net_usage_txt_UID'>na</span>                          <span id='vps_net_usage_txt_UID'>na</span>
469                  </td>                  </td>
470  -->  -->
471                  <td width='100' align='right' class='small'>                  <td width='100' align='right' class='small' valign='bottom'>
472                          <img src='gfx/icon-disk.png' style='vertical-align: middle;'>                          <img src='gfx/icon-disk.png' style='vertical-align: middle;'>
473                          <span id='vps_disk_usage_txt_UID'><?php echo format_storage($harddrive_size); ?></span>                          <span id='vps_disk_usage_txt_UID'><?php echo format_storage($harddrive_size); ?></span>
474                  </td>                  </td>
475                  </tr>                  </tr>
476                    
477    
478          </table>          </table>
479          <!-- MACHINE END -->          <!-- MACHINE END -->
480    
481  <?PHP } ?>  <?PHP } ?>
482  </td></tr></table>  </td></tr></table>
483    
484    <br>
485    
486    <div style="display: none;">
487    
488    <div id="dialog-cd" title="Select CD">
489    
490            <table border=0>
491                    <tr>
492                            <td>Current: <span id="cdcurrent"></span></td>
493                    </tr>
494                    <tr>
495                            <td>
496                                    CD: <select id="cdselector"></select>
497                            </td>
498                    </tr>
499            </table>
500    </div>
501    
502    </div>
503    
504    <ul id="menu">
505            <li action="memory"><a href="#">Set Memory Size</a></li>
506            <li action="cpu"><a href="#">Set CPU Count</a></li>
507    
508    
509    <!--    <li><a href="#">Item 3</a>
510                    <ul>
511                            <li><a href="#">Item 3-1</a></li>
512                            <li><a href="#">Item 3-2</a></li>
513                            <li><a href="#">Item 3-3</a></li>
514                            <li><a href="#">Item 3-4</a></li>
515                            <li><a href="#">Item 3-5</a></li>
516                    </ul>
517            </li>
518            <li><a href="#">Item 4</a></li>
519            <li><a href="#">Item 5</a></li>-->
520    </ul>
521    
522  </body></html>  </body></html>
523    

Legend:
Removed from v.1881  
changed lines
  Added in v.2018

  ViewVC Help
Powered by ViewVC 1.1.20