Generate an error message when using RRDp with graph - this can not work reliably...
[rrdtool.git] / bindings / perl-piped / RRDp.pm
index e61a28b..c3fbf91 100644 (file)
@@ -45,6 +45,9 @@ start RRDtool. The argument must be the path to the RRDtool executable
 pass commands on to RRDtool. check the RRDtool documentation for
 more info on the RRDtool commands.
 
 pass commands on to RRDtool. check the RRDtool documentation for
 more info on the RRDtool commands.
 
+B<Note>: Due to design limitations, B<RRDp::cmd> does not support the
+C<graph -> command - use C<graphv -> instead.
+
 =item $answer = B<RRDp::read>
 
 read RRDtool's response to your command. Note that the $answer variable will
 =item $answer = B<RRDp::read>
 
 read RRDtool's response to your command. Note that the $answer variable will
@@ -102,6 +105,7 @@ For more information on how to use RRDtool, check the manpages.
 Tobias Oetiker <tobi@oetiker.ch>
 
 =cut
 Tobias Oetiker <tobi@oetiker.ch>
 
 =cut
+
 #'  this is to make cperl.el happy
 
 use strict;
 #'  this is to make cperl.el happy
 
 use strict;
@@ -120,7 +124,7 @@ sub cmd (@);
 sub end ();
 sub read ();
 
 sub end ();
 sub read ();
 
-$VERSION=1.2023;
+$VERSION=1.3002;
 
 sub start ($){
   croak "rrdtool is already running"
 
 sub start ($){
   croak "rrdtool is already running"
@@ -146,7 +150,6 @@ sub read () {
   my $buffer;
   my $nfound;
   my $timeleft;
   my $buffer;
   my $nfound;
   my $timeleft;
-  my $ERR = 0;
   vec($inmask,fileno(RRDreadHand),1) = 1; # setup select mask for Reader
   while (1) {
     my $rout;    
   vec($inmask,fileno(RRDreadHand),1) = 1; # setup select mask for Reader
   while (1) {
     my $rout;    
@@ -163,11 +166,14 @@ sub read () {
       $RRDp::error = undef;
       if ($line =~  m|^ERROR|) {       
        $RRDp::error_mode eq 'catch' ? $RRDp::error = $line : croak $line;
       $RRDp::error = undef;
       if ($line =~  m|^ERROR|) {       
        $RRDp::error_mode eq 'catch' ? $RRDp::error = $line : croak $line;
-       $ERR = 1;
+        $RRDp::sys = undef;
+        $RRDp::user = undef;
+        $RRDp::real = undef;
+       return undef;
       } 
       } 
-      elsif ($line =~ m|^OK u:([\d\.]+) s:([\d\.]+) r:([\d\.]+)|){
+      elsif ($line =~ m|^OK(?: u:([\d\.]+) s:([\d\.]+) r:([\d\.]+))?|){
        ($RRDp::sys,$RRDp::user,$RRDp::real)=($1,$2,$3);
        ($RRDp::sys,$RRDp::user,$RRDp::real)=($1,$2,$3);
-       return $ERR == 1 ? undef : \$buffer;
+       return \$buffer;
       } else {
        $buffer .= $line. "\n";
       }
       } else {
        $buffer .= $line. "\n";
       }
@@ -184,6 +190,13 @@ sub cmd (@){
   }
   $cmd =~ s/\n/ /gs;
   $cmd =~ s/\s/ /gs;
   }
   $cmd =~ s/\n/ /gs;
   $cmd =~ s/\s/ /gs;
+
+  # The generated graphs aren't necessarily terminated by a newline,
+  # causing RRDp::read() to wait for a line matching '^OK' forever.
+  if ($cmd =~ m/^\s*graph\s+-\s+/) {
+    croak "RRDp does not support the 'graph -' command - "
+        . "use 'graphv -' instead";
+  }
   print RRDwriteHand "$cmd\n";
 }
 
   print RRDwriteHand "$cmd\n";
 }