Subversion Repositories havirt

Rev

Rev 3 | Rev 5 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3 Rev 4
Line 1... Line 1...
1
#!/usr/bin/env perl
1
#!/usr/bin/env perl
2
 
2
 
3
# All functions related to maniplating a specific node
3
# All functions related to maniplating a specific node
4
# part of havirt.
4
# part of havirt.
5
 
5
 
-
 
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
 
6
# v0.0.1 20240602 RWR
24
# v0.0.1 20240602 RWR
7
# Initial setup
25
# Initial setup
8
 
26
 
9
package node;
27
package node;
10
 
28
 
11
 
-
 
12
our $VERSION = '0.01';
-
 
13
use warnings;
29
use warnings;
14
use strict;  
30
use strict;  
15
 
31
 
-
 
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");
-
 
36
 
-
 
37
 
-
 
38
use Data::Dumper;
16
 
39
 
17
use Exporter;
40
use Exporter;
18
 
41
 
19
our @ISA = qw( Exporter );
42
our @ISA = qw( Exporter );
20
our @EXPORT = qw( 
43
our @EXPORT = qw( 
21
                  &node
44
                  &node &list &update &scan
22
                );
45
                );
23
 
46
 
-
 
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
);
-
 
58
 
24
 
59
 
25
sub loadNodeDB {
60
sub loadNodeDB {
26
   print "In loadNodeDB, reading $main::nodeDBName\n";
61
   print "In loadNodeDB, reading $main::nodeDBName\n" if $main::DEBUG > 1;
27
   return if $main::nodeDB;
62
   return if $main::nodeDB;
28
   $main::nodeDB = &main::readDB( $main::nodeDBName );
63
   $main::nodeDB = &main::readDB( $main::nodeDBName );
29
   print "Success\n";
64
   print "Success\n" if $main::DEBUG > 1;
30
}
65
}
31
 
66
 
32
sub getDomainsOnNode {
67
sub getDomainsOnNode {
33
   my $node = shift;
68
   my $node = shift;
34
   my @nodeList = grep { /^\s*\d/ } `ssh $node 'virsh list'`;
69
   my @nodeList = grep { /^\s*\d/ } `ssh $node 'virsh list'`;
Line 40... Line 75...
40
   my %hash = map{ $_ => time } @nodeList;
75
   my %hash = map{ $_ => time } @nodeList;
41
   return \%hash;
76
   return \%hash;
42
}
77
}
43
 
78
 
44
 
79
 
45
sub node {
80
sub list {
-
 
81
   my @header;
-
 
82
   my @data;
46
   my $action = lc shift;
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
 
47
 
102
 
-
 
103
sub update {
-
 
104
   &loadNodeDB();
-
 
105
   my $return;
48
   my %conversion = ( 
106
   my @targets;
49
     'CPU frequency' => 'clock',
107
   if ( $main::targetNode ) {
50
     'CPU model' => 'cpu_model',
108
      push @targets, $main::targetNode;
-
 
109
   } else {
51
     'CPU socket(s)' => 'cpu_socket',
110
      @targets = keys %$main::nodeDB;
-
 
111
   }
52
     'CPU(s)' => 'cpu_count',
112
   while ( my $nodename = shift ) {
-
 
113
      print "Updating $nodename\n" if $main::DEBUG;
53
     'Core(s) per socket' => 'cpu_cores',
114
      $return = `ssh $nodename 'virsh nodeinfo'`;
-
 
115
      print "Output of ssh $nodename 'virsh nodeinfo' is\n" . $return if $main::DEBUG;
54
     'Memory size' => 'memory',
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 ) {
55
     'NUMA cell(s)' => 'numa_cells',
120
            $value = $1;
-
 
121
         }
-
 
122
         $key = $conversion{$key} if exists( $conversion{$key} );
56
     'Thread(s) per core' => 'threads_per_core'
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";
57
   );
129
}      
58
 
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
 
-
 
151
sub node {
-
 
152
   my $action = lc shift;
59
 
153
 
60
   print "In node, action is $action\n" if $main::DEBUG;
154
   print "In node, action is $action\n" if $main::DEBUG;
61
   my $return = '';
155
   my $return = '';
62
   &loadNodeDB();
156
   &loadNodeDB();
63
   if ( $action eq 'update' ) { # read information for nodes and update database
157
   if ( $action eq 'update' ) { # read information for nodes and update database
64
      @_ = keys %$main::nodeDB if ( $_[0] eq 'ALL' );
-
 
65
      while ( my $nodename = shift ) {
-
 
66
         print "Updating $nodename\n" if $main::DEBUG;
-
 
67
         $return = `ssh $nodename 'virsh nodeinfo'`;
-
 
68
         print "Output of ssh $nodename 'virsh nodeinfo' is\n" . $return if $main::DEBUG;
-
 
69
         my @nodeinfo = split( "\n", $return );
-
 
70
         for ( my $i = 0; $i < @nodeinfo; $i++ ) {
-
 
71
            my ($key, $value) = split( /:\s+/, $nodeinfo[$i] );
-
 
72
            if ( $value =~ m/^(\d+)\s+[a-z]+$/i ) {
-
 
73
               $value = $1;
158
      return &update( @_ );
74
            }
-
 
75
            $key = $conversion{$key} if exists( $conversion{$key} );
-
 
76
            $main::nodeDB->{$nodename}->{$key} = $value;
-
 
77
         } # for
-
 
78
      } # while
-
 
79
      print "main::nodeDB state after update\n" . Dumper( $main::nodeDB ) if $main::DEBUG;
-
 
80
      &writeDB( $main::nodeDBName, $main::nodeDB );
-
 
81
   } elsif ( $action eq 'list' ) { # dump database as a tab separated file with headers
159
   } elsif ( $action eq 'list' ) { # dump database to screen
82
      my @header;
-
 
83
      my @data;
-
 
84
      foreach my $node ( sort keys %$main::nodeDB ) {
-
 
85
         unless ( @header ) {
-
 
86
            # just grab the keys for headers
-
 
87
            @header = sort keys %{ $main::nodeDB->{$node} };
-
 
88
            # put Node at the beginning
-
 
89
            unshift ( @header, 'Node' );
-
 
90
         }
-
 
91
         my @line;
160
      return &list();
92
         push @line, $node;
-
 
93
         foreach my $column (sort keys %{ $main::nodeDB->{$node} }) {
-
 
94
            push @line, $main::nodeDB->{$node}->{$column};
-
 
95
         }
-
 
96
         push (@data, \@line );
-
 
97
      }
-
 
98
      $return = &main::report_screen( \@header, \@data );
-
 
99
      #$return = join( "\t", @header ) . "\n";
-
 
100
      #$return .= join( "\n", @data ) . "\n";
-
 
101
   } elsif ( $action eq 'scan' ) {
161
   } elsif ( $action eq 'scan' ) {
102
      foreach my $node ( keys %$main::nodeDB ) {
162
      return &scan();
103
         $main::nodePopulations->{$node}->{'running'} = &getDomainsOnNode( $node );
-
 
104
         $main::nodePopulations->{$node}->{'lastchecked'} = time;
-
 
105
      }
-
 
106
      &writeDB( $main::nodePopulationDBName,$main::nodePopulations );
-
 
107
   } # if..elsif
163
   } # if..elsif
108
   return $return;
164
   return $return;
109
}
165
}
110
 
166
 
111
 
167