Subversion Repositories havirt

Rev

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

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