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
# read the node database into memory, if it was not already loaded
3 rodolico 73
sub loadNodeDB {
4 rodolico 74
   print "In loadNodeDB, reading $main::nodeDBName\n" if $main::DEBUG > 1;
3 rodolico 75
   return if $main::nodeDB;
76
   $main::nodeDB = &main::readDB( $main::nodeDBName );
4 rodolico 77
   print "Success\n" if $main::DEBUG > 1;
3 rodolico 78
}
79
 
5 rodolico 80
# scans a node to determine which domains are running on it
3 rodolico 81
sub getDomainsOnNode {
82
   my $node = shift;
83
   my @nodeList = grep { /^\s*\d/ } `ssh $node 'virsh list'`;
84
   for ( my $i = 0; $i < @nodeList; $i++ ) {
85
      if ( $nodeList[$i] =~ m/\s*\d+\s*([^ ]+)/ ) {
86
         $nodeList[$i] = $1;
87
      }
88
   }
89
   my %hash = map{ $_ => time } @nodeList;
90
   return \%hash;
91
}
92
 
5 rodolico 93
# lists hardware capabilities of all nodes (virsh nodeinfo)
4 rodolico 94
sub list {
95
   my @header;
96
   my @data;
97
   my $return;
98
   &loadNodeDB();
99
   foreach my $node ( sort keys %$main::nodeDB ) {
100
      unless ( @header ) {
101
         # just grab the keys for headers
102
         @header = sort keys %{ $main::nodeDB->{$node} };
103
         # put Node at the beginning
104
         unshift ( @header, 'Node' );
105
      }
106
      my @line;
107
      push @line, $node;
108
      foreach my $column (sort keys %{ $main::nodeDB->{$node} }) {
109
         push @line, $main::nodeDB->{$node}->{$column};
110
      }
111
      push (@data, \@line );
112
   }
7 rodolico 113
   return &main::report( \@header, \@data );
4 rodolico 114
}
115
 
5 rodolico 116
# Get information about a node. Really only needs to be done when a node is
117
# first defined, or if there is a hardware upgrade
9 rodolico 118
# reads information off of the stack (@_), but will add to that if --target
119
# was defined
4 rodolico 120
sub update {
121
   &loadNodeDB();
122
   my $return;
123
   my @targets;
124
   if ( $main::targetNode ) {
9 rodolico 125
      push @_, $main::targetNode;
4 rodolico 126
   }
9 rodolico 127
   @_ = keys %$main::nodeDB unless @_;
128
   while ( my $nodename = shift  ) {
4 rodolico 129
      print "Updating $nodename\n" if $main::DEBUG;
130
      $return = `ssh $nodename 'virsh nodeinfo'`;
131
      print "Output of ssh $nodename 'virsh nodeinfo' is\n" . $return if $main::DEBUG;
132
      my @nodeinfo = split( "\n", $return );
133
      for ( my $i = 0; $i < @nodeinfo; $i++ ) {
134
         my ($key, $value) = split( /:\s+/, $nodeinfo[$i] );
135
         if ( $value =~ m/^(\d+)\s+[a-z]+$/i ) {
136
            $value = $1;
137
         }
138
         $key = $conversion{$key} if exists( $conversion{$key} );
139
         $main::nodeDB->{$nodename}->{$key} = $value;
140
      } # for
141
   } # while
142
   print "main::nodeDB state after update\n" . Dumper( $main::nodeDB ) if $main::DEBUG;
143
   &main::writeDB( $main::nodeDBName, $main::nodeDB );
144
   return "Node has been updated\n";
145
}      
146
 
5 rodolico 147
 
148
# check one or more nodes and determine which domains are running on them.
149
# defaults to everything in the node database, but the -t can have it run on only one
150
# this is the function that should be run every few minutes on one of the servers
4 rodolico 151
sub scan {
152
   &loadNodeDB();
153
   &main::loadNodePopulations();
154
   print Dumper( $main::nodePopulations ) if $main::DEBUG > 2;
155
   my @targets;
156
   if ( $main::targetNode ) {
157
      push @targets, $main::targetNode;
158
   } else {
159
      @targets = keys %$main::nodeDB;
160
   }
161
   print "Scanning " . join( "\n", @targets ) . "\n" if $main::DEBUG;
162
   foreach my $node (@targets) {
163
      $main::nodePopulations->{$node}->{'running'} = &getDomainsOnNode( $node );
164
      $main::nodePopulations->{$node}->{'lastchecked'} = time;
165
      print Dumper( $main::nodePopulations->{$node} ) if $main::DEBUG > 2;
166
   }
167
   &main::writeDB( $main::nodePopulationDBName,$main::nodePopulations );
168
   return "Node(s) updated\n";
5 rodolico 169
}
4 rodolico 170
 
3 rodolico 171
 
5 rodolico 172
# add a new node. This is the same as doing an update on a node that doesn't exist.
173
sub add {
174
#   &loadNodeDB();
175
#   print "Adding $main::targetNode as new node\n" if $main::DEBUG;
176
#   $main::nodeDB->{$main::targetNode} = '';
177
#   print "Calling update for new node\n" if $main::DEBUG;
178
   &update();
3 rodolico 179
}
180