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

Contents of /misc/xenconsole/index.php

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1958 - (show annotations) (download)
Tue Mar 26 13:43:07 2013 UTC (11 years, 1 month ago) by torben
File size: 12802 byte(s)
addmenu options for adjusting memory and vcpu count
1 <?php
2 include("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 $(document).ready( function() {
64 $("#menu").hide();
65 setInterval(refreshData, 60000);
66 refreshData();
67 $('.console').click( function() {
68 var session = $(this).data('session');
69 var conurl = $(this).data('conurl');
70 var name = $(this).data('name');
71 openConsole(conurl,session,name);
72 });
73
74 $('#logo').click( function() {
75 refreshData();
76 });
77
78 $('.settings').click( function(event) {
79 menu_uuid = $(this).attr('uuid');
80
81 $('#menu').menu( {
82 select: function(event2,ui) {
83 var action = $(ui.item).attr("action");
84 if (action == "memory") {
85 doAction("setMemory", menu_uuid, "");
86 }
87 if (action == "cpu") {
88 doAction("setCPU", menu_uuid, "");
89 }
90 },
91 create: function(event3,ui) {
92 menu = 1;
93 }
94 /*position: {
95 my: "left", of: event
96 }*/
97 });
98 $('#menu').show().position( {my: "left top", of: event} );
99 event.stopPropagation();
100 });
101
102 $(document).click( function(event) {
103 closeMenu();
104 });
105
106 });
107
108 function closeMenu() {
109 if (menu > 0) {
110 $("#menu").menu("destroy").hide();
111 menu = 0;
112 }
113 }
114
115 function refreshData() {
116 $("#logo").hide();
117 $.get('ajaxdata.php', function(xml) {
118 $(xml).find('host').each(function() {
119 var memtotal = $(this).find('memtotal').text();
120 var memfree = $(this).find('memfree').text();
121 var cpuavg = $(this).find('cpuavg').text();
122
123 memtotal = Math.round ( memtotal / (1024*1024) );
124 memfree = Math.round ( memfree / (1024*1024) );
125 var memused = memtotal - memfree;
126 var mem_percentage = Math.round( (memused/memtotal) * 100);
127
128 //alert(memused + ' ' + mem_percentage);
129
130 $('#server_memory_usage').attr('src', 'usagebar.php?usage=' + mem_percentage);
131 $('#server_memory_usage').attr('alt', mem_percentage + '%');
132 $('#server_memory_usage').attr('title', mem_percentage + '%');
133 $('#server_memory_usage_txt').text( memused + '/' + memtotal + 'MB');
134
135
136 $('#server_cpu_usage').attr('src', 'usagebar.php?usage=' + cpuavg);
137 $('#server_cpu_usage').attr('alt', cpuavg + '%');
138 $('#server_cpu_usage').attr('title', cpuavg + '%');
139 $('#server_cpu_usage_txt').text( cpuavg+ '%');
140 });
141 $(xml).find('vm').each(function() {
142 var name = $(this).find('name').text();
143 var state = $(this).find('state').text();
144 var network = $(this).find('network').text();
145 var state = $(this).find('state').text();
146 var conurl = $(this).find('conurl').text();
147 var session = $(this).find('session').text();
148 var os = $(this).find('os').text();
149 var guestversion = $(this).find('guestversion').text();
150 var cpuavg = $(this).find('cpuavg').text();
151 var curmem = $(this).find('curmem').text();
152 var maxmem = $(this).find('maxmem').text();
153 var cpus = $(this).find('cpus').text();
154
155 var mempercent = Math.round( (curmem*100) / maxmem );
156
157 name = name.replace(" ", "_");
158 name = name.replace(".", "_");
159
160 var id = "#vm_" + name;
161
162 var vm = $(id);
163
164 if (guestversion != '') {
165 os += ' Guest Tools: ' + guestversion;
166 }
167
168 vm.find('.vps_memory_usage_txt_UID').text( maxmem + " MB");
169 vm.find('.vps_cpu_usage_txt_UID').text( cpus + " VCPU");
170
171 if (state == "Running") {
172 vm.find('.state').css("background-image", "url('gfx/vps_topgreen.png')");
173 vm.find('.network').show();
174 vm.find('.network').text( '(IP: ' + network + ')' );
175 vm.find('.console').show();
176 vm.find('.console').data('conurl', conurl);
177 vm.find('.console').data('session', session);
178 vm.find('.console').data('name', name);
179 vm.find('.settings').hide();
180 vm.find('.actionstop').show();
181 vm.find('.actionstart').hide();
182 vm.find('.os').text(' - ' + os);
183
184
185 vm.find('.cpu_graph').show();
186 vm.find('.cpu_graph').attr('src', 'usagebar.php?usage=' + cpuavg);
187 vm.find('.cpu_graph').attr('title', cpuavg + '%');
188
189 vm.find('.mem_graph').show();
190 vm.find('.mem_graph').attr('src', 'usagebar.php?usage=' + mempercent);
191 vm.find('.mem_graph').attr('title', curmem + ' / ' + maxmem + ' MB' );
192 } else {
193 if (state == "Halted") {
194 vm.find('.state').css("background-image", "url('gfx/vps_topred.png')");
195 } else {
196 vm.find('.state').css("background-image", "url('gfx/vps_topyellow.png')");
197 }
198 vm.find('.os').text('');
199
200 vm.find('.network').hide();
201 vm.find('.console').hide();
202 vm.find('.settings').show();
203 vm.find('.actionstop').hide();
204 vm.find('.actionstart').show();
205 vm.find('.cpu_graph').hide();
206 vm.find('.mem_graph').hide();
207 }
208
209 });
210 $('#logo').show();
211 });
212 }
213
214 function doAction(action, uuid, vm) {
215 var val="";
216
217 var key = prompt("Key");
218 if (key == "" || key == null) {
219 return;
220 }
221 document.body.style.cursor = 'wait';
222 $('#vm_' + vm).find('.state').css("background-image", "url('gfx/vps_topyellow.png')");
223
224 if (action == "setMemory") {
225 val = prompt("Set memory target");
226 if (val == "" || val == null) {
227 return;
228 }
229 }
230 if (action == "setCPU") {
231 val = prompt("Set CPU count");
232 if (val == "" || val == null) {
233 return;
234 }
235 }
236
237 var url = "action.php?action=" + action + "&uuid=" + uuid + "&key=" + key + "&val=" + val;;
238 var response = $.get(url, function(data) {
239 if (data != 'OK') {
240 alert(data);
241 }
242 document.body.style.cursor = 'default'
243
244 refreshData();
245 });
246 }
247
248 function openConsole(url, session, name) {
249 if (url == '')
250 return;
251
252 var key = prompt("Key");
253 if (key == "" || key == null) {
254 return;
255 }
256 var url = "console.php?url=" + url + "&session=" + session + "&key=" + key + "&name=" + name;
257
258 //window.location = url;
259 //$('#mainwindow').load(url);
260 window.open(url);
261 }
262
263
264
265 </script>
266
267 <style>
268 body {
269 background: #eee;
270 margin-top: 5px;
271 font-family:verdana,helvetica,arial,sans-serif;
272 font-size: 14px;
273
274 }
275
276 .maintable {
277 background: #fff;
278 margin-left:auto; // Smart center
279 margin-right:auto;
280 padding: 0;
281 border: 3px solid #999;
282 }
283
284 .small {
285 font-family:verdana,helvetica,arial,sans-serif;
286 font-size: 11px;
287 }
288
289 .toptable {
290 border-bottom: 3px solid #999;
291 }
292
293 .vps {
294 margin: 4;
295 border: 1px solid #222;
296 }
297 </style>
298 </head>
299 <body>
300
301 <table class='maintable' width='800' align='center' cellpadding='0' cellspacing='0'><tr><td>
302
303 <table width='100%' background='gfx/topbg.png' cellpadding='3' cellspacing='0' border='0' class='toptable'>
304 <tr>
305 <td rowspan='2' width='160'><img src='gfx/citrix-logo.png' id='logo'></td>
306 <td colspan='4' class='small' align='right'><?php echo $namelabel . " / " . $xenversion;?><br><?php echo $license;?></td>
307 </tr>
308 <tr>
309 <td width='150'></td>
310 <td width='150'></td>
311 <td width='150' align='right' class='small'><img src='gfx/icon-cpu.png'>
312 <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>
313
314 <td width='150' align='right' class='small'><img src='gfx/icon-memory.png'>
315 <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>
316 <!--
317 <td width='150' align='right' class='small'><img src='gfx/icon-network.png'>
318 <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>
319 <td width='150' align='right' class='small'><img src='gfx/icon-disk.png'>
320 <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>
321 </td>
322 -->
323 </tr></table>
324
325
326
327 <?php
328 // List all machines
329 foreach($vms_array as $vm) {
330 if ($vm["is_a_template"] != 0 || $vm["is_control_domain"] != 0) {
331 continue;
332 }
333
334 $name = $vm["name_label"];
335 $description = $vm["name_description"];
336 $uuid = $vm["uuid"];
337 $state = $vm["power_state"] ;
338 $memory = $vm['memory_target'];
339 $harddrive_size = 0;
340 $cpu_count = $vm["VCPUs_max"];
341
342 $clean_name = str_replace(" ", "_", $name);
343 $clean_name = str_replace(".", "_", $clean_name);
344
345
346 foreach ($vm["VBDs"] as $vbds) {
347 $vbd = $xenserver->VBD__get_record($vbds);
348 if ($vbd["type"] == 'Disk') {
349 $vdi = $xenserver->VDI__get_record( $vbd["VDI"] );
350 $harddrive_size += $vdi["virtual_size"];
351 }
352 }
353
354
355
356 ?>
357 <!-- MACHINE -->
358 <table width='99%' cellpadding='3' cellspacing='0' border='0' class='vps' id='vm_<?php echo $clean_name;?>'>
359 <tr background='gfx/vps_topbg.png' >
360 <td width='10' class='small state'>&nbsp;</td>
361 <td colspan='2' class='small' title='<?php echo $description;?>'><b><?php echo $name; ?></b><span class='os'></span></td>
362 <td colspan='2' class='small' align='right'>
363 <span class='network'></span>
364 <a href='#' style='display:none;' class='console'>
365 <img src='gfx/icon-terminal.png' style='vertical-align: middle;'>
366 </a>
367 <a href='#' style='display:none;' class='settings' uuid='<?php echo $uuid;?>'>
368 <img src='gfx/icon-settings16.png' style='vertical-align: middle;'>
369 </a>
370 </td>
371 </tr>
372
373 <tr bgcolor='#eee'>
374 <td></td>
375
376 <td width='150' class='small'>
377 <span class="actionstop" style='display:none'>
378 start |
379 <a href='#' onclick="doAction('shutdown','<?php echo $uuid;?>','<?php echo $clean_name;?>')" >stop</a> |
380 <a href='#' onclick="doAction('hardshutdown','<?php echo $uuid;?>','<?php echo $clean_name;?>')" >force shutdown</a>
381 </span>
382
383 <span class="actionstart">
384 <a href='#' onclick="doAction('start','<?php echo $uuid;?>','<?php echo $clean_name;?>')" >start</a> |
385 stop |
386 force shutdown
387 </span>
388
389 </td>
390
391 <td width='100' align='right' class='small' valign='top'>
392 <img src='usagebar.php?usage=1' width='102' height='16' title='static dummy data' class='cpu_graph'><br>
393 <img src='gfx/icon-cpu.png' style='vertical-align: middle;'>
394 <span class='vps_cpu_usage_txt_UID'><?php echo $cpu_count; ?> VCPU</span>
395 </td>
396 <td width='100' align='right' class='small'>
397 <img src='usagebar.php?usage=1' width='102' height='16' title='static dummy data' class='mem_graph'><br>
398 <img src='gfx/icon-memory.png' style='vertical-align: middle;'>
399 <span class='vps_memory_usage_txt_UID'><?php echo format_memory($memory); ?></span>
400 </td>
401 <!--
402 <td width='100' align='right' class='small'>
403 <img src='gfx/icon-network.png' style='vertical-align: middle;'>
404 <span id='vps_net_usage_txt_UID'>na</span>
405 </td>
406 -->
407 <td width='100' align='right' class='small' valign='bottom'>
408 <img src='gfx/icon-disk.png' style='vertical-align: middle;'>
409 <span id='vps_disk_usage_txt_UID'><?php echo format_storage($harddrive_size); ?></span>
410 </td>
411 </tr>
412
413
414 </table>
415 <!-- MACHINE END -->
416
417 <?PHP } ?>
418 </td></tr></table>
419
420 <br>
421
422 <ul id="menu">
423 <li action="memory"><a href="#">Set Memory Size</a></li>
424 <li action="cpu"><a href="#">Set CPU Count</a></li>
425
426
427 <!-- <li><a href="#">Item 3</a>
428 <ul>
429 <li><a href="#">Item 3-1</a></li>
430 <li><a href="#">Item 3-2</a></li>
431 <li><a href="#">Item 3-3</a></li>
432 <li><a href="#">Item 3-4</a></li>
433 <li><a href="#">Item 3-5</a></li>
434 </ul>
435 </li>
436 <li><a href="#">Item 4</a></li>
437 <li><a href="#">Item 5</a></li>-->
438 </ul>
439
440 </body></html>

  ViewVC Help
Powered by ViewVC 1.1.20