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

Contents of /misc/xenconsole/index.php

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.20