27 |
|
|
28 |
/* Establish session with Xenserver */ |
/* Establish session with Xenserver */ |
29 |
$xenserver = new XenApi($url, $login, $password); |
$xenserver = new XenApi($url, $login, $password); |
|
$vms_array = $xenserver->VM__get_all_records(); |
|
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 |
|
$mem_total = $host_metrics["memory_total"]; |
36 |
|
$mem_free = $host_metrics["memory_free"]; |
37 |
|
|
38 |
|
$cpus = $host["cpu_info"]["cpu_count"]; |
39 |
|
$cpusum = 0.0; |
40 |
|
for ($i=0; $i<$cpus; $i++) { |
41 |
|
$cpusum += $xenserver->host__query_data_source($hosts_array[0], "cpu" . $i); |
42 |
|
} |
43 |
|
$cpuavg = round ( ($cpusum/$cpus) * 100); |
44 |
|
|
45 |
header("Content-Type: text/xml"); |
header("Content-Type: text/xml"); |
46 |
echo "<xen>"; |
echo "<xen>"; |
47 |
|
echo "<host>"; |
48 |
|
echo "<memtotal>{$mem_total}</memtotal>"; |
49 |
|
echo "<memfree>{$mem_free}</memfree>"; |
50 |
|
echo "<cpuavg>{$cpuavg}</cpuavg>"; |
51 |
|
echo "</host>"; |
52 |
|
|
53 |
|
|
54 |
|
|
55 |
|
$vms_array = $xenserver->VM__get_all_records(); |
56 |
// List all machines |
// List all machines |
57 |
foreach($vms_array as $vm) { |
foreach($vms_array as $vmref => $vm) { |
58 |
if ($vm["is_a_template"] != 0 || $vm["is_control_domain"] != 0) { |
if ($vm["is_a_template"] != 0 || $vm["is_control_domain"] != 0) { |
59 |
continue; |
continue; |
60 |
} |
} |
63 |
$name = $vm["name_label"]; |
$name = $vm["name_label"]; |
64 |
$uuid = $vm["uuid"]; |
$uuid = $vm["uuid"]; |
65 |
$state = $vm["power_state"] ; |
$state = $vm["power_state"] ; |
66 |
|
$cpus = $vm["VCPUs_max"]; |
67 |
|
|
68 |
|
|
69 |
$network = ""; |
$network = ""; |
70 |
$url = ""; |
$url = ""; |
71 |
$session = ""; |
$session = ""; |
72 |
|
$os = ""; |
73 |
|
$cpuavg = 0.0; |
74 |
|
$curmem = 0; |
75 |
|
$maxmem = 0; |
76 |
|
|
77 |
if ( $vm["power_state"] == "Running" ) { |
if ( $vm["power_state"] == "Running" ) { |
78 |
if ($vm["guest_metrics"] != "OpaqueRef:NULL") {//guest tools installed |
if ($vm["guest_metrics"] != "OpaqueRef:NULL") {//guest tools installed |
79 |
$guest_metrics = $xenserver->VM_guest_metrics__get_record( $vm["guest_metrics"] ); |
$guest_metrics = $xenserver->VM_guest_metrics__get_record( $vm["guest_metrics"] ); |
80 |
$network = implode( ",", $guest_metrics["networks"]) ; |
$network = implode( ",", $guest_metrics["networks"]) ; |
81 |
|
$tmpos = explode('|', $guest_metrics["os_version"]["name"]); |
82 |
|
$os = $tmpos[0]; |
83 |
} |
} |
84 |
|
$cpusum = 0.0; |
85 |
|
for ($i=0; $i<$cpus; $i++) { |
86 |
|
$cpusum += $xenserver->VM__query_data_source($vmref, "cpu" . $i); |
87 |
|
} |
88 |
|
$cpuavg = round ( ($cpusum/$cpus) * 100); |
89 |
|
$maxmem = round($xenserver->VM__query_data_source($vmref, "memory") / (1024*1024) ); |
90 |
|
$free = round( $xenserver->VM__query_data_source($vmref, "memory_internal_free") / 1024 ); |
91 |
|
$curmem = $maxmem - $free; |
92 |
|
|
93 |
foreach($vm["consoles"] as $conref) { |
foreach($vm["consoles"] as $conref) { |
94 |
$con = $xenserver->console__get_record( $conref ); |
$con = $xenserver->console__get_record( $conref ); |
109 |
echo "<network>{$network}</network>"; |
echo "<network>{$network}</network>"; |
110 |
echo "<conurl>{$url}</conurl>"; |
echo "<conurl>{$url}</conurl>"; |
111 |
echo "<session>{$session}</session>"; |
echo "<session>{$session}</session>"; |
112 |
|
echo "<os>{$os}</os>"; |
113 |
|
echo "<cpuavg>{$cpuavg}</cpuavg>"; |
114 |
|
echo "<curmem>{$curmem}</curmem>"; |
115 |
|
echo "<maxmem>{$maxmem}</maxmem>"; |
116 |
|
|
117 |
echo "</vm>"; |
echo "</vm>"; |
118 |
} |
} |