Perl Hijk to query influxdb

#1

Trying to use perl to query influxdb ! Any help is appreciated.

My Perl Code snippet :

use warnings;
use strict;

use Hijk ();
use URI::Escape qw(uri_escape);
use Data::Dumper;


# Source  influxdb
my $ihost = "cricket" ;
my $iport = 8086;
my $idb = "telegraf" ;
my $iuser = "myuser" ;
my $ipass = "pass" ;
my $epoch = "ms" ;
my $iprecision  = "rfc3339" ;
my $iquery = "SELECT usage_idle FROM cpu WHERE cpu = 'cpu-total' LIMIT 5" ;


my $res = Hijk::request({
    method       => "GET",
    host         => $ihost,
    port         => $iport,
    path         => "/query",
    query_string => "db=$idb&user=$ipass&password=$ipass&precision=$iprecision&query=$iquery" ,
    body         => ""

});

if ($res->{status} != 200) {
print Dumper($res);
}

die "Expecting an 'OK' response  " unless $res->{status} == 200;

say $res->{body};

exit;

$ perl conn_influxdb.pl
Useless use of hash element in void context at ./conn_influxdb.pl line 61.
$VAR1 = {
‘body’ => ‘400 Bad Request’,
‘head’ => {
‘Connection’ => ‘close’,
‘Content-Type’ => ‘text/plain; charset=utf-8’
},
‘proto’ => ‘HTTP/1.1’,
‘status’ => 400
};
Expecting an ‘OK’ response at ./conn_influxdb.pl line 59.

#2

I was basically trying to use InfluxDB::Lineprotocol the perl module and it needs hijk ( heard its the simplest and fastest ) to do the calls! I see lots of “Write” examples with LineProocol and hijk but no "get " so any help is appreciated.

#3

Well I was able to auth using Hijk

head => [ ‘Authorization’ => "Basic $user:passwd ",],

but the error still comes…

$VAR1 = {
          'body' => '{"error":"missing required parameter \\"q\\""}
',
          'head' => {
                      'X-Influxdb-Version' => '1.3.7',
                      'Request-Id' => '624fd4d3-c3eb-11e7-a0eb-000000000000',
                      'Content-Length' => '45',
                      'Content-Type' => 'application/json',
                      'Date' => 'Tue, 07 Nov 2017 18:42:19 GMT'
                    },
          'proto' => 'HTTP/1.1',
          'status' => 400
        };
#4

The query parameter is q, not query.

#5

Thanks… but still getting error !

code snippet!

my $res = Hijk::request({
    method       => "GET",
    parse_chunked => 1 ,
    host         => $ihost,
    port         => $iport,
    head         => [ 'Authorization' => "Basic $digest",],
    path         => "/query",
    query_string =>  qq(db=$idb&user=$iuser&password=$ipass&precision=$iprecision&query=$iquery),
}) ;

if ($res->{status} != 200) {
print Dumper($res);
}

die "Expecting an 'OK' response  " unless $res->{status} == 200;

print Dumper($res);
say $res->{body};
exit;

OutPut !

$VAR1 = {
          'body' => '{"error":"missing required parameter \\"q\\""}
',
          'head' => {
                      'X-Influxdb-Version' => '1.3.7',
                      'Request-Id' => 'e4b2fd23-cbc9-11e7-8cbe-000000000000',
                      'Content-Length' => '45',
                      'Content-Type' => 'application/json',
                      'Date' => 'Fri, 17 Nov 2017 19:02:44 GMT'
                    },
          'proto' => 'HTTP/1.1',
          'status' => 400
        };
Expecting an 'OK' response   at ./conn_hijk2.pl line 66.
#6

This has to have &q=$iquery, not &query=.

#7

partial success!

$VAR1 = {
          'body' => '{"results":[{"statement_id":0,"error":"query interrupted"}]}
',
          'head' => {
                      'X-Influxdb-Version' => '1.3.7',
                      'Connection' => 'close',
                      'Request-Id' => '8b88c05e-cbef-11e7-9329-000000000000',
                      'Content-Type' => 'application/json',
                      'Transfer-Encoding' => 'chunked',
                      'Date' => 'Fri, 17 Nov 2017 23:32:15 GMT'
                    },
          'proto' => 'HTTP/1.1',
          'status' => 200
        };