Subversion Repositories havirt

Rev

Rev 26 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
8 rodolico 1
#!/usr/bin/env perl
2
 
3
# All functions related to maniplating/reporting on cluster
4
# part of havirt.
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
 
24
# v0.0.1 20240602 RWR
25
# Initial setup
26 rodolico 26
#
27
# v1.2.0 20240826 RWR
28
# Added some code to migrate domains if node placed in maintenance mode
29
# Added a lot of 'verbose' print lines, and modified for new flag structure
30
#
8 rodolico 31
 
26 rodolico 32
 
8 rodolico 33
package cluster;
34
 
35
use warnings;
36
use strict;  
37
 
38
# define the version number
39
# see https://metacpan.org/pod/release/JPEACOCK/version-0.97/lib/version.pod
40
use version;
26 rodolico 41
our $VERSION = version->declare("1.2.0");
8 rodolico 42
 
43
 
44
use Data::Dumper;
45
 
46
use Exporter;
47
 
48
our @ISA = qw( Exporter );
49
our @EXPORT = qw( 
50
                  &list
26 rodolico 51
                  &iscsi
8 rodolico 52
                );
53
 
11 rodolico 54
sub help {
55
   my @return;
56
   push @return, 'cluster status';
57
   push @return, "\t[--format|-f screen|tsv] - displays some stats on cluster resources used";
39 rodolico 58
   push @return, 'cluster balance';
59
   push @return, "\tBalances resources by moving domains between nodes";
26 rodolico 60
   push @return, 'cluster iscsi';
61
   push @return, "\tdisplays list of all iSCSI targets 'known' by system";
62
   push @return, 'cluster iscsi add ip-or-dns-name';
63
   push @return, "\tAdds iscsi target to system";
64
   push @return, 'cluster iscsi delete  ip-or-dns-name';
65
   push @return, "\tDelete iSCSI target processed by system. ip-or-dns-name MUST be exact";
66
   push @return, 'cluster iscsi update [node ...]';
67
   push @return, "\tPerforms an update to add new iSCSI targets on one or more nodes";
68
   push @return, "\tScans all iSCSI targets, looking for new shares on each, then performs";
69
   push @return, "\ta login, adding it to the node. DOES NOT delete old targets at this";
70
   push @return, "\ttime. If no nodes passed in, will perform function on all nodes not";
71
   push @return, "\tin maintenance mode";
72
 
11 rodolico 73
   return join( "\n", @return ) . "\n";
74
}
75
 
10 rodolico 76
sub status {
77
   my $return = '';
13 rodolico 78
   &main::readDB();
25 rodolico 79
   my @header = ('Node','Threads','Memory','Domains','vcpu','mem_used', 'Status' );
10 rodolico 80
   my @data;
81
   my $usedmem = 0;
82
   my $usedcpu = 0;
83
   my $availmem = 0;
84
   my $availcpu = 0;
85
   my $totalDomains = 0;
25 rodolico 86
   my $maintenance = 0;
13 rodolico 87
   foreach my $node (sort keys %{ $main::statusDB->{'node'} } ) {
10 rodolico 88
      my $memory = 0;
89
      my $vcpus = 0;
90
      my $count = 0;
13 rodolico 91
      foreach my $domain ( keys %{ $main::statusDB->{'nodePopulation'}->{$node}->{'running'} } ) {
92
         $memory += $main::statusDB->{'virt'}->{$domain}->{'memory'};
93
         $vcpus += $main::statusDB->{'virt'}->{$domain}->{'vcpu'};
10 rodolico 94
         $count++;
95
      }
25 rodolico 96
      push @data, [ $node,$main::statusDB->{'node'}->{$node}->{cpu_count},$main::statusDB->{'node'}->{$node}->{memory},$count,$vcpus,$memory, $main::statusDB->{'node'}->{$node}->{maintenance} ? 'Maintenance' : 'Online' ];
10 rodolico 97
      $usedmem += $memory;
98
      $usedcpu += $vcpus;
99
      $totalDomains += $count;
13 rodolico 100
      $availmem += $main::statusDB->{'node'}->{$node}->{memory};
101
      $availcpu += $main::statusDB->{'node'}->{$node}->{cpu_count};
26 rodolico 102
      $maintenance += $main::statusDB->{'node'}->{$node}->{maintenance} ? 0 : 1;
10 rodolico 103
   } # outer for
25 rodolico 104
   push @data, [ 'Total',$availcpu,$availmem,$totalDomains,$usedcpu,$usedmem, $maintenance ];
10 rodolico 105
   return &main::report( \@header, \@data );
106
}
26 rodolico 107
 
108
# perform various functions on iSCSI target definitions
109
# on all nodes
110
 
111
 
112
sub iscsi {
113
   my $action = shift;
114
   my @return;
115
   if ( $action && $action eq 'add' ) {
116
      &main::readDB(1);
117
      while ( my $target = shift ) {
118
         $main::statusDB->{'cluster'}->{'iscsi'}->{$target} = '';
119
      }
120
      &main::writeDB();
121
   } elsif ( $action && $action eq 'delete' ) {
122
      my $target = shift;
123
      &main::readDB(1);
124
      delete $main::statusDB->{'cluster'}->{'iscsi'}->{$target} if exists $main::statusDB->{'cluster'}->{'iscsi'}->{$target};
125
      &main::writeDB();
126
   } elsif ( $action && $action eq 'update' ) {
127
      &main::readDB();
128
      # if they did not give us a node, do all of them
129
      @_ = keys %{ $main::statusDB->{'node'} } unless @_;
130
      while ( my $node = shift ) { # process each node on stack
131
         if ( $main::statusDB->{'node'}->{$node}->{'maintenance'} ) {
132
            print "Not processing node $node since it is in maintenance mode\n" if $main::config->{'flags'}->{'verbose'};
133
         } else { # actually do the work
134
            push @return, &updateISCITargets( $node );
135
         }
136
      } # while
137
   }
138
   &main::readDB();
139
   push @return, "iSCSI targets are";
140
   if ( $main::statusDB->{'cluster'}->{'iscsi'} ) {
141
      push @return, join( "\n",  keys %{ $main::statusDB->{'cluster'}->{'iscsi'} } );
142
   } else {
143
      push @return, "None Defined";
144
   }
145
   return join( "\n", @return ) . "\n";
146
}
147
 
148
# updates iSCSI targets on $node
149
# scans each target defined and compares it to the current session
150
# adding new targets if they exist
151
# NOTE: does not delete targets which no longer exist on server
152
sub updateISCITargets {
153
   my $node = shift;
154
   my $command;
155
   my %targets;
156
   my @return;
157
   push @return, "Processing iSCSI targets on $node";
158
   print Dumper( keys %{ $main::statusDB->{'cluster'}->{'iscsi'} } ) if $main::config->{'flags'}->{'debug'};
159
   foreach my $server (keys %{ $main::statusDB->{'cluster'}->{'iscsi'} } ) {
160
      print "\n" . '-'x40 . "\nGetting targets on server $server\n" . '-'x40 . "\n" if $main::config->{'flags'}->{'verbose'};
161
      $command = &main::makeCommand( $node, "iscsiadm -m discovery -t st -p $server" );
162
      my @list = `$command`;
163
      chomp @list;
164
      # @list contains lines of type
165
      # 10.19.209.2:3260,1 iqn.2014-11.net.dailydata.castor:simon0
166
      # split them apart and add them to the hash
167
      foreach my $entry ( @list ) {
168
         my ( $portal, $targetName ) = split( ' ', $entry );
169
         # $portal has some extra info after a comma, so clean it up
170
         $portal =~ m/^([0-9:.]+)/;
171
         $portal = $1;
172
         # some targets return multiple IP's for a given name, so 
173
         # only add them if they are in this IP
174
         $targets{ $targetName } = $portal if $portal =~ m/^$server/;
175
         print "$targetName\t$targets{ $targetName }\n" if $main::config->{'flags'}->{'verbose'};
176
      } # foreach
177
   } # while
178
   print "\n" . '-'x40 . "\nGetting active sessions\n". '-'x40 . "\n" if $main::config->{'flags'}->{'verbose'};
179
   # now, get active sessions so we can filter them
180
   $command = &main::makeCommand( $node, "iscsiadm -m session" );
181
   my @activeSessions = `$command`;;
182
   chomp @activeSessions;
183
   foreach my $session ( @activeSessions ) {
184
      $session =~ m/^.*[^0-9:.]([0-9,:.]+).*(iqn\S*)/;
185
      my ( $portal,$targetName ) = ( $1,$2 );
186
      print "$portal\t$targetName" if $main::config->{'flags'}->{'verbose'};
187
      if ( exists( $targets{$targetName} ) ) {
188
         print "\tNOT updating\n" if $main::config->{'flags'}->{'verbose'};
189
         delete $targets{ $targetName };
190
      } else {
191
         print "Needs to be added\n" if $main::config->{'flags'}->{'verbose'};
192
      }
193
   }
194
 
195
   # check if we have any new entries and bail if not
196
   if ( scalar keys %targets ) {
197
      # We have new entries, so run them;
198
      foreach my $targetName ( sort keys %targets ) {
199
         my $portal = $targets{$targetName};
200
         push @return, "Adding $targetName";
201
         $command = &main::makeCommand( $node, "iscsiadm -m node --targetname '$targetName' --portal '$portal' --login" );
202
         if ( $main::config->{'flags'}->{'dryrun'} ) {
203
            push @return, $command;
204
         } else {
205
          `$command`;
206
         }
207
      }
208
   } else {
209
      push @return, "No new entries";
210
   }
211
   return join( "\n", @return ) . "\n";
39 rodolico 212
} # updateISCITargets
213
 
214
sub balance {
215
   return "This function not implemented yet\n";
216
}