InfluxDB-HTTP-0.04/000755 000765 000024 00000000000 13323713572 013627 5ustar00rsestaff000000 000000 InfluxDB-HTTP-0.04/Changes000644 000765 000024 00000001041 13323713502 015107 0ustar00rsestaff000000 000000 Revision history for InfluxDB-HTTP 0.04 2018-07-18 - Remove dependency on Method::Signatures (ang@open.ch). 0.03 2018-07-12 - Improved JSON error handling and configurable LWP UserAgent timeout (kb@open.ch). 0.02 2017-08-02 - Support precision= parameter in write (nicolai.langfeldt@broadnet.no). - Support retention_policy parameter in write. - Expose HTTP::Response in returned results. 0.01 2016-05-27 - First version, released on an unsuspecting world. Still some rough edges. InfluxDB-HTTP-0.04/MANIFEST000644 000765 000024 00000000404 13323713572 014756 0ustar00rsestaff000000 000000 Changes lib/InfluxDB/HTTP.pm Makefile.PL MANIFEST This list of files README.pod t/test.pl META.yml Module YAML meta-data (added by MakeMaker) META.json Module JSON meta-data (added by MakeMaker) InfluxDB-HTTP-0.04/t/000755 000765 000024 00000000000 13323713571 014071 5ustar00rsestaff000000 000000 InfluxDB-HTTP-0.04/README.pod000644 000765 000024 00000011514 13323713502 015263 0ustar00rsestaff000000 000000 =head1 NAME InfluxDB::HTTP - The Perl way to interact with InfluxDB! =head1 VERSION Version 0.04 =head1 SYNOPSIS InfluxDB::HTTP allows you to interact with the InfluxDB HTTP API. The module essentially provides one method per InfluxDB HTTP API endpoint, that is C, C and C. use InfluxDB::HTTP; my $influx = InfluxDB::HTTP->new(); my $ping_result = $influx->ping(); print "$ping_result\n"; my $query = $influx->query( [ 'SELECT Lookups FROM _internal.monitor.runtime WHERE time > '.(time - 60)*1000000000, 'SHOW DATABASES'], epoch => 's', ); print "$query\n"; =head1 SUBROUTINES/METHODS =head2 RETURN VALUES AND ERROR HANDLING C is relied upon for returning data from subroutines. The respective result object can always be used as string and evaluated on a boolean basis. A result object evaluating to false indicates an error and a corresponding error message is provided in the attribute C: my $ping = $influx->ping(); print $ping->error unless ($ping); Furthermore, all result objects provide access to the C object that is returned by InfluxDB in the attribute C. =head2 new host => 'localhost', port => 8086, timeout => 600 Passing C, C and/or C is optional, defaulting to the InfluxDB defaults or to 3 minutes for the timeout. The timeout is in seconds. Returns an instance of InfluxDB::HTTP. =head2 ping Pings the InfluxDB instance configured in the constructor (i.e. by C and C). Returned object evaluates to true or false depending on whether the ping was successful or not. If true, then it contains a C attribute that indicates the InfluxDB version running on the pinged server. The C attribute is extracted from the C HTTP response header, which is part of the HTTP response from the pinged InfluxDB instance. my $ping = $influx->ping(); print $ping->version if ($ping); =head2 query query, database => "DATABASE", chunk_size => CHUNK_SIZE, epoch => "ns" Used to query the InfluxDB instance. All parameters but the first one are optional. The C parameter can either be a String or a Perl ArrayRef of Strings, where every String contains a valid InfluxDB query. If the returned object evaluates to true, indicating that the query was successful, then the returned object's C attribute contains the entire response from InfluxDB as Perl hash. Additionally the attribute C provides the request identifier as set in the HTTP reponse headers by InfluxDB. This can for example be useful for correlating requests with log files. =head2 write measurement, database => "DATABASE", precision => "ns", retention_policy => "RP" Writes data into InfluxDB. The parameter C can either be a String or an ArrayRef of Strings, where each String contains one valid InfluxDB LineProtocol statement. All of those mesaurements are then sent to InfluxDB and the specified database. The returned object evaluates to true if the write was successful, and otherwise to false. The optional argument precision can be given if a precsion different than "ns" is used in the line protocol. InfluxDB docs suggest that using a coarser precision than ns can save space and processing. In many cases "s" or "m" might do. The optional argument retention_policy can be used to specify a retention policy other than the default retention policy of the selected database. =head2 get_lwp_useragent Returns the internally used LWP::UserAgent instance for possible modifications (e.g. to configure an HTTP proxy). =head1 AUTHOR Raphael Seebacher, C<< >> =head1 BUGS Please report any bugs or feature requests to L. =head1 LICENSE AND COPYRIGHT MIT License Copyright (c) 2016 Raphael Seebacher Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. InfluxDB-HTTP-0.04/META.yml000644 000765 000024 00000000774 13323713572 015110 0ustar00rsestaff000000 000000 --- abstract: 'The Perl way to interact with InfluxDB!' author: - 'Raphael Seebacher ' build_requires: Test::More: '0' configure_requires: ExtUtils::MakeMaker: '0' dynamic_config: 1 generated_by: 'ExtUtils::MakeMaker version 7.12, CPAN::Meta::Converter version 2.143240' license: perl meta-spec: url: http://module-build.sourceforge.net/META-spec-v1.4.html version: '1.4' name: InfluxDB-HTTP no_index: directory: - t - inc requires: perl: '5.006' version: '0.04' InfluxDB-HTTP-0.04/lib/000755 000765 000024 00000000000 13323713571 014374 5ustar00rsestaff000000 000000 InfluxDB-HTTP-0.04/Makefile.PL000644 000765 000024 00000001313 12721701630 015570 0ustar00rsestaff000000 000000 use 5.006; use strict; use warnings; use ExtUtils::MakeMaker; WriteMakefile( NAME => 'InfluxDB::HTTP', AUTHOR => q{Raphael Seebacher }, VERSION_FROM => 'lib/InfluxDB/HTTP.pm', ABSTRACT_FROM => 'lib/InfluxDB/HTTP.pm', LICENSE => 'perl_5', PL_FILES => {}, MIN_PERL_VERSION => 5.006, CONFIGURE_REQUIRES => { 'ExtUtils::MakeMaker' => 0, }, BUILD_REQUIRES => { 'Test::More' => 0, }, PREREQ_PM => { #'ABC' => 1.6, #'Foo::Bar::Module' => 5.0401, }, dist => { COMPRESS => 'gzip -9f', SUFFIX => 'gz', }, clean => { FILES => 'InfluxDB-HTTP-*' }, ); InfluxDB-HTTP-0.04/META.json000644 000765 000024 00000001566 13323713572 015260 0ustar00rsestaff000000 000000 { "abstract" : "The Perl way to interact with InfluxDB!", "author" : [ "Raphael Seebacher " ], "dynamic_config" : 1, "generated_by" : "ExtUtils::MakeMaker version 7.12, CPAN::Meta::Converter version 2.143240", "license" : [ "perl_5" ], "meta-spec" : { "url" : "http://search.cpan.org/perldoc?CPAN::Meta::Spec", "version" : "2" }, "name" : "InfluxDB-HTTP", "no_index" : { "directory" : [ "t", "inc" ] }, "prereqs" : { "build" : { "requires" : { "Test::More" : "0" } }, "configure" : { "requires" : { "ExtUtils::MakeMaker" : "0" } }, "runtime" : { "requires" : { "perl" : "5.006" } } }, "release_status" : "stable", "version" : "0.04" } InfluxDB-HTTP-0.04/lib/InfluxDB/000755 000765 000024 00000000000 13323713571 016047 5ustar00rsestaff000000 000000 InfluxDB-HTTP-0.04/lib/InfluxDB/HTTP.pm000644 000765 000024 00000023124 13323713502 017160 0ustar00rsestaff000000 000000 package InfluxDB::HTTP; use strict; use warnings; require Exporter; our @ISA = qw(Exporter); our @EXPORT_OK = (); our @EXPORT = (); use JSON::MaybeXS; use LWP::UserAgent; use Object::Result; use URI; our $VERSION = '0.04'; sub new { my $class = shift; my %args = ( host => 'localhost', port => 8086, timeout => 180, @_, ); my ($host, $port, $timeout) = @args{'host', 'port', 'timeout'}; my $self = { host => $host, port => $port, }; my $ua= LWP::UserAgent->new(); $ua->agent("InfluxDB-HTTP/$VERSION"); $ua->timeout($timeout); $self->{lwp_user_agent} = $ua; bless $self, $class; return $self; } sub get_lwp_useragent { my ($self) = @_; return $self->{lwp_user_agent}; } sub ping { my ($self) = @_; my $uri = $self->_get_influxdb_http_api_uri('ping'); my $response = $self->{lwp_user_agent}->head($uri->canonical()); if (! $response->is_success()) { my $error = $response->message(); result { raw { return $response; } error { return $error; } { return "Error pinging InfluxDB: $error"; } { return; } } } my $version = $response->header('X-Influxdb-Version'); result { raw { return $response; } version { return $version; } { return "Ping successful: InfluxDB version $version"; } { return 1; } } } sub query { my $self = shift; my $query = shift; my %args = (epoch => 'ns', @_); my ($database, $chunk_size, $epoch) = @args{'database', 'chunk_size', 'epoch'}; die "Missing argument 'query'" if !$query; die "Argument epoch '$epoch' is not one of (h,m,s,ms,u,ns)" if $epoch !~ /^(h|m|s|ms|u|ns)$/; if (ref($query) eq 'ARRAY') { $query = join(';', @$query); } my $uri = $self->_get_influxdb_http_api_uri('query'); $uri->query_form( q => $query, ($database ? (db => $database) : ()), ($chunk_size ? (chunk_size => $chunk_size) : ()), ($epoch ? (epoch => $epoch) : ()) ); my $response = $self->{lwp_user_agent}->post($uri->canonical()); chomp(my $content = $response->content()); my $error; if ($response->is_success()) { local $@; my $data = eval { decode_json($content) }; $error = $@; if ($data) { $error = $data->{error}; } if (!$error) { result { raw { return $response; } data { return $data; } results { return $data->{results}; } request_id { return $response->header('Request-Id'); } { return "Returned data: $content"; } { return 1; } } } } else { $error = $content; } result { raw { return $response; } error { return $error; } { return "Error executing query: $error"; } { return; } } } sub write { my $self = shift; my $measurement = shift; my %args = @_; my ($database, $precision, $retention_policy) = @args{'database', 'precision', 'retention_policy'}; die "Missing argument 'measurement'" if !$measurement; die "Missing argument 'database'" if !$database; die "Argument precision '$precision' is set and not one of (h,m,s,ms,u,ns)" if $precision && $precision !~ /^(h|m|s|ms|u|ns)$/; if (ref($measurement) eq 'ARRAY') { $measurement = join("\n", @$measurement); } my $uri = $self->_get_influxdb_http_api_uri('write'); $uri->query_form( db => $database, ($precision ? (precision => $precision) : ()), ($retention_policy ? (rp => $retention_policy) : ()) ); my $response = $self->{lwp_user_agent}->post($uri->canonical(), Content => $measurement); chomp(my $content = $response->content()); if ($response->code() != 204) { local $@; my $data = eval { decode_json($content) }; my $error = $@; $error = $data->{error} if (!$error && $data); result { raw { return $response; } error { return $error; } { return "Error executing write: $error"; } { return; } } } result { raw { return $response; } { return "Write successful"; } { return 1; } } } sub _get_influxdb_http_api_uri { my ($self, $endpoint) = @_; die "Missing argument 'endpoint'" if !$endpoint; my $uri = URI->new(); $uri->scheme('http'); $uri->host($self->{host}); $uri->port($self->{port}); $uri->path($endpoint); return $uri; } 1; __END__ =head1 NAME InfluxDB::HTTP - The Perl way to interact with InfluxDB! =head1 VERSION Version 0.04 =head1 SYNOPSIS InfluxDB::HTTP allows you to interact with the InfluxDB HTTP API. The module essentially provides one method per InfluxDB HTTP API endpoint, that is C, C and C. use InfluxDB::HTTP; my $influx = InfluxDB::HTTP->new(); my $ping_result = $influx->ping(); print "$ping_result\n"; my $query = $influx->query( [ 'SELECT Lookups FROM _internal.monitor.runtime WHERE time > '.(time - 60)*1000000000, 'SHOW DATABASES'], epoch => 's', ); print "$query\n"; =head1 SUBROUTINES/METHODS =head2 RETURN VALUES AND ERROR HANDLING C is relied upon for returning data from subroutines. The respective result object can always be used as string and evaluated on a boolean basis. A result object evaluating to false indicates an error and a corresponding error message is provided in the attribute C: my $ping = $influx->ping(); print $ping->error unless ($ping); Furthermore, all result objects provide access to the C object that is returned by InfluxDB in the attribute C. =head2 new host => 'localhost', port => 8086, timeout => 600 Passing C, C and/or C is optional, defaulting to the InfluxDB defaults or to 3 minutes for the timeout. The timeout is in seconds. Returns an instance of InfluxDB::HTTP. =head2 ping Pings the InfluxDB instance configured in the constructor (i.e. by C and C). Returned object evaluates to true or false depending on whether the ping was successful or not. If true, then it contains a C attribute that indicates the InfluxDB version running on the pinged server. The C attribute is extracted from the C HTTP response header, which is part of the HTTP response from the pinged InfluxDB instance. my $ping = $influx->ping(); print $ping->version if ($ping); =head2 query query, database => "DATABASE", chunk_size => CHUNK_SIZE, epoch => "ns" Used to query the InfluxDB instance. All parameters but the first one are optional. The C parameter can either be a String or a Perl ArrayRef of Strings, where every String contains a valid InfluxDB query. If the returned object evaluates to true, indicating that the query was successful, then the returned object's C attribute contains the entire response from InfluxDB as Perl hash. Additionally the attribute C provides the request identifier as set in the HTTP reponse headers by InfluxDB. This can for example be useful for correlating requests with log files. =head2 write measurement, database => "DATABASE", precision => "ns", retention_policy => "RP" Writes data into InfluxDB. The parameter C can either be a String or an ArrayRef of Strings, where each String contains one valid InfluxDB LineProtocol statement. All of those mesaurements are then sent to InfluxDB and the specified database. The returned object evaluates to true if the write was successful, and otherwise to false. The optional argument precision can be given if a precsion different than "ns" is used in the line protocol. InfluxDB docs suggest that using a coarser precision than ns can save space and processing. In many cases "s" or "m" might do. The optional argument retention_policy can be used to specify a retention policy other than the default retention policy of the selected database. =head2 get_lwp_useragent Returns the internally used LWP::UserAgent instance for possible modifications (e.g. to configure an HTTP proxy). =head1 AUTHOR Raphael Seebacher, C<< >> =head1 BUGS Please report any bugs or feature requests to L. =head1 LICENSE AND COPYRIGHT MIT License Copyright (c) 2016 Raphael Seebacher Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. =cut InfluxDB-HTTP-0.04/t/test.pl000755 000765 000024 00000012462 13323713472 015415 0ustar00rsestaff000000 000000 #!/usr/bin/perl use strict; use warnings; use Const::Fast; use JSON::MaybeXS; use File::Spec; use File::Basename; use File::Temp; use File::Slurper qw(write_text); use IPC::Run qw(run); use Test::More; use Test::Exception; BEGIN { sub get_directory_of_this_file { my (undef, $filename) = caller; return dirname(File::Spec->rel2abs( $filename )); } use lib get_directory_of_this_file() . '/../lib/'; } const my $PORT => 17755; const my $TMPDIR_HANDLE => File::Temp->newdir(CLEANUP => 1); const my $TMPDIR => $TMPDIR_HANDLE->dirname(); const my $TIME => time(); const my $DATABASE => "test_$TIME"; const my $FIELD => 'f1'; const my $TAGS => 'server=zrh01'; const my $VALUE => 123; const my $M_NAME => 'm_test'; const my $M_BASE => "$M_NAME,$TAGS"; const my $M_OK => $M_BASE.' '.$FIELD.'='.$VALUE; const my $M_BAD_TYPE => $M_BASE.' '.$FIELD.'="bad"'; const my $M_BAD_FORMAT => $M_BASE.$FIELD.'=bad'; const my $Q_OK => "SELECT LAST($FIELD) FROM $DATABASE.\"autogen\".$M_NAME"; sub main { my $influx_pid = setup_influx(); eval { require_ok( 'InfluxDB::HTTP' ); my $influx = InfluxDB::HTTP->new(port => $PORT); test_ping($influx); setup($influx); test_write($influx); test_query($influx); test_argument_checking($influx); cleanup($influx); done_testing(); }; diag($@) if $@; cleanup_influx($influx_pid); return; } sub setup_influx { my $conf = get_test_conf(); write_text("$TMPDIR/influx.conf", $conf); # check if influxd is found before forking eval { my $out_and_err; run(['influxd', 'version'], '>&', \$out_and_err); }; plan(skip_all => $@) if $@; my $pid; defined($pid = fork()) or die "unable to fork: $!\n"; if ($pid == 0) { exec("influxd -config $TMPDIR/influx.conf"); warn "unable to exec 'influxd -config $TMPDIR/influx.conf': $!\n"; exit 1; } sleep 1; # wait for influxdb to start return $pid; } sub cleanup_influx { my $pid = shift; kill 'KILL', $pid; return; } sub test_ping { my $influx = shift; my $ping = $influx->ping(); ok($ping, 'ping'); return; } sub setup { my $influx = shift; my $rv = $influx->query("CREATE DATABASE $DATABASE"); ok($rv, 'CREATE DATABASE'); return; } sub cleanup { my $influx = shift; my $rv = $influx->query("DROP DATABASE $DATABASE"); ok($rv, 'DROP DATABASE'); return; } sub test_write { my $influx = shift; my $rv; $rv = $influx->write($M_OK, database => $DATABASE, precision => 's'); ok($rv, 'successful write'); $rv = $influx->write($M_BAD_FORMAT, database => $DATABASE, precision => 's'); ok(!$rv, 'write with bad format'); $rv = $influx->write($M_BAD_TYPE, database => $DATABASE, precision => 's'); ok(!$rv, 'write with bad data type'); return; } sub test_query { my $influx = shift; my $rv = $influx->query($Q_OK, epoch => 's'); ok($rv, 'query'); # [{"series":[{"values":[[1530794448,123]],"columns":["time","last"],"name":"m_test"}],"statement_id":0}] is($rv->data()->{results}->[0]->{series}->[0]->{values}->[0]->[1], $VALUE, 'query result'); return; } sub test_argument_checking { my $influx = shift; dies_ok { $influx->query() } 'query: check argument query'; dies_ok { $influx->query('q', epoch => 'f') } 'query: check argument epoch'; dies_ok { $influx->write() } 'write: check argument measurement'; dies_ok { $influx->write('m') } 'write: check argument database'; dies_ok { $influx->write('q', database => 'd', precision => 'f') } 'write: check argument precision'; return; } # ------------------------------------------------------------------------------ sub get_test_conf { return <<"END"; reporting-disabled = true [logging] level = "warn" suppress-logo = true [meta] dir = "$TMPDIR/meta" retention-autocreate = true logging-enabled = true [data] dir = "$TMPDIR/data" engine = "tsm1" wal-dir = "$TMPDIR/wal" wal-logging-enabled = true query-log-enabled = true cache-max-memory-size = 0 max-points-per-block = 0 max-series-per-database = 0 max-values-per-tag = 0 data-logging-enabled = true index-version = "tsi1" [coordinator] write-timeout = "10s" max-concurrent-queries = 0 query-timeout = "0s" log-queries-after = "0s" max-select-point = 0 max-select-series = 0 max-select-buckets = 0 [retention] enabled = true check-interval = "30m0s" [shard-precreation] enabled = true check-interval = "10m0s" advance-period = "30m0s" [admin] enabled = false [monitor] store-enabled = true store-database = "_internal" store-interval = "10s" [subscriber] enabled = true http-timeout = "30s" [http] enabled = true bind-address = ":$PORT" auth-enabled = false log-enabled = false write-tracing = false https-enabled = false max-row-limit = 0 max-connection-limit = 0 shared-secret = "" realm = "InfluxDB" [[graphite]] enabled = false [[collectd]] enabled = false [[opentsdb]] enabled = false [[udp]] enabled = false [continuous_queries] log-enabled = true enabled = true run-interval = "1s" query-stats-enabled = true END } # ------------------------------------------------------------------------------ main;