23 |
} |
} |
24 |
} |
} |
25 |
|
|
26 |
|
function get_network_string($networks) { |
27 |
|
global $hideipv6; |
28 |
|
|
29 |
|
$ret = ""; |
30 |
|
foreach($networks as $net) { |
31 |
|
if ($hideipv6 == true) { |
32 |
|
if ( strpos($net, ":") !== false) |
33 |
|
continue; |
34 |
|
} |
35 |
|
|
36 |
|
if ($ret != "") |
37 |
|
$ret .= ","; |
38 |
|
|
39 |
|
$ret .= $net; |
40 |
|
} |
41 |
|
return $ret; |
42 |
|
} |
43 |
|
|
44 |
include('xenapi.php'); |
include('xenapi.php'); |
45 |
|
|
46 |
/* Establish session with Xenserver */ |
/* Establish session with Xenserver */ |
47 |
$xenserver = new XenApi($url, $login, $password); |
$xenserver = new XenApi($url, $login, $password); |
48 |
|
|
49 |
|
//Opbyg cache arrays |
50 |
|
$consolemap = array(); |
51 |
|
$consoles = $xenserver->console__get_all_records(); |
52 |
|
foreach($consoles as $key => $val) { |
53 |
|
$consolemap[$key] = $val; |
54 |
|
} |
55 |
|
|
56 |
|
$guestmetricmap = array(); |
57 |
|
$guestmetrics = $xenserver->VM_guest_metrics__get_all_records(); |
58 |
|
foreach($guestmetrics as $key => $val) { |
59 |
|
$guestmetricmap[$key] = $val; |
60 |
|
} |
61 |
|
|
62 |
|
//find data |
63 |
|
|
64 |
$hosts_array = $xenserver->host__get_all(); |
$hosts_array = $xenserver->host__get_all(); |
65 |
$host = $xenserver->host__get_record($hosts_array[0]); |
$host = $xenserver->host__get_record($hosts_array[0]); |
66 |
$host_metrics = $xenserver->host_metrics__get_record($host["metrics"]); |
$host_metrics = $xenserver->host_metrics__get_record($host["metrics"]); |
68 |
$mem_total = $host_metrics["memory_total"]; |
$mem_total = $host_metrics["memory_total"]; |
69 |
$mem_free = $host_metrics["memory_free"]; |
$mem_free = $host_metrics["memory_free"]; |
70 |
|
|
71 |
$vms_array = $xenserver->VM__get_all_records(); |
$cpus = $host["cpu_info"]["cpu_count"]; |
72 |
|
$cpusum = 0.0; |
73 |
|
for ($i=0; $i<$cpus; $i++) { |
74 |
|
$cpusum += $xenserver->host__query_data_source($hosts_array[0], "cpu" . $i); |
75 |
|
} |
76 |
|
$cpuavg = round ( ($cpusum/$cpus) * 100); |
77 |
|
|
78 |
header("Content-Type: text/xml"); |
header("Content-Type: text/xml"); |
79 |
echo "<xen>"; |
echo "<xen>"; |
80 |
echo "<host>"; |
echo "<host>"; |
81 |
echo "<memtotal>{$mem_total}</memtotal>"; |
echo "<memtotal>{$mem_total}</memtotal>"; |
82 |
echo "<memfree>{$mem_free}</memfree>"; |
echo "<memfree>{$mem_free}</memfree>"; |
83 |
|
echo "<cpuavg>{$cpuavg}</cpuavg>"; |
84 |
echo "</host>"; |
echo "</host>"; |
85 |
|
|
86 |
|
|
87 |
|
|
88 |
|
$vms_array = $xenserver->VM__get_all_records(); |
89 |
// List all machines |
// List all machines |
90 |
foreach($vms_array as $vmref => $vm) { |
foreach($vms_array as $vmref => $vm) { |
91 |
if ($vm["is_a_template"] != 0 || $vm["is_control_domain"] != 0) { |
if ($vm["is_a_template"] != 0 || $vm["is_control_domain"] != 0) { |
104 |
$session = ""; |
$session = ""; |
105 |
$os = ""; |
$os = ""; |
106 |
$cpuavg = 0.0; |
$cpuavg = 0.0; |
107 |
if ( $vm["power_state"] == "Running" ) { |
$curmem = 0; |
108 |
|
$maxmem = 0; |
109 |
|
$guestversion = ""; |
110 |
|
|
111 |
|
if ( $vm["power_state"] == "Running" ) { |
112 |
if ($vm["guest_metrics"] != "OpaqueRef:NULL") {//guest tools installed |
if ($vm["guest_metrics"] != "OpaqueRef:NULL") {//guest tools installed |
113 |
$guest_metrics = $xenserver->VM_guest_metrics__get_record( $vm["guest_metrics"] ); |
//$guest_metrics = $xenserver->VM_guest_metrics__get_record( $vm["guest_metrics"] ); |
114 |
$network = implode( ",", $guest_metrics["networks"]) ; |
$guest_metrics = $guestmetricmap[ $vm["guest_metrics"] ]; |
115 |
$os = $guest_metrics["os_version"]["name"]; |
$network = get_network_string($guest_metrics["networks"]) ; |
116 |
|
$tmpos = explode('|', $guest_metrics["os_version"]["name"]); |
117 |
|
$os = $tmpos[0]; |
118 |
|
$guestversion = $guest_metrics["PV_drivers_version"]["major"] . "." . $guest_metrics["PV_drivers_version"]["minor"] . "." . $guest_metrics["PV_drivers_version"]["micro"]; |
119 |
|
|
120 |
|
$maxmem = round($xenserver->VM__query_data_source($vmref, "memory") / (1024*1024) ); |
121 |
|
$free = round( $xenserver->VM__query_data_source($vmref, "memory_internal_free") / 1024 ); |
122 |
|
$curmem = $maxmem - $free; |
123 |
|
} else { |
124 |
|
$maxmem = $vm["memory_static_max"] / (1024*1024); |
125 |
} |
} |
126 |
|
|
127 |
$cpusum = 0.0; |
$cpusum = 0.0; |
128 |
for ($i=0; $i<$cpus; $i++) { |
for ($i=0; $i<$cpus; $i++) { |
129 |
$cpusum += $xenserver->VM__query_data_source($vmref, "cpu" . $i); |
$cpusum += $xenserver->VM__query_data_source($vmref, "cpu" . $i); |
130 |
} |
} |
131 |
$cpuavg = round ( ($cpusum/$cpus) * 100); |
$cpuavg = round ( ($cpusum/$cpus) * 100); |
|
|
|
132 |
|
|
133 |
foreach($vm["consoles"] as $conref) { |
foreach($vm["consoles"] as $conref) { |
134 |
$con = $xenserver->console__get_record( $conref ); |
//$con = $xenserver->console__get_record( $conref ); |
135 |
|
$con = $consolemap[$conref]; |
136 |
|
|
137 |
if ($con["protocol"] == "rfb") { |
if ($con["protocol"] == "rfb") { |
138 |
|
$session = $xenserver->get_id(); |
139 |
$url = urlencode( $con["location"] ); |
$url = urlencode( $con["location"] ); |
|
$session = $xenserver->get_id(); |
|
140 |
} |
} |
141 |
} |
} |
142 |
|
|
143 |
} |
} else { |
144 |
|
$maxmem = $vm["memory_static_max"] / (1024*1024); |
145 |
|
} |
146 |
|
|
147 |
|
|
148 |
|
|
153 |
echo "<conurl>{$url}</conurl>"; |
echo "<conurl>{$url}</conurl>"; |
154 |
echo "<session>{$session}</session>"; |
echo "<session>{$session}</session>"; |
155 |
echo "<os>{$os}</os>"; |
echo "<os>{$os}</os>"; |
156 |
|
echo "<guestversion>{$guestversion}</guestversion>"; |
157 |
echo "<cpuavg>{$cpuavg}</cpuavg>"; |
echo "<cpuavg>{$cpuavg}</cpuavg>"; |
158 |
|
echo "<curmem>{$curmem}</curmem>"; |
159 |
|
echo "<maxmem>{$maxmem}</maxmem>"; |
160 |
|
echo "<cpus>{$cpus}</cpus>"; |
161 |
|
|
162 |
echo "</vm>"; |
echo "</vm>"; |
163 |
} |
} |