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

Contents of /misc/xenconsole/index.php

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2026 - (show annotations) (download)
Fri Jul 19 13:20:33 2013 UTC (10 years, 10 months ago) by torben
File size: 16036 byte(s)
disable mount/eject cd buttons according to mounted drive state
1 <?php
2 require("config.php");
3
4 function format_memory($size) {
5 if (1024 > $size) {
6 return "$size B";
7 } else if (pow(1024,2) > $size) {
8 return round(($size / 1024),2) . " kB";
9 } else {
10 return round(($size / pow(1024,2)), 2) . " MB";
11 }
12 }
13
14 function format_storage($size) {
15 if (1024 > $size) {
16 return "$size B";
17 } else if (pow(1024,2) > $size) {
18 return round(($size / 1024),2) . " kB";
19 } else if (pow(1024,3) > $size) {
20 return round(($size / pow(1024,2)), 2) . " MB";
21 } else if (pow(1024,4) > $size) {
22 return round(($size / pow(1024,3)), 2). " GB";
23 }
24 }
25
26 include('xenapi.php');
27
28 /* Establish session with Xenserver */
29 $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();
42
43 $namelabel = $host["name_label"];
44
45 ?>
46 <html>
47 <head>
48 <title>XenServer::<?php echo $namelabel;?></title>
49
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'>
60 var menu = 0;
61 var menu_uuid = '';
62
63 var user = "";
64 var password = "";
65 var loggedin = false;
66
67 function isLoggedIn() {
68 if (loggedin == false) {
69 alert("You need to login to perform this operation");
70 }
71 return loggedin;
72 }
73
74
75 $(document).ready( function() {
76 $("#menu").hide();
77 setInterval(refreshData, 60000);
78 refreshData();
79 $('.console').click( function() {
80 var session = $(this).data('session');
81 var conurl = $(this).data('conurl');
82 var name = $(this).data('name');
83 openConsole(conurl,session,name);
84 });
85
86 $('#logo').click( function() {
87 refreshData();
88 });
89
90 $('.settings').click( function(event) {
91 menu_uuid = $(this).attr('uuid');
92
93 $('#menu').menu( {
94 select: function(event2,ui) {
95 var action = $(ui.item).attr("action");
96 if (action == "memory") {
97 doAction("setMemory", menu_uuid, "");
98 }
99 if (action == "cpu") {
100 doAction("setCPU", menu_uuid, "");
101 }
102 },
103 create: function(event3,ui) {
104 menu = 1;
105 }
106 /*position: {
107 my: "left", of: event
108 }*/
109 });
110 $('#menu').show().position( {my: "left top", of: event} );
111 event.stopPropagation();
112 });
113
114 $('.cd').click( function(event) {
115 var uuid = $(this).attr('uuid');
116 cdSelectorDialog(uuid);
117 });
118 $('#loginlink').click( function(event) {
119 loginDialog();
120 });
121
122 $(document).click( function(event) {
123 closeMenu();
124 });
125 $('#dialog-login').keypress(function(e) {
126 if (e.keyCode == $.ui.keyCode.ENTER) {
127 loginDialogSubmit();
128 }
129 });
130 });
131
132 function loginDialog() {
133 $('#dialog-login').dialog({
134 modal: true,
135 height: 210,
136 width: 325,
137 buttons: {
138 Login: loginDialogSubmit
139 }
140 });
141 }
142 function loginDialogSubmit() {
143 var params = $('#loginform').serialize();
144
145 $.get('login.php?' + params, function(data) {
146 if (data == "OK") {
147 loggedin = true;
148 username = $('#username').val();
149 password = $('#password').val();
150
151 $('#login').html("Logged in as <i>" + username + "</i>");
152 } else {
153 alert(data);
154 }
155 $("#dialog-login").dialog( "close" );
156 });
157 }
158
159 function cdSelectorDialog(uuid) {
160 if (isLoggedIn() == false)
161 return;
162
163 $('#cdselector').html('');
164 $('#cdselector').load( 'getisolist.php' );
165 var cddata;
166
167 $.getJSON('getcdinfo.php?uuid=' + uuid, function(data) {
168 cddata = data;
169 if (data.ISO != '') {
170 $('#cdcurrent').html( data.ISO );
171 $(":button:contains('Mount')").prop("disabled", true).addClass("ui-state-disabled");
172 } else {
173 $('#cdcurrent').html('<i>No ISO currently mounted</i>');
174 $(":button:contains('Eject')").prop("disabled", true).addClass("ui-state-disabled");
175 }
176 });
177
178 $('#dialog-cd').dialog({
179 modal: true,
180 width: 800,
181 height: 300,
182 buttons: {
183 Mount: function() {
184 $( this ).dialog( "close" );
185 var vdi = $("#cdselector").val();
186 cdAction("mount", cddata.VBD, vdi);
187 },
188 Eject: function() {
189 $( this ).dialog( "close" );
190 cdAction("eject", cddata.VBD, "");
191 },
192 Cancel: function() {
193 $( this ).dialog( "close" );
194 }
195
196 }
197 });
198 }
199
200 function closeMenu() {
201 if (menu > 0) {
202 $("#menu").menu("destroy").hide();
203 menu = 0;
204 }
205 }
206
207 function refreshData() {
208 $("#logo").hide();
209 $.get('ajaxdata.php', function(xml) {
210 $(xml).find('host').each(function() {
211 var memtotal = $(this).find('memtotal').text();
212 var memfree = $(this).find('memfree').text();
213 var cpuavg = $(this).find('cpuavg').text();
214
215 memtotal = Math.round ( memtotal / (1024*1024) );
216 memfree = Math.round ( memfree / (1024*1024) );
217 var memused = memtotal - memfree;
218 var mem_percentage = Math.round( (memused/memtotal) * 100);
219
220 //alert(memused + ' ' + mem_percentage);
221
222 $('#server_memory_usage').attr('src', 'usagebar.php?usage=' + mem_percentage);
223 $('#server_memory_usage').attr('alt', mem_percentage + '%');
224 $('#server_memory_usage').attr('title', mem_percentage + '%');
225 $('#server_memory_usage_txt').text( memused + '/' + memtotal + 'MB');
226
227
228 $('#server_cpu_usage').attr('src', 'usagebar.php?usage=' + cpuavg);
229 $('#server_cpu_usage').attr('alt', cpuavg + '%');
230 $('#server_cpu_usage').attr('title', cpuavg + '%');
231 $('#server_cpu_usage_txt').text( cpuavg+ '%');
232 });
233 $(xml).find('vm').each(function() {
234 var name = $(this).find('name').text();
235 var state = $(this).find('state').text();
236 var network = $(this).find('network').text();
237 var state = $(this).find('state').text();
238 var conurl = $(this).find('conurl').text();
239 var session = $(this).find('session').text();
240 var os = $(this).find('os').text();
241 var guestversion = $(this).find('guestversion').text();
242 var cpuavg = $(this).find('cpuavg').text();
243 var curmem = $(this).find('curmem').text();
244 var maxmem = $(this).find('maxmem').text();
245 var cpus = $(this).find('cpus').text();
246
247 var mempercent = Math.round( (curmem*100) / maxmem );
248
249 name = name.replace(/ /g, "_");
250 name = name.replace(/\./g, "_");
251 name = name.replace(/\(/g, "_");
252 name = name.replace(/\)/g, "_");
253
254 var id = "#vm_" + name;
255
256 var vm = $(id);
257
258 if (guestversion != '') {
259 os += ' Guest Tools: ' + guestversion;
260 }
261
262 vm.find('.vps_memory_usage_txt_UID').text( maxmem + " MB");
263 vm.find('.vps_cpu_usage_txt_UID').text( cpus + " VCPU");
264
265 if (state == "Running") {
266 vm.find('.state').css("background-image", "url('gfx/vps_topgreen.png')");
267 vm.find('.network').show();
268 vm.find('.network').text( '(IP: ' + network + ')' );
269 vm.find('.console').show();
270 vm.find('.console').data('conurl', conurl);
271 vm.find('.console').data('session', session);
272 vm.find('.console').data('name', name);
273 vm.find('.settings').hide();
274 vm.find('.actionstop').show();
275 vm.find('.actionstart').hide();
276 vm.find('.os').text(' - ' + os);
277
278
279 vm.find('.cpu_graph').show();
280 vm.find('.cpu_graph').attr('src', 'usagebar.php?usage=' + cpuavg);
281 vm.find('.cpu_graph').attr('title', cpuavg + '%');
282
283 vm.find('.mem_graph').show();
284 vm.find('.mem_graph').attr('src', 'usagebar.php?usage=' + mempercent);
285 vm.find('.mem_graph').attr('title', curmem + ' / ' + maxmem + ' MB' );
286 } else {
287 if (state == "Halted") {
288 vm.find('.state').css("background-image", "url('gfx/vps_topred.png')");
289 } else {
290 vm.find('.state').css("background-image", "url('gfx/vps_topyellow.png')");
291 }
292 vm.find('.os').text('');
293
294 vm.find('.network').hide();
295 vm.find('.console').hide();
296 vm.find('.settings').show();
297 vm.find('.actionstop').hide();
298 vm.find('.actionstart').show();
299 vm.find('.cpu_graph').hide();
300 vm.find('.mem_graph').hide();
301 }
302
303 });
304 $('#logo').show();
305 });
306 }
307
308 function doAction(action, uuid, vm) {
309 var val="";
310
311 if (isLoggedIn() == false)
312 return;
313
314 document.body.style.cursor = 'wait';
315 $('#vm_' + vm).find('.state').css("background-image", "url('gfx/vps_topyellow.png')");
316
317 if (action == "setMemory") {
318 val = prompt("Set memory target");
319 val *= (1024*1024);
320 if (val == "" || val == null) {
321 return;
322 }
323 }
324 if (action == "setCPU") {
325 val = prompt("Set CPU count");
326 if (val == "" || val == null) {
327 return;
328 }
329 }
330
331 var url = "action.php?action=" + action + "&uuid=" + uuid + "&key=" + password + "&val=" + val;;
332 var response = $.get(url, function(data) {
333 if (data != 'OK') {
334 alert(data);
335 }
336 document.body.style.cursor = 'default'
337
338 refreshData();
339 });
340 }
341
342 function cdAction(action, vbd,vdi) {
343 if (isLoggedIn() == false)
344 return;
345
346 document.body.style.cursor = 'wait';
347 var url = "cdaction.php?action=" + action + "&VBD=" + vbd + "&key=" + password + "&VDI=" + vdi;
348 var response = $.get(url, function(data) {
349 if (data != 'OK') {
350 alert(data);
351 }
352 document.body.style.cursor = 'default'
353 });
354
355 }
356
357 function openConsole(url, session, name) {
358 if (url == '')
359 return;
360
361 if (isLoggedIn() == false)
362 return;
363
364 var url = "console.php?url=" + url + "&session=" + session + "&key=" + password + "&name=" + name;
365
366 //window.location = url;
367 //$('#mainwindow').load(url);
368 window.open(url);
369 }
370
371
372
373 </script>
374
375 <style>
376 body {
377 background: #eee;
378 margin-top: 5px;
379 font-family:verdana,helvetica,arial,sans-serif;
380 font-size: 14px;
381
382 }
383
384 .maintable {
385 background: #fff;
386 margin-left:auto; // Smart center
387 margin-right:auto;
388 padding: 0;
389 border: 3px solid #999;
390 }
391
392 .small {
393 font-family:verdana,helvetica,arial,sans-serif;
394 font-size: 11px;
395 }
396
397 .toptable {
398 border-bottom: 3px solid #999;
399 }
400
401 .vps {
402 margin: 4;
403 border: 1px solid #222;
404 }
405 </style>
406 </head>
407 <body>
408
409 <table class='maintable' width='800' align='center' cellpadding='0' cellspacing='0'><tr><td>
410
411 <table width='100%' background='gfx/topbg.png' cellpadding='3' cellspacing='0' border='0' class='toptable'>
412 <tr>
413 <td rowspan='2' width='160'><img src='gfx/citrix-logo.png' id='logo'></td>
414 <td colspan='4' class='small' align='right'><?php echo $namelabel . " / " . $xenversion;?><br><?php echo $license;?></td>
415 </tr>
416 <tr>
417 <td width='150'>
418 <span id="login" class='small'><i>Not <a href="#" id="loginlink">logged in</a></i>
419 </span>
420 </td>
421 <td width='150'></td>
422 <td width='150' align='right' class='small'><img src='gfx/icon-cpu.png'>
423 <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>
424
425 <td width='150' align='right' class='small'><img src='gfx/icon-memory.png'>
426 <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>
427 <!--
428 <td width='150' align='right' class='small'><img src='gfx/icon-network.png'>
429 <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>
430 <td width='150' align='right' class='small'><img src='gfx/icon-disk.png'>
431 <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>
432 </td>
433 -->
434 </tr></table>
435
436
437
438 <?php
439 // List all machines
440 foreach($vms_array as $vm) {
441 if ($vm["is_a_template"] != 0 || $vm["is_control_domain"] != 0) {
442 continue;
443 }
444
445 $name = $vm["name_label"];
446 $description = $vm["name_description"];
447 $uuid = $vm["uuid"];
448 $state = $vm["power_state"] ;
449 $memory = $vm['memory_target'];
450 $harddrive_size = 0;
451 $cpu_count = $vm["VCPUs_max"];
452
453 $clean_name = str_replace(" ", "_", $name);
454 $clean_name = str_replace(".", "_", $clean_name);
455 $clean_name = str_replace("(", "_", $clean_name);
456 $clean_name = str_replace(")", "_", $clean_name);
457
458
459 foreach ($vm["VBDs"] as $vbds) {
460 $vbd = $xenserver->VBD__get_record($vbds);
461 if ($vbd["type"] == 'Disk') {
462 $vdi = $xenserver->VDI__get_record( $vbd["VDI"] );
463 $harddrive_size += $vdi["virtual_size"];
464 }
465 }
466
467
468
469 ?>
470 <!-- MACHINE -->
471 <table width='99%' cellpadding='3' cellspacing='0' border='0' class='vps' id='vm_<?php echo $clean_name;?>'>
472 <tr background='gfx/vps_topbg.png' >
473 <td width='10' class='small state'>&nbsp;</td>
474 <td colspan='2' class='small' title='<?php echo $description;?>'><b><?php echo $name; ?></b><span class='os'></span></td>
475 <td colspan='2' class='small' align='right'>
476 <span class='network'></span>
477 <a href='#' class='cd' uuid='<?php echo $uuid;?>' >
478 <img src='gfx/icon-cd.gif' style='vertical-align: middle;'></a>
479 <a href='#' style='display:none;' class='console'>
480 <img src='gfx/icon-terminal.png' style='vertical-align: middle;'>
481 </a>
482
483 <a href='#' style='display:none;' class='settings' uuid='<?php echo $uuid;?>'>
484 <img src='gfx/icon-settings16.png' style='vertical-align: middle;'>
485 </a>
486 </td>
487 </tr>
488
489 <tr bgcolor='#eee'>
490 <td></td>
491
492 <td width='150' class='small'>
493 <span class="actionstop" style='display:none'>
494 start |
495 <a href='#' onclick="doAction('shutdown','<?php echo $uuid;?>','<?php echo $clean_name;?>')" >stop</a> |
496 <a href='#' onclick="doAction('hardshutdown','<?php echo $uuid;?>','<?php echo $clean_name;?>')" >force shutdown</a>
497 </span>
498
499 <span class="actionstart">
500 <a href='#' onclick="doAction('start','<?php echo $uuid;?>','<?php echo $clean_name;?>')" >start</a> |
501 stop |
502 force shutdown
503 </span>
504
505 </td>
506
507 <td width='100' align='right' class='small' valign='top'>
508 <img src='usagebar.php?usage=1' width='102' height='16' title='static dummy data' class='cpu_graph'><br>
509 <img src='gfx/icon-cpu.png' style='vertical-align: middle;'>
510 <span class='vps_cpu_usage_txt_UID'><?php echo $cpu_count; ?> VCPU</span>
511 </td>
512 <td width='100' align='right' class='small'>
513 <img src='usagebar.php?usage=1' width='102' height='16' title='static dummy data' class='mem_graph'><br>
514 <img src='gfx/icon-memory.png' style='vertical-align: middle;'>
515 <span class='vps_memory_usage_txt_UID'><?php echo format_memory($memory); ?></span>
516 </td>
517 <!--
518 <td width='100' align='right' class='small'>
519 <img src='gfx/icon-network.png' style='vertical-align: middle;'>
520 <span id='vps_net_usage_txt_UID'>na</span>
521 </td>
522 -->
523 <td width='100' align='right' class='small' valign='bottom'>
524 <img src='gfx/icon-disk.png' style='vertical-align: middle;'>
525 <span id='vps_disk_usage_txt_UID'><?php echo format_storage($harddrive_size); ?></span>
526 </td>
527 </tr>
528
529
530 </table>
531 <!-- MACHINE END -->
532
533 <?PHP } ?>
534 </td></tr></table>
535
536 <br>
537
538 <div style="display: none;">
539
540 <div id="dialog-login" title="Login">
541 <form id="loginform">
542 <table border="0">
543 <tr>
544 <td>Username: </td>
545 <td><input type="text" id="username" name="username"></td>
546 <tr>
547 <tr>
548 <td>Password: </td>
549 <td><input type="password" id="password" name="password"></td>
550 </tr>
551 </table>
552 </form>
553 </div>
554
555 <div id="dialog-cd" title="Select CD">
556
557 <table border=0>
558 <tr>
559 <td>Current: <span id="cdcurrent"></span></td>
560 </tr>
561 <tr>
562 <td>
563 CD: <select id="cdselector"></select>
564 </td>
565 </tr>
566 </table>
567 </div>
568
569 </div>
570
571 <ul id="menu">
572 <li action="memory"><a href="#">Set Memory Size</a></li>
573 <li action="cpu"><a href="#">Set CPU Count</a></li>
574
575
576 <!-- <li><a href="#">Item 3</a>
577 <ul>
578 <li><a href="#">Item 3-1</a></li>
579 <li><a href="#">Item 3-2</a></li>
580 <li><a href="#">Item 3-3</a></li>
581 <li><a href="#">Item 3-4</a></li>
582 <li><a href="#">Item 3-5</a></li>
583 </ul>
584 </li>
585 <li><a href="#">Item 4</a></li>
586 <li><a href="#">Item 5</a></li>-->
587 </ul>
588
589 </body></html>

  ViewVC Help
Powered by ViewVC 1.1.20