[rrd-users] Re: newbie help please - RRDTOOL GPRINT/LTIME (using Perl module)

Matchan, Neil W Neil.W.Matchan at team.telstra.com
Mon May 12 02:15:39 MEST 2003


Thanks all,
Went for the RRDs::fetch solution.

Couldn't get anything except 1974 dates using "$my_start = localtime($data->[0]) ;" so
I used a bit of code from the RRDTOOL web site.

For others who may want to do this in future, heres the code:


#
#######################################################################################
#                             Create a RRDTOOL Graph                                  #
#######################################################################################
#
sub makeTrafficGraph: {
  my $rrdGraphWidth = ($graphwidth / $rrdRow);
  if ($rrdRow ne "1") {
    $rrdGraphWidth = ($rrdGraphWidth - $graphwidthadjust);
  }

  ($graphTimeTextStart, $graphTimeTextEnd) = getRRDates("$rrdroot$network$rrdname\.rrd");

  my $graphTitle = ("$rrdDevice\, $interfaceName");
  if (($deviceText eq "") && ($portName ne "")) { $graphTitle = $portName };
  if (($deviceText ne "") && ($portName eq "")) { $graphTitle = $deviceText };
  if (($graphBaseLine eq "") && ($graphType ne "Memory")) { $graphBaseLine = "1000" };
  $graphBaseLine = "1000";

  my $imageName = ("$rrdname\_$rrdInterval\_$rrdPeriod\_$rrdType\_$graphType\_$rrdRow");
  RRDs::graph "$rrdroot$network$imageName.png",
  "--title", "$graphTitle",
  "--start", "$graphStartDate",
  "--end", "$graphEndDate",
  "--step", "300",
  "--color", "GRID#999999",
  "--color", "FONT#000000",
  "--color", "SHADEB#000000",
  "--color", "CANVAS#FFFFFF",
  "--imgformat","PNG",
  "--width=$rrdGraphWidth",
  "--base", "$graphBaseLine",
  "--height=$graphheight",
  "--lower-limit", "0",
  "--vertical-label","$graphVerticalTitle",
  "DEF:r01bytesave0=$rrdroot$network$rrdname\.rrd:ds0:$rrdType",
  "DEF:r01bytesave1=$rrdroot$network$rrdname\.rrd:ds1:$rrdType",
  "CDEF:r01inbound=r01bytesave0,UN,0,r01bytesave0,IF,$graphMultiplier,*",
  "CDEF:r01outbound=r01bytesave1,UN,0,r01bytesave1,IF,$graphMultiplier,*",
  "CDEF:r01time=r01bytesave1,POP,LTIME",
  "AREA:r01inbound#00D000",
  "LINE1:r01inbound#00D000:$rrdDevice Inbound  $interfaceName\, Speed $speed, Starts $graphTimeTextStart",
  "LINE1:r01outbound#0000D0:$rrdDevice Outbound $interfaceName\, Speed $speed, Ends   $graphTimeTextEnd";

  if ($ERROR = RRDs::error) {
    print $cgi->h3("ERROR Graph: $ERROR\n");
  }
  return ("/rrdtool/$network$imageName");
}


#
################################################################################################
#                  Extract RRD samples 'start' and 'end' times.                                #
################################################################################################
#
sub getRRDates {
  my $rrdname = shift(@_);
  my ($start,$step,$names,$data) = RRDs::fetch("$rrdname", "MAX",
  "--start", "$graphStartDate",
  "--end", "$graphEndDate");

  if ($ERROR = RRDs::error) {
    print $cgi->h3("ERROR Graph: $ERROR\n");
    $graphTimeTextStart = "not available";
    $graphTimeTextEnd = "not available";
    return("$graphTimeTextStart", "$graphTimeTextEnd");
   }

  my ($temp0,$temp1,$temp2,$temp3,$temp4,$temp5,$temp6,$temp7) = localtime($start);
  $temp4 = ($temp4 + 1);
  if ($temp1 < 10) { $temp1 = "0" . $temp1 };
  if ($temp2 < 10) { $temp2 = "0" . $temp2 };
  if ($temp3 < 10) { $temp3 = "0" . $temp3 };
  if ($temp4 < 10) { $temp4 = "0" . $temp4 };
  my $graphTimeTextStart = ($temp2 . "\\:" . $temp1 . "hrs on " . ($temp5 + 1900) . $temp4 . $temp3);
  #
  #############################################################################################################
  # Loop until end of sample period reached. Add 'step' interval to 'start' time to calculate 'end' time.     #
  # I use to use "$start = $start + ($step * ($arraysize - 1));" but this doesn't allow for the value of      #
  # 'step' changing within the specified sample period. Looping and extracting each instance of 'step' avoids #
  # this problem.  Some adjustment is required as we always end one 'step' period past our end date/time.     #
  #############################################################################################################
  #
  foreach my $line (@$data) {
    $start = $start + $step;
    }
  $start = $start - $step;

  my ($temp0,$temp1,$temp2,$temp3,$temp4,$temp5,$temp6,$temp7) = localtime($start);
  $temp4 = ($temp4 + 1);
  if ($temp1 < 10) { $temp1 = "0" . $temp1 };
  if ($temp2 < 10) { $temp2 = "0" . $temp2 };
  if ($temp3 < 10) { $temp3 = "0" . $temp3 };
  if ($temp4 < 10) { $temp4 = "0" . $temp4 };
  my $graphTimeTextEnd = ($temp2 . "\\:" . $temp1 . "hrs on " . ($temp5 + 1900) . $temp4 . $temp3);
  return("$graphTimeTextStart", "$graphTimeTextEnd");
  }

Many thanks to those who helped,
Neil M

-----Original Message-----
From: Stanley Hopcroft [mailto:Stanley.Hopcroft at IPAustralia.Gov.AU]
Sent: Friday, 9 May 2003 5:16 PM
To: Matchan, Neil W
Cc: rrd_user_mail_list
Subject: Re: [rrd-users] newbie help please - RRDTOOL GPRINT/LTIME
(using Perl module)


Dear Sir,

I hope I have understood you correctly but I think you have done all the
hard stuff already (and I thank you for it because I am using rrdcgi at
the moment and you have provided a wonderful example of doing the same
thing with RRDs::graph that I can use with CGI.pm [in this case to have
a fill out form to get the start and end dates]).

Perhaps I haven't understood your question but if you only want the 
start and end dates in a consistent format (struct tm or time_t or 
localtime format) all I think you need do is

1 Use RRDs::fetch to get the start and end data

2 use Perls localtime function in a scalar context on the first element 
of the array ref that fetch returns

somthing like

(undef, undef, $data) = RRDs::fetch(<your_rrd>, <your_CF>, '--start',
<your_start>, '--end', <your_start> + step>) ;

$my_start = localtime($data->[0]) ;

Yours sincerely.

-- 
------------------------------------------------------------------------
Stanley Hopcroft
------------------------------------------------------------------------

'...No man is an island, entire of itself; every man is a piece of the
continent, a part of the main. If a clod be washed away by the sea,
Europe is the less, as well as if a promontory were, as well as if a
manor of thy friend's or of thine own were. Any man's death diminishes
me, because I am involved in mankind; and therefore never send to know
for whom the bell tolls; it tolls for thee...'

from Meditation 17, J Donne.

--
Unsubscribe mailto:rrd-users-request at list.ee.ethz.ch?subject=unsubscribe
Help        mailto:rrd-users-request at list.ee.ethz.ch?subject=help
Archive     http://www.ee.ethz.ch/~slist/rrd-users
WebAdmin    http://www.ee.ethz.ch/~slist/lsg2.cgi



More information about the rrd-users mailing list