189 |
rodolico |
1 |
#!/usr/bin/env perl
|
|
|
2 |
use warnings;
|
|
|
3 |
use strict;
|
|
|
4 |
|
190 |
rodolico |
5 |
# Description: Processes libvirt Clients
|
189 |
rodolico |
6 |
|
|
|
7 |
our $VERSION = '1.0';
|
|
|
8 |
|
190 |
rodolico |
9 |
# Looks at various libvirt information about running virtuals
|
|
|
10 |
# Written by RWR and BNR, 20210228
|
189 |
rodolico |
11 |
|
|
|
12 |
BEGIN {
|
|
|
13 |
push @INC, shift;
|
|
|
14 |
}
|
|
|
15 |
|
|
|
16 |
use library;
|
|
|
17 |
|
|
|
18 |
# category we will use for all values found
|
|
|
19 |
# see sysinfo for a list of valid categories
|
|
|
20 |
my $CATEGORY = 'xen';
|
|
|
21 |
|
|
|
22 |
# run the commands necessary to do whatever you want to do
|
|
|
23 |
# The library entered above has some helper routines
|
|
|
24 |
# validCommandOnSystem -- passed a name, returns the fully qualified path or
|
|
|
25 |
# '' if it does not exist
|
|
|
26 |
# cleanUp - passed a delimiter and a string, does the following (delimiter
|
|
|
27 |
# can be '')
|
|
|
28 |
# chomps the string (removes trailing newlines)
|
|
|
29 |
# removes all text BEFORE the delimiter, the delimiter, and any
|
|
|
30 |
# whitespace
|
|
|
31 |
# thus, the string 'xxI Am x a weird string' with a newline will
|
|
|
32 |
# become
|
|
|
33 |
# 'a weird string' with no newline
|
|
|
34 |
|
|
|
35 |
# now, return the tab delimited output (to STDOUT). $CATEGORY is the first
|
|
|
36 |
# item, name as recognized by sysinfo is the second and the value is
|
|
|
37 |
# the last one. For multiple entries, place on separate lines (ie, newline
|
|
|
38 |
# separated)
|
|
|
39 |
|
190 |
rodolico |
40 |
my $xl = &validCommandOnSystem( 'virsh' );
|
|
|
41 |
# print "My command is $xl\n";
|
|
|
42 |
exit 1 unless $xl;
|
|
|
43 |
$xl .= ' list';
|
|
|
44 |
# print "My command is $xl\n";
|
|
|
45 |
# die;
|
189 |
rodolico |
46 |
|
|
|
47 |
sub parseOutput {
|
|
|
48 |
my $output = shift;
|
|
|
49 |
my @lines = split( "\n", $output );
|
|
|
50 |
my %domu;
|
190 |
rodolico |
51 |
return ( 'noname' ) unless $lines[0] =~ m/^\s*id\s+name\s+state\s*$/i;
|
189 |
rodolico |
52 |
for ( my $i = 2; $i < @lines; $i++ ) {
|
190 |
rodolico |
53 |
# print "Working on $i, value is {$lines[$i]}\n";
|
|
|
54 |
$lines[$i] =~ s/^\s*//;
|
|
|
55 |
# print "After cleanup, value is {$lines[$i]}\n";
|
|
|
56 |
my ( $id,$name ) = split( /\s+/, $lines[$i] );
|
|
|
57 |
$domu{$name}{'id'} = $id;
|
189 |
rodolico |
58 |
}
|
|
|
59 |
return \%domu;
|
|
|
60 |
}
|
|
|
61 |
|
190 |
rodolico |
62 |
my $output = `$xl`;
|
|
|
63 |
|
|
|
64 |
#print $output;
|
|
|
65 |
#die;
|
|
|
66 |
|
189 |
rodolico |
67 |
my $hier = &parseOutput( $output );
|
|
|
68 |
|
190 |
rodolico |
69 |
#die;
|
|
|
70 |
|
189 |
rodolico |
71 |
foreach my $domu ( sort keys %$hier ) {
|
|
|
72 |
my $temp = $$hier{$domu};
|
|
|
73 |
foreach my $key ( sort keys %$temp ) {
|
|
|
74 |
print "$CATEGORY\tvirtual\t$domu\t$key\t$$temp{$key}\n";
|
|
|
75 |
}
|
|
|
76 |
} # foreach
|
|
|
77 |
|
|
|
78 |
# if you have not done an exit state above (1 indicating no data), do one
|
|
|
79 |
# here (exit 0 indicates success)
|
|
|
80 |
# NOTE: you can bail early with exit 1 if you can not process anything
|
|
|
81 |
# because it is the wrong system or something
|
|
|
82 |
exit 0;
|