/[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 1957 by torben, Tue Mar 26 13:15:05 2013 UTC
# 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    
62  $(document).ready( function() {  $(document).ready( function() {
63          loadServer();          $("#menu").hide();
64            setInterval(refreshData, 60000);        
65            refreshData();
66            $('.console').click( function() {
67                    var session = $(this).data('session');
68                    var conurl = $(this).data('conurl');
69                    var name = $(this).data('name');
70                    openConsole(conurl,session,name);
71            });
72    
73            $('#logo').click( function() {
74                    refreshData();
75            });
76    
77            $('.settings').click( function(event) {
78    
79                    $('#menu').menu( {
80                            select: function(event2,ui) {
81                                            alert( ui.item.text() );
82                                    },
83                            create: function(event3,ui) {
84                                    menu = 1;
85                                    }
86                            /*position: {
87                                    my: "left", of: event
88                            }*/
89                    });
90                    $('#menu').show().position( {my: "left top", of: event} );
91                    event.stopPropagation();
92            });
93    
94            $(document).click( function(event) {
95                    closeMenu();
96            });
97    
98  });  });
99    
100  function doAction(action, uuid) {  function closeMenu() {
101            if (menu > 0) {
102                    $("#menu").menu("destroy").hide();
103                    menu = 0;
104            }
105    }
106    
107    function refreshData() {
108            $("#logo").hide();      
109            $.get('ajaxdata.php', function(xml) {
110                    $(xml).find('host').each(function() {
111                            var memtotal = $(this).find('memtotal').text();
112                            var memfree = $(this).find('memfree').text();  
113                            var cpuavg = $(this).find('cpuavg').text();
114    
115                            memtotal = Math.round ( memtotal / (1024*1024) );
116                            memfree = Math.round ( memfree / (1024*1024) );
117                            var memused = memtotal - memfree;
118                            var mem_percentage = Math.round( (memused/memtotal) * 100);
119    
120                            //alert(memused + ' ' + mem_percentage);
121    
122                            $('#server_memory_usage').attr('src', 'usagebar.php?usage=' + mem_percentage);
123                            $('#server_memory_usage').attr('alt', mem_percentage + '%');
124                            $('#server_memory_usage').attr('title', mem_percentage + '%');
125                            $('#server_memory_usage_txt').text( memused + '/' + memtotal + 'MB');
126    
127    
128                            $('#server_cpu_usage').attr('src', 'usagebar.php?usage=' + cpuavg);
129                            $('#server_cpu_usage').attr('alt', cpuavg + '%');
130                            $('#server_cpu_usage').attr('title', cpuavg + '%');
131                            $('#server_cpu_usage_txt').text( cpuavg+ '%');
132                    });
133                    $(xml).find('vm').each(function() {
134                            var name = $(this).find('name').text();
135                            var state = $(this).find('state').text();
136                            var network = $(this).find('network').text();
137                            var state = $(this).find('state').text();
138                            var conurl = $(this).find('conurl').text();
139                            var session = $(this).find('session').text();
140                            var os = $(this).find('os').text();
141                            var guestversion = $(this).find('guestversion').text();
142                            var cpuavg = $(this).find('cpuavg').text();
143                            var curmem = $(this).find('curmem').text();
144                            var maxmem = $(this).find('maxmem').text();
145    
146                            var mempercent = Math.round( (curmem*100) / maxmem );
147    
148                            name = name.replace(" ", "_");
149                            name = name.replace(".", "_");
150                            
151                            var id = "#vm_" + name;
152    
153                            var vm = $(id);                
154    
155                            if (guestversion != '') {
156                                    os += ' Guest Tools: ' + guestversion;
157                            }
158    
159                            vm.find('.vps_memory_usage_txt_UID').text( maxmem + " MB");
160    
161                            if (state == "Running") {
162                                    vm.find('.state').css("background-image", "url('gfx/vps_topgreen.png')");
163                                    vm.find('.network').show();
164                                    vm.find('.network').text( '(IP: ' + network + ')' );
165                                    vm.find('.console').show();
166                                    vm.find('.console').data('conurl', conurl);
167                                    vm.find('.console').data('session', session);
168                                    vm.find('.console').data('name', name);
169                                    vm.find('.settings').hide();
170                                    vm.find('.actionstop').show();
171                                    vm.find('.actionstart').hide();
172                                    vm.find('.os').text(' - ' + os);
173    
174    
175                                    vm.find('.cpu_graph').show();
176                                    vm.find('.cpu_graph').attr('src', 'usagebar.php?usage=' + cpuavg);
177                                    vm.find('.cpu_graph').attr('title',  cpuavg + '%');
178    
179                                    vm.find('.mem_graph').show();
180                                    vm.find('.mem_graph').attr('src', 'usagebar.php?usage=' + mempercent);
181                                    vm.find('.mem_graph').attr('title',  curmem + ' / ' + maxmem + ' MB' );
182                            } else {
183                                    if (state == "Halted") {
184                                            vm.find('.state').css("background-image", "url('gfx/vps_topred.png')");
185                                    } else {
186                                            vm.find('.state').css("background-image", "url('gfx/vps_topyellow.png')");
187                                    }
188                                    vm.find('.os').text('');
189    
190                                    vm.find('.network').hide();
191                                    vm.find('.console').hide();
192                                    vm.find('.settings').show();
193                                    vm.find('.actionstop').hide();
194                                    vm.find('.actionstart').show();
195                                    vm.find('.cpu_graph').hide();
196                                    vm.find('.mem_graph').hide();
197                            }
198                            
199                    });
200                    $('#logo').show();
201           });
202    }
203    
204    function doAction(action, uuid, vm) {
205            var val="";
206    
207          var key = prompt("Key");          var key = prompt("Key");
208          if (key == "" || key == null) {          if (key == "" || key == null) {
209                  return;                  return;
210          }          }
211          document.body.style.cursor = 'wait';          document.body.style.cursor = 'wait';
212            $('#vm_' + vm).find('.state').css("background-image", "url('gfx/vps_topyellow.png')");
213    
214          var url = "action.php?action=" + action + "&uuid=" + uuid + "&key=" + key;          if (action == "setMemory") {
215                    val = prompt("Set memory target");
216                    if (val == "" || val == null) {
217                            alert("" + val);
218                            return;
219                    }
220            }
221    
222            var url = "action.php?action=" + action + "&uuid=" + uuid + "&key=" + key + "&val=" + val;;
223          var response = $.get(url, function(data) {          var response = $.get(url, function(data) {
224                  alert(data);                  if (data != 'OK') {
225                  document.body.style.cursor = 'default'                          alert(data);
                 if (data == "OK") {  
                         window.location.reload();  
226                  }                  }
227                    document.body.style.cursor = 'default'
228    
229                    refreshData();
230          });          });
231  }  }
232    
233  function console(url, session) {  function openConsole(url, session, name) {
234          if (url == '')          if (url == '')
235                  return;                  return;
236    
# Line 67  function console(url, session) { Line 238  function console(url, session) {
238          if (key == "" || key == null) {          if (key == "" || key == null) {
239                  return;                  return;
240          }          }
241          var url = "console.php?url=" + url + "&session=" + session + "&key=" + key;          var url = "console.php?url=" + url + "&session=" + session + "&key=" + key + "&name=" + name;
242    
243          //window.location = url;          //window.location = url;
244          //$('#mainwindow').load(url);          //$('#mainwindow').load(url);
245          window.open(url);          window.open(url);
246  }  }
247    
 function loadServer() {  
         $('#mainwindow').load('server.php');  
 }  
248    
 function loadvm(uuid) {  
         $('#mainwindow').load('vm.php?uuid=' + uuid );  
 }  
249    
250  </script>  </script>
251    
# Line 122  body { Line 287  body {
287    
288  <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'>
289  <tr>  <tr>
290          <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>
291          <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>
292  </tr>  </tr>
293  <tr>  <tr>
294    <td width='150'></td>
295    <td width='150'></td>
296          <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'>
297                  <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>
298  <!--  
299          <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'>
300                  <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>
301  -->  <!--
302          <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'>
303                  <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>
304          <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'>
305                  <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>
   
306          </td>          </td>
307    -->
308  </tr></table>  </tr></table>
309    
310    
# Line 150  foreach($vms_array as $vm) { Line 317  foreach($vms_array as $vm) {
317          }          }
318    
319          $name = $vm["name_label"];          $name = $vm["name_label"];
320            $description = $vm["name_description"];
321          $uuid = $vm["uuid"];          $uuid = $vm["uuid"];
322          $state = $vm["power_state"] ;          $state = $vm["power_state"] ;
323          $memory = $vm['memory_target'];          $memory = $vm['memory_target'];
324          $harddrive_size = 0;          $harddrive_size = 0;
325          $cpu_count = $vm["VCPUs_max"];          $cpu_count = $vm["VCPUs_max"];
326    
327            $clean_name = str_replace(" ", "_", $name);
328            $clean_name = str_replace(".", "_", $clean_name);
329            
330    
331          foreach ($vm["VBDs"] as $vbds) {          foreach ($vm["VBDs"] as $vbds) {
332                  $vbd = $xenserver->VBD__get_record($vbds);                  $vbd = $xenserver->VBD__get_record($vbds);
333                  if ($vbd["type"] == 'Disk') {                  if ($vbd["type"] == 'Disk') {
# Line 164  foreach($vms_array as $vm) { Line 336  foreach($vms_array as $vm) {
336                  }                  }
337          }          }
338    
         $vps_topbg = 'vps_topyellow.png';  
   
         if ($state == "Running") {  
                 $vps_topbg = 'vps_topgreen.png';  
         } else if ($state == "Halted") {  
                 $vps_topbg = 'vps_topred.png';  
         }  
339    
340          $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();  
                 }  
         }  
   
   
   
 //      echo "<a href='#' onclick=\"loadvm('$uuid');\">$name</a><br>\n";  
341  ?>  ?>
342          <!-- MACHINE -->          <!-- MACHINE -->
343          <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;?>'>
344                  <tr background='gfx/vps_topbg.png'>                  <tr background='gfx/vps_topbg.png'  >
345                          <td width='10' background='gfx/<?php echo $vps_topbg; ?>' class='small'>&nbsp;</td>                          <td width='10' class='small state'>&nbsp;</td>
346                          <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>
347                          <td colspan='2' class='small' align='right'>                          <td colspan='2' class='small' align='right'>
348                                  <?php echo $network;?>                                  <span class='network'></span>
349                                  <a href="#" onclick="console('<?php echo $url;?>','<?php echo $session;?>')"><img src='gfx/icon-terminal.png' style='vertical-align: middle;'></a>                                  <a href='#' style='display:none;' class='console'>
350                                            <img src='gfx/icon-terminal.png' style='vertical-align: middle;'>
351                                    </a>
352                                    <a href='#' style='display:none;' class='settings'>
353                                            <img src='gfx/icon-settings16.png' style='vertical-align: middle;'>
354                                    </a>
355                          </td>                          </td>
356                  </tr>                  </tr>
357    
# Line 212  foreach($vms_array as $vm) { Line 359  foreach($vms_array as $vm) {
359                  <td></td>                  <td></td>
360    
361                  <td width='150' class='small'>                  <td width='150' class='small'>
362                          <?php                          <span class="actionstop" style='display:none'>
363                          if ($state == "Running") {                                  start |
364                                  echo "start | ";                                  <a href='#' onclick="doAction('shutdown','<?php echo $uuid;?>','<?php echo $clean_name;?>')" >stop</a> |
365                                  echo "<a href='#' onclick=\"doAction('shutdown','$uuid')\">stop</a> | ";                                  <a href='#' onclick="doAction('hardshutdown','<?php echo $uuid;?>','<?php echo $clean_name;?>')" >force shutdown</a> |
366                                  echo "<a href='#' onclick=\"doAction('hardshutdown','$uuid')\">force shutdown</a>";                                  set memory
367                          } else {                          </span>
368                                  echo "<a href='#' onclick=\"doAction('start','$uuid')\">start</a> | ";                          
369                                  echo "stop | ";                          <span class="actionstart">                      
370                                  echo "force shutdown";                                  <a href='#' onclick="doAction('start','<?php echo $uuid;?>','<?php echo $clean_name;?>')" >start</a> |
371                          }                                  stop |
372                          ?>                                  force shutdown |
373    
374                                    <a href='#' onclick="doAction('setMemory','<?php echo $uuid;?>','<?php echo $clean_name;?>')" >set memory</a>
375                            </span>
376    
377                  </td>                  </td>
378    
379                  <td width='100' align='right' class='small'>                  <td width='100' align='right' class='small' valign='top'>
380                            <img src='usagebar.php?usage=1' width='102' height='16' title='static dummy data' class='cpu_graph'><br>
381                          <img src='gfx/icon-cpu.png' style='vertical-align: middle;'>                          <img src='gfx/icon-cpu.png' style='vertical-align: middle;'>
382                          <span id='vps_cpu_usage_txt_UID'><?php echo $cpu_count; ?> VCPU</span>                          <span id='vps_cpu_usage_txt_UID'><?php echo $cpu_count; ?> VCPU</span>
383                  </td>                  </td>
384                  <td width='100' align='right' class='small'>                  <td width='100' align='right' class='small'>
385                            <img src='usagebar.php?usage=1' width='102' height='16' title='static dummy data' class='mem_graph'><br>
386                          <img src='gfx/icon-memory.png' style='vertical-align: middle;'>                          <img src='gfx/icon-memory.png' style='vertical-align: middle;'>
387                          <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>
388                  </td>                  </td>
389  <!--  <!--
390                  <td width='100' align='right' class='small'>                  <td width='100' align='right' class='small'>
# Line 240  foreach($vms_array as $vm) { Line 392  foreach($vms_array as $vm) {
392                          <span id='vps_net_usage_txt_UID'>na</span>                          <span id='vps_net_usage_txt_UID'>na</span>
393                  </td>                  </td>
394  -->  -->
395                  <td width='100' align='right' class='small'>                  <td width='100' align='right' class='small' valign='bottom'>
396                          <img src='gfx/icon-disk.png' style='vertical-align: middle;'>                          <img src='gfx/icon-disk.png' style='vertical-align: middle;'>
397                          <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>
398                  </td>                  </td>
399                  </tr>                  </tr>
400                    
401    
402          </table>          </table>
403          <!-- MACHINE END -->          <!-- MACHINE END -->
404    
405  <?PHP } ?>  <?PHP } ?>
406  </td></tr></table>  </td></tr></table>
407    
408    <br>
409    
410    <ul id="menu">
411            <li><a href="#">Set Memory Size</a></li>
412            <li><a href="#">Set CPU Count</a></li>
413    
414    
415    <!--    <li><a href="#">Item 3</a>
416                    <ul>
417                            <li><a href="#">Item 3-1</a></li>
418                            <li><a href="#">Item 3-2</a></li>
419                            <li><a href="#">Item 3-3</a></li>
420                            <li><a href="#">Item 3-4</a></li>
421                            <li><a href="#">Item 3-5</a></li>
422                    </ul>
423            </li>
424            <li><a href="#">Item 4</a></li>
425            <li><a href="#">Item 5</a></li>-->
426    </ul>
427    
428  </body></html>  </body></html>
429    

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

  ViewVC Help
Powered by ViewVC 1.1.20