Subversion Repositories havirt

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 rodolico 1
#! /usr/bin/env perl
2
 
3
use strict;
4
use warnings;
3 rodolico 5
 
6
# havirt
7
# Basically an extension of virsh which will perform actions on virtuals 
8
# running on multiple, connected hypervisors (virsh calls them nodes)
9
# existing as a cluster of hypervisors where virtuals can be shut down,
10
# started and migrated at need.
11
#
12
# Progam consists of one executable (havirt) and multiple Perl Modules
13
# (*.pm), each of which encompasses a function. However, this is NOT
14
# written as an Object Oriented system.
15
#
16
# havirt --help gives a brief help screen.
17
 
18
# Copyright 2024 Daily Data, Inc.
19
# 
20
# Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following 
21
# conditions are met:
22
#
23
#   Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
24
#   Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer 
25
#   in the documentation and/or other materials provided with the distribution.
26
#   Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived
27
#   from this software without specific prior written permission.
28
# 
29
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
30
# NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
31
# THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
32
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
34
# OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35
 
36
 
2 rodolico 37
#use experimental "switch";
38
 
39
# requires File::Slurp. 
40
# In Debian derivatives
41
# apt install libfile-slurp-perl
42
 
43
# apt install libxml-libxml-perl libyaml-tiny-perl
44
 
45
 
46
BEGIN {
47
   use FindBin;
48
   use File::Spec;
49
   # use libraries from the directory this script is in
6 rodolico 50
   use Cwd 'abs_path';
51
   use File::Basename;
52
   use lib dirname( abs_path( __FILE__ ) );
2 rodolico 53
}
54
 
6 rodolico 55
 
56
 
57
 
58
 
3 rodolico 59
use havirt; # Load all our shared stuff
60
 
2 rodolico 61
use Data::Dumper;
62
use YAML::Tiny;
63
 
4 rodolico 64
# define the version number
65
# see https://metacpan.org/pod/release/JPEACOCK/version-0.97/lib/version.pod
66
use version;
67
our $VERSION = version->declare("0.0.1");
68
 
69
 
70
# see https://perldoc.perl.org/Getopt/Long.html
71
use Getopt::Long;
72
# allow -vvn (ie, --verbose --verbose --dryrun)
73
Getopt::Long::Configure ("bundling");
74
 
75
 
2 rodolico 76
# global variables
77
my $scriptDir = $FindBin::RealBin;
78
my $scriptName = $FindBin::Script;
79
my $confDir = "$scriptDir/conf";
80
my $dbDir = "$scriptDir/var";
3 rodolico 81
our $nodeDBName = "$dbDir/node.yaml";
4 rodolico 82
our $domainDBName = "$dbDir/domains.yaml";
3 rodolico 83
our $nodePopulationDBName = "$dbDir/node_population.yaml";
2 rodolico 84
 
85
# these contain the values from the databases
86
# loaded on demand
3 rodolico 87
our $nodeDB;
88
our $virtDB;
89
our $nodePopulations;
2 rodolico 90
 
4 rodolico 91
# options variables
92
our $reportFormat = 'screen';
93
our $targetNode = '';
94
our $dryRun = 1;
95
our $DEBUG = 0;
96
my $help = 0;
97
my $version = 0;
2 rodolico 98
 
99
sub help {
100
   print "$0 command [argument]\n";
101
   print "where command is one of\n";
4 rodolico 102
   print "\tnode update [-t NODE] # update a given node (defaults to all)\n";
2 rodolico 103
   print "\tnode list # display tab delimited list of node specs\n";
4 rodolico 104
   print "\tnode scan [-t NODE] # update list of domains on node (defaults to all)\n ";
2 rodolico 105
   print "\tdomain update ALL|RUNNING|[domain] [domain]... # update domains\n";
106
   print "\tdomain list ALL|RUNNING|[domain] [domain]... # display tab delimited list of domain specs\n";
107
   print "\tcluster status # report of memory and vcpu status on all nodes\n";
4 rodolico 108
   print "Some flags can be used where appropriate\n";
109
   print "\t--help|-h # show this screen\n";
110
   print "\t--version|-v # show version of program\n";
111
   print "\t--format|-f screen|tsv # output of list commands is either padded for screen or Tab Delim\n";
112
   print "\t--target|-t NODE # the action use NODE for the target of actions\n";
113
   print "\t--dryrun|-n # does not perform the actions, simply shows what commands would be executed\n";
114
   print "\t--debug|d # increases verbosity, does not actually perform actions (--dry-run assumed)\n";
2 rodolico 115
}
116
 
117
sub loadVirtDB {
118
   return if $virtDB;
119
   $virtDB = &readDB( $domainDBName );
120
}
121
 
122
sub domain {
123
   my $action = lc shift;
124
   my $return = '';
125
   &loadVirtDB();
126
   &loadNodePopulations();
127
   @_ = keys( %$virtDB ) if ( $_[0] && $_[0] eq 'ALL' );
128
   if ( $_[0] && $_[0] eq 'RUNNING' ) {
129
      my @running;
130
      foreach my $node ( keys %$nodePopulations ) {
131
         push @running, keys %{ $nodePopulations->{$node}->{'running'} };
132
      }
133
      @_ = @running;
134
   }
135
   if ( $action eq 'update' ) { # download xml to var and update database
136
      while ( my $virt = shift ) {
137
         &parseDomain( $virt );
138
      } # while
139
      &writeDB( $domainDBName, $virtDB );
140
   } elsif ( $action eq 'list' ) { # dump domain as a tab separated data file
141
      my @return;
142
      foreach my $node ( keys %$nodePopulations ) {
143
         foreach my $virt (keys %{$nodePopulations->{$node}->{'running'}} ) {
144
            push @return, &listDomain( $virt, $node );
145
         }
146
      }
147
      $return = join( "\n", sort @return ) . "\n";;
148
   }
149
   return $return;;
150
} # sub domain
151
 
152
sub listDomain {
153
   my ($virt,$node) = @_;
154
   my @return;
155
   push @return, $virt;
156
   push @return, $node;
157
   foreach my $column ( sort keys %{ $virtDB->{$virt} } ) {
158
      push @return, $virtDB->{$virt}->{$column};
159
   }
160
   return join( "\t", @return);
161
}
162
 
163
 
164
 
165
# get the XML definition file of a running domain off of whatever
166
# node it is running on, and save it to disk
167
sub getVirtConfig {
168
   my ($virt,$filename) = @_;
169
   my $return;
170
   print "In getVirtConfig looking for $virt with file $filename\n" if $DEBUG;
171
   if ( -f $filename ) {
172
      open XML, "<$filename" or die "Could not read from $filename: $!\n";
173
      $return = join( '', <XML> );
174
      close XML;
175
   } else {
176
      &loadNodePopulations();
177
      #die Dumper( $nodePopulations );
178
      foreach my $node ( keys %$nodePopulations ) {
179
         print "getVirtConfig Looking on $node for $virt\n";
180
         if ( exists( $nodePopulations->{$node}->{'running'}->{$virt} ) ) { # we found it
181
            print "Found $virt on node $node\n";
182
            $return = `ssh $node 'virsh dumpxml $virt'`;
183
            open XML,">$filename" or die "Could not write to $filename: $!\n";
184
            print XML $return;
185
            close XML;
186
         } # if
187
      } # foreach
188
   } # if..else
189
   return $return;
190
} # sub getVirtConfig
191
 
192
sub getXMLValue {
193
   my ( $key, $string ) = @_;
194
   my $start = "<$key";
195
   my $end = "</$key>";
196
   $string =~ m/$start([^>]*)>([^<]+)$end/;
197
   return ($1,$2);
198
}
199
 
200
sub parseDomain {
201
   my ($virt, $nodePopulations ) = @_;
202
 
203
   my @keysToSave = ( 'uuid', 'memory', 'vcpu' );
204
   my $filename = "$confDir/$virt.xml";
205
   my $xml = &getVirtConfig( $virt, $filename );
206
   my ($param,$value) = &getXMLValue( 'uuid', $xml );
207
   $virtDB->{$virt}->{'uuid'} = $value;
208
   ($param,$value) = &getXMLValue( 'memory', $xml );
209
   $virtDB->{$virt}->{'memory'} = $value;
210
   ($param,$value) = &getXMLValue( 'vcpu', $xml );
211
   $virtDB->{$virt}->{'vcpu'} = $value;
212
 
213
   $xml =~ m/type='vnc' port='(\d+)'/;
214
   $virtDB->{$virt}->{'vnc'} = $1;
215
}
216
 
217
sub cluster {
218
   my $action = lc shift;
219
   my $return = '';
220
   if ( $action eq 'status' ) {
221
      &loadVirtDB();
222
      &loadNodePopulations();
223
      &loadNodeDB();
224
      print "Node\tThreads\tMemory\tDomains\tvcpu\tmem_used\n";
225
      my $usedmem = 0;
226
      my $usedcpu = 0;
227
      my $availmem = 0;
228
      my $availcpu = 0;
229
      my $totalDomains = 0;
230
      foreach my $node (sort keys %$nodeDB ) {
231
         my $memory = 0;
232
         my $vcpus = 0;
233
         my $count = 0;
234
         foreach my $domain ( keys %{ $nodePopulations->{$node}->{'running'} } ) {
235
            $memory += $virtDB->{$domain}->{'memory'};
236
            $vcpus += $virtDB->{$domain}->{'vcpu'};
237
            $count++;
238
         }
239
         $return .= "$node\t$nodeDB->{$node}->{cpu_count}\t$nodeDB->{$node}->{memory}\t$count\t$vcpus\t$memory\n";
240
         $usedmem += $memory;
241
         $usedcpu += $vcpus;
242
         $totalDomains += $count;
243
         $availmem += $nodeDB->{$node}->{memory};
244
         $availcpu += $nodeDB->{$node}->{cpu_count};
245
      } # outer for
246
      $return .= "Total\t$availcpu\t$availmem\t$totalDomains\t$usedcpu\t$usedmem\n";
247
   }
248
   return $return;
249
}
250
 
251
 
4 rodolico 252
# handle any command line parameters that may have been passed in
253
 
254
GetOptions (
255
   'format|f=s' => \$reportFormat,
256
   'target|t=s' => \$targetNode,
257
   'dryrun|n!' => \$dryRun,
258
   'debug|d+' => \$DEBUG,
259
   'help|h' => \$help,
260
   'version|v' => \$version
261
) or die "Error parsing command line\n";
262
 
6 rodolico 263
my $command = shift; # the first one is the actual subsection
264
my $action = shift; # second is action to run
4 rodolico 265
 
6 rodolico 266
if ( $help || ! $command ) { &help() ; exit; }
4 rodolico 267
if ( $version ) { use File::Basename; print basename($0) . " v$VERSION\n"; exit; }
268
 
2 rodolico 269
 
5 rodolico 270
print "Parameters are\nreportFormat\t$reportFormat\ntargetNode\t$targetNode\ndryRun\t$dryRun\nDEBUG\t$DEBUG\n" if $DEBUG;
271
print "Command = $command\nAction = $action\n" if $DEBUG;
4 rodolico 272
 
2 rodolico 273
if ( $command eq 'node' ) {
3 rodolico 274
   require node;
275
   Module->import( qw/node/ );
4 rodolico 276
   print &{\&{"node::$action"}}();
2 rodolico 277
} elsif ( $command eq 'domain' ) {
278
   print &domain( $action, @ARGV );
279
} elsif ( $command eq 'cluster' ) {
280
   print &cluster( $action, @ARGV );
281
} else {
282
   &help();
283
}
284
 
285
 
286
1;