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 my $rrd = Bnew> I my $answer = B<$rrd-EdoCommand> I my $status = B<$rrd-Eend> =head1 DESCRIPTION With this module you can safely communicate with the rrdtool. =over 8 =item use B Load the RRDpo module. =item B<$rrd = RRDp-Enew> I start rrdtool. The argument must be the path to the rrdtool executable =item B<$answer = $rrd-EdoCommand> I 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-Eend> 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 Modified by Teun van Eijsden =cut #' this is to make cperl.el happy