diff --git a/ceph_reweight.pl b/ceph_reweight.pl new file mode 100644 index 0000000..7778667 --- /dev/null +++ b/ceph_reweight.pl @@ -0,0 +1,65 @@ +#!/usr/bin/perl +# +# This script reweights osd by 0.02 if utilization overgrows 85% +# it also reweight osd 80% full osds by 0.01 as a "bonus". +# It assumes that ceph cluster is less than 75-77% packed with data. + +use strict; +use warnings "all"; +use Fcntl; + +use JSON::PP; +use Data::Dumper; +sub help(); + +my $dry = 1; + +if (defined($ARGV[0]) and ($ARGV[0] eq '--do-it')) { + my $jstr = `ceph osd df --format=json-pretty`; + $jstr =~ s/\-nan/0/gi; # ceph tends to return broken json if custer is not in perfect shape + my $j = decode_json $jstr; + + foreach my $osd (@{$j->{'nodes'}}) { +# skip all unrelated objects + next unless (defined($osd->{'utilization'})); +# skip all objects that are not 80% full + next if ($osd->{'utilization'} < 80.000001); + my $new_weight = 1; + + if (($osd->{'utilization'} >= 80.000001) and ($osd->{'utilization'} < 85.000001)) { + $new_weight = $osd->{'reweight'} - 0.01; + printf ("osd.%s is %02.6f%% full, reweight it %01.6f -> %01.6f\n", $osd->{'id'}, $osd->{'utilization'}, $osd->{'reweight'}, $new_weight); + } + + if ($osd->{'utilization'} >= 85.000001) { + $new_weight = $osd->{'reweight'} - 0.02; + printf ("osd.%s is %02.6f%% full, reweight it %01.6f -> %01.6f\n", $osd->{'id'}, $osd->{'utilization'}, $osd->{'reweight'}, $new_weight); + } + + if (defined($ARGV[1])) { + help () if ($ARGV[1] ne '--dry'); + } else { + system('ceph', 'osd', 'reweght', $osd->{'id'}, $new_weight); + } + } +} else { + help(); +} + +sub help() { + print <