← Back to team overview

graphite-dev team mailing list archive

Re: [Question #227568]: Read datapoint from graphite programmatically

 

Question #227568 on Graphite changed:
https://answers.launchpad.net/graphite/+question/227568

Earthlink posted a new comment:
Thanks for the response and the hint.
 
Sharing here the outcome of my experimentation in the hope of saving "rate" fans from going through the same pain (or maybe improving my development).

So I created the following snippet (my frontend script for collecting
sFLOW samples is in Perl):

#!/usr/bin/perl -w
use strict;
use WWW::Mechanize;
use JSON -support_by_pp;

my $GRAPHITE_SERVER="localhost";
my $metric=<your metric>;
my $time_window="2min";

my @arr = fetch_json_page("http://$GRAPHITE_SERVER/render?target=$metric&from=-$time_window&format=json";);
print "\nTime=$arr[0], Sample=$arr[1]\n";

sub fetch_json_page
{
  my @last_dp=('-1','-1');
  my ($json_url) = @_;
  my $browser = WWW::Mechanize->new();
  eval{
    $browser->get( $json_url );
    my $content = $browser->content();
    my $json = new JSON;

    my $json_text = $json->allow_nonref->utf8->relaxed->escape_slash->loose->allow_singlequote->allow_barekey->decode($content);
    my $rootarrayref= $json_text;
    my $objhashref = $ { $json_text }[0];
    my $datapoint_arrayref = $objhashref->{datapoints}; 
    my @datapoints = reverse(@{$datapoint_arrayref});
    foreach (@datapoints){
      my @datapoint = @{$_};
      if (defined($datapoint[0])){
        @last_dp=($datapoint[1],$datapoint[0]);
        last;
      }
    }
  };
  if($@){
    print "[[JSON ERROR]] JSON parser crashed! $@\n";
  }
  return @last_dp;
}

What the above essentially does is go to the graphite store (whisper?)
and retrieve the latest valid (non-empty) sample seen in the last 2
minutes (or [-1,-1] if no valid sample exists in this time window).

I went on and plugged this into my frontend script (sflow2graphite).
Although it worked all the way, the raete results are
inaccurate/fluctuating (compared the result to the rate of a
synthetically generated network flow of steady rate). I suspect that
this is due to the latest sample being aggregated (although I don't use
any aggregation) or otherwise being stored in a delayed/gradual fashion
to the graphite store.

Bottomline: for a quick solution: it makes more sense to work on your
frontend script and just store the last seen <value,timestamp> of the
desired metric(s) in an appropriate datastructure (e.g. hashmap) and use
it to compute current rates.

-- 
You received this question notification because you are a member of
graphite-dev, which is an answer contact for Graphite.