line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Linux::Virt::Plugin::Libvirt; |
2
|
|
|
|
|
|
|
{ |
3
|
|
|
|
|
|
|
$Linux::Virt::Plugin::Libvirt::VERSION = '0.15'; |
4
|
|
|
|
|
|
|
} |
5
|
|
|
|
|
|
|
BEGIN { |
6
|
1
|
|
|
1
|
|
1218
|
$Linux::Virt::Plugin::Libvirt::AUTHORITY = 'cpan:TEX'; |
7
|
|
|
|
|
|
|
} |
8
|
|
|
|
|
|
|
# ABSTRACT: generic libvirt plugin for Linux::Virt |
9
|
|
|
|
|
|
|
|
10
|
1
|
|
|
1
|
|
35
|
use 5.010_000; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
42
|
|
11
|
1
|
|
|
1
|
|
7
|
use mro 'c3'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
9
|
|
12
|
1
|
|
|
1
|
|
31
|
use feature ':5.10'; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
116
|
|
13
|
|
|
|
|
|
|
|
14
|
1
|
|
|
1
|
|
6
|
use Moose; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
7
|
|
15
|
1
|
|
|
1
|
|
10633
|
use namespace::autoclean; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
14
|
|
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
# use IO::Handle; |
18
|
|
|
|
|
|
|
# use autodie; |
19
|
|
|
|
|
|
|
# use MooseX::Params::Validate; |
20
|
|
|
|
|
|
|
|
21
|
1
|
|
|
1
|
|
755
|
use XML::Simple; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
extends 'Linux::Virt::Plugin'; |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
has 'type' => ( |
26
|
|
|
|
|
|
|
'is' => 'ro', |
27
|
|
|
|
|
|
|
'isa' => 'Str', |
28
|
|
|
|
|
|
|
'lazy' => 1, |
29
|
|
|
|
|
|
|
'builder' => '_init_type', |
30
|
|
|
|
|
|
|
); |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
sub _init_priority { return 10; } |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
sub _init_type { |
35
|
|
|
|
|
|
|
return 'libvirt'; |
36
|
|
|
|
|
|
|
} |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
sub is_host { |
39
|
|
|
|
|
|
|
my $self = shift; |
40
|
|
|
|
|
|
|
return 1 if -x '/usr/bin/virsh'; |
41
|
|
|
|
|
|
|
return; |
42
|
|
|
|
|
|
|
} |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
sub is_vm { |
45
|
|
|
|
|
|
|
return; |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
sub vms { |
49
|
|
|
|
|
|
|
my $self = shift; |
50
|
|
|
|
|
|
|
my $vserver_ref = shift; |
51
|
|
|
|
|
|
|
my $opts = shift || {}; |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
return unless -x '/usr/bin/virsh'; |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
local $ENV{LANG} = "C"; |
56
|
|
|
|
|
|
|
my $VIRSH; |
57
|
|
|
|
|
|
|
my $cmd = "/usr/bin/virsh -c qemu:///system list --all"; |
58
|
|
|
|
|
|
|
if ( !open( $VIRSH, '-|', $cmd ) ) { |
59
|
|
|
|
|
|
|
my $msg = "Could not execute /usr/bin/virsh! Is libvirt-bin installed?: $!"; |
60
|
|
|
|
|
|
|
$self->logger()->log( message => $msg, level => 'warning', ); |
61
|
|
|
|
|
|
|
return; |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
my %vms = (); |
64
|
|
|
|
|
|
|
while ( my $line = <$VIRSH> ) { |
65
|
|
|
|
|
|
|
next if $line =~ m/^\s*Id\s+Name\s+State/i; |
66
|
|
|
|
|
|
|
next if $line =~ m/^-----/; |
67
|
|
|
|
|
|
|
$line =~ s/^\s+//; |
68
|
|
|
|
|
|
|
$line =~ s/\s+$//; |
69
|
|
|
|
|
|
|
my ( $id, $name, $state ) = split( /\s+/, $line ); |
70
|
|
|
|
|
|
|
next unless $name; |
71
|
|
|
|
|
|
|
$vms{$name}{'id'} = $id; |
72
|
|
|
|
|
|
|
$vms{$name}{'name'} = $name; |
73
|
|
|
|
|
|
|
$vms{$name}{'state'} = $state; |
74
|
|
|
|
|
|
|
} ## end while ( my $line = <$VIRSH>) |
75
|
|
|
|
|
|
|
close($VIRSH); |
76
|
|
|
|
|
|
|
$cmd = "/usr/bin/virsh -c qemu:///system dumpxml "; |
77
|
|
|
|
|
|
|
foreach my $vm ( keys %vms ) { |
78
|
|
|
|
|
|
|
next unless $vms{$vm}{'name'}; |
79
|
|
|
|
|
|
|
$cmd = $cmd . $vms{$vm}{'name'}; |
80
|
|
|
|
|
|
|
if ( open( $VIRSH, '-|', $cmd ) ) { |
81
|
|
|
|
|
|
|
my @xml = <$VIRSH>; |
82
|
|
|
|
|
|
|
close($VIRSH); |
83
|
|
|
|
|
|
|
my $xml_ref; |
84
|
|
|
|
|
|
|
eval { $xml_ref = XMLin( join( "", @xml ) ); }; |
85
|
|
|
|
|
|
|
next if $@; |
86
|
|
|
|
|
|
|
$vms{$vm}{'type'} = $xml_ref->{'type'}; |
87
|
|
|
|
|
|
|
$vms{$vm}{'memmax'} = $xml_ref->{'memory'}; |
88
|
|
|
|
|
|
|
$vms{$vm}{'memcur'} = $xml_ref->{'currentMemory'}; |
89
|
|
|
|
|
|
|
$vms{$vm}{'vcpu'} = $xml_ref->{'vcpu'}; |
90
|
|
|
|
|
|
|
$vms{$vm}{'virt_arch'} = $xml_ref->{'os'}->{'type'}->{'arch'}; |
91
|
|
|
|
|
|
|
} ## end if ( open( $VIRSH, '-|'...)) |
92
|
|
|
|
|
|
|
my $name = $vms{$vm}{'name'}; |
93
|
|
|
|
|
|
|
if ( $self->type() ne 'libvirt' && $self->type() ne lc( $vms{$vm}{'type'} ) ) { |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
# Print skip this vm if the type doesn't match the requested one |
96
|
|
|
|
|
|
|
next; |
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
$vserver_ref->{$name}{'id'} = $vms{$name}{'id'}; |
99
|
|
|
|
|
|
|
$vserver_ref->{$name}{'virt'}{'type'} = $vms{$vm}{'type'}; |
100
|
|
|
|
|
|
|
$vserver_ref->{$name}{'virt'}{'arch'} = $vms{$vm}{'virt_arch'}; |
101
|
|
|
|
|
|
|
$vserver_ref->{$name}{'limits'}{'mem'}{'current'} = $vms{$vm}{'memcur'}; |
102
|
|
|
|
|
|
|
$vserver_ref->{$name}{'limits'}{'mem'}{'min'} = $vms{$vm}{'memcur'}; |
103
|
|
|
|
|
|
|
$vserver_ref->{$name}{'limits'}{'mem'}{'max'} = $vms{$vm}{'memmax'}; |
104
|
|
|
|
|
|
|
$vserver_ref->{$name}{'limits'}{'mem'}{'soft'} = $vms{$vm}{'memmax'}; |
105
|
|
|
|
|
|
|
$vserver_ref->{$name}{'limits'}{'mem'}{'hard'} = $vms{$vm}{'memmax'}; |
106
|
|
|
|
|
|
|
$vserver_ref->{$name}{'limits'}{'mem'}{'hits'} = 0; |
107
|
|
|
|
|
|
|
} ## end foreach my $vm ( keys %vms ) |
108
|
|
|
|
|
|
|
return 1; |
109
|
|
|
|
|
|
|
} ## end sub vms |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
no Moose; |
112
|
|
|
|
|
|
|
__PACKAGE__->meta->make_immutable; |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
1; |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
__END__ |
117
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
=pod |
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
=encoding utf-8 |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
=head1 NAME |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
Linux::Virt::Plugin::Libvirt - generic libvirt plugin for Linux::Virt |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
=head1 METHODS |
127
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
=head2 is_host |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
Returns a true value if this is run on a Libvirt equipped host. |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
=head2 is_vm |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
Always returns false. Subclasses should override this with an apt implementation. |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
=head2 vms |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
List al running VMs. |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
=head1 NAME |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
Linux::Virt::Plugin::Libvirt - Libvirt Plugin for Linux::Virt. |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
=head1 virsh vs. Sys::Virt |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
Why not use Sys::Virt here? |
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
As of the time of this writing Sys::Virt is broken. Virsh is much more stable |
149
|
|
|
|
|
|
|
and easier to use. |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
=head1 AUTHOR |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
Dominik Schulz <dominik.schulz@gauner.org> |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
This software is copyright (c) 2012 by Dominik Schulz. |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
160
|
|
|
|
|
|
|
the same terms as the Perl 5 programming language system itself. |
161
|
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
=cut |