Skip to content

Commit 9b9dac2

Browse files
committed
fix: Support Qemu virtualization inventory on windows
Closes #957
1 parent 6cc61f6 commit 9b9dac2

File tree

3 files changed

+50
-6
lines changed

3 files changed

+50
-6
lines changed

Changes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ inventory:
1717
* PR #996: Added RuDesktop remote management inventory support
1818
* PR #1003: Add ESET Endpoint Antivirus for Linux support
1919
* PR #1010: Update vCpu support for Qemu virtualization
20+
* fix #957: Support Qemu virtualization inventory on windows
2021

2122
remoteinventory:
2223
* fix RedHat RHN systemid set as WINPRODID

lib/GLPI/Agent/Task/Inventory/Virtualization/Qemu.pm

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,27 @@
11
package GLPI::Agent::Task::Inventory::Virtualization::Qemu;
2-
# With Qemu 0.10.X, some option will be added to get more and easly information (UUID, memory, ...)
32

43
use strict;
54
use warnings;
65

76
use parent 'GLPI::Agent::Task::Inventory::Module';
87

8+
use UNIVERSAL::require;
9+
910
use GLPI::Agent::Tools;
1011
use GLPI::Agent::Tools::Unix;
1112
use GLPI::Agent::Tools::Virtualization;
1213

1314
sub isEnabled {
15+
# On win32, we have to search for any existing qemu process
16+
if (OSNAME eq 'MSWin32') {
17+
GLPI::Agent::Tools::Win32->use();
18+
my $running_qemu = first { $_->{Name} =~ /^qemu-system-/ } getWMIObjects(
19+
class => "Win32_Process",
20+
properties => [ qw/Name/ ]
21+
);
22+
return $running_qemu ? 1 : 0;
23+
}
24+
1425
# Avoid duplicated entry with libvirt
1526
return if canRun('virsh');
1627

@@ -38,19 +49,28 @@ sub _parseProcessList {
3849
$values->{name} = $1 if !$values->{name};
3950
} elsif ($option =~ m/^name ([^\s,]+)/) {
4051
$values->{name} = $1;
41-
} elsif ($option =~ m/^m .*size=(\S+)/) {
52+
} elsif ($option =~ m/^m (?:size=)?(\S+)/) {
4253
my ($mem) = split(/,/,$1);
43-
$values->{mem} = getCanonicalSize($mem);
54+
$mem .= "b" unless $mem =~ /^\d+$/;
55+
$values->{mem} = getCanonicalSize($mem, 1024);
4456
} elsif ($option =~ m/^m (\S+)/) {
4557
$values->{mem} = getCanonicalSize($1);
4658
} elsif ($option =~ m/^uuid (\S+)/) {
4759
$values->{uuid} = $1;
4860
} elsif ($option =~ m/^enable-kvm|accel=kvm/) {
4961
$values->{vmtype} = "kvm";
5062
} elsif ($option =~ m/^smp (\S+)$/) {
51-
my ($cpus) = grep { /^(?:cpus=)?\d+$/ } split(/,/, $1);
63+
my @cpu_args = split(/,/, $1);
64+
my ($cpus) = grep { /^(?:cpus=)?\d+$/ } @cpu_args;
5265
if ($cpus && $cpus =~ /(\d+)$/) {
5366
$values->{vcpu} = int($1);
67+
} else {
68+
my ($cores) = grep { /^(?:cores=)?\d+$/ } @cpu_args;
69+
my ($threads) = grep { /^(?:threads=)?\d+$/ } @cpu_args;
70+
my ($sockets) = grep { /^(?:sockets=)?\d+$/ } @cpu_args;
71+
$values->{vcpu} = int($1 || 1) if $cores && $cores =~ /(\d+)$/;
72+
$values->{vcpu} *= int($1 || 1) if $threads && $threads =~ /(\d+)$/;
73+
$values->{vcpu} *= int($1 || 1) if $sockets && $sockets =~ /(\d+)$/;
5474
}
5575
}
5676

@@ -72,14 +92,27 @@ sub _parseProcessList {
7292
return $values;
7393
}
7494

95+
sub _win32ProcessList {
96+
GLPI::Agent::Tools::Win32->use();
97+
98+
return map {
99+
{
100+
CMD => $_->{CommandLine}
101+
}
102+
} grep { $_->{Name} =~ /^qemu-system-/ && !empty($_->{CommandLine}) } getWMIObjects(
103+
class => "Win32_Process",
104+
properties => [ qw/Name CommandLine/ ]
105+
);
106+
}
107+
75108
sub doInventory {
76109
my (%params) = @_;
77110

78111
my $inventory = $params{inventory};
79112
my $logger = $params{logger};
80113

81114
# check only qemu instances
82-
foreach my $process (getProcesses(
115+
foreach my $process (OSNAME eq 'MSWin32' ? _win32ProcessList() : getProcesses(
83116
filter => qr/(qemu|kvm|qemu-kvm|qemu-system\S+) .*\S/x,
84117
namespace => "same",
85118
logger => $logger,

t/tasks/inventory/virtualization/qemu.t

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,17 @@ my @tests = (
6464
'qemu-system-x86_64 -m 4G -smp 4 -cpu IvyBridge -cdrom .\VMware-VMvisor-Installer-8.0U3e-24677879.x86_64.iso -boot d -vga std -nic user -device usb-ehci -device usb-host,vendorid=0x058F,productid=0x6387 -d guest_errors,unimp,pcall -D qemu_debug.log',
6565
_result => {
6666
name => '.\VMware-VMvisor-Installer-8.0U3e-24677879.x86_64.iso',
67-
mem => undef,
67+
mem => 4096,
68+
vcpu => 4,
69+
vmtype => 'qemu',
70+
}
71+
},
72+
{
73+
CMD =>
74+
'qemu-system-x86_64 -m 4G -smp cores=2,threads=2,sockets=1 -cpu IvyBridge -cdrom .\VMware-VMvisor-Installer-8.0U3e-24677879.x86_64.iso -boot d -vga std -nic user -device usb-ehci -device usb-host,vendorid=0x058F,productid=0x6387 -d guest_errors,unimp,pcall -D qemu_debug.log',
75+
_result => {
76+
name => '.\VMware-VMvisor-Installer-8.0U3e-24677879.x86_64.iso',
77+
mem => 4096,
6878
vcpu => 4,
6979
vmtype => 'qemu',
7080
}

0 commit comments

Comments
 (0)