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

Contents of /misc/xenconsole/index.php

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1892 - (show annotations) (download)
Sun Dec 2 16:30:24 2012 UTC (11 years, 5 months ago) by torben
File size: 9854 byte(s)
add per vm cpu graph
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
44 ?>
45 <html>
46 <head>
47 <title>XenServer::SERVER</title>
48 <script type='text/javascript' src="jquery-1.8.2.min.js"></script>
49
50 <script type='text/javascript'>
51 $(document).ready( function() {
52 setInterval(refreshData, 60000);
53 refreshData();
54 $('.console').click( function() {
55 var session = $(this).data('session');
56 var conurl = $(this).data('conurl');
57 console(conurl,session);
58 });
59
60 $('#logo').click( function() {
61 refreshData();
62 });
63 });
64
65 function refreshData() {
66 $.get('ajaxdata.php', function(xml) {
67 $(xml).find('host').each(function() {
68 var memtotal = $(this).find('memtotal').text();
69 var memfree = $(this).find('memfree').text();
70 var cpuavg = $(this).find('cpuavg').text();
71
72 memtotal = Math.round ( memtotal / (1024*1024) );
73 memfree = Math.round ( memfree / (1024*1024) );
74 var memused = memtotal - memfree;
75 var mem_percentage = Math.round( (memused/memtotal) * 100);
76
77 //alert(memused + ' ' + mem_percentage);
78
79 $('#server_memory_usage').attr('src', 'usagebar.php?usage=' + mem_percentage);
80 $('#server_memory_usage').attr('alt', mem_percentage + '%');
81 $('#server_memory_usage').attr('title', mem_percentage + '%');
82 $('#server_memory_usage_txt').text( memused + '/' + memtotal + 'MB');
83
84
85 $('#server_cpu_usage').attr('src', 'usagebar.php?usage=' + cpuavg);
86 $('#server_cpu_usage').attr('alt', cpuavg + '%');
87 $('#server_cpu_usage').attr('title', cpuavg + '%');
88 $('#server_cpu_usage_txt').text( cpuavg+ '%');
89 });
90 $(xml).find('vm').each(function() {
91 var name = $(this).find('name').text();
92 var state = $(this).find('state').text();
93 var network = $(this).find('network').text();
94 var state = $(this).find('state').text();
95 var conurl = $(this).find('conurl').text();
96 var session = $(this).find('session').text();
97 var os = $(this).find('os').text();
98 var cpuavg = $(this).find('cpuavg').text();
99
100 name = name.replace(" ", "_");
101 name = name.replace(".", "_");
102
103 var id = "#vm_" + name;
104
105 var vm = $(id);
106
107 if (state == "Running") {
108 vm.find('.state').css("background-image", "url('gfx/vps_topgreen.png')");
109 vm.find('.network').show();
110 vm.find('.network').text( '(IP: ' + network + ')' );
111 vm.find('.console').show();
112 vm.find('.console').data('conurl', conurl);
113 vm.find('.console').data('session', session);
114 vm.find('.actionstop').show();
115 vm.find('.actionstart').hide();
116 vm.find('.os').text(' - ' + os);
117
118
119 vm.find('.cpu_graph').show();
120 vm.find('.cpu_graph').attr('src', 'usagebar.php?usage=' + cpuavg);
121 vm.find('.cpu_graph').attr('title', cpuavg + '%');
122 } else {
123 if (state == "Halted") {
124 vm.find('.state').css("background-image", "url('gfx/vps_topred.png')");
125 } else {
126 vm.find('.state').css("background-image", "url('gfx/vps_topyellow.png')");
127 }
128 vm.find('.network').hide();
129 vm.find('.console').hide();
130 vm.find('.actionstop').hide();
131 vm.find('.actionstart').show();
132 vm.find('.cpu_graph').hide();
133 vm.find('.mem_graph').hide();
134 }
135
136 })
137 });
138 }
139
140 function doAction(action, uuid, vm) {
141 var key = prompt("Key");
142 if (key == "" || key == null) {
143 return;
144 }
145 document.body.style.cursor = 'wait';
146 $('#vm_' + vm).find('.state').css("background-image", "url('gfx/vps_topyellow.png')");
147
148 var url = "action.php?action=" + action + "&uuid=" + uuid + "&key=" + key;
149 var response = $.get(url, function(data) {
150 if (data != 'OK') {
151 alert(data);
152 }
153 document.body.style.cursor = 'default'
154
155 refreshData();
156 });
157 }
158
159 function console(url, session) {
160 if (url == '')
161 return;
162
163 var key = prompt("Key");
164 if (key == "" || key == null) {
165 return;
166 }
167 var url = "console.php?url=" + url + "&session=" + session + "&key=" + key;
168
169 //window.location = url;
170 //$('#mainwindow').load(url);
171 window.open(url);
172 }
173
174
175
176 </script>
177
178 <style>
179 body {
180 background: #eee;
181 margin-top: 5px;
182 font-family:verdana,helvetica,arial,sans-serif;
183 font-size: 14px;
184
185 }
186
187 .maintable {
188 background: #fff;
189 margin-left:auto; // Smart center
190 margin-right:auto;
191 padding: 0;
192 border: 3px solid #999;
193 }
194
195 .small {
196 font-family:verdana,helvetica,arial,sans-serif;
197 font-size: 11px;
198 }
199
200 .toptable {
201 border-bottom: 3px solid #999;
202 }
203
204 .vps {
205 margin: 4;
206 border: 1px solid #222;
207 }
208 </style>
209 </head>
210 <body>
211
212 <table class='maintable' width='800' align='center' cellpadding='0' cellspacing='0'><tr><td>
213
214 <table width='100%' background='gfx/topbg.png' cellpadding='3' cellspacing='0' border='0' class='toptable'>
215 <tr>
216 <td rowspan='2' width='160'><img src='gfx/citrix-logo.png' id='logo'></td>
217 <td colspan='4' class='small' align='right'><?php echo $xenversion;?><br><?php echo $license;?></td>
218 </tr>
219 <tr>
220 <td width='150' align='right' class='small'><img src='gfx/icon-cpu.png'>
221 <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>
222
223 <td width='150' align='right' class='small'><img src='gfx/icon-memory.png'>
224 <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>
225
226 <td width='150' align='right' class='small'><img src='gfx/icon-network.png'>
227 <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>
228 <td width='150' align='right' class='small'><img src='gfx/icon-disk.png'>
229 <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>
230
231 </td>
232 </tr></table>
233
234
235
236 <?php
237 // List all machines
238 foreach($vms_array as $vm) {
239 if ($vm["is_a_template"] != 0 || $vm["is_control_domain"] != 0) {
240 continue;
241 }
242
243 $name = $vm["name_label"];
244 $uuid = $vm["uuid"];
245 $state = $vm["power_state"] ;
246 $memory = $vm['memory_target'];
247 $harddrive_size = 0;
248 $cpu_count = $vm["VCPUs_max"];
249
250 $clean_name = str_replace(" ", "_", $name);
251 $clean_name = str_replace(".", "_", $clean_name);
252
253
254 foreach ($vm["VBDs"] as $vbds) {
255 $vbd = $xenserver->VBD__get_record($vbds);
256 if ($vbd["type"] == 'Disk') {
257 $vdi = $xenserver->VDI__get_record( $vbd["VDI"] );
258 $harddrive_size += $vdi["virtual_size"];
259 }
260 }
261
262
263
264 ?>
265 <!-- MACHINE -->
266 <table width='99%' cellpadding='3' cellspacing='0' border='0' class='vps' id='vm_<?php echo $clean_name;?>'>
267 <tr background='gfx/vps_topbg.png' >
268 <td width='10' class='small state'>&nbsp;</td>
269 <td colspan='2' class='small'><b><?php echo $name; ?></b><span class='os'></span></td>
270 <td colspan='2' class='small' align='right'>
271 <span class='network'></span>
272 <a href='#' style='display:none;' class='console'>
273 <img src='gfx/icon-terminal.png' style='vertical-align: middle;'>
274 </a>
275 </td>
276 </tr>
277
278 <tr bgcolor='#eee'>
279 <td></td>
280
281 <td width='150' class='small'>
282 <span class="actionstop" style='display:none'>
283 start |
284 <a href='#' onclick="doAction('shutdown','<?php echo $uuid;?>','<?php echo $clean_name;?>')" >stop</a> |
285 <a href='#' onclick="doAction('hardshutdown','<?php echo $uuid;?>','<?php echo $clean_name;?>')" >force shutdown</a>
286 </span>
287
288 <span class="actionstart">
289 <a href='#' onclick="doAction('start','<?php echo $uuid;?>','<?php echo $clean_name;?>')" >start</a> |
290 stop |
291 force shutdown
292 </span>
293
294 </td>
295
296 <td width='100' align='right' class='small' valign='top'>
297 <img src='usagebar.php?usage=1' width='102' height='16' title='static dummy data' class='cpu_graph'><br>
298 <img src='gfx/icon-cpu.png' style='vertical-align: middle;'>
299 <span id='vps_cpu_usage_txt_UID'><?php echo $cpu_count; ?> VCPU</span>
300 </td>
301 <td width='100' align='right' class='small'>
302 <img src='usagebar.php?usage=1' width='102' height='16' title='static dummy data' class='mem_graph'><br>
303 <img src='gfx/icon-memory.png' style='vertical-align: middle;'>
304 <span id='vps_memory_usage_txt_UID'><?php echo format_memory($memory); ?></span>
305 </td>
306 <!--
307 <td width='100' align='right' class='small'>
308 <img src='gfx/icon-network.png' style='vertical-align: middle;'>
309 <span id='vps_net_usage_txt_UID'>na</span>
310 </td>
311 -->
312 <td width='100' align='right' class='small' valign='top'>
313 <img src='gfx/icon-disk.png' style='vertical-align: middle;'>
314 <span id='vps_disk_usage_txt_UID'><?php echo format_storage($harddrive_size); ?></span>
315 </td>
316 </tr>
317
318
319 </table>
320 <!-- MACHINE END -->
321
322 <?PHP } ?>
323 </td></tr></table>
324 </body></html>

  ViewVC Help
Powered by ViewVC 1.1.20