Subversion Repositories havirt

Rev

Rev 3 | Rev 5 | Go to most recent revision | 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;
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( 
4 rodolico 44
                  &node &list &update &scan
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
 
4 rodolico 59
 
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
 
67
sub getDomainsOnNode {
68
   my $node = shift;
69
   my @nodeList = grep { /^\s*\d/ } `ssh $node 'virsh list'`;
70
   for ( my $i = 0; $i < @nodeList; $i++ ) {
71
      if ( $nodeList[$i] =~ m/\s*\d+\s*([^ ]+)/ ) {
72
         $nodeList[$i] = $1;
73
      }
74
   }
75
   my %hash = map{ $_ => time } @nodeList;
76
   return \%hash;
77
}
78
 
79
 
4 rodolico 80
sub list {
81
   my @header;
82
   my @data;
83
   my $return;
84
   &loadNodeDB();
85
   foreach my $node ( sort keys %$main::nodeDB ) {
86
      unless ( @header ) {
87
         # just grab the keys for headers
88
         @header = sort keys %{ $main::nodeDB->{$node} };
89
         # put Node at the beginning
90
         unshift ( @header, 'Node' );
91
      }
92
      my @line;
93
      push @line, $node;
94
      foreach my $column (sort keys %{ $main::nodeDB->{$node} }) {
95
         push @line, $main::nodeDB->{$node}->{$column};
96
      }
97
      push (@data, \@line );
98
   }
99
   $return = &main::report( \@header, \@data );
100
}
101
 
102
 
103
sub update {
104
   &loadNodeDB();
105
   my $return;
106
   my @targets;
107
   if ( $main::targetNode ) {
108
      push @targets, $main::targetNode;
109
   } else {
110
      @targets = keys %$main::nodeDB;
111
   }
112
   while ( my $nodename = shift ) {
113
      print "Updating $nodename\n" if $main::DEBUG;
114
      $return = `ssh $nodename 'virsh nodeinfo'`;
115
      print "Output of ssh $nodename 'virsh nodeinfo' is\n" . $return if $main::DEBUG;
116
      my @nodeinfo = split( "\n", $return );
117
      for ( my $i = 0; $i < @nodeinfo; $i++ ) {
118
         my ($key, $value) = split( /:\s+/, $nodeinfo[$i] );
119
         if ( $value =~ m/^(\d+)\s+[a-z]+$/i ) {
120
            $value = $1;
121
         }
122
         $key = $conversion{$key} if exists( $conversion{$key} );
123
         $main::nodeDB->{$nodename}->{$key} = $value;
124
      } # for
125
   } # while
126
   print "main::nodeDB state after update\n" . Dumper( $main::nodeDB ) if $main::DEBUG;
127
   &main::writeDB( $main::nodeDBName, $main::nodeDB );
128
   return "Node has been updated\n";
129
}      
130
 
131
sub scan {
132
   &loadNodeDB();
133
   &main::loadNodePopulations();
134
   print Dumper( $main::nodePopulations ) if $main::DEBUG > 2;
135
   my @targets;
136
   if ( $main::targetNode ) {
137
      push @targets, $main::targetNode;
138
   } else {
139
      @targets = keys %$main::nodeDB;
140
   }
141
   print "Scanning " . join( "\n", @targets ) . "\n" if $main::DEBUG;
142
   foreach my $node (@targets) {
143
      $main::nodePopulations->{$node}->{'running'} = &getDomainsOnNode( $node );
144
      $main::nodePopulations->{$node}->{'lastchecked'} = time;
145
      print Dumper( $main::nodePopulations->{$node} ) if $main::DEBUG > 2;
146
   }
147
   &main::writeDB( $main::nodePopulationDBName,$main::nodePopulations );
148
   return "Node(s) updated\n";
149
}   
150
 
3 rodolico 151
sub node {
152
   my $action = lc shift;
153
 
154
   print "In node, action is $action\n" if $main::DEBUG;
155
   my $return = '';
156
   &loadNodeDB();
157
   if ( $action eq 'update' ) { # read information for nodes and update database
4 rodolico 158
      return &update( @_ );
159
   } elsif ( $action eq 'list' ) { # dump database to screen
160
      return &list();
3 rodolico 161
   } elsif ( $action eq 'scan' ) {
4 rodolico 162
      return &scan();
3 rodolico 163
   } # if..elsif
164
   return $return;
165
}
166
 
167