+ print "Resizing ${rrdfile}.${field} from " . $rrdinfo->{'rra[0].rows'} .
+ " to ${keep} samples.\n";
+
+ (my $start, my $ostep, my $names, my $data) =
+ RRDs::fetch("${rrdfile}.${field}",
+ "-s " . (time() - ($rrdinfo->{'rra[0].rows'} * $rrdinfo->{'step'})),
+ "AVERAGE");
+
+ if (RRDs::error) {
+ print "Error while fetching data: " . RRDs::error . "\n";
+ exit 1;
+ }
+
+ rrdcreate("${rrdfile}.${field}.new", "${field}", (${start}-${ostep}));
+
+ print "Preserving data since " . localtime($start) . "\n";
+
+ my $pos = $start;
+ foreach my $line (@$data) {
+ my $vline = "${pos}";
+
+ foreach my $val (@$line) {
+ $val = 'U' if (!defined($val));
+ $vline .= ":${val}";
+ }
+ RRDs::update("${rrdfile}.${field}.new", $vline) or die "Can't insert data\n";
+
+ if (RRDs::error) {
+ print "Error while updating: " . RRDs::error . "\n";
+ exit 1;
+ }
+ $pos += $ostep;
+
+ if ((($pos-$start)/$ostep) == $#$data) {
+ last;
+ }
+ }
+
+ rename("${rrdfile}.${field}", "${rrdfile}.${field}.old") or die "Can't rename old file: $!\n";
+ rename("${rrdfile}.${field}.new", "${rrdfile}.${field}") or die "Can't rename new file: $!\n";
+
+ $rrdinfo = RRDs::info("${rrdfile}.${field}");
+ if (RRDs::error) {
+ print "Error while getting info: " . RRDs::error . "\n";
+ exit 1;
+ }
+
+ if ($rrdinfo->{'rra[0].rows'} != $keep) {
+ print "Failed!\n";
+ exit 1;
+ }