Hello.

Some time ago Robin Johnson (robbat2 AT gentoo.org) wrote patch to make
it possible to use a bit different but more comfortable syntax for
rrdresize.

rrdtool resize filename rra-num [+|-|=]rows

So it's possible to avoid GROW or SHRINK in in command line and just
write +rows. What do you think is rrdresize worth such change?

Patch in attachment.

With best regards,
-- 
Peter.
diff -Naur rrdtool-1.3.3.orig/doc/rrdresize.pod rrdtool-1.3.3/doc/rrdresize.pod
--- rrdtool-1.3.3.orig/doc/rrdresize.pod	2008-03-15 13:39:48.000000000 +0300
+++ rrdtool-1.3.3/doc/rrdresize.pod	2008-09-16 20:53:42.000000000 +0400
@@ -6,6 +6,8 @@
 
 B<rrdtool> B<resize> I<filename> I<rra-num>  B<GROW>I<|>B<SHRINK> I<rows>
 
+B<rrdtool> B<resize> I<filename> I<rra-num>  [B<+>B<->B<=>]I<rows>
+
 =head1 DESCRIPTION
 
 The B<resize> function is used to modify the number of rows in
@@ -23,17 +25,20 @@
 
 =item B<GROW>
 
-used if you want to add extra rows to an RRA. The extra rows will be inserted
-as the rows that are oldest.
+(old style) used if you want to add extra rows to an RRA. The extra rows will
+be inserted as the rows that are oldest.
 
 =item B<SHRINK>
 
-used if you want to remove rows from an RRA. The rows that will be removed
-are the oldest rows.
+(old style) used if you want to remove rows from an RRA. The rows that will be
+removed are the oldest rows.
 
-=item I<rows>
+=item [B<+>B<->B<=>]I<rows>
 
-the number of rows you want to add or remove.
+the number of rows you want to add or remove. when prefixed with any of B<+>,
+B<-> or B<=>, the B<GROW>I<|>B<SHRINK> argument is not required, and instead
+the prefix specifies to add (B<+>) I<rows>, remove (B<->) I<rows>, or set to
+exactly (B<=>) I<rows>.
 
 =back
 
diff -Naur rrdtool-1.3.3.orig/src/rrd_resize.c rrdtool-1.3.3/src/rrd_resize.c
--- rrdtool-1.3.3.orig/src/rrd_resize.c	2008-09-15 00:32:10.000000000 +0400
+++ rrdtool-1.3.3/src/rrd_resize.c	2008-09-16 22:23:43.000000000 +0400
@@ -19,7 +19,7 @@
     unsigned long l, rra;
     long      modify;
     unsigned long target_rra;
-    int       grow = 0, shrink = 0;
+    int       grow = 0, shrink = 0, setsize = 0, newstyle = 0;
     char     *endptr;
     rrd_file_t *rrd_file, *rrd_out_file;
 
@@ -28,7 +28,7 @@
         rrd_set_error("resize.rrd is a reserved name");
         return (-1);
     }
-    if (argc != 5) {
+    if (argc != 5 && argc != 4) {
         rrd_set_error("wrong number of parameters");
         return (-1);
     }
@@ -39,18 +39,39 @@
         grow = 1;
     else if (!strcmp(argv[3], "SHRINK"))
         shrink = 1;
+    else if (argv[3][0] == '=' || argv[3][0] == '-' || argv[3][0] == '+')
+        newstyle = 3;
     else {
         rrd_set_error("I can only GROW or SHRINK");
         return (-1);
     }
 
-    modify = strtol(argv[4], &endptr, 0);
+    // if the size starts with a character (=-+) then it's the new style
+    // instead. This is an extra mode so we can allow
+    // rrdtool resize FOO.rrd 5 GROW =5
+    if(argc == 5 && (argv[4][0] == '=' || argv[4][0] == '-' || argv[4][0] == '+')) {
+        newstyle = 4;
+    }
+    if(newstyle > 0) {
+        setsize = argv[newstyle][0] == '=' ? 1 : 0;
+        /* If we see a + or a -, then the codepath is simple */
+        grow = argv[newstyle][0] == '+' ? 1 : 0;
+        shrink = argv[newstyle][0] == '-' ? 1 : 0;
+        modify = strtol(argv[newstyle]+1, &endptr, 0);
+        if(modify == 0) {
+            rrd_set_error("Refusing to add/delete/set with 0 rows");
+            return (-1);
+        }
+    } else {
+        /* this is the old code branch */
+        modify = strtol(argv[4], &endptr, 0);
 
-    if ((modify < 1)) {
-        rrd_set_error("Please grow or shrink with at least 1 row");
-        return (-1);
+        if ((modify < 1)) {
+            rrd_set_error("Please grow or shrink with at least 1 row");
+            return (-1);
+        }
     }
-
+ 
     if (shrink)
         modify = -modify;
 
@@ -74,6 +95,18 @@
         return (-1);
     }
 
+    /* if we are in setsize mode, we need to work out what modify factor to use */
+    if(setsize > 0) {
+        long oldsize = rrdold.rra_def[target_rra].row_cnt;
+        long newsize = modify;
+        // old=5, new=5 => modify = 0
+        if(newsize == oldsize) {
+            rrd_set_error("RRA is already set to that size!");
+            return(-1);
+        }
+        modify = newsize-oldsize;
+    }
+
     if (modify < 0)
         if ((long) rrdold.rra_def[target_rra].row_cnt <= -modify) {
             rrd_set_error("This RRA is not that big");
diff -Naur rrdtool-1.3.3.orig/src/rrd_tool.c rrdtool-1.3.3/src/rrd_tool.c
--- rrdtool-1.3.3.orig/src/rrd_tool.c	2008-09-15 00:32:10.000000000 +0400
+++ rrdtool-1.3.3/src/rrd_tool.c	2008-09-16 21:13:20.000000000 +0400
@@ -186,6 +186,7 @@
     const char *help_resize =
         N_
         (" * resize - alter the length of one of the RRAs in an RRD\n\n"
+         "\trrdtool resize filename rranum <+|-|=>rows\n"
          "\trrdtool resize filename rranum GROW|SHRINK rows\n\n");
     const char *help_xport =
         N_("* xport - generate XML dump from one or several RRD\n\n"
_______________________________________________
rrd-developers mailing list
[email protected]
https://lists.oetiker.ch/cgi-bin/listinfo/rrd-developers

Reply via email to