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";
|
26 |
rodolico |
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 |
|
11 |
rodolico |
71 |
return join( "\n", @return ) . "\n";
|
|
|
72 |
}
|
|
|
73 |
|
10 |
rodolico |
74 |
sub status {
|
|
|
75 |
my $return = '';
|
13 |
rodolico |
76 |
&main::readDB();
|
25 |
rodolico |
77 |
my @header = ('Node','Threads','Memory','Domains','vcpu','mem_used', 'Status' );
|
10 |
rodolico |
78 |
my @data;
|
|
|
79 |
my $usedmem = 0;
|
|
|
80 |
my $usedcpu = 0;
|
|
|
81 |
my $availmem = 0;
|
|
|
82 |
my $availcpu = 0;
|
|
|
83 |
my $totalDomains = 0;
|
25 |
rodolico |
84 |
my $maintenance = 0;
|
13 |
rodolico |
85 |
foreach my $node (sort keys %{ $main::statusDB->{'node'} } ) {
|
10 |
rodolico |
86 |
my $memory = 0;
|
|
|
87 |
my $vcpus = 0;
|
|
|
88 |
my $count = 0;
|
13 |
rodolico |
89 |
foreach my $domain ( keys %{ $main::statusDB->{'nodePopulation'}->{$node}->{'running'} } ) {
|
|
|
90 |
$memory += $main::statusDB->{'virt'}->{$domain}->{'memory'};
|
|
|
91 |
$vcpus += $main::statusDB->{'virt'}->{$domain}->{'vcpu'};
|
10 |
rodolico |
92 |
$count++;
|
|
|
93 |
}
|
25 |
rodolico |
94 |
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 |
95 |
$usedmem += $memory;
|
|
|
96 |
$usedcpu += $vcpus;
|
|
|
97 |
$totalDomains += $count;
|
13 |
rodolico |
98 |
$availmem += $main::statusDB->{'node'}->{$node}->{memory};
|
|
|
99 |
$availcpu += $main::statusDB->{'node'}->{$node}->{cpu_count};
|
26 |
rodolico |
100 |
$maintenance += $main::statusDB->{'node'}->{$node}->{maintenance} ? 0 : 1;
|
10 |
rodolico |
101 |
} # outer for
|
25 |
rodolico |
102 |
push @data, [ 'Total',$availcpu,$availmem,$totalDomains,$usedcpu,$usedmem, $maintenance ];
|
10 |
rodolico |
103 |
return &main::report( \@header, \@data );
|
|
|
104 |
}
|
26 |
rodolico |
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
|