Subversion Repositories havirt

Rev

Go to most recent revision | Details | 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;
35
our $VERSION = version->declare("0.0.1");
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";
68
   return join( "\n", @return ) . "\n";
69
}
70
 
71
 
5 rodolico 72
# lists hardware capabilities of all nodes (virsh nodeinfo)
4 rodolico 73
sub list {
74
   my @header;
75
   my @data;
76
   my $return;
12 rodolico 77
   &main::readDB();
78
   foreach my $node ( sort keys %{$main::statusDB->{'node'}} ) {
4 rodolico 79
      unless ( @header ) {
80
         # just grab the keys for headers
12 rodolico 81
         @header = sort keys %{ $main::statusDB->{'node'}->{$node} };
4 rodolico 82
         # put Node at the beginning
83
         unshift ( @header, 'Node' );
84
      }
85
      my @line;
86
      push @line, $node;
12 rodolico 87
      foreach my $column (sort keys %{ $main::statusDB->{'node'}->{$node} }) {
88
         push @line, $main::statusDB->{'node'}->{$node}->{$column};
4 rodolico 89
      }
90
      push (@data, \@line );
91
   }
7 rodolico 92
   return &main::report( \@header, \@data );
4 rodolico 93
}
94
 
5 rodolico 95
# Get information about a node. Really only needs to be done when a node is
96
# first defined, or if there is a hardware upgrade
9 rodolico 97
# reads information off of the stack (@_), but will add to that if --target
98
# was defined
4 rodolico 99
sub update {
12 rodolico 100
   &main::readDB( 1 ); # open and lock so we can write to it later
4 rodolico 101
   my $return;
102
   my @targets;
103
   if ( $main::targetNode ) {
9 rodolico 104
      push @_, $main::targetNode;
4 rodolico 105
   }
12 rodolico 106
   @_ = keys %{$main::statusDB->{'node'}} unless @_;
9 rodolico 107
   while ( my $nodename = shift  ) {
4 rodolico 108
      print "Updating $nodename\n" if $main::DEBUG;
109
      $return = `ssh $nodename 'virsh nodeinfo'`;
110
      print "Output of ssh $nodename 'virsh nodeinfo' is\n" . $return if $main::DEBUG;
111
      my @nodeinfo = split( "\n", $return );
112
      for ( my $i = 0; $i < @nodeinfo; $i++ ) {
113
         my ($key, $value) = split( /:\s+/, $nodeinfo[$i] );
114
         if ( $value =~ m/^(\d+)\s+[a-z]+$/i ) {
115
            $value = $1;
116
         }
117
         $key = $conversion{$key} if exists( $conversion{$key} );
12 rodolico 118
         $main::statusDB->{'node'}->{$nodename}->{$key} = $value;
4 rodolico 119
      } # for
120
   } # while
12 rodolico 121
   print "main::statusDB->{'node'} state after update\n" . Dumper( $main::statusDB->{'node'} ) if $main::DEBUG;
122
   &main::writeDB();
4 rodolico 123
   return "Node has been updated\n";
124
}      
125
 
5 rodolico 126
 
127
# check one or more nodes and determine which domains are running on them.
128
# defaults to everything in the node database, but the -t can have it run on only one
129
# this is the function that should be run every few minutes on one of the servers
4 rodolico 130
sub scan {
15 rodolico 131
   return &main::scan(@_);
5 rodolico 132
}
4 rodolico 133
 
3 rodolico 134
 
5 rodolico 135
# add a new node. This is the same as doing an update on a node that doesn't exist.
136
sub add {
12 rodolico 137
#   &main::readDB();
5 rodolico 138
#   print "Adding $main::targetNode as new node\n" if $main::DEBUG;
12 rodolico 139
#   $main::statusDB->{'node'}->{$main::targetNode} = '';
5 rodolico 140
#   print "Calling update for new node\n" if $main::DEBUG;
12 rodolico 141
   &update( @_ );
3 rodolico 142
}
143