Subversion Repositories havirt

Rev

Rev 4 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

#!/usr/bin/env perl

# All functions related to maniplating a specific node
# part of havirt.

# v0.0.1 20240602 RWR
# Initial setup

package node;


our $VERSION = '0.01';
use warnings;
use strict;  


use Exporter;

our @ISA = qw( Exporter );
our @EXPORT = qw( 
                  &node
                );


sub loadNodeDB {
   print "In loadNodeDB, reading $main::nodeDBName\n";
   return if $main::nodeDB;
   $main::nodeDB = &main::readDB( $main::nodeDBName );
   print "Success\n";
}

sub getDomainsOnNode {
   my $node = shift;
   my @nodeList = grep { /^\s*\d/ } `ssh $node 'virsh list'`;
   for ( my $i = 0; $i < @nodeList; $i++ ) {
      if ( $nodeList[$i] =~ m/\s*\d+\s*([^ ]+)/ ) {
         $nodeList[$i] = $1;
      }
   }
   my %hash = map{ $_ => time } @nodeList;
   return \%hash;
}


sub node {
   my $action = lc shift;

   my %conversion = ( 
     'CPU frequency' => 'clock',
     'CPU model' => 'cpu_model',
     'CPU socket(s)' => 'cpu_socket',
     'CPU(s)' => 'cpu_count',
     'Core(s) per socket' => 'cpu_cores',
     'Memory size' => 'memory',
     'NUMA cell(s)' => 'numa_cells',
     'Thread(s) per core' => 'threads_per_core'
   );


   print "In node, action is $action\n" if $main::DEBUG;
   my $return = '';
   &loadNodeDB();
   if ( $action eq 'update' ) { # read information for nodes and update database
      @_ = keys %$main::nodeDB if ( $_[0] eq 'ALL' );
      while ( my $nodename = shift ) {
         print "Updating $nodename\n" if $main::DEBUG;
         $return = `ssh $nodename 'virsh nodeinfo'`;
         print "Output of ssh $nodename 'virsh nodeinfo' is\n" . $return if $main::DEBUG;
         my @nodeinfo = split( "\n", $return );
         for ( my $i = 0; $i < @nodeinfo; $i++ ) {
            my ($key, $value) = split( /:\s+/, $nodeinfo[$i] );
            if ( $value =~ m/^(\d+)\s+[a-z]+$/i ) {
               $value = $1;
            }
            $key = $conversion{$key} if exists( $conversion{$key} );
            $main::nodeDB->{$nodename}->{$key} = $value;
         } # for
      } # while
      print "main::nodeDB state after update\n" . Dumper( $main::nodeDB ) if $main::DEBUG;
      &writeDB( $main::nodeDBName, $main::nodeDB );
   } elsif ( $action eq 'list' ) { # dump database as a tab separated file with headers
      my @header;
      my @data;
      foreach my $node ( sort keys %$main::nodeDB ) {
         unless ( @header ) {
            # just grab the keys for headers
            @header = sort keys %{ $main::nodeDB->{$node} };
            # put Node at the beginning
            unshift ( @header, 'Node' );
         }
         my @line;
         push @line, $node;
         foreach my $column (sort keys %{ $main::nodeDB->{$node} }) {
            push @line, $main::nodeDB->{$node}->{$column};
         }
         push (@data, \@line );
      }
      $return = &main::report_screen( \@header, \@data );
      #$return = join( "\t", @header ) . "\n";
      #$return .= join( "\n", @data ) . "\n";
   } elsif ( $action eq 'scan' ) {
      foreach my $node ( keys %$main::nodeDB ) {
         $main::nodePopulations->{$node}->{'running'} = &getDomainsOnNode( $node );
         $main::nodePopulations->{$node}->{'lastchecked'} = time;
      }
      &writeDB( $main::nodePopulationDBName,$main::nodePopulations );
   } # if..elsif
   return $return;
}