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

Contents of /misc/xenconsole/index.php

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.20