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

Contents of /misc/xenconsole/index.php

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2021 - (show annotations) (download)
Fri Jul 19 08:10:08 2013 UTC (10 years, 10 months ago) by torben
File size: 15864 byte(s)
make console work correctly with new login function
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 } else {
172 $('#cdcurrent').html('<i>No ISO currently mounted</i>');
173 }
174 });
175
176 $('#dialog-cd').dialog({
177 modal: true,
178 width: 800,
179 height: 300,
180 buttons: {
181 Mount: function() {
182 $( this ).dialog( "close" );
183 var vdi = $("#cdselector").val();
184 cdAction("mount", cddata.VBD, vdi);
185 },
186 Eject: function() {
187 $( this ).dialog( "close" );
188 cdAction("eject", cddata.VBD, "");
189 },
190 Cancel: function() {
191 $( this ).dialog( "close" );
192 }
193
194 }
195 });
196 }
197
198 function closeMenu() {
199 if (menu > 0) {
200 $("#menu").menu("destroy").hide();
201 menu = 0;
202 }
203 }
204
205 function refreshData() {
206 $("#logo").hide();
207 $.get('ajaxdata.php', function(xml) {
208 $(xml).find('host').each(function() {
209 var memtotal = $(this).find('memtotal').text();
210 var memfree = $(this).find('memfree').text();
211 var cpuavg = $(this).find('cpuavg').text();
212
213 memtotal = Math.round ( memtotal / (1024*1024) );
214 memfree = Math.round ( memfree / (1024*1024) );
215 var memused = memtotal - memfree;
216 var mem_percentage = Math.round( (memused/memtotal) * 100);
217
218 //alert(memused + ' ' + mem_percentage);
219
220 $('#server_memory_usage').attr('src', 'usagebar.php?usage=' + mem_percentage);
221 $('#server_memory_usage').attr('alt', mem_percentage + '%');
222 $('#server_memory_usage').attr('title', mem_percentage + '%');
223 $('#server_memory_usage_txt').text( memused + '/' + memtotal + 'MB');
224
225
226 $('#server_cpu_usage').attr('src', 'usagebar.php?usage=' + cpuavg);
227 $('#server_cpu_usage').attr('alt', cpuavg + '%');
228 $('#server_cpu_usage').attr('title', cpuavg + '%');
229 $('#server_cpu_usage_txt').text( cpuavg+ '%');
230 });
231 $(xml).find('vm').each(function() {
232 var name = $(this).find('name').text();
233 var state = $(this).find('state').text();
234 var network = $(this).find('network').text();
235 var state = $(this).find('state').text();
236 var conurl = $(this).find('conurl').text();
237 var session = $(this).find('session').text();
238 var os = $(this).find('os').text();
239 var guestversion = $(this).find('guestversion').text();
240 var cpuavg = $(this).find('cpuavg').text();
241 var curmem = $(this).find('curmem').text();
242 var maxmem = $(this).find('maxmem').text();
243 var cpus = $(this).find('cpus').text();
244
245 var mempercent = Math.round( (curmem*100) / maxmem );
246
247 name = name.replace(/ /g, "_");
248 name = name.replace(/\./g, "_");
249 name = name.replace(/\(/g, "_");
250 name = name.replace(/\)/g, "_");
251
252 var id = "#vm_" + name;
253
254 var vm = $(id);
255
256 if (guestversion != '') {
257 os += ' Guest Tools: ' + guestversion;
258 }
259
260 vm.find('.vps_memory_usage_txt_UID').text( maxmem + " MB");
261 vm.find('.vps_cpu_usage_txt_UID').text( cpus + " VCPU");
262
263 if (state == "Running") {
264 vm.find('.state').css("background-image", "url('gfx/vps_topgreen.png')");
265 vm.find('.network').show();
266 vm.find('.network').text( '(IP: ' + network + ')' );
267 vm.find('.console').show();
268 vm.find('.console').data('conurl', conurl);
269 vm.find('.console').data('session', session);
270 vm.find('.console').data('name', name);
271 vm.find('.settings').hide();
272 vm.find('.actionstop').show();
273 vm.find('.actionstart').hide();
274 vm.find('.os').text(' - ' + os);
275
276
277 vm.find('.cpu_graph').show();
278 vm.find('.cpu_graph').attr('src', 'usagebar.php?usage=' + cpuavg);
279 vm.find('.cpu_graph').attr('title', cpuavg + '%');
280
281 vm.find('.mem_graph').show();
282 vm.find('.mem_graph').attr('src', 'usagebar.php?usage=' + mempercent);
283 vm.find('.mem_graph').attr('title', curmem + ' / ' + maxmem + ' MB' );
284 } else {
285 if (state == "Halted") {
286 vm.find('.state').css("background-image", "url('gfx/vps_topred.png')");
287 } else {
288 vm.find('.state').css("background-image", "url('gfx/vps_topyellow.png')");
289 }
290 vm.find('.os').text('');
291
292 vm.find('.network').hide();
293 vm.find('.console').hide();
294 vm.find('.settings').show();
295 vm.find('.actionstop').hide();
296 vm.find('.actionstart').show();
297 vm.find('.cpu_graph').hide();
298 vm.find('.mem_graph').hide();
299 }
300
301 });
302 $('#logo').show();
303 });
304 }
305
306 function doAction(action, uuid, vm) {
307 var val="";
308
309 if (isLoggedIn() == false)
310 return;
311
312 document.body.style.cursor = 'wait';
313 $('#vm_' + vm).find('.state').css("background-image", "url('gfx/vps_topyellow.png')");
314
315 if (action == "setMemory") {
316 val = prompt("Set memory target");
317 val *= (1024*1024);
318 if (val == "" || val == null) {
319 return;
320 }
321 }
322 if (action == "setCPU") {
323 val = prompt("Set CPU count");
324 if (val == "" || val == null) {
325 return;
326 }
327 }
328
329 var url = "action.php?action=" + action + "&uuid=" + uuid + "&key=" + password + "&val=" + val;;
330 var response = $.get(url, function(data) {
331 if (data != 'OK') {
332 alert(data);
333 }
334 document.body.style.cursor = 'default'
335
336 refreshData();
337 });
338 }
339
340 function cdAction(action, vbd,vdi) {
341 if (isLoggedIn() == false)
342 return;
343
344 document.body.style.cursor = 'wait';
345 var url = "cdaction.php?action=" + action + "&VBD=" + vbd + "&key=" + password + "&VDI=" + vdi;
346 var response = $.get(url, function(data) {
347 if (data != 'OK') {
348 alert(data);
349 }
350 document.body.style.cursor = 'default'
351 });
352
353 }
354
355 function openConsole(url, session, name) {
356 if (url == '')
357 return;
358
359 if (isLoggedIn() == false)
360 return;
361
362 var url = "console.php?url=" + url + "&session=" + session + "&key=" + password + "&name=" + name;
363
364 //window.location = url;
365 //$('#mainwindow').load(url);
366 window.open(url);
367 }
368
369
370
371 </script>
372
373 <style>
374 body {
375 background: #eee;
376 margin-top: 5px;
377 font-family:verdana,helvetica,arial,sans-serif;
378 font-size: 14px;
379
380 }
381
382 .maintable {
383 background: #fff;
384 margin-left:auto; // Smart center
385 margin-right:auto;
386 padding: 0;
387 border: 3px solid #999;
388 }
389
390 .small {
391 font-family:verdana,helvetica,arial,sans-serif;
392 font-size: 11px;
393 }
394
395 .toptable {
396 border-bottom: 3px solid #999;
397 }
398
399 .vps {
400 margin: 4;
401 border: 1px solid #222;
402 }
403 </style>
404 </head>
405 <body>
406
407 <table class='maintable' width='800' align='center' cellpadding='0' cellspacing='0'><tr><td>
408
409 <table width='100%' background='gfx/topbg.png' cellpadding='3' cellspacing='0' border='0' class='toptable'>
410 <tr>
411 <td rowspan='2' width='160'><img src='gfx/citrix-logo.png' id='logo'></td>
412 <td colspan='4' class='small' align='right'><?php echo $namelabel . " / " . $xenversion;?><br><?php echo $license;?></td>
413 </tr>
414 <tr>
415 <td width='150'>
416 <span id="login" class='small'><i>Not <a href="#" id="loginlink">logged in</a></i>
417 </span>
418 </td>
419 <td width='150'></td>
420 <td width='150' align='right' class='small'><img src='gfx/icon-cpu.png'>
421 <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>
422
423 <td width='150' align='right' class='small'><img src='gfx/icon-memory.png'>
424 <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>
425 <!--
426 <td width='150' align='right' class='small'><img src='gfx/icon-network.png'>
427 <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>
428 <td width='150' align='right' class='small'><img src='gfx/icon-disk.png'>
429 <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>
430 </td>
431 -->
432 </tr></table>
433
434
435
436 <?php
437 // List all machines
438 foreach($vms_array as $vm) {
439 if ($vm["is_a_template"] != 0 || $vm["is_control_domain"] != 0) {
440 continue;
441 }
442
443 $name = $vm["name_label"];
444 $description = $vm["name_description"];
445 $uuid = $vm["uuid"];
446 $state = $vm["power_state"] ;
447 $memory = $vm['memory_target'];
448 $harddrive_size = 0;
449 $cpu_count = $vm["VCPUs_max"];
450
451 $clean_name = str_replace(" ", "_", $name);
452 $clean_name = str_replace(".", "_", $clean_name);
453 $clean_name = str_replace("(", "_", $clean_name);
454 $clean_name = str_replace(")", "_", $clean_name);
455
456
457 foreach ($vm["VBDs"] as $vbds) {
458 $vbd = $xenserver->VBD__get_record($vbds);
459 if ($vbd["type"] == 'Disk') {
460 $vdi = $xenserver->VDI__get_record( $vbd["VDI"] );
461 $harddrive_size += $vdi["virtual_size"];
462 }
463 }
464
465
466
467 ?>
468 <!-- MACHINE -->
469 <table width='99%' cellpadding='3' cellspacing='0' border='0' class='vps' id='vm_<?php echo $clean_name;?>'>
470 <tr background='gfx/vps_topbg.png' >
471 <td width='10' class='small state'>&nbsp;</td>
472 <td colspan='2' class='small' title='<?php echo $description;?>'><b><?php echo $name; ?></b><span class='os'></span></td>
473 <td colspan='2' class='small' align='right'>
474 <span class='network'></span>
475 <a href='#' class='cd' uuid='<?php echo $uuid;?>' >
476 <img src='gfx/icon-cd.png' style='vertical-align: middle;'>
477 </a>
478 <a href='#' style='display:none;' class='console'>
479 <img src='gfx/icon-terminal.png' style='vertical-align: middle;'>
480 </a>
481 <a href='#' style='display:none;' class='settings' uuid='<?php echo $uuid;?>'>
482 <img src='gfx/icon-settings16.png' style='vertical-align: middle;'>
483 </a>
484 </td>
485 </tr>
486
487 <tr bgcolor='#eee'>
488 <td></td>
489
490 <td width='150' class='small'>
491 <span class="actionstop" style='display:none'>
492 start |
493 <a href='#' onclick="doAction('shutdown','<?php echo $uuid;?>','<?php echo $clean_name;?>')" >stop</a> |
494 <a href='#' onclick="doAction('hardshutdown','<?php echo $uuid;?>','<?php echo $clean_name;?>')" >force shutdown</a>
495 </span>
496
497 <span class="actionstart">
498 <a href='#' onclick="doAction('start','<?php echo $uuid;?>','<?php echo $clean_name;?>')" >start</a> |
499 stop |
500 force shutdown
501 </span>
502
503 </td>
504
505 <td width='100' align='right' class='small' valign='top'>
506 <img src='usagebar.php?usage=1' width='102' height='16' title='static dummy data' class='cpu_graph'><br>
507 <img src='gfx/icon-cpu.png' style='vertical-align: middle;'>
508 <span class='vps_cpu_usage_txt_UID'><?php echo $cpu_count; ?> VCPU</span>
509 </td>
510 <td width='100' align='right' class='small'>
511 <img src='usagebar.php?usage=1' width='102' height='16' title='static dummy data' class='mem_graph'><br>
512 <img src='gfx/icon-memory.png' style='vertical-align: middle;'>
513 <span class='vps_memory_usage_txt_UID'><?php echo format_memory($memory); ?></span>
514 </td>
515 <!--
516 <td width='100' align='right' class='small'>
517 <img src='gfx/icon-network.png' style='vertical-align: middle;'>
518 <span id='vps_net_usage_txt_UID'>na</span>
519 </td>
520 -->
521 <td width='100' align='right' class='small' valign='bottom'>
522 <img src='gfx/icon-disk.png' style='vertical-align: middle;'>
523 <span id='vps_disk_usage_txt_UID'><?php echo format_storage($harddrive_size); ?></span>
524 </td>
525 </tr>
526
527
528 </table>
529 <!-- MACHINE END -->
530
531 <?PHP } ?>
532 </td></tr></table>
533
534 <br>
535
536 <div style="display: none;">
537
538 <div id="dialog-login" title="Login">
539 <form id="loginform">
540 <table border="0">
541 <tr>
542 <td>Username: </td>
543 <td><input type="text" id="username" name="username"></td>
544 <tr>
545 <tr>
546 <td>Password: </td>
547 <td><input type="password" id="password" name="password"></td>
548 </tr>
549 </table>
550 </form>
551 </div>
552
553 <div id="dialog-cd" title="Select CD">
554
555 <table border=0>
556 <tr>
557 <td>Current: <span id="cdcurrent"></span></td>
558 </tr>
559 <tr>
560 <td>
561 CD: <select id="cdselector"></select>
562 </td>
563 </tr>
564 </table>
565 </div>
566
567 </div>
568
569 <ul id="menu">
570 <li action="memory"><a href="#">Set Memory Size</a></li>
571 <li action="cpu"><a href="#">Set CPU Count</a></li>
572
573
574 <!-- <li><a href="#">Item 3</a>
575 <ul>
576 <li><a href="#">Item 3-1</a></li>
577 <li><a href="#">Item 3-2</a></li>
578 <li><a href="#">Item 3-3</a></li>
579 <li><a href="#">Item 3-4</a></li>
580 <li><a href="#">Item 3-5</a></li>
581 </ul>
582 </li>
583 <li><a href="#">Item 4</a></li>
584 <li><a href="#">Item 5</a></li>-->
585 </ul>
586
587 </body></html>

  ViewVC Help
Powered by ViewVC 1.1.20