-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRebalance_VMware_Clients_to_Groups.pl
More file actions
463 lines (432 loc) · 20.5 KB
/
Rebalance_VMware_Clients_to_Groups.pl
File metadata and controls
463 lines (432 loc) · 20.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
#!/usr/bin/perl -w
$plan = @_;
# Set up email
$MAILADDRS='peter.reed.ctr\@navy.mil jeffrey.l.rodriguez.ctr\@navy.mil ashley.nguyen.ctr\@navy.mil blake.arcement.ctr\@navy.mil cody.p.crawford.ctr\@navy.mil';
#$MAILADDRS='peter.reed.ctr\@navy.mil';
$date = `/usr/bin/date '+%y%m%d%H%M%S'`;
chomp $date;
$filename = "/home/scriptid/scripts/BACKUPS/REBALANCE/rebalance\_$date";
open(MAIL,">$filename") or die "Can't open file $filename\n";
print MAIL "/usr/bin/mailx -s 'Rebalance VADP Groups on both servers ($date)' $MAILADDRS <<ENDMAIL\n";
# If plan is set to 'plan' then clients will not be moved the changes will just be documented
if (-t STDIN && -t STDOUT) {
# We are running from a terminal and not batch
if ( lc($plan) =~ /plan/) {
print "Plan=$plan\n";
} else {
print "Do you want to just see the plan of what will be done enter 'plan': ";
$plan = lc(<STDIN>);
chomp $plan;
}
}
if ($plan =~ /plan/) {
print MAIL "\n\n**************************************************************\n";
print MAIL " * No changes will be made. This is the plan for the changes *\n";
print MAIL "**************************************************************\n\n";
}
unshift (@INC,"/home/scriptid/scripts/BACKUPS/SUBROUTINES");
require append_group;
require build_output_record;
require create_group_name_from_esx_host_name;
require check_backups_on_other_server;
require handle_nsradmin_line_continuations;
require is_client_running_jobquery;
require pinger;
require read_tabvInfo_old;
require remove_clients_from_group;
require resolv_name;
require start_group;
require stop_group;
require show_group;
require testport;
require update_clients_group;
# Purpose of utility is to find new clients and to move Windows clients into groups based
# on the blade that they are loaded. We will only move clients that are in the VADP groups.
# If VMware clients are larger than "600GB' they will be backed up under a LARGE group.
# If VMware clients are Linux they go into a normal group.
# The following tags are in the comment field in NetWorker.
# A:; - AFTD
# D:; - decom
# L:; - Linux
# U:; - UNIX
# V:; - Open VMS
# W:; - Windows
# P:; - Production
# I:; - Ignore
# N:; - No Automatic Reruns
# Clients are added to backups if the are up, in DNS, and responding on port 7937
# Clients are moved if they are in NORMAL, LARGE, or VADP groups.
# Clients can also be moved based on the time that they take to backup.
# NORMAL groups might not be required, thinking they should be merged into the VADP groups helping
# reduce the load on the VMware infrastructure. Needs to be looked at.
# First pass tuning will be to connect all clients in the same group based on their blade.
# Parallelism needs to be determined to minimize snapshot load (we have memory size and disk space size
# and even the LUNs).
# Handle groups as a client update
#
#------------------------------------------------------------------------------------------------------------
#
$BACKUPSERVER = 'sscprodeng';
$ALTERNATESERVER = 'sscprodeng2'; # If blank then only uses BACKUPSERVER
(@servers) = ($BACKUPSERVER,$ALTERNATESERVER);
#################################################################################################################################
# Set up rap logs to track changes
#################################################################################################################################
$date = `/usr/bin/date '+%y%m%d%H%M%S'`;
chomp $date;
open (RAP,">>/home/scriptid/scripts/BACKUPS/RAP/VADP_RAP_$date.log") or die "Could not open /home/scriptid/scripts/BACKUPS/RAP/VADP_RAP_$date.log\n";
#################################################################################################################################
# Need to compute totalsize once and only want to compute it on the server on which it is scheduled, it could have been moved
# Don't need to include on the server machines that are not scheduled, that are Decom'd, that are not VADP
# Nothing this script does changes the above
print MAIL "\n\n*** R e b a l a n c e V M w a r e C l i e n t s t o G r o u p s (Version 3.2.6.0) ***\n\n";
print MAIL "Check /home/scriptid/scripts/BACKUPS/RAP/VADP_RAP_$date.log for a list of all clients and why they were excluded...\n\n";
print MAIL "Building list of VADP servers in backups\n";
foreach $server (@servers) {
$total_server{$server} = 0;
print MAIL "\tBuilding server list for $server\n";
$nsrpass = ". type:NSR client'\n'show name\\;scheduled backup\\;group\\;backup command\\;action'\n'print";
(@return) = handle_nsradmin_line_continuations($server,$nsrpass); # Get list back from nsradmin but concatenate long lines like group and comment
print MAIL "\t\tNumber of records returned = $#return\n";
for ($i=0; $i<=$#return; $i++) {
next if $return[$i] =~ /^\s*$/; # Skip blank lines
if ($return[$i] =~ /name/) {
if ($return[$i+1] =~ /Disabled/) { # Scheduled Backup
print RAP "INFO: Skipped client $return[$i] because it is disabled\n";
$i+=3;
next;
}
if ($return[$i+2] =~ /DECOM/) { # Group
print RAP "INFO: Skipped client $return[$i] because it is in the DECOMgroup\n";
$i+=3;
next;
}
if ($return[$i+2] =~ /MIDNIGHT/) { # Group
print RAP "INFO: Skipped client $return[$i] because it is in the MIDNIGHT Group\n";
$i+=3;
next;
}
if ($return[$i+2] =~ /BigandSlow/) { # Group
print RAP "INFO: Skipped client $return[$i] because it is in the BigandSlow Group\n";
$i+=3;
next;
}
if ($return[$i+2] =~ /Nuclear/) { # Group
print RAP "INFO: Skipped client $return[$i] because it is in a Nuclear Group\n";
$i+=3;
next;
}
if ($return[$i+2] =~ /PROBLEMS/) { # Group
print RAP "INFO: Skipped client $return[$i] because it is in the PROBLEMS Group\n";
$i+=3;
next;
}
if ($return[$i+2] !~ /VADP/) { # Only process VADP Groups
print RAP "INFO: Skipped client $return[$i] because it's not VADP\n";
if ($return[$i+2] =~ /group: \;/) { # Group
print RAP "WARN: client $return[$i] is in NO Group on $server and won't be automatically moved\n";
print MAIL "WARN: client $return[$i] is in NO Group on $server and won't be automatically moved\n";
print "WARN: client $return[$i] is in NO Group on $server and won't be automatically moved\n";
}
if ($return[$i+2] =~ /group: INDEX\;/) { # Group
print RAP "WARN: client $return[$i] is only in group INDEX on $server and won't be automatically moved\n";
print MAIL "WARN: client $return[$i] is only in group INDEX on $server and won't be automatically moved\n";
print "WARN: client $return[$i] is only in group INDEX on $server and won't be automatically moved\n";
}
$i+=3;
next;
}
if ($return[$i+3] !~ /nsrvadp_save/) { # backup command
print RAP "INFO: Skipped client $return[$i] because it doesn't have nsrvadp_save set\n";
$i+=3;
next;
}
# Save this info
($client) = ($return[$i] =~ /^.*name: (.*)\;$/);
$client = lc($client); # Lower case clients
if (defined $which_server{$client}) {
print MAIL "ERROR: $client enabled on both backup servers\n";
$i+=4;
} else {
$which_server{$client} = $server;
$total_server{$server} +=1;
$i+=2;
#chomp $return[$i];
($which_group{$client}) = ($return[$i] =~ /^.*group: (.*)\;$/);
$i+=2;
}
}
}
# Build a list of the max size for each server
(@return) = `/usr/sbin/mminfo -s $server -r 'client,name,totalsize' -q 'level=full,savetime>last month'`;
# This utility is used to determine the maximum size of a client full backup
#print MAIL " Building max backups for clients on $server\n";
foreach $record (@return) {
next if $record =~ /client/;
chomp $record;
($client,$name,$totalsize) = split(' ',$record);
next if $name =~ /index/;
next if $totalsize =~ /ROLES/;
next if $totalsize =~ /SYSTEM/;
next if $totalsize =~ /ASR/;
next if $totalsize =~ /FILES/;
next if $totalsize =~ /OTHER/;
next if $totalsize =~ /USER/;
next if $totalsize =~ /DB:/;
next if $totalsize =~ /STATE:/;
#print "$client,$name,$totalsize\n";
$index = "$client|$name"; # Index is client and filesystem
if (!defined $max{$index} ) {
$max{"$index"} = $ totalsize;
} else {
if ($totalsize > $max{"$index"}) { $max{"$index"} = $totalsize };
}
}
# Now process records to determine max full per client
foreach $key (sort keys (%max)) {
($client,$name) = split(/\|/,$key);
$client = lc($client);
if (!defined $total{$client}) {
$total{$client} = $max{$key};
} else {
$total{$client} += $max{$key};
}
}
# total is indexed by lowercase client
# which_server now contains a list of all clients and which server backs it up.
print MAIL "\t\tThere are $total_server{$server} VADP clients on server $server\n";
}
#################################################################################################################################
# Get info from RvTools
# RvTools has all the x86 Virtuals Linux, Windows, VADP backups
# All indexed by lowercase client
print "\nBefore Read of Virtual Center Information\n";
read_tabvInfo_old();
print "After Read of Virtual Center Information\n\n";
#################################################################################################################################
# Process by existing VADP BAckup clients
$count = 0;
$total_VMs = 0;
$return = build_output_record(-80,$output,'CLIENT',18,0,-1,-1 ,'',1,18);
$return = build_output_record(0,$output,'LEVEL',10,0,1,1 ,'',19,29);
$header2= build_output_record(0,$output,'MESSAGE',51,0,0,0 ,'',30,80);
print MAIL "$header2\n";
our %Host;
foreach $key (sort keys (%which_server)) {
if ($count == 100) {
$return = build_output_record(-80,$output,'Processing',18,0,-1,-1 ,'',1,18);
$return = build_output_record(0,$output,'INFO',10,0,1,1 ,'',19,29);
$return = build_output_record(0,$output,"*** Virtual Machine $total_VMs ***",51,0,0,0 ,'',30,80);
print MAIL "$return\n";
$count = 0;
}
$count +=1;
$total_VMs+=1;
if (!defined $Powerstate{$key}) {
$return = build_output_record(-80,$output,$key,18,0,-1,-1 ,'',1,18);
$return = build_output_record(0,$output,'WARN',10,0,1,1 ,'',19,29);
$return = build_output_record(0,$output,'The Powerstate is undefined',51,0,0,0 ,'',30,80);
print MAIL "$return\n";
next;
} elsif ($Powerstate{$key} =~ /poweredOff/) {
$return = build_output_record(-80,$output,$key,18,0,-1,-1 ,'',1,18);
$return = build_output_record(0,$output,'WARN',10,0,1,1 ,'',19,29);
$return = build_output_record(0,$output,'The VM is powered off and will be ignored',51,0,0,0 ,'',30,80);
print MAIL "$return\n";
next;
}
# Check to see if the client can be backed up
$dns = resolv_name($key);
if ($dns =~ /Not in DNS/) {
$return = build_output_record(-80,$output,$key,18,0,-1,-1 ,'',1,18);
$return = build_output_record(0,$output,'ERROR',10,0,1,1 ,'',19,29);
$return = build_output_record(0,$output,'The Client is not in DNS and will be skipped',51,0,0,0 ,'',30,80);
print MAIL "$return\n";
delete $which_group{$key};
delete $which_server{$key};
next;
} elsif ($which_group{$key} =~ /NavyNuclear/ ) {
$return = build_output_record(-80,$output,$key,18,0,-1,-1 ,'',1,18);
$return = build_output_record(0,$output,'INFO',10,0,1,1 ,'',19,29);
$return = build_output_record(0,$output,'Client in Navy Nuclear group and will be skipped',51,0,0,0 ,'',30,80);
print MAIL "$return\n";
delete $which_group{$key};
delete $which_server{$key};
next;
} else {
$ping = pinger($key);
if ($ping =~ /up/) {
$port = testport($key,7937);
if ($port =~ 'Client listening') {
########################################################################################
# These are tests to determine whether to use VADP or Not
# The server is in DNS is up and listening on the port and it needs to be added
########################################################################################
if (!defined $total{$key}) {
$return = build_output_record(-80,$output,$key,18,0,-1,-1 ,'',1,18);
$return = build_output_record(0,$output,'ERROR',10,0,1,1 ,'',19,29);
$return = build_output_record(0,$output,'The Client is not being backed up',51,0,0,0 ,'',30,80);
print MAIL "$return\n";
next;
} else {
if ($total{$key} > 644245094400) {
delete $which_group{$key};
delete $which_server{$key};
$return = build_output_record(-80,$output,$key,18,0,-1,-1 ,'',1,18);
$return = build_output_record(0,$output,'WARN',10,0,1,1 ,'',19,29);
$return = build_output_record(0,$output,'Client is > 600GB and will not be moved',51,0,0,0 ,'',30,80);
print MAIL "$return\n";
next;
}
}
} else {
$port = "Not listening on port 7937";
$return = build_output_record(-80,$output,$key,18,0,-1,-1 ,'',1,18);
$return = build_output_record(0,$output,'WARN',10,0,1,1 ,'',19,29);
$return = build_output_record(0,$output,"Client is not listening on port 7937",51,0,0,0 ,'',30,80);
print MAIL "$return\n";
}
} else {
$return = build_output_record(-80,$output,$key,18,0,-1,-1 ,'',1,18);
$return = build_output_record(0,$output,'WARN',10,0,1,1 ,'',19,29);
$return = build_output_record(0,$output,"$port, $dns, won't ping",51,0,0,0 ,'',30,80);
print MAIL "$return\n";
}
}
# Only trying to find what ESX host the client is on
# $ key is the client, $HOST{$key} is the IP address of the blade the client is on
$hostname = reverse_lookup($Host{$key},$Host{$key});
if ($hostname =~ /No DNS Name/) {
$return = build_output_record(-80,$output,$key,18,0,-1,-1 ,'',1,18);
$return = build_output_record(0,$output,'ERROR',10,0,1,1 ,'',19,29);
$return = build_output_record(0,$output,"No DNS entry for blade $Host{$key}",51,0,0,0 ,'',30,80);
print MAIL "$return\n";
delete $which_group{$key};
delete $which_server{$key};
} else {
$hostname =~ s/\..*$//;
($dest_grpname{$key}) = create_group_name_from_esx_host_name($hostname);
# Check to see the the destination group exists and is enabled
my $nsrpass = ". type: NSR group\\;name:$dest_grpname{$key}'\n'show autostart\\;start time\\;schedule time'\n'print";
(@return3) = `/usr/bin/echo $nsrpass | /usr/sbin/nsradmin -s $which_server{$key} -i - `;
foreach $vvv (@return3) {
next if $vvv =~ /Current query set/;
if ($vvv =~ /No resources found/) {
$zone = lc $Cluster{$key};
$zone =~ s/.*zone//;
if ( $zone =~ /^\s*$/) {$zone = 1};
print RAP "ERROR: The Group $dest_grpname{$key}(Reverse DNS $Host{$key}) Zone $zone for $key doesn't exist on $which_server{$key} and needs to be created\n";
print MAIL "ERROR: The Group $dest_grpname{$key}(Reverse DNS $Host{$key}) Zone $zone for $key doesn't exist on $which_server{$key} and needs to be created\n";
last;
} elsif ($vvv =~ /autostart: Disabled/) {
print RAP "ERROR: Group $dest_grpname{$key} isn't enabled but client $key is moved\n";
print MAIL "ERROR: Group $dest_grpname{$key} isn't enabled but client $key is moved\n";
last;
} elsif ($vvv =~ /autostart: Disabled/) {
print RAP "ERROR: Group $dest_grpname{$key} isn't enabled but client $key is moved\n";
print MAIL "ERROR: Group $dest_grpname{$key} isn't enabled but client $key is moved\n";
last;
} elsif ($vvv =~ /schedule time: \S+\;/) {
print RAP "WARN: Group $dest_grpname{$key} has scheduled time set\n";
print MAIL "WARN: Group $dest_grpname{$key} has scheduled time set\n";
last;
}
}
if ($which_group{$key} =~ /$dest_grpname{$key}/) {
# The destination group matches the source group
delete $which_group{$key};
delete $which_server{$key};
#$return = build_output_record(-80,$output,$key,18,0,-1,-1 ,'',1,18);
#$return = build_output_record(0,$output,'INFO',10,0,1,1 ,'',19,29);
#$return = build_output_record(0,$output,"Destination group same as Source no change",51,0,0,0 ,'',30,80);
#print MAIL "$return\n";
}
}
}
#################################################################################################################################
print MAIL "\n";
$return = build_output_record(-106,$output,'CLIENT',18,0,-1,-1 ,'',1,18);
$return = build_output_record(0,$output,'BACKUP SERVER',14,0,1,1 ,'',23,36);
$header2= build_output_record(0,$output,'SOURCE/DESTINATION GROUP',25,0,0,0 ,'',36,106);
print MAIL "$header2\n";
$dashes = '-' x 106;
$return = build_output_record(-106,$output,$dashes,106,0,-1,-1 ,'',1,106);
print MAIL "$return\n";
$i = 0;
foreach $val (sort keys %which_group) {
#print "\n\nKey of which=$val***\n";
#print "value of which=$which_group{$val}***\n";
# Host is the ip address of the ESX host
#print "Host=$Host{$val}***\n";
if (!defined $dest_grpname{$val}) {
#print "Destination group not defined for VM ***$val***\n";
$dest_grpname{$val} = ' ';
} else {
#print "Destination group name=$dest_grpname{$val}\n";
}
#($source_group) = ($which_group{$val} =~ /.*(VADP-\S+)[,;].*$/);
$source_group = $which_group{$val};
if ($source_group =~ /^\s*$/) {
print MAIL "Source Group undefined or blank\n";
}
# Check to see if the destination group is the same as the source group
if ($source_group =~ /$dest_grpname{$val}/) {next};
@source_array = split(/, /,$source_group);
undef @dest_array;
foreach $val (@source_array) {
if ($val =~ /^VADP-/) {next};
push (@dest_array,$val);
}
# Make sure that INDEX is in the group
if ( grep(/INDEX/,@dest_array) ) {
# Has INDEX Group nothing required
} else {
push (@dest_array,'INDEX');
}
push (@dest_array,$dest_grpname{$val});
# Put the groups together separated by comma space
$dest_group = join(", ",@dest_array);
$return = build_output_record(-106,$output,$i,3,0,1,1 ,'',1,3);
$return = build_output_record(0,$output,$val,18,0,-1,-1 ,'',6,23);
$return = build_output_record(0,$output,'SOURCE',10,0,0,0 ,'',24,35);
$return = build_output_record(0,$output,$source_group,70,0,0,0 ,'',36,106);
print MAIL "$return\n";
$return = build_output_record(-106,$output,$which_server{$val},11,0,0,0 ,'',24,35);
$return = build_output_record(0,$output,$dest_group,70,0,0,0 ,'',36,106);
print MAIL "$return\n";
$i++;
#last if ($i > 120);
# UPDATE CLIENT moving it to new group
if ($plan !~ /plan/) {
#print RAP "INFO: Moved client:$val from group $which_server{$val} to group $dest_group\n";
print RAP "INFO: Moved client:$val from group sscprodeng2 to group $dest_group\n";
#update_clients_group ($which_server{$val}, $val, $dest_group);
update_clients_group ($which_server{$val}, $val, $dest_group);
}
$return = build_output_record(-106,$output,$dashes,106,0,-1,-1 ,'',1,106);
print MAIL "$return\n";
}
print MAIL "ENDMAIL\n";
$return = `/usr/bin/sh $filename > /dev/null 2>&1`;
close MAIL;
close RAP;
sub reverse_lookup {
my ($check,$key) = @_;
my $IP = $check;
$name = "No DNS Name";
if ($IP !~ /\./) {
print MAIL "WARN: No reverse lookup for $key\n";
} else {
use Socket;
$iaddr = inet_aton("$IP");
$name = gethostbyaddr($iaddr, AF_INET);
if ( !defined $name ) {
$return = build_output_record(-80,$output,$check,18,0,-1,-1 ,'',1,18);
$return = build_output_record(0,$output,'ERROR',10,0,1,1 ,'',19,29);
$return = build_output_record(0,$output,"No reverse lookup for $key",51,0,0,0 ,'',30,80);
print MAIL "$return\n";
$name = "No DNS Name";
}
}
return $name;
}