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