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