3 |
rodolico |
1 |
#!/usr/bin/env perl
|
|
|
2 |
|
|
|
3 |
# All functions related to maniplating a specific node
|
|
|
4 |
# part of havirt.
|
|
|
5 |
|
|
|
6 |
# v0.0.1 20240602 RWR
|
|
|
7 |
# Initial setup
|
|
|
8 |
|
|
|
9 |
package node;
|
|
|
10 |
|
|
|
11 |
|
|
|
12 |
our $VERSION = '0.01';
|
|
|
13 |
use warnings;
|
|
|
14 |
use strict;
|
|
|
15 |
|
|
|
16 |
|
|
|
17 |
use Exporter;
|
|
|
18 |
|
|
|
19 |
our @ISA = qw( Exporter );
|
|
|
20 |
our @EXPORT = qw(
|
|
|
21 |
&node
|
|
|
22 |
);
|
|
|
23 |
|
|
|
24 |
|
|
|
25 |
sub loadNodeDB {
|
|
|
26 |
print "In loadNodeDB, reading $main::nodeDBName\n";
|
|
|
27 |
return if $main::nodeDB;
|
|
|
28 |
$main::nodeDB = &main::readDB( $main::nodeDBName );
|
|
|
29 |
print "Success\n";
|
|
|
30 |
}
|
|
|
31 |
|
|
|
32 |
sub getDomainsOnNode {
|
|
|
33 |
my $node = shift;
|
|
|
34 |
my @nodeList = grep { /^\s*\d/ } `ssh $node 'virsh list'`;
|
|
|
35 |
for ( my $i = 0; $i < @nodeList; $i++ ) {
|
|
|
36 |
if ( $nodeList[$i] =~ m/\s*\d+\s*([^ ]+)/ ) {
|
|
|
37 |
$nodeList[$i] = $1;
|
|
|
38 |
}
|
|
|
39 |
}
|
|
|
40 |
my %hash = map{ $_ => time } @nodeList;
|
|
|
41 |
return \%hash;
|
|
|
42 |
}
|
|
|
43 |
|
|
|
44 |
|
|
|
45 |
sub node {
|
|
|
46 |
my $action = lc shift;
|
|
|
47 |
|
|
|
48 |
my %conversion = (
|
|
|
49 |
'CPU frequency' => 'clock',
|
|
|
50 |
'CPU model' => 'cpu_model',
|
|
|
51 |
'CPU socket(s)' => 'cpu_socket',
|
|
|
52 |
'CPU(s)' => 'cpu_count',
|
|
|
53 |
'Core(s) per socket' => 'cpu_cores',
|
|
|
54 |
'Memory size' => 'memory',
|
|
|
55 |
'NUMA cell(s)' => 'numa_cells',
|
|
|
56 |
'Thread(s) per core' => 'threads_per_core'
|
|
|
57 |
);
|
|
|
58 |
|
|
|
59 |
|
|
|
60 |
print "In node, action is $action\n" if $main::DEBUG;
|
|
|
61 |
my $return = '';
|
|
|
62 |
&loadNodeDB();
|
|
|
63 |
if ( $action eq 'update' ) { # read information for nodes and update database
|
|
|
64 |
@_ = keys %$main::nodeDB if ( $_[0] eq 'ALL' );
|
|
|
65 |
while ( my $nodename = shift ) {
|
|
|
66 |
print "Updating $nodename\n" if $main::DEBUG;
|
|
|
67 |
$return = `ssh $nodename 'virsh nodeinfo'`;
|
|
|
68 |
print "Output of ssh $nodename 'virsh nodeinfo' is\n" . $return if $main::DEBUG;
|
|
|
69 |
my @nodeinfo = split( "\n", $return );
|
|
|
70 |
for ( my $i = 0; $i < @nodeinfo; $i++ ) {
|
|
|
71 |
my ($key, $value) = split( /:\s+/, $nodeinfo[$i] );
|
|
|
72 |
if ( $value =~ m/^(\d+)\s+[a-z]+$/i ) {
|
|
|
73 |
$value = $1;
|
|
|
74 |
}
|
|
|
75 |
$key = $conversion{$key} if exists( $conversion{$key} );
|
|
|
76 |
$main::nodeDB->{$nodename}->{$key} = $value;
|
|
|
77 |
} # for
|
|
|
78 |
} # while
|
|
|
79 |
print "main::nodeDB state after update\n" . Dumper( $main::nodeDB ) if $main::DEBUG;
|
|
|
80 |
&writeDB( $main::nodeDBName, $main::nodeDB );
|
|
|
81 |
} elsif ( $action eq 'list' ) { # dump database as a tab separated file with headers
|
|
|
82 |
my @header;
|
|
|
83 |
my @data;
|
|
|
84 |
foreach my $node ( sort keys %$main::nodeDB ) {
|
|
|
85 |
unless ( @header ) {
|
|
|
86 |
# just grab the keys for headers
|
|
|
87 |
@header = sort keys %{ $main::nodeDB->{$node} };
|
|
|
88 |
# put Node at the beginning
|
|
|
89 |
unshift ( @header, 'Node' );
|
|
|
90 |
}
|
|
|
91 |
my @line;
|
|
|
92 |
push @line, $node;
|
|
|
93 |
foreach my $column (sort keys %{ $main::nodeDB->{$node} }) {
|
|
|
94 |
push @line, $main::nodeDB->{$node}->{$column};
|
|
|
95 |
}
|
|
|
96 |
push (@data, \@line );
|
|
|
97 |
}
|
|
|
98 |
$return = &main::report_screen( \@header, \@data );
|
|
|
99 |
#$return = join( "\t", @header ) . "\n";
|
|
|
100 |
#$return .= join( "\n", @data ) . "\n";
|
|
|
101 |
} elsif ( $action eq 'scan' ) {
|
|
|
102 |
foreach my $node ( keys %$main::nodeDB ) {
|
|
|
103 |
$main::nodePopulations->{$node}->{'running'} = &getDomainsOnNode( $node );
|
|
|
104 |
$main::nodePopulations->{$node}->{'lastchecked'} = time;
|
|
|
105 |
}
|
|
|
106 |
&writeDB( $main::nodePopulationDBName,$main::nodePopulations );
|
|
|
107 |
} # if..elsif
|
|
|
108 |
return $return;
|
|
|
109 |
}
|
|
|
110 |
|
|
|
111 |
|