package RRDpo;

use strict;
use Fcntl;
use Carp;
use IO::Handle;
use IPC::Open2;
use vars qw($RRDpid $VERSION);
my $RRDpid;

$VERSION = 1.00;

sub new()
{
  my $type = shift;
  my $self = {};
  bless ($self, $type);
  my $rrdtool = shift;
  $RRDpid = open2 \*READHANDLE,\*WRITEHANDLE, $rrdtool,"-" || croak "Can't Start rrdtool: $!";
  WRITEHANDLE->autoflush(); #flush after every write
  fcntl READHANDLE, F_SETFL,O_NONBLOCK|O_NDELAY; #make readhandle NON BLOCKING
  return $self;
}

sub doCommand ()
{
  my $self = shift;
  my $command = shift;
  print WRITEHANDLE "$command\n";
  my $inmask = 0;
  my $srbuf;
  my $minibuf;
  my $buffer;
  my $nfound;
  my $timeleft;
  my $ERR = 0;
  vec($inmask,fileno(READHANDLE),1) = 1; # setup select mask for Reader
  while (1)
  {
    my $rout;    
    $nfound = select($rout=$inmask,undef,undef,2);
    if ($nfound == 0)
    {
      # here, we could do something sensible ...
      next;
    }
    sysread(READHANDLE,$srbuf,4096);
    $minibuf .= $srbuf;
    while ($minibuf =~ s|^(.+?)\n||s)
    {
      my $line = $1;
      if ($line =~  m|^ERROR|)
      {
	croak $line;
	$ERR = 1;
      } 
      elsif ($line =~ m|^OK u:([\d\.]+) s:([\d\.]+) r:([\d\.]+)|)
      {
	($RRDp::sys,$RRDp::user,$RRDp::real)=($1,$2,$3);
	return $ERR == 1 ? undef : \$buffer;
      }
      else
      {
	$buffer .= $line. "\n";
      }
    }
  }
}

sub end ()
{
  my $self = shift;
  close WRITEHANDLE;
  close READHANDLE;
  waitpid $RRDpid,0;
  return $?;
}

1;

=head1 NAME

RRDp - Attach rrdtool from within a perl script via a set of pipes;

=head1 SYNOPSIS

use B<RRDpo>

my $rrd = B<RRDpo-E<gt>new> I<path to rrdtool executable>

my $answer = B<$rrd-E<gt>doCommand>  I<rrdtool commandline>

my $status = B<$rrd-E<gt>end>

=head1 DESCRIPTION

With this module you can safely communicate with the rrdtool.

=over 8

=item  use B<RRDpo>

Load the RRDpo module.

=item B<$rrd = RRDp-E<gt>new> I<path to rrdtool executable>

start rrdtool. The argument must be the path to the rrdtool executable

=item B<$answer = $rrd-E<gt>doCommand> I<rrdtool commandline>

pass commands on to rrdtool. check the rrdtool documentation for
more info on the rrdtool commands.
This returns rrdtools response. Note that the $answer variable will
only contain a pointer to the returned data. The reason for this is, that
rrdtool can potentially return quite excessive amounts of data
and we don't want to copy this around in memory. So when you want to
access the contents of $answer you have to use $$answer which dereferences
the variable.

=item $status = B<$rrd-E<gt>end>

terminates rrdtool and returns rrdtools status ...

=back


=head1 EXAMPLE

use RRDpo;
my $rrd = new RRDpo ("/usr/local/bin/rrdtool");
my $answer = $rrd->doCommand ('create demo.rrd --step 100 DS:in:GAUGE:100:U:U RRA:AVERAGE:0.5:1:10');
print $$answer;

=head1 SEE ALSO

For more information on how to use rrdtool, check the manpages.

=head1 AUTHOR

Tobias Oetiker <oetiker@ee.ethz.ch>
Modified by Teun van Eijsden <teun@chello.nl>

=cut
#'  this is to make cperl.el happy
