Subversion Repositories havirt

Rev

Rev 20 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
3 rodolico 1
#!/usr/bin/env perl
2
 
3
# All functions related to maniplating a specific node
4
# part of havirt.
5
 
4 rodolico 6
# Copyright 2024 Daily Data, Inc.
7
# 
8
# Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following 
9
# conditions are met:
10
#
11
#   Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
12
#   Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer 
13
#   in the documentation and/or other materials provided with the distribution.
14
#   Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived
15
#   from this software without specific prior written permission.
16
# 
17
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
18
# NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
19
# THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
22
# OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23
 
3 rodolico 24
# v0.0.1 20240602 RWR
25
# Initial setup
26
 
27
package node;
28
 
29
use warnings;
30
use strict;  
31
 
4 rodolico 32
# define the version number
33
# see https://metacpan.org/pod/release/JPEACOCK/version-0.97/lib/version.pod
34
use version;
19 rodolico 35
our $VERSION = version->declare("1.0.0");
3 rodolico 36
 
4 rodolico 37
 
38
use Data::Dumper;
39
 
3 rodolico 40
use Exporter;
41
 
42
our @ISA = qw( Exporter );
43
our @EXPORT = qw( 
5 rodolico 44
                  &node &list &update &scan &add
3 rodolico 45
                );
46
 
4 rodolico 47
# Converts from output of node info to a key we want to use
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
);
3 rodolico 58
 
9 rodolico 59
# show a help screen
60
sub help {
61
   my @return;
62
   push @return, "node update [nodename|-t nodename]";
63
   push @return, "\tUpdates capabilities on one or more nodes, default is all nodes";
64
   push @return, "node list [--format|-f screen|tsv]";
65
   push @return, "\tLists all nodes with some statistics about them as screen or tsv (default screen)";
66
   push @return, "node scan [nodename|-t nodename]";
67
   push @return, "\tUpdates list of domains on one or more existing nodes, default is all nodes";
17 rodolico 68
   push @return, "node maintenance nodename [on|off]";
69
   push @return, "\ton - set maintenance flag; no domains can be started/migrated to node";
70
   push @return, "\toff - Allows domains to be migrated/started on node";
71
   push @return, "\tnothing - displays current maintenance flag";
72
   push @return, "\tNote: a node with any domains running can not have maintenance mode turned on";
9 rodolico 73
   return join( "\n", @return ) . "\n";
74
}
75
 
76
 
5 rodolico 77
# lists hardware capabilities of all nodes (virsh nodeinfo)
4 rodolico 78
sub list {
79
   my @header;
80
   my @data;
81
   my $return;
12 rodolico 82
   &main::readDB();
83
   foreach my $node ( sort keys %{$main::statusDB->{'node'}} ) {
4 rodolico 84
      unless ( @header ) {
85
         # just grab the keys for headers
12 rodolico 86
         @header = sort keys %{ $main::statusDB->{'node'}->{$node} };
4 rodolico 87
         # put Node at the beginning
88
         unshift ( @header, 'Node' );
89
      }
90
      my @line;
91
      push @line, $node;
12 rodolico 92
      foreach my $column (sort keys %{ $main::statusDB->{'node'}->{$node} }) {
93
         push @line, $main::statusDB->{'node'}->{$node}->{$column};
4 rodolico 94
      }
95
      push (@data, \@line );
96
   }
7 rodolico 97
   return &main::report( \@header, \@data );
4 rodolico 98
}
99
 
5 rodolico 100
# Get information about a node. Really only needs to be done when a node is
101
# first defined, or if there is a hardware upgrade
9 rodolico 102
# reads information off of the stack (@_), but will add to that if --target
103
# was defined
4 rodolico 104
sub update {
12 rodolico 105
   &main::readDB( 1 ); # open and lock so we can write to it later
4 rodolico 106
   my $return;
18 rodolico 107
   my @requiredFields = ( 'maintenance' );
4 rodolico 108
   my @targets;
109
   if ( $main::targetNode ) {
9 rodolico 110
      push @_, $main::targetNode;
4 rodolico 111
   }
12 rodolico 112
   @_ = keys %{$main::statusDB->{'node'}} unless @_;
9 rodolico 113
   while ( my $nodename = shift  ) {
4 rodolico 114
      print "Updating $nodename\n" if $main::DEBUG;
115
      $return = `ssh $nodename 'virsh nodeinfo'`;
116
      print "Output of ssh $nodename 'virsh nodeinfo' is\n" . $return if $main::DEBUG;
117
      my @nodeinfo = split( "\n", $return );
118
      for ( my $i = 0; $i < @nodeinfo; $i++ ) {
119
         my ($key, $value) = split( /:\s+/, $nodeinfo[$i] );
120
         if ( $value =~ m/^(\d+)\s+[a-z]+$/i ) {
121
            $value = $1;
122
         }
123
         $key = $conversion{$key} if exists( $conversion{$key} );
12 rodolico 124
         $main::statusDB->{'node'}->{$nodename}->{$key} = $value;
4 rodolico 125
      } # for
18 rodolico 126
      foreach my $field ( @requiredFields ) {
127
         $main::statusDB->{'node'}->{$nodename}->{$field} = '' 
128
            unless defined ( $main::statusDB->{'node'}->{$nodename}->{$field} );
129
      } # foreach
4 rodolico 130
   } # while
12 rodolico 131
   print "main::statusDB->{'node'} state after update\n" . Dumper( $main::statusDB->{'node'} ) if $main::DEBUG;
132
   &main::writeDB();
4 rodolico 133
   return "Node has been updated\n";
134
}      
135
 
5 rodolico 136
 
137
# check one or more nodes and determine which domains are running on them.
138
# defaults to everything in the node database, but the -t can have it run on only one
139
# this is the function that should be run every few minutes on one of the servers
4 rodolico 140
sub scan {
15 rodolico 141
   return &main::scan(@_);
5 rodolico 142
}
4 rodolico 143
 
3 rodolico 144
 
5 rodolico 145
# add a new node. This is the same as doing an update on a node that doesn't exist.
146
sub add {
12 rodolico 147
#   &main::readDB();
5 rodolico 148
#   print "Adding $main::targetNode as new node\n" if $main::DEBUG;
12 rodolico 149
#   $main::statusDB->{'node'}->{$main::targetNode} = '';
5 rodolico 150
#   print "Calling update for new node\n" if $main::DEBUG;
12 rodolico 151
   &update( @_ );
3 rodolico 152
}
153
 
17 rodolico 154
sub maintenance {
155
   my ( $node, $action ) = @_;
156
   &main::readDB(1);
157
   if ( $action ) {
158
      $main::statusDB->{'node'}->{$node}->{'maintenance'} = ( lc( $action ) eq 'on' ) ? 1 : 0;
159
   }
160
   &main::writeDB();
161
   return "Maintenance set to " . ( $main::statusDB->{'node'}->{$node}->{'maintenance'} ? 'On' : 'Off' ) . "\n";
162
}
163