forked from mogilefs/MogileFS-Utils
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mogadm
executable file
·1740 lines (1412 loc) · 48.9 KB
/
mogadm
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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/perl
# vim:ts=4 sw=4 ft=perl et:
use strict;
use warnings;
use Getopt::Long;
use LWP::Simple; # FIXME: use of this makes 'mog check' hang too long when multiple things down
use Socket;
my @topcmds = qw(check stats host device domain class slave fsck rebalance settings);
my $usage = {
check => {
des => "Check the state of the MogileFS world.",
},
stats => {
des => "Show MogileFS system statistics. (DEPRECATED: use mogstats instead)",
},
settings => {
des => "Change/list server settings.",
subcmd => {
list => {
des => "List all server settings",
},
set => {
args => "<key> <value>",
des => "Set server setting 'key' to 'value'.",
},
},
},
host => {
des => "Add/modify hosts.",
subcmd => {
list => {
des => "List all hosts.",
},
add => {
des => "Add a host to MogileFS.",
args => "<hostname> [opts]",
opts => {
"<hostname>" => "Hostname of machine",
"--status=s" => "One of {alive,down,readonly}. Default 'down'.",
"--ip=s" => "IP address of machine.",
"--port=i" => "HTTP port of mogstored",
"--getport=i" => "Alternate HTTP port serving readonly traffic",
"--altip=s" => "Alternate IP that is machine is reachable from",
"--altmask=s" => "Netmask which, when matches client, uses alt IP",
},
},
modify => {
des => "Modify a host's properties.",
args => "<hostname> [opts]",
opts => {
"<hostname>" => "Host name.",
"--status=s" => "One of {alive,down,readonly}.",
"--ip=s" => "IP address of machine.",
"--port=i" => "HTTP port of mogstored",
"--getport=i" => "Alternate HTTP port serving readonly traffic",
"--altip=s" => "Alternate IP that is machine is reachable from",
"--altmask=s" => "Netmask which, when matches client, uses alt IP",
},
},
mark => {
des => "Change the status of a host. (equivalent to 'modify --status')",
args => "<hostname> <status>",
opts => {
"<hostname>" => "Host name to bring up or down.",
"<status>" => "One of {alive,down,readonly}.",
}
},
delete => {
des => "Delete a host.",
args => "<hostname>",
opts => {
"<hostname>" => "Host name to delete.",
},
},
},
},
device => {
des => "Add/modify devices.",
subcmd => {
list => {
des => "List all devices, for each host.",
args => "[opts]",
opts => {
"--all" => "Include dead devices in list.",
"--hostname=s" => "Limit results to a given host.",
},
},
summary => {
des => "List the summary of devices, for each host.",
args => "[opts]",
opts => {
"--status=s" => "Devices of status A. Defaults to 'alive,readonly,drain'",
"--hostname=s" => "Limit results to a given host.",
},
},
add => {
des => "Add a device to a host.",
args => "<hostname> <devid> [opts]",
opts => {
"<hostname>" => "Hostname to add a device",
"<devid>" => "Numeric devid. Never reuse these.",
"--status=s" => "One of 'alive' or 'down'. Defaults to 'alive'.",
},
},
mark => {
des => "Mark a device as {alive,dead,down,drain,readonly}",
args => "<hostname> <devid> <status>",
opts => {
"<hostname>" => "Hostname of device",
"<devid>" => "Numeric devid to modify.",
"<status>" => "One of {alive,dead,down,drain,readonly}",
},
},
modify => {
des => "Modify a device's properties.",
args => "<hostname> <devid> [opts]",
opts => {
"<hostname>" => "Hostname of device",
"<devid>" => "Numeric devid to modify.",
"--status=s" => "One of {alive,dead,down,drain,readonly}",
"--weight=i" => "Positive numeric weight for device",
},
},
next => {
des => "Show the next available devid.",
},
},
},
domain => {
des => "Add/modify domains (namespaces)",
subcmd => {
list => {
des => "List all hosts.",
},
add => {
des => "Add a domain (namespace)",
args => "<domain>",
opts => {
"<domain>" => "Domain (namespace) to add.",
},
},
delete => {
des => "Delete a domain.",
args => "<domain>",
opts => {
"<domain>" => "Domain (namespace) to add.",
},
},
},
},
class => {
des => "Add/modify file classes.",
subcmd => {
list => {
des => "List all classes, for each domain.",
},
add => {
des => "Add a file class to a domain.",
args => "<domain> <class> [opts]",
opts => {
"<domain>" => "Domain to add class to.",
"<class>" => "Name of class to add.",
"--mindevcount=i" => "Minimum number of replicas.",
"--replpolicy=s" => "Replication policy string.",
"--hashtype=s" => "Hash algorithm string ('MD5', 'NONE').",
},
},
modify => {
des => "Modify properties of a file class.",
args => "<domain> <class> [opts]",
opts => {
"<domain>" => "Domain to add class to.",
"<class>" => "Name of class to add.",
"--mindevcount=i" => "Minimum number of replicas.",
"--replpolicy=s" => "Replication policy string.",
"--hashtype=s" => "Hash algorithm string ('MD5', 'NONE').",
},
},
delete => {
des => "Delete a file class from a domain.",
args => "<domain> <class>",
opts => {
"<domain>" => "Domain of class to delete.",
"<class>" => "Class to delete.",
},
},
},
},
slave => {
des => 'Manipulate slave database information in a running mogilefsd.',
subcmd => {
list => {
des => 'List current store slave nodes.',
},
add => {
des => 'Add a slave node for store usage',
args => '<slave_key> [opts]',
opts => {
'--dsn=s' => "DBI DSN specifying what database to connect to.",
'--username=s' => "DBI username for connecting.",
'--password=s' => "DBI password for connecting.",
},
},
modify => {
des => 'Modify a slave node for store usage',
args => '<slave_key> [opts]',
opts => {
'--dsn=s' => "DBI DSN specifying what database to connect to.",
'--username=s' => "DBI username for connecting.",
'--password=s' => "DBI password for connecting.",
},
},
delete => {
des => 'Delete a slave node for store usage',
args => '<slave_key>',
},
},
},
rebalance => {
des => "Control file rebalancing operations.",
subcmd => {
start => {
des => 'Start a rebalance job',
},
stop => {
des => 'Stop a rebalance job',
},
status => {
des => 'Show status of current rebalance job',
},
settings => {
des => 'Display rebalance settings',
},
test => {
des => 'Show what devices the current policy would match',
},
reset => {
des => 'Reset an existing policy',
},
policy => {
des => 'Add or adjust the current policy',
args => '[opts]',
opts => {
'--options=s' => "Policy string (see docs/wiki for details)",
},
},
},
},
fsck => {
des => "Control a background filesystem check operation.",
subcmd => {
start => {
des => 'Start (or resume) background fsck',
},
stop => {
des => 'Stop (pause) background fsck',
},
status => {
des => 'Show fsck status',
},
reset => {
des => 'Reset fsck position back to the beginning',
args => '[opts]',
opts => {
'--policy-only' => "Check repl policy (assumed locations); don't stat storage nodes",
'--startpos=i' => "FID to start at.",
}
},
clearlog => {
des => 'Clear the fsck log',
},
printlog => {
des => 'Display the fsck log',
},
taillog => {
des => 'Tail the fsck log',
},
},
},
};
# load up our config files
my %opts;
Getopt::Long::Configure("require_order", "pass_through");
GetOptions(
"trackers=s" => \$opts{trackers},
"config=s" => \$opts{config},
"lib=s" => \$opts{lib},
"help" => \$opts{help},
"verbose" => \$opts{verbose},
) or abortWithUsage();
Getopt::Long::Configure("require_order", "no_pass_through");
my @configs = ($opts{config}, "$ENV{HOME}/.mogilefs.conf", "/etc/mogilefs/mogilefs.conf");
foreach my $fn (reverse @configs) {
next unless $fn && -e $fn;
open FILE, "<$fn"
or die "unable to open $fn: $!\n";
while (<FILE>) {
s/\#.*//;
next unless m!^\s*(\w+)\s*=\s*(.+?)\s*$!;
$opts{$1} = $2 unless ( defined $opts{$1} );
}
close FILE;
}
# bail for help
abortWithUsage() if $opts{help};
# make sure we have at least a topcmd
my $topcmd = shift(@ARGV);
abortWithUsage() unless $topcmd && $usage->{$topcmd};
# break up the trackers and ensure we got some
if ($opts{trackers}) {
$opts{trackers} = [ split(/\s*,\s*/, $opts{trackers}) ];
}
fail_text('no_trackers')
unless ($opts{trackers} && @{$opts{trackers}}) || detect_local_tracker();
# okay, load up the libraries that we need
if ($opts{lib}) {
eval "use lib '$opts{lib}';";
}
eval "use MogileFS::Admin; use MogileFS::Client; 1;" or fail_text('cant_find_module');
# dispatch if it's special
if ($topcmd eq 'check') {
die "Unknown options/arguments to 'check' command.\n" if @ARGV;
cmd_check();
} elsif ($topcmd eq 'stats') {
die "Unknown options/arguments to 'stats' command.\n" if @ARGV;
cmd_stats();
}
# get the verb
my $verb = shift(@ARGV) or
abort_with_topcmd_help($topcmd);
my $cmdinfo = $usage->{$topcmd}{subcmd}{$verb};
abort_with_topcmd_help($topcmd) unless $cmdinfo;
my $badargs = sub {
my $msg = shift;
abort_with_topcmd_help($topcmd, $verb, $msg);
};
# get the non-option (non --foo) arguments:
my %cmdargs;
if (my $args = $cmdinfo->{args}) {
my @args = split(/ /, $args);
foreach my $arg (@args) {
# positional (but named) parameter
if ($arg =~ /^<(.+)>$/) {
my $argname = $1;
my $val = shift @ARGV;
# map e.g. "dev5" to 5
if ($argname eq "devid" && $val && $val =~ /^dev(\d+)$/) {
$val = $1;
}
$badargs->("Missing argument '$argname'") unless defined $val;
$badargs->("Unexpected option. Expected argument '$argname'") if $val =~ /^-/;
$cmdargs{$argname} = $val;
} elsif ($arg eq "[opts]") {
# handled later.
} else {
die "INTERNAL ERROR.";
}
}
$badargs->("Unexpected extra argument.") if @ARGV && $ARGV[0] !~ /^-/;
} else {
$badargs->("Unexpected arguments when expecting none.") if @ARGV;
}
# parse the options
if (my $opts = $cmdinfo->{opts}) {
my %getopts;
foreach (keys %$opts) {
my $k = $_;
next if $k =~ /^</; # don't care about these.
die "BOGUS KEY: '$k'" unless $k =~ /^--([\w\-]+)(=.+)?$/;
my ($oname, $type) = ($1, $2 || "");
$getopts{"$oname$type"} = \$cmdargs{$1};
}
GetOptions(%getopts)
or abort_with_topcmd_help($topcmd, $verb);
}
# see what we should do
my $cmdsub = do {
no strict 'refs';
*{"cmd_${topcmd}_${verb}"} or abortWithUsage();
};
# now call our lovely lovely sub
$cmdsub->(\%cmdargs);
exit 0;
sub detect_local_tracker {
require IO::Socket::INET;
my $loctrack = "127.0.0.1:7001";
my $sock = IO::Socket::INET->new(PeerAddr => $loctrack, Timeout => 1);
return 0 unless $sock;
$opts{trackers} = [$loctrack];
return 1;
}
###########################################################################
## command routines
###########################################################################
sub cmd_check {
# step 1: we want to check each tracker for responsiveness
my $now = time();
my ($hosts, $devices);
$| = 1;
print "Checking trackers...\n";
foreach my $t (@{$opts{trackers}}) {
print " $t ... ";
my $mogadm = mogadm($t);
if ($mogadm) {
my $lhosts = hosts($mogadm);
my $ldevs = devices($mogadm);
if ($lhosts && $ldevs) {
print "OK\n";
$hosts = $lhosts;
$devices = $ldevs;
} else {
print "REQUEST FAILURE (is the tracker up?)\n";
}
} else {
print "INITIAL FAILURE (bad configuration?)\n";
}
}
# we should have hosts if we get here
fail_text('no_hosts') unless $hosts;
print "\n";
# step 2: now hit each of the hosts for responsiveness
print "Checking hosts...\n";
my @urls;
foreach my $hostid (sort { $a <=> $b } keys %$hosts) {
printf " [%2d] %s ... ", $hostid, $hosts->{$hostid}->{hostname};
if ($hosts->{$hostid}->{status} eq 'alive') {
my $url = 'http://' . $hosts->{$hostid}->{hostip} . ':' . $hosts->{$hostid}->{http_port} . '/';
my $file = get($url);
if (defined $file) {
print "OK\n";
push @urls, [ $hostid, $url ];
} else {
print "REQUEST FAILURE FETCHING: $url\n";
}
} else {
print "skipping; status = $hosts->{$hostid}->{status}\n";
}
}
# everything should be chill
fail_text('no_devices') unless @urls;
print "\n";
# step 3: check devices for each host
print "Checking devices...\n";
printf " host device %10s %10s %10s %7s %7s %4s\n", 'size(G)', 'used(G)', 'free(G)', 'use% ', 'ob state', 'I/O%';
printf " ---- ------------ ---------- ---------- ---------- ------ ---------- -----\n";
my %total;
# Initialize to zero so that the total outputs doesn't need to check for undefined.
map { $total{$_} = 0; } qw(total used avail);
foreach my $hosturl (@urls) {
my ($hostid, $url) = @$hosturl;
my $devs = $devices->{$hostid};
DEV: foreach my $devid (sort { $a <=> $b } keys %$devs) {
my $dev = $devs->{$devid};
my $status = $dev->{status} || "??";
next if $status eq "dead";
printf " [%2d] %-7s", $hostid, "dev$devid";
my $usage = get($url . "/dev$devid/usage");
if (! defined $usage) {
print "REQUEST FAILURE FETCHING: $url" . "dev$devid/usage\n";
next;
}
if (length($usage) < 1) {
print "USAGE FILE BROKEN OR EMPTY: $url" . "dev$devid/usage\n";
next;
}
my %data = ( map { split(/:\s+/, $_) } split(/\r?\n/, $usage) );
foreach (qw(time used total avail)) {
$data{$_} = 0 if (!$data{$_} ||
$data{$_} !~ /\A\d+(\.\d+)?\Z/);
}
foreach (qw(available device disk)) {
if (! exists $data{$_} || !$data{$_}) {
print "MISSING FIELD ($_) FROM USAGE FILE: $url" . "dev$devid/usage\n";
next DEV;
}
}
$data{age} = $now - $data{time};
$data{used} /= 1024**2;
$data{total} /= 1024**2;
$data{available} /= 1024**2;
$data{avail} = $data{available};
my $pct = 100 - $data{available}/$data{total}*100;
$total{used} += $data{used};
$total{avail} += $data{avail};
$total{total} += $data{total};
my $util = "N/A";
if (defined($dev->{utilization}) && $dev->{utilization} =~ /\A\d+(\.\d+)?\Z/) {
$util = sprintf("%.1f", $dev->{utilization});
}
printf(" %10.3f %10.3f %10.3f %6.2f%% %-7s %5s\n",
(map { $data{$_} } qw(total used avail)),
$pct, ($dev->{observed_state} || "?"),
$util);
}
}
my $pct = 0;
# Avoid division by zero
$pct = 100 - $total{avail}/$total{total}*100 if($total{total} > 0);
printf " ---- ------------ ---------- ---------- ---------- ------\n";
printf " total:%10.3f %10.3f %10.3f %6.2f%%\n", (map { $total{$_} } qw(total used avail)), $pct;
# if we get here, all's well
ok();
}
sub cmd_stats {
fail("mogadm stats is deprecated by new 'mogstats' utility");
}
sub cmd_host_list {
my $hosts = hosts();
fail_text('no_hosts') unless $hosts;
foreach my $hostid (sort keys %$hosts) {
my $host = $hosts->{$hostid};
print "$host->{hostname} [$hostid]: $host->{status}\n";
my @data = (
'IP', "$host->{hostip}:$host->{http_port}",
'Alt IP', $host->{altip},
'Alt Mask', $host->{altmask},
'GET Port', $host->{http_get_port},
);
while (my ($k, $v) = splice(@data, 0, 2)) {
next unless $v;
printf " %-10s\%s\n", "$k:", $v;
}
print "\n";
}
ok();
}
sub cmd_host_add {
my $args = shift;
my $hosts = hosts_byname();
fail_text('no_hosts') unless $hosts;
my $name = delete $args->{hostname};
cmd_help_die("No hostname") unless $name;
fail('Host already exists.') if $hosts->{$name};
# make sure we have an ip
unless ($args->{ip}) {
my $addr = gethostbyname($name);
fail_text('host_add_no_ip') unless $addr;
$args->{ip} = inet_ntoa($addr);
}
# defaults
$args->{port} ||= 7500;
$args->{status} ||= 'down';
# FIXME: verify the status can't be 'alive' if we can't get to ip:port
# OR BETTER: also make default status the reachability of that ip:port
# now create the host
my $mogadm = mogadm();
$mogadm->create_host($name, $args);
if ($mogadm->err) {
fail("Failure creating host: " . $mogadm->errstr);
}
ok('Host has been created.');
}
sub cmd_host_modify {
my $args = shift;
my $name = delete $args->{hostname};
# FIXME: verify the status can't be 'alive' if we can't get to ip:port
# now modify the host
my $mogadm = mogadm();
$mogadm->update_host($name, $args);
if ($mogadm->err) {
fail("Failure modifying host: " . $mogadm->errstr);
}
ok('Host has been modified.');
}
sub cmd_host_delete {
my $args = shift;
my $name = delete $args->{hostname};
# now modify the host
my $mogadm = mogadm();
$mogadm->delete_host($name);
if ($mogadm->err) {
fail("Failure deleting host: " . $mogadm->errstr);
}
ok('Host has been deleted.');
}
sub cmd_host_mark {
my $args = shift;
my $mogadm = mogadm();
$mogadm->update_host($args->{hostname}, { status => $args->{status} });
if ($mogadm->err) {
fail("Failure updating host status: " . $mogadm->errstr);
}
ok('Host status updated.');
}
sub cmd_domain_list {
# actually lists domains and classes
my $domains = domains() or
fail_text('no_domains');
# now iterate
printf " %-20s %-20s %-12s %-12s %-7s\n", "domain", "class", "mindevcount", "replpolicy", "hashtype";
printf "%-20s %-20s %-12s %-12s %-7s\n", '-' x 20, '-' x 20, '-' x 13, '-' x 12, '-' x 7;
foreach my $domain (sort keys %$domains) {
foreach my $class (sort keys %{$domains->{$domain}}) {
my $dom = $domains->{$domain}->{$class};
printf " %-20s %-20s %-8d %-13s %-7s\n", $domain, $class,
$dom->{mindevcount} || 0, $dom->{replpolicy} || '',
$dom->{hashtype} || '';
}
print "\n";
}
ok();
}
sub cmd_domain_add {
my $args = shift;
my $domain = delete $args->{domain};
# create
my $mogadm = mogadm();
$mogadm->create_domain($domain);
if ($mogadm->err) {
fail('Error creating domain: ' . $mogadm->errstr);
}
ok('Domain created.');
}
sub cmd_domain_delete {
my $args = shift;
my $domain = $args->{domain};
# destroy
my $mogadm = mogadm();
$mogadm->delete_domain($domain);
if ($mogadm->err) {
fail('Error deleting domain: ' . $mogadm->errstr);
}
ok('Domain deleted.');
}
sub cmd_class_list {
# same, pass it through
cmd_domain_list();
}
sub cmd_class_add {
my $args = shift;
my $domain = delete $args->{domain};
my $class = delete $args->{class};
cmd_help_die() unless $domain && $class;
$args->{mindevcount} ||= 2;
$args->{replpolicy} ||= '';
my $mogadm = mogadm();
$mogadm->create_class($domain, $class, $args);
if ($mogadm->err) {
fail('Error creating class: ' . $mogadm->errstr);
}
ok('Class created.');
}
sub cmd_class_modify {
my $args = shift;
my $domain = delete $args->{domain};
my $class = delete $args->{class};
cmd_help_die() unless $domain && $class;
$args->{mindevcount} ||= 2;
$args->{replpolicy} ||= '';
my $mogadm = mogadm();
$mogadm->update_class($domain, $class, $args);
if ($mogadm->err) {
fail('Error updating class: ' . $mogadm->errstr);
}
ok('Class updated.');
}
sub cmd_class_delete {
my $args = shift;
my $domain = $args->{domain};
my $class = $args->{class};
cmd_help_die() unless $domain && $class;
my $mogadm = mogadm();
$mogadm->delete_class($domain, $class);
if ($mogadm->err) {
fail('Error deleting class: ' . $mogadm->errstr);
}
ok('Class deleted.');
}
sub cmd_device_add {
my $args = shift;
my $hosts = hosts() or
fail_text('no_hosts');
my $host = $args->{hostname};
my $devid = $args->{devid};
my $state = $args->{status} || "alive";
cmd_help_die("devid should be numeric") unless $devid =~ /^\d+$/;
# FIXME: server should be fixed to verify via HTTP that the devid directory exists
my $mogadm = mogadm();
$mogadm->create_device(hostname => $host, devid => $devid, state => $state);
if ($mogadm->err) {
fail('Error adding device: ' . $mogadm->errstr);
}
ok('Device added.');
}
sub warn_on_drain {
my $args = shift;
if ($args->{status} && $args->{status} eq 'drain') {
print "***NOTE***: As of server version 2.40 'drain' has changed. See docs/wiki\n";
}
}
sub cmd_device_mark {
my $args = shift;
warn_on_drain($args);
my $mogadm = mogadm();
$mogadm->change_device_state($args->{hostname},
$args->{devid},
$args->{status});
if ($mogadm->err) {
fail('Error updating device: ' . $mogadm->errstr);
}
ok('Device updated.');
}
sub cmd_device_modify {
my $args = shift;
my $hostname = delete $args->{hostname};
my $devid = delete $args->{devid};
warn_on_drain($args);
my $mogadm = mogadm();
$mogadm->update_device($hostname, $devid, $args);
if ($mogadm->err) {
fail('Error updating device: ' . $mogadm->errstr);
}
ok('Device updated.');
}
sub cmd_device_list {
my $args = shift;
my $hosts = hosts();
fail_text('no_hosts') unless $hosts;
my $devs = devices();
fail_text('no_devices') unless $devs;
my $hostname = $args->{hostname};
foreach my $hostid (sort keys %$hosts) {
my $host = $hosts->{$hostid};
if (defined $hostname && $hostname ne $host->{hostname}) {
next;
}
print "$host->{hostname} [$hostid]: $host->{status}\n";
printf "%7s %-7s %10s %10s %10s %10s\n", '', '', 'used(G)', 'free(G)', 'total(G)', 'weight(%)';
foreach my $devid (sort keys %{$devs->{$hostid} || {}}) {
my $dev = $devs->{$hostid}->{$devid};
next if $dev->{status} eq "dead" && ! $args->{all};
my $total = $dev->{mb_total} / 1024;
my $used = $dev->{mb_used} / 1024;
my $free = $total - $used;
printf "%7s: %7s %10.3f %10.3f %10.3f %10u\n", "dev$devid", $dev->{status}, $used, $free, $total, $dev->{weight};
}
print "\n";
}
ok();
}
sub cmd_device_summary {
my $args = shift;
my $hostname = $args->{hostname};
my %show_state;
$show_state{$_} = 1 foreach split(/,/, ($args->{status} || "alive,readonly,drain"));
my $hosts = hosts();
fail_text('no_hosts') unless $hosts;
my $devs = devices();
fail_text('no_devices') unless $devs;
printf "%-15s %6s %7s %8s %8s %8s %8s\n", 'Hostname', 'HostID', 'Status', 'used(G)', 'free(G)', 'total(G)', '%Used';
foreach my $hostid (sort keys %$hosts) {
my $host = $hosts->{$hostid};
if (defined $hostname && $hostname ne $host->{hostname}) {
next;
}
my ($total,$used) = (0, 0);
foreach my $devid (sort keys %{$devs->{$hostid} || {}}) {
my $dev = $devs->{$hostid}->{$devid};
next unless $show_state{$dev->{status}};
my $devtotal = $dev->{mb_total} / 1024;
my $devused = $dev->{mb_used} / 1024;
$total += $devtotal;
$used += $devused;
}
my $free = $total - $used;
printf "%-15s [%4d]: %6s", $host->{hostname}, $hostid, $host->{status};
printf " %8.3f %8.3f %8.3f ", $used, $free, $total;
printf "%8.2f", 100*$used/$total if $total;
print "\n";
}
ok();
}
sub cmd_device_next {
my $devs = mogadm()->get_devices;
fail_text('no_devices') unless @$devs;
my $maxid = 0;
foreach my $dev (@$devs) {
my $devid = $dev->{devid};
if ($devid > $maxid) {
$maxid = $devid;
}
}
print($maxid + 1, "\n");
ok();
}
sub cmd_slave_list {
my $mogadm = mogadm();
my $slaves = $mogadm->slave_list();
foreach my $key (sort keys %$slaves) {
my $slavedata = $slaves->{$key};
my ($dsn, $username, $password) = @$slavedata;
print "$key --dsn=$dsn --username=$username --password=$password\n";
}
ok();
}
sub cmd_slave_add {
my $mogadm = mogadm();
my $args = shift;
my $rc = $mogadm->slave_add($args->{slave_key}, $args->{dsn}, $args->{username}, $args->{password});
if ($rc) {
ok("Slave added");
} else {
fail("Slave failed to be added");
}
}
sub cmd_slave_modify {
my $mogadm = mogadm();
my $args = shift;
my $key = delete $args->{slave_key} or cmd_help_die("Key argument is required");
my $rc = $mogadm->slave_modify($key, %$args);
if ($rc) {
ok("Slave modify success");
} else {
fail("Slave modify failure: " . $mogadm->errstr);
}
}
sub cmd_slave_delete {
my $mogadm = mogadm();
my $args = shift;
my $rc = $mogadm->slave_delete($args->{slave_key});
if ($rc) {
ok("Slave deleted");
} else {
fail("Slave delete failed");
}
}
sub cmd_rebalance_start {
my $mogadm = mogadm();
my $res = $mogadm->rebalance_start || fail($mogadm->errstr);
ok("rebalance started");
}
sub cmd_rebalance_stop {
my $mogadm = mogadm();
my $res = $mogadm->rebalance_stop || fail($mogadm->errstr);
ok("rebalance stopped");
}
sub cmd_rebalance_reset {
my $mogadm = mogadm();
my $res = $mogadm->rebalance_reset || fail($mogadm->errstr);
ok("rebalance reset");
}
# TODO: Make output prettier? Put hostname next to device name, print device
# info?
sub cmd_rebalance_test {
my $mogadm = mogadm();
my $res = $mogadm->rebalance_test || fail($mogadm->errstr);
print "Tested rebalance policy...\n";
my $s = $mogadm->server_settings;
print "Policy: ", $s->{rebal_policy}, "\n\n";
print "Source devices:\n";
for my $dev (sort split /,/, $res->{sdevs}) {
print " - ", $dev, "\n";
}
print "Destination devices:\n";
for my $dev (sort split /,/, $res->{ddevs}) {
print " - ", $dev, "\n";
}
}
sub cmd_rebalance_status {
my $mogadm = mogadm();
my $ss = $mogadm->server_settings or fail ($mogadm->errstr);
my $res = $mogadm->rebalance_status or fail ($mogadm->errstr);
if ($ss->{rebal_host}) {