Chinaunix首页 | 论坛 | 博客
  • 博客访问: 590125
  • 博文数量: 60
  • 博客积分: 3993
  • 博客等级: 中校
  • 技术积分: 1572
  • 用 户 组: 普通用户
  • 注册时间: 2005-08-08 17:08
文章分类

全部博文(60)

文章存档

2012年(7)

2011年(35)

2010年(8)

2009年(7)

2008年(3)

分类:

2010-06-12 18:40:11

此脚本只是用来测试Perl的libvirt接口模块,发现以下问题:
    1 在跨度较大的libvirt的版本情况下会直接产生段错误
    2 在当前最高的0.8.1版本下做连接压力测试时会导致xend-inotify打开文件数过多,
      造成out of memory 问题,此时libvirtd进程未宕,但已经无法连接
    3 许多方法没有实现或有问题,会导致脚本退出
 
 
在Python下发现的从安装有0.7版本Python API的服务器访问0.6.3版本libvirtd有很大几率会使远端libvirtd产生段错误的问题在Perl测试中没有发现。
 

#!/usr/bin/perl
use strict;
use Sys::Virt;
use XML::Simple;

my %states = (
  &Sys::Virt::Domain::STATE_NOSTATE => "nostate",
  &Sys::Virt::Domain::STATE_RUNNING => "running",
  &Sys::Virt::Domain::STATE_BLOCKED => "blocked",
  &Sys::Virt::Domain::STATE_PAUSED => "paused",
  &Sys::Virt::Domain::STATE_SHUTDOWN => "shutdown",
  &Sys::Virt::Domain::STATE_SHUTOFF => "shutoff",
  &Sys::Virt::Domain::STATE_CRASHED => "crashed",
  &Sys::Virt::Domain::STATE_RUNNING => "running",
);

print "ARG0 => ipadd, ARG1 => username, ARG2 => password\n";
my ($node_ip,$username,$password);
$node_ip=$ARGV[0] or $node_ip="192.168.1.1";
#my $addr="xen+ssh://".$node_ip;

my $addr="xen+tcp://".$node_ip;
$username=$ARGV[1] or $username='test';
$password=$ARGV[2] or $password='123456';
print "addr:$addr\n";
print "username:$username\n";
print "password:$password\n";

#conn#

print "conn $addr ... ";
my $vmm;
eval{
        local $SIG{ALRM}=sub{die "Sys::Virt->new timeout\n"};
        alarm 3;
        #$vmm=Sys::Virt->new(address => $addr);

        $vmm=Sys::Virt->new(address => $addr,
                                                auth=>1,
                                                credlist=>[Sys::Virt::CRED_AUTHNAME,Sys::Virt::CRED_PASSPHRASE,],
                                                callback=> sub {
                                                        my $creds=shift;
                                                        foreach my $cred (@$creds) {
                                                                if ($cred->{type}==Sys::Virt::CRED_AUTHNAME) {
                                                                        $cred->{result}=$username;
                                                                }
                                                                if ($cred->{type}==Sys::Virt::CRED_PASSPHRASE) {
                                                                        $cred->{result}=$password;
                                                                }
                                                        }
                                                },
        );
        alarm 0;
};
if ($@ =~/Sys::Virt->new timeout/) {
        print "Receive ALRM ! QUIT!\n";
        exit 1;
}
if (! defined($vmm)) {
        print "Sys::Virt->new ERROR! QUIT!\n";
        exit 2;
} else {
        print "OK\n";
}

print "\n##Info##\n";
#get hostname#

my $hostname=$vmm->get_hostname();
print "\tHostname:$hostname\n";

#get uri#

my $uri=$vmm->get_uri();
print "\turi:$uri\n";

#get devices#

my @devs=$vmm->list_node_devices('net');
foreach my $dev (@devs) {
        printf("\tNetwork interface: %-20s\n",substr($dev->get_name,4));

}


&list_doms;

while (1) {
 #start: $dom->create()
 #remove: $dom->undefine() 
 #temporarily stop: $dom->suspend() 
 #resume: $dom->resume() 
 #snapshot: $dom->save($filename)
 #core dump: $dom->core_dump($filename)
 #destroy: $dom->destroy()
 #shutdown: $dom->shutdown()
 #reboot: $dom->reboot()

        print "OPER(start shutdown reboot destroy suspend resume snapshot core_dump):";
        my $do=<STDIN>;
        chomp $do;
        if (defined $do) {
                if (($do eq 'list') or ($do eq '')) {
                        &list_doms;
                } else {
                        print "DomainName:";
                        my $name=<STDIN>;
                        chomp $name;
                        if (defined $name) {
                                dom_control($name,$do);
                        }
                }
        } else {
                &list_doms;
        }
}


#print @devs;


sub list_doms {
        #get all doms#

        print "\n##Domains##\n";
        my @alldoms;
         #active doms#

        my @doms = $vmm->list_domains();
        foreach my $dom (@doms) {
                push(@alldoms,$dom->get_name);
        }
         #inactive doms#

        my @names=$vmm->list_defined_domain_names(100); #defined maxdoms=100

        foreach my $name (@names) {
                push(@alldoms,$name);
        }
        #list all doms#

        foreach my $name (@alldoms) {
                printf("%-20s%-7s%-7s%-6s%-7s%10s%10s\n","Name","Id","Status","OS","memory","nrVirtCpu","stat");
                #if ($name eq 'Domain-0') { next;}

                my $dom=$vmm->get_domain_by_name($name);
                #if ($name eq 'xen-windows-test01') { $dom->create()}

                my $id=$dom->get_id();
                my $uuid=$dom->get_uuid_string();
                my $active=$dom->is_active();
                my $type=$dom->get_os_type();
                my $info=$dom->get_info();
                my %info=%$info;
                #sub memory_stats load error

                #my $m_stats=$dom->memory_stats(0);

                printf("%-20s%-7s%-7s%-6s%-5dMB%10s%10s\n",$name,$id,$active,$type,$info{'memory'}/1024,$info{'nrVirtCpu'},$states{$info{'state'}});
                my $xml=$dom->get_xml_description();
                xml($xml);

                print "#"x80,"\n";
        }
}

sub xml {
        my $xml=shift;
        my $xs=XML::Simple->new();
        my $ref=$xs->XMLin($xml);
        my %hash = %{$ref};
        my ($on_poweroff,$on_reboot,$on_crash);
        $on_poweroff=$hash{'on_poweroff'};
        $on_reboot=$hash{'on_reboot'};
        $on_crash=$hash{'on_crash'};
        print "\t#OPER#\n";
        printf("\t\ton_poweroff=%s on_reboot=%s on_crash=%s\n",$on_poweroff,$on_reboot,$on_crash);
        my %device=%{$hash{'devices'}};
        my (%graphics,@disk,@interface);
        #interface#

        print "\t#interface#\n";
        if (ref($device{'interface'}) eq 'HASH') {
                my %h=%{$device{'interface'}};
                push(@interface,\%h);
        } elsif (ref($device{'interface'}) eq 'ARRAY') {
                @interface=@{$device{'interface'}};
        } else {
        }
        foreach my $i (@interface) {
                my %i=%{$i};
                my ($type,%source,%target,%mac);
                my $source;
                $type=$i{'type'};
                if ($type =~ /ethernet/) {
                        $type="ethernet";
                        $source="NULL";
                } else {
                        $type="bridge";
                        %source=%{$i{'source'}};
                        $source=$source{"$type"};
                }
                my $target=${$i{'target'}}{'dev'};
                my $mac=${$i{'mac'}}{'address'};
                print "\t\ttype:$type ";
                print "source:$source ";
                print "target:$target ";
                print "mac:$mac\n";
        }
        #disk#

        print "\t#disk#\n";
        if (ref($device{'disk'}) eq 'HASH') {
                my %h=%{$device{'disk'}};
                push(@disk,\%h);
        } elsif (ref($device{'disk'}) eq 'ARRAY') {
                @disk=@{$device{'disk'}};
        } else {
        }

        foreach my $d (@disk) {
                my %d=%{$d};
                my ($device,$type,$target_bus,$target_dev,$file);
                $device=$d{'disk'};
                $type=$d{'type'};
                $target_bus=${$d{'target'}}{'bus'};
                $target_dev=${$d{'target'}}{'dev'};
                $file=${$d{'source'}}{'file'};
                printf("\t\tdevice:%s type:%s target_bus:%s target_dev:%s file:%s\n",$device,$type,$target_bus,$target_dev,$file);
        }
}

sub dom_control {
        my ($name,$oper)=@_;
        my $filename="/tmp/".$name."_".time;
#dom control#

 #start: $dom->create()

 #remove: $dom->undefine()

 #temporarily stop: $dom->suspend()

 #resume: $dom->resume()

 #snapshot: $dom->save($filename)

 #core dump: $dom->core_dump($filename)

 #destroy: $dom->destroy()

 #shutdown: $dom->shutdown()

 #reboot: $dom->reboot()

        my $dom=$vmm->get_domain_by_name($name);
        if (!defined($dom)) {
                print "Domain $name control error!\n";
                exit;
        }
        if (defined $oper) {
                if ($oper eq "start") {
                        $dom->create();
                } elsif ($oper eq "undefine") {
                        $dom->undefine();
                } elsif ($oper eq "suspend") {
                        $dom->suspend();
                } elsif ($oper eq "resume") {
                        $dom->resume();
                } elsif ($oper eq "snapshot") {
                        $dom->save($filename."snapshot");
                } elsif ($oper eq "core_dump") {
                        $dom->core_dump($filename."core_dump");
                } elsif ($oper eq "destroy") {
                        $dom->destroy();
                } elsif ($oper eq "shutdown") {
                        $dom->shutdown();
                } elsif ($oper eq "reboot") {
                        $dom->reboot();
                } else {
                        print "Operate error!\n";
                        exit;
                }
        }
}


阅读(1549) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~