#!/usr/bin/env perl use warnings; use strict; # Description: Processes libvirt Clients our $VERSION = '1.0'; # Looks at various libvirt information about running virtuals # Written by RWR and BNR, 20210228 BEGIN { push @INC, shift; } use library; # category we will use for all values found # see sysinfo for a list of valid categories my $CATEGORY = 'xen'; # run the commands necessary to do whatever you want to do # The library entered above has some helper routines # validCommandOnSystem -- passed a name, returns the fully qualified path or # '' if it does not exist # cleanUp - passed a delimiter and a string, does the following (delimiter # can be '') # chomps the string (removes trailing newlines) # removes all text BEFORE the delimiter, the delimiter, and any # whitespace # thus, the string 'xxI Am x a weird string' with a newline will # become # 'a weird string' with no newline # now, return the tab delimited output (to STDOUT). $CATEGORY is the first # item, name as recognized by sysinfo is the second and the value is # the last one. For multiple entries, place on separate lines (ie, newline # separated) my $xl = &validCommandOnSystem( 'virsh' ); # print "My command is $xl\n"; exit 1 unless $xl; $xl .= ' list'; # print "My command is $xl\n"; # die; sub parseOutput { my $output = shift; my @lines = split( "\n", $output ); my %domu; return ( 'noname' ) unless $lines[0] =~ m/^\s*id\s+name\s+state\s*$/i; for ( my $i = 2; $i < @lines; $i++ ) { # print "Working on $i, value is {$lines[$i]}\n"; $lines[$i] =~ s/^\s*//; # print "After cleanup, value is {$lines[$i]}\n"; my ( $id,$name ) = split( /\s+/, $lines[$i] ); $domu{$name}{'id'} = $id; } return \%domu; } my $output = `$xl`; #print $output; #die; my $hier = &parseOutput( $output ); #die; foreach my $domu ( sort keys %$hier ) { my $temp = $$hier{$domu}; foreach my $key ( sort keys %$temp ) { print "$CATEGORY\tvirtual\t$domu\t$key\t$$temp{$key}\n"; } } # foreach # if you have not done an exit state above (1 indicating no data), do one # here (exit 0 indicates success) # NOTE: you can bail early with exit 1 if you can not process anything # because it is the wrong system or something exit 0;