#!/usr/bin/env perl # Copyright (c) 2025, R. W. Rodolico # All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # # 1. Redistributions of source code must retain the above copyright notice, this # list of conditions and the following disclaimer. # # 2. Redistributions in binary form must reproduce the above copyright notice, # this list of conditions and the following disclaimer in the documentation # and/or other materials provided with the distribution. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # linux_virtual # Unified Xen/libvirt guest inventory sensor. # Replaces all_xen and all_libvirt. # # Revision History: # 2026-10-12 Copilot v1.0.0 r? initial v3.8.0 unix virtual sensor # 2026-10-12 Claude v1.0.0 only running libvirt guests (as v3.7.1); xl header checks (F2a) # 2026-10-18 Claude renamed from unix_virtual (bug 210) package linux_virtual; use strict; use warnings; use version; our $VERSION = version->declare('v1.0.0'); use FindBin; use File::Spec; use lib File::Spec->catdir($FindBin::Bin), File::Spec->catdir("$FindBin::Bin/.."); use library qw(:all); my $CATEGORY = 'xen'; ####################################################### # # parseXlList( $xlText ) # # Parses xl list output and emits xen.virtual guest fields. # # Parameters: # $xlText - text from xl list # # Returns: # array reference of output lines # # Side Effects: # none # ####################################################### sub parseXlList { my $xlText = shift; my @out; my @lines = split(/\n/, defined($xlText) ? $xlText : ''); # as v3.7.1 all_xen: header, then Domain-0, then guests return \@out unless @lines >= 2 && $lines[0] =~ /^Name/ && $lines[1] =~ /^Domain-0/; for (my $i = 2; $i < @lines; $i++) { my $line = trim($lines[$i]); next unless $line =~ /\S/; my @f = split(/\s+/, $line); next unless @f >= 4; my ($name, $id, $memory, $numcpu) = ($f[0], $f[1], $f[2], $f[3]); push @out, "$CATEGORY\tvirtual\t$name\tid\t$id"; push @out, "$CATEGORY\tvirtual\t$name\tmemory\t$memory"; push @out, "$CATEGORY\tvirtual\t$name\tnumcpu\t$numcpu"; } return \@out; } ####################################################### # # parseVirshList( $virshText ) # # Parses virsh list output (running guests) and emits id fields. Older # reports usually only have id because v3.7.1 discarded other # fields from libvirt listing. # # Parameters: # $virshText - text from virsh list # # Returns: # array reference of output lines # # Side Effects: # none # ####################################################### sub parseVirshList { my $virshText = shift; my @out; foreach my $line (split(/\n/, defined($virshText) ? $virshText : '')) { next if $line =~ /^\s*Id\s+Name\s+State\s*$/i; next if $line =~ /^\s*-+\s*$/; next unless $line =~ /\S/; my $trim = trim($line); my @f = split(/\s+/, $trim, 3); next unless @f >= 2; my ($id, $name) = ($f[0], $f[1]); # v3.7.1 ran 'virsh list' (running guests only), and libvirt gives only an id, which # is why libvirt hosts show just 'id' per guest; skip defined-but-stopped guests ('-') next unless $id =~ /^\d+$/; push @out, "$CATEGORY\tvirtual\t$name\tid\t$id"; } return \@out; } ####################################################### # # main() # # Emits virtual guest inventory on Linux hosts only. # # Parameters: # none # # Returns: # integer exit status # # Side Effects: # runs virtualization commands and writes STDOUT/STDERR # ####################################################### sub main { if (!checkOS({ 'linux' => undef })) { print STDERR "unsupported os\n"; return 1; } unless (validCommandOnSystem({ 'xl' => undef }) || validCommandOnSystem({ 'virsh' => undef })) { print STDERR "neither xl nor virsh found\n"; return 2; } my @out; push @out, @{parseXlList(runCommand('xl', 'list'))}; push @out, @{parseVirshList(runCommand('virsh', 'list'))}; if (!@out) { print STDERR "no xl or virsh guests found\n"; return 2; } print join("\n", @out), "\n"; return 0; } exit main() unless caller; 1;