ia64/xen-unstable

annotate tools/examples/xeninfo.pl @ 19648:f0e2df69a8eb

x86 hvm: Allow cross-vendor migration

Intercept #UD and emulate SYSCALL/SYSENTER/SYSEXIT as necessary.

Signed-off-by: Christoph Egger <Christoph.Egger@amd.com>
Signed-off-by: Keir Fraser <keir.fraser@citrix.com>
author Keir Fraser <keir.fraser@citrix.com>
date Tue May 26 15:01:36 2009 +0100 (2009-05-26)
parents 33dcf04d7715
children b51f9542ab14
rev   line source
ewan@15175 1 #!/usr/bin/perl -w
ewan@15175 2
ewan@15175 3 #############################################################################################################
ewan@15175 4 # #
ewan@15175 5 # Developed by Ingard Mev├ąg @ Oslo University College, spring 2007 #
ewan@15175 6 # ingard [at] mevaag [dot] no #
ewan@15175 7 # #
ewan@15175 8 # This work is licensed under the Creative Commons Attribution-Noncommercial-Share Alike 3.0 License. #
ewan@15175 9 # To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/3.0/ or send a letter #
ewan@15175 10 # to Creative Commons, 171 Second Street, Suite 300, San Francisco, California, 94105, USA. #
ewan@15175 11 # #
ewan@15175 12 #############################################################################################################
ewan@15175 13
ewan@15175 14 use strict;
ewan@15175 15 # http://search.cpan.org/~rjray/RPC-XML-0.59/lib/RPC/XML/Client.pm
ewan@15175 16 require RPC::XML;
ewan@15175 17 require RPC::XML::Client;
ewan@15175 18
ewan@15175 19 # for debug purposes
ewan@15175 20 #use Data::Dumper;
ewan@15175 21
ewan@15175 22 ##### CONFIG ######
ewan@15175 23
keir@16739 24 my %xenhosts = ("192.0.2.10" => {"port" => "9363"},
keir@16739 25 "192.0.2.11" => {"port" => "9363"},
keir@16739 26 "192.0.2.12" => {"port" => "9363"},
keir@16739 27 "192.0.2.13" => {"port" => "9363"});
ewan@15175 28
ewan@15175 29 ##### CONFIG END ###
ewan@15175 30
ewan@15175 31 ##### STATIC VARS #####
ewan@15175 32 my %host_info;
ewan@15175 33
ewan@15175 34 #######################
ewan@15175 35 sub apiconnect
ewan@15175 36 {
ewan@15175 37 foreach my $xenhost (keys %xenhosts)
ewan@15175 38 {
ewan@15175 39 my $xen = RPC::XML::Client->new("http://$xenhost:$xenhosts{$xenhost}{'port'}");
ewan@15175 40 my $session = $xen->simple_request("session.login_with_password", "user","");
ewan@15175 41 if (! $session)
ewan@15175 42 {
ewan@15175 43 print "Can't connect to $xenhost :(\n";
ewan@15175 44 $xenhosts{$xenhost} = {'xen' => $xen, 'session' => ""};
ewan@15175 45 }
ewan@15175 46 else
ewan@15175 47 {
ewan@15175 48 $xenhosts{$xenhost} = {'xen' => $xen, 'session' => $session->{'Value'}};
ewan@15175 49 print "Connected successfully to $xenhost..\n";
ewan@15175 50 }
ewan@15175 51 }
ewan@15175 52 }
ewan@15175 53
ewan@15175 54 sub validate_response
ewan@15175 55 {
ewan@15175 56 my ($result_ref) = @_;
ewan@15175 57 if ($result_ref->{'Status'} eq "Success")
ewan@15175 58 {
ewan@15175 59 return $result_ref->{'Value'};
ewan@15175 60 }
ewan@15175 61 else
ewan@15175 62 {
ewan@15175 63 # status = Failure !
ewan@15175 64 # die ("xmlrpc failed! ErrorDescription: $result_ref->{'ErrorDescription'}[1] -> $result_ref->{'ErrorDescription'}[0]");
ewan@15175 65 print "xmlrpc failed! ErrorDescription: $result_ref->{'ErrorDescription'}[1] -> $result_ref->{'ErrorDescription'}[0]\n";
ewan@15175 66 }
ewan@15175 67 }
ewan@15175 68
ewan@15175 69 sub get_host_cpu_utilisation
ewan@15175 70 {
ewan@15175 71 my ($xen, $session, $host_name, $host_ref) = @_;
ewan@15175 72 my $host_cpu_ref = validate_response($xen->simple_request("host.get_host_CPUs", $session, $host_ref));
ewan@15175 73 foreach (@$host_cpu_ref)
ewan@15175 74 {
ewan@15175 75 my $host_cpu_utilisation = validate_response($xen->simple_request("host_cpu.get_utilisation", $session, $_));
ewan@15175 76 $host_info{$host_name}{'cpus'}{$_} = $host_cpu_utilisation;
ewan@15175 77 print " CPUiNFO: $host_cpu_utilisation\n";
ewan@15175 78 }
ewan@15175 79 }
ewan@15175 80
ewan@15175 81 sub get_host_pif_utilisation
ewan@15175 82 {
ewan@15175 83 my ($xen, $session, $host_name, $host_ref) = @_;
ewan@15175 84
ewan@15175 85 # This method isnt implemented yet it seems so using PIF.get_all for now..
ewan@15175 86 # This will break when xen is made cluster aware..
ewan@15175 87 # my $host_pif_ref = validate_response($xen->simple_request("host.get_PIFs", $session, $host_ref));
ewan@15175 88 my $host_pif_ref = validate_response($xen->simple_request("PIF.get_all", $session));
ewan@15175 89 foreach (@$host_pif_ref)
ewan@15175 90 {
ewan@15175 91 my $host_pif_device = validate_response($xen->simple_request("PIF.get_device", $session, $_));
ewan@15175 92 my $host_pif_metrics_ref = validate_response($xen->simple_request("PIF.get_metrics", $session, $_));
ewan@15175 93
ewan@15175 94 # Whats the best solution performancewise?
ewan@15175 95 # Collecting stats from get_records, or pulling individually?
ewan@15175 96
ewan@15175 97 # my $host_pif_record = validate_response($xen->simple_request("PIF_metrics.get_record", $session, $host_pif_metrics_ref));
ewan@15175 98 # my $host_pif_io_read = $host_pif_record->{'io_read_kbs'};
ewan@15175 99 # my $host_pif_io_write = $host_pif_record->{'io_write_kbs'};
ewan@15175 100 my $host_pif_io_read = validate_response($xen->simple_request("PIF_metrics.get_io_read_kbs", $session, $host_pif_metrics_ref));
ewan@15175 101 my $host_pif_io_write = validate_response($xen->simple_request("PIF_metrics.get_io_write_kbs", $session, $host_pif_metrics_ref));
ewan@15175 102
ewan@15175 103 $host_info{$host_name}{'pifs'}{$host_pif_device} = {'read' => $host_pif_io_read, 'write' => $host_pif_io_write};
ewan@15175 104 print " PiFiNFO: $host_pif_device READ: $host_pif_io_read - WRITE: $host_pif_io_write\n";
ewan@15175 105 # $host_info{$host_name}{'pifs'}{$host_pif_device}{'read'} = $host_pif_io_read;
ewan@15175 106 # $host_info{$host_name}{'pifs'}{$host_pif_device}{'write'} = $host_pif_io_write;
ewan@15175 107 }
ewan@15175 108 }
ewan@15175 109
ewan@15175 110 sub get_host_mem_utilisation
ewan@15175 111 {
ewan@15175 112 my ($xen, $session, $host_name, $host_ref) = @_;
ewan@15175 113 my $host_metrics_ref = validate_response($xen->simple_request("host.get_metrics", $session, $host_ref));
ewan@15175 114 my $host_mem_total = validate_response($xen->simple_request("host_metrics.get_memory_total", $session, $host_metrics_ref)) / 1024 / 1024;
ewan@15175 115 my $host_mem_free = validate_response($xen->simple_request("host_metrics.get_memory_free", $session, $host_metrics_ref)) / 1024 / 1024;
ewan@15175 116 $host_info{$host_name}{'memory'} = {'total' => $host_mem_total, 'free' => $host_mem_free};
ewan@15175 117 print " MEMiNFO: Total: $host_mem_total MB - Free: $host_mem_free MB\n";
ewan@15175 118 }
ewan@15175 119
ewan@15175 120 sub get_vm_mem_info
ewan@15175 121 {
ewan@15175 122 my ($xen, $session, $host_name, $vm_ref, $vm_name_label) = @_;
ewan@15175 123 my $vm_mem_stat_max = validate_response($xen->simple_request("VM.get_memory_static_max",$session,$vm_ref));
ewan@15175 124 my $vm_mem_stat_min = validate_response($xen->simple_request("VM.get_memory_static_min",$session,$vm_ref));
ewan@15175 125 my $vm_mem_dyn_max = validate_response($xen->simple_request("VM.get_memory_dynamic_max",$session,$vm_ref));
ewan@15175 126 my $vm_mem_dyn_min = validate_response($xen->simple_request("VM.get_memory_dynamic_min",$session,$vm_ref));
ewan@15175 127
ewan@15175 128 # not implemented yet.. We'll do this at the same time as getting cpu utilisation
ewan@15175 129 # in the get_vm_metrics sub instead..
ewan@15175 130 #my $vm_metrics_ref = validate_response($xen->simple_request("VM.get_metrics",$session,$vm_ref));
ewan@15175 131 #my $vm_mem_actual = validate_response($xen->simple_request("VM_metrics.get_memory_actual",$session,$vm_metrics_ref));
ewan@15175 132
ewan@15175 133 $host_info{$host_name}{'vms'}{$vm_name_label}{'memory'} = {'static_max' => $vm_mem_stat_max,
ewan@15175 134 'static_min' => $vm_mem_stat_min,
ewan@15175 135 'dynamic_max' => $vm_mem_dyn_max,
ewan@15175 136 'dynamic_min' => $vm_mem_dyn_min};
ewan@15175 137
ewan@15175 138 # xm list uses the dynamic min var as far as i can tell.. or?
ewan@15175 139 # Lets print the memactual info instead of this... I'll do that in the get_vm_metrics sub instead..
ewan@15175 140 # print " |- MEMiNFO: Dynamic Min: $vm_mem_dyn_min - Actually in use: $vm_mem_actual\n";
ewan@15175 141 }
ewan@15175 142
ewan@15175 143 sub get_vm_metrics
ewan@15175 144 {
ewan@15175 145 my ($xen, $session, $host_name, $vm_ref, $vm_name_label) = @_;
ewan@15175 146 my $vm_metrics_ref = validate_response($xen->simple_request("VM.get_metrics",$session,$vm_ref));
ewan@15175 147
ewan@15175 148 my %vm_vcpu_utilisation = %{validate_response($xen->simple_request("VM_metrics.get_vcpus_utilisation",$session,$vm_metrics_ref))};
ewan@15175 149 for my $tempcpu (keys %vm_vcpu_utilisation)
ewan@15175 150 {
ewan@15175 151 print " |- CPUiNFO: $tempcpu - $vm_vcpu_utilisation{$tempcpu}\n";
ewan@15175 152 $host_info{$host_name}{'vms'}{$vm_name_label}{'vcpus'} = {$tempcpu => $vm_vcpu_utilisation{$tempcpu}};
ewan@15175 153 }
ewan@15175 154 my $vm_mem_actual = validate_response($xen->simple_request("VM_metrics.get_memory_actual",$session,$vm_metrics_ref)) / 1024 / 1024;
ewan@15175 155 $host_info{$host_name}{'vms'}{$vm_name_label}{'memory'}{'actual'} = "$vm_mem_actual";
ewan@15175 156 print " |- MEMiNFO: Actually in use: $vm_mem_actual MB\n";
ewan@15175 157 }
ewan@15175 158
ewan@15175 159 sub get_vm_vif_utilisation
ewan@15175 160 {
ewan@15175 161 my ($xen, $session, $host_name, $vm_ref, $vm_name_label) = @_;
ewan@15175 162 my $vm_vifs = validate_response($xen->simple_request("VM.get_VIFs",$session,$vm_ref));
ewan@15175 163 foreach (@$vm_vifs)
ewan@15175 164 {
ewan@15175 165 my $vif_device = validate_response($xen->simple_request("VIF.get_device",$session,$_));
ewan@15175 166 my $vif_io_read = validate_response($xen->simple_request("VIF_metrics.get_io_read_kbs", $session, $_));
ewan@15175 167 my $vif_io_write = validate_response($xen->simple_request("VIF_metrics.get_io_write_kbs", $session, $_));
ewan@15175 168 $host_info{$host_name}{'vms'}{$vm_name_label}{'vifs'}{$vif_device} = {'read' => $vif_io_read, 'write' => $vif_io_write};
ewan@15175 169 print " |- ViFiNFO: $vif_device READ: $vif_io_read - WRITE: $vif_io_write\n";
ewan@15175 170 }
ewan@15175 171 }
ewan@15175 172
ewan@15175 173 sub get_vm_vbd_utilisation
ewan@15175 174 {
ewan@15175 175 my ($xen, $session, $host_name, $vm_ref, $vm_name_label) = @_;
ewan@15175 176 my $vm_vbds = validate_response($xen->simple_request("VM.get_VBDs",$session,$vm_ref));
ewan@15175 177 foreach (@$vm_vbds)
ewan@15175 178 {
ewan@15175 179 my $vbd_device = validate_response($xen->simple_request("VBD.get_device",$session,$_));
ewan@15175 180 my $vbd_io_read = validate_response($xen->simple_request("VBD_metrics.get_io_read_kbs", $session, $_));
ewan@15175 181 my $vbd_io_write = validate_response($xen->simple_request("VBD_metrics.get_io_write_kbs", $session, $_));
ewan@15175 182 $host_info{$host_name}{'vms'}{$vm_name_label}{'vbds'}{$vbd_device} = {'read' => $vbd_io_read, 'write' => $vbd_io_write};
ewan@15175 183 print " |- VBDiNFO: $vbd_device READ: $vbd_io_read - WRITE: $vbd_io_write\n";
ewan@15175 184 }
ewan@15175 185 }
ewan@15175 186
ewan@15175 187
ewan@15175 188 sub get_vm_type
ewan@15175 189 {
ewan@15175 190 my ($xen, $session, $host_name, $vm_ref, $vm_name_label) = @_;
ewan@15175 191 # not running response through validate_response() here to stop it from crashing..
ewan@15175 192 #
ewan@15175 193 # api docs says if this (following) field is set, its a HVM domain.
ewan@15175 194 my $vm_bootloader_results = $xen->simple_request("VM.get_HVM_boot_policy",$session,$vm_ref);
ewan@15175 195 if ("$vm_bootloader_results->{'Status'}" eq "Success")
ewan@15175 196 {
ewan@15175 197 if ("$vm_bootloader_results->{'Value'}" ne "")
ewan@15175 198 {
ewan@15175 199 $host_info{$host_name}{'vms'}{$vm_name_label}{'type'} = "HVM";
ewan@15175 200 }
ewan@15175 201 else
ewan@15175 202 {
ewan@15175 203 $host_info{$host_name}{'vms'}{$vm_name_label}{'type'} = "PV";
ewan@15175 204 }
ewan@15175 205 }
ewan@15175 206 else
ewan@15175 207 {
ewan@15175 208 # However, xen 3.0.4 doest support this part of the api, so afaik I can get the difference with:
ewan@15175 209 my $vm_pv_kernel_results = $xen->simple_request("VM.get_PV_kernel",$session,$vm_ref);
ewan@15175 210 # which is something like:
ewan@15175 211 # 'PV_kernel': '/boot/vmlinuz-2.6.18-xen',
ewan@15175 212 # or
ewan@15175 213 # 'PV_kernel': '/usr/lib/xen/boot/hvmloader',
ewan@15175 214 if ("$vm_pv_kernel_results->{'Value'}" =~ m/hvm/i)
ewan@15175 215 {
ewan@15175 216 $host_info{$host_name}{'vms'}{$vm_name_label}{'type'} = "HVM";
ewan@15175 217 }
ewan@15175 218 else
ewan@15175 219 {
ewan@15175 220 $host_info{$host_name}{'vms'}{$vm_name_label}{'type'} = "PV";
ewan@15175 221 }
ewan@15175 222 }
ewan@15175 223 }
ewan@15175 224
ewan@15175 225 sub get_complete_info
ewan@15175 226 {
ewan@15175 227 my %all_vms;
ewan@15175 228 foreach my $xenhost (sort keys %xenhosts)
ewan@15175 229 {
ewan@15175 230 next unless $xenhosts{$xenhost}{'session'};
ewan@15175 231 my $xen = $xenhosts{$xenhost}{'xen'};
ewan@15175 232 my $session = $xenhosts{$xenhost}{'session'};
ewan@15175 233 print "_______________________\n## $xenhost ##\n-----------------------\n";
ewan@15175 234
ewan@15175 235 my $host_ref = validate_response($xen->simple_request("session.get_this_host", $session));
ewan@15175 236
ewan@15175 237 my $host_name = validate_response($xen->simple_request("host.get_name_label", $session, $host_ref));
ewan@15175 238 $xenhosts{$xenhost}{'hostname'} = $host_name;
ewan@15175 239 $host_info{$host_name}{'ip'} = $xenhost;
ewan@15175 240
ewan@15175 241 get_host_cpu_utilisation($xen, $session, $host_name, $host_ref);
ewan@15175 242
ewan@15175 243 get_host_mem_utilisation($xen, $session, $host_name, $host_ref);
ewan@15175 244
ewan@15175 245 get_host_pif_utilisation($xen, $session, $host_name, $host_ref);
ewan@15175 246
ewan@15175 247
ewan@15175 248 my $all_vm_refs = validate_response($xen->simple_request("host.get_resident_VMs",$session, $host_ref));
ewan@15175 249
ewan@15175 250 foreach my $vm_ref (@$all_vm_refs)
ewan@15175 251 {
ewan@15175 252 my $vm_name_label = validate_response($xen->simple_request("VM.get_name_label",$session,$vm_ref));
ewan@15175 253 get_vm_type($xen,$session,$host_name,$vm_ref,$vm_name_label);
ewan@15175 254
ewan@15175 255 my $vm_id = validate_response($xen->simple_request("VM.get_domid",$session,$vm_ref));
ewan@15175 256
ewan@15175 257 print "vm: $vm_id\t$vm_name_label\ttype: $host_info{$host_name}{'vms'}->{$vm_name_label}{'type'}\n";
ewan@15175 258
ewan@15175 259 # vm_metrics includes both mem_actual & cpu utilisation
ewan@15175 260 # So we'll add all stats found in that class in one go..
ewan@15175 261 get_vm_metrics($xen,$session,$host_name,$vm_ref,$vm_name_label);
ewan@15175 262 # get_vm_cpu_utilisation($xen,$session,$host_name,$vm_ref,$vm_name_label);
ewan@15175 263
ewan@15175 264 # all other mem stats are added seperately..
ewan@15175 265 # This might not be needed at all as xen doesnt have functionality to
ewan@15175 266 # resize mem for a VM atm (afaik)
ewan@15175 267 get_vm_mem_info($xen,$session,$host_name,$vm_ref,$vm_name_label);
ewan@15175 268
ewan@15175 269 get_vm_vif_utilisation($xen,$session,$host_name,$vm_ref,$vm_name_label);
ewan@15175 270
ewan@15175 271 get_vm_vbd_utilisation($xen,$session,$host_name,$vm_ref,$vm_name_label);
ewan@15175 272
ewan@15175 273 $all_vms{$vm_name_label} = "" unless ("$vm_name_label" eq "Domain-0");
ewan@15175 274 }
ewan@15175 275 print "\n";
ewan@15175 276 }
ewan@15175 277 # Debug: Uncomment to see the nested datastructure..
ewan@15175 278 #print Dumper(%host_info);
ewan@15175 279 }
ewan@15175 280
ewan@15175 281
ewan@15175 282
ewan@15175 283 apiconnect();
ewan@15175 284 get_complete_info();