Virt/Plugin/Libvirt.pm

filter: Statementstmt 31.7% 27 / 85 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 61.5% 8 / 13 Podpod 100% 3 / 3 totaltotal 29.0% 38 / 131 CC 15 SCAR 47.0
CC
15
Cov
24%
vms
210.0
is_host
6.0
_init_priority
2.0
CRAP 109.9
« Virt/Plugin/Vserver.pm ↑ Summary Virt/Plugin/LXC.pm »
1 package Linux::Virt::Plugin::Libvirt;
2 {
3 2 $Linux::Virt::Plugin::Libvirt::VERSION = '0.15';
4 }
5 2 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 $Linux::Virt::Plugin::Libvirt::AUTHORITY = 'cpan:TEX';
7 }
8 # ABSTRACT: generic libvirt plugin for Linux::Virt
9  
10 2 ▶use 5.010_000;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use mro 'c3';
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use feature ':5.10';
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 2 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use namespace::autoclean;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 # use IO::Handle;
18 # use autodie;
19 # use MooseX::Params::Validate;
20  
21 2 ▶use XML::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 2 extends 'Linux::Virt::Plugin';
24  
25 2 has 'type' => (
26 'is' => 'ro',
27 'isa' => 'Str',
28 'lazy' => 1,
29 'builder' => '_init_type',
30 );
31  
32 0 ▶sub _init_priority { return 10; }
Subroutine _init_priority: not called CC=1 SCAR=6.9
33  
34 0 ▶sub _init_type {
Subroutine _init_type: not called CC=1 SCAR=6.9
35 0 return 'libvirt';
36 }
37  
38 0 ▶sub is_host {
Subroutine is_host: not called CC=2 SCAR=17.9
Pod documented
39 0 my $self = shift;
40 0 ▶ return 1 if -x '/usr/bin/virsh';
Branch: 1 branch
BranchTrueFalseTotal
if -x "/usr/bin/virsh" 0 0 0
41 0 return;
42 }
43  
44 0 ▶sub is_vm {
Subroutine is_vm: not called CC=1 SCAR=6.9
Pod documented
45 0 return;
46 }
47  
48 0 ▶sub vms {
Subroutine vms: not called CC=14 SCAR=53.5
Pod documented
49 0 my $self = shift;
50 0 my $vserver_ref = shift;
51 0 ▶ my $opts = shift || {};
Condition0%0/2
shift() || {}
l!l
00
MC/DC0%0/1
shift() || {}
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
52  
53 0 ▶ return unless -x '/usr/bin/virsh';
Branch: 1 branch
BranchTrueFalseTotal
unless -x "/usr/bin/virsh" 0 0 0
54  
55 0 local $ENV{LANG} = "C";
56 0 my $VIRSH;
57 0 my $cmd = "/usr/bin/virsh -c qemu:///system list --all";
58 0 ▶ if ( !open( $VIRSH, '-|', $cmd ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (open $VIRSH, "-|", $cmd) 0 0 0
59 0 my $msg = "Could not execute /usr/bin/virsh! Is libvirt-bin installed?: $!";
60 0 $self->logger()->log( message => $msg, level => 'warning', );
61 0 return;
62 }
63 0 my %vms = ();
64 0 ▶ while ( my $line = <$VIRSH> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $VIRSH)) 0 0 0
65 0 ▶ next if $line =~ m/^\s*Id\s+Name\s+State/i;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\s*Id\s+Name\s+State/i 0 0 0
66 0 ▶ next if $line =~ m/^-----/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^-----/ 0 0 0
67 0 $line =~ s/^\s+//;
68 0 $line =~ s/\s+$//;
69 0 my ( $id, $name, $state ) = split( /\s+/, $line );
70 0 ▶ next unless $name;
Branch: 1 branch
BranchTrueFalseTotal
unless $name 0 0 0
71 0 $vms{$name}{'id'} = $id;
72 0 $vms{$name}{'name'} = $name;
73 0 $vms{$name}{'state'} = $state;
74 } ## end while ( my $line = <$VIRSH>)
75 0 close($VIRSH);
76 0 $cmd = "/usr/bin/virsh -c qemu:///system dumpxml ";
77 0 foreach my $vm ( keys %vms ) {
78 0 ▶ next unless $vms{$vm}{'name'};
Branch: 1 branch
BranchTrueFalseTotal
unless $vms{$vm}{"name"} 0 0 0
79 0 $cmd = $cmd . $vms{$vm}{'name'};
80 0 ▶ if ( open( $VIRSH, '-|', $cmd ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (open $VIRSH, "-|", $cmd) 0 0 0
81 0 my @xml = <$VIRSH>;
82 0 close($VIRSH);
83 0 my $xml_ref;
84 0 eval { $xml_ref = XMLin( join( "", @xml ) ); };
85 0 ▶ next if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
86 0 $vms{$vm}{'type'} = $xml_ref->{'type'};
87 0 $vms{$vm}{'memmax'} = $xml_ref->{'memory'};
88 0 $vms{$vm}{'memcur'} = $xml_ref->{'currentMemory'};
89 0 $vms{$vm}{'vcpu'} = $xml_ref->{'vcpu'};
90 0 $vms{$vm}{'virt_arch'} = $xml_ref->{'os'}->{'type'}->{'arch'};
91 } ## end if ( open( $VIRSH, '-|'...))
92 0 my $name = $vms{$vm}{'name'};
93 0 ▶ if ( $self->type() ne 'libvirt' && $self->type() ne lc( $vms{$vm}{'type'} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->type ne "libvirt" and $self->type ne lc $vms{$vm}{"type"}) 0 0 0
Condition0%0/3
$self->type ne "libvirt" and $self->type ne lc $vms{$vm}{"type"}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->type ne "libvirt" and $self->type ne lc $vms{$vm}{"type"}
$self->type ne "libvirt" $self->type ne lc $vms{$vm}{"type"}
Truth table0%0/3
A and B
A= $self->type ne "libvirt"B= $self->type ne lc $vms{$vm}{"type"}
ABresult
0X0
100
111
94  
95 # Print skip this vm if the type doesn't match the requested one
96 0 next;
97 }
98 0 $vserver_ref->{$name}{'id'} = $vms{$name}{'id'};
99 0 $vserver_ref->{$name}{'virt'}{'type'} = $vms{$vm}{'type'};
100 0 $vserver_ref->{$name}{'virt'}{'arch'} = $vms{$vm}{'virt_arch'};
101 0 $vserver_ref->{$name}{'limits'}{'mem'}{'current'} = $vms{$vm}{'memcur'};
102 0 $vserver_ref->{$name}{'limits'}{'mem'}{'min'} = $vms{$vm}{'memcur'};
103 0 $vserver_ref->{$name}{'limits'}{'mem'}{'max'} = $vms{$vm}{'memmax'};
104 0 $vserver_ref->{$name}{'limits'}{'mem'}{'soft'} = $vms{$vm}{'memmax'};
105 0 $vserver_ref->{$name}{'limits'}{'mem'}{'hard'} = $vms{$vm}{'memmax'};
106 0 $vserver_ref->{$name}{'limits'}{'mem'}{'hits'} = 0;
107 } ## end foreach my $vm ( keys %vms )
108 0 return 1;
109 } ## end sub vms
110  
111 2 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
112 2 __PACKAGE__->meta->make_immutable;
113  
114 2 1;
115  
116 __END__
« Virt/Plugin/Vserver.pm ↑ Summary Virt/Plugin/LXC.pm »