view release on metacpan or search on metacpan
use EV;
use AnyEvent;
use AnyEvent::Socket;
use AnyEvent::Handle;
use AnyEvent::InfluxDB;
use Monitoring::Plugin::Performance;
my $db = AnyEvent::InfluxDB->new(
server => 'http://localhost:8086',
username => 'admin',
password => 'password',
);
my $hdl;
tcp_server undef, 8888, sub {
my ($fh, $host, $port) = @_;
$hdl = AnyEvent::Handle->new(
fh => $fh,
);
This version is meant to be used with InfluxDB v1.0.0 or newer.
METHODS
new
my $db = AnyEvent::InfluxDB->new(
server => 'http://localhost:8086',
# authenticate using Basic credentials
username => 'admin',
password => 'password',
# or use JWT token
jwt => 'JWT_TOKEN_BLOB'
);
Returns object representing given InfluDB "server" connected using
optionally provided username "username" and password "password".
Default value of "server" is "http://localhost:8086".
If the server protocol is "https" then by default no validation of
remote host certificate is performed. This can be changed by setting
"ssl_options" parameter with any options accepted by AnyEvent::TLS.
my $db = AnyEvent::InfluxDB->new(
...
ssl_options => {
measurement "measurement".
The required "on_success" code reference is executed if request was
successful, otherwise executes the required "on_error" code reference.
User Management
create_user
$cv = AE::cv;
$db->create_user(
# raw query
q => "CREATE USER jdoe WITH PASSWORD 'mypassword' WITH ALL PRIVILEGES",
# or query created from arguments
username => 'jdoe',
password => 'mypassword',
all_privileges => 1,
# callbacks
on_success => $cv,
on_error => sub {
$cv->croak("Failed to create user: @_");
}
);
$cv->recv;
Creates user with "username" and "password". If flag "all_privileges" is
set to true created user will be granted cluster administration
privileges.
Note: "password" will be automatically enclosed in single quotes.
The required "on_success" code reference is executed if request was
successful, otherwise executes the required "on_error" code reference.
set_user_password
$cv = AE::cv;
$db->set_user_password(
# raw query
q => "SET PASSWORD FOR jdoe = 'otherpassword'",
# or query created from arguments
username => 'jdoe',
password => 'otherpassword',
# callbacks
on_success => $cv,
on_error => sub {
$cv->croak("Failed to set password: @_");
}
);
$cv->recv;
Sets password to "password" for the user identified by "username".
Note: "password" will be automatically enclosed in single quotes.
The required "on_success" code reference is executed if request was
successful, otherwise executes the required "on_error" code reference.
show_users
$cv = AE::cv;
$db->show_users(
on_success => $cv,
on_error => sub {
$cv->croak("Failed to list users: @_");
lib/AnyEvent/InfluxDB.pm view on Meta::CPAN
$AnyEvent::InfluxDB::VERSION = '1.0.2.0';
use AnyEvent;
use AnyEvent::HTTP;
use URI;
use URI::QueryParam;
use JSON qw(decode_json);
use List::MoreUtils qw(zip);
use URI::Encode::XS qw( uri_encode );
use Moo;
has [qw( ssl_options username password jwt on_request )] => (
is => 'ro',
predicate => 1,
);
has 'server' => (
is => 'rw',
default => 'http://localhost:8086',
);
has '_is_ssl' => (
lib/AnyEvent/InfluxDB.pm view on Meta::CPAN
my ($self) = @_;
return $self->server =~ /^https/;
}
sub _build__server_uri {
my ($self) = @_;
my $url = URI->new( $self->server, 'http' );
if ( $self->has_username && $self->has_password ) {
$url->query_param( 'u' => $self->username );
$url->query_param( 'p' => $self->password );
}
return $url;
}
sub _make_url {
my ($self, $path, $params) = @_;
my $url = $self->_server_uri->clone;
$url->path($path);
lib/AnyEvent/InfluxDB.pm view on Meta::CPAN
sub create_user {
my ($self, %args) = @_;
my $q;
if ( exists $args{q} ) {
$q = $args{q};
} else {
$q = 'CREATE USER '. $args{username}
.' WITH PASSWORD \''. $args{password} .'\'';
$q .= ' WITH ALL PRIVILEGES' if $args{all_privileges};
}
my $url = $self->_make_url('/query', {
q => $q
});
$self->_http_request( POST => $url,
sub {
lib/AnyEvent/InfluxDB.pm view on Meta::CPAN
if ( $headers->{Status} eq '200' ) {
$args{on_success}->();
} else {
$args{on_error}->( $body );
}
}
);
}
sub set_user_password {
my ($self, %args) = @_;
my $q;
if ( exists $args{q} ) {
$q = $args{q};
} else {
$q = 'SET PASSWORD FOR '. $args{username}
.' = \''. $args{password} .'\'';
}
my $url = $self->_make_url('/query', {
q => $q
});
$self->_http_request( POST => $url,
sub {
my ($body, $headers) = @_;
lib/AnyEvent/InfluxDB.pm view on Meta::CPAN
use EV;
use AnyEvent;
use AnyEvent::Socket;
use AnyEvent::Handle;
use AnyEvent::InfluxDB;
use Monitoring::Plugin::Performance;
my $db = AnyEvent::InfluxDB->new(
server => 'http://localhost:8086',
username => 'admin',
password => 'password',
);
my $hdl;
tcp_server undef, 8888, sub {
my ($fh, $host, $port) = @_;
$hdl = AnyEvent::Handle->new(
fh => $fh,
);
lib/AnyEvent/InfluxDB.pm view on Meta::CPAN
=head1 METHODS
=head2 new
my $db = AnyEvent::InfluxDB->new(
server => 'http://localhost:8086',
# authenticate using Basic credentials
username => 'admin',
password => 'password',
# or use JWT token
jwt => 'JWT_TOKEN_BLOB'
);
Returns object representing given InfluDB C<server> connected using optionally
provided username C<username> and password C<password>.
Default value of C<server> is C<http://localhost:8086>.
If the server protocol is C<https> then by default no validation of remote
host certificate is performed. This can be changed by setting C<ssl_options>
parameter with any options accepted by L<AnyEvent::TLS>.
my $db = AnyEvent::InfluxDB->new(
...
ssl_options => {
lib/AnyEvent/InfluxDB.pm view on Meta::CPAN
my $db = AnyEvent::InfluxDB->new(
...
on_request => sub {
my ($method, $url, $post_data) = @_;
print "$method $url\n";
print "$post_data\n" if $post_data;
}
);
=for Pod::Coverage has_jwt jwt has_on_request has_password has_ssl_options has_username on_request password server ssl_options username
=head2 ping
$cv = AE::cv;
$db->ping(
wait_for_leader => 2,
on_success => $cv,
on_error => sub {
$cv->croak("Failed to ping cluster leader: @_");
lib/AnyEvent/InfluxDB.pm view on Meta::CPAN
The required C<on_success> code reference is executed if request was successful,
otherwise executes the required C<on_error> code reference.
=head2 User Management
=head3 create_user
$cv = AE::cv;
$db->create_user(
# raw query
q => "CREATE USER jdoe WITH PASSWORD 'mypassword' WITH ALL PRIVILEGES",
# or query created from arguments
username => 'jdoe',
password => 'mypassword',
all_privileges => 1,
# callbacks
on_success => $cv,
on_error => sub {
$cv->croak("Failed to create user: @_");
}
);
$cv->recv;
Creates user with C<username> and C<password>. If flag C<all_privileges> is set
to true created user will be granted cluster administration privileges.
Note: C<password> will be automatically enclosed in single quotes.
The required C<on_success> code reference is executed if request was successful,
otherwise executes the required C<on_error> code reference.
=head3 set_user_password
$cv = AE::cv;
$db->set_user_password(
# raw query
q => "SET PASSWORD FOR jdoe = 'otherpassword'",
# or query created from arguments
username => 'jdoe',
password => 'otherpassword',
# callbacks
on_success => $cv,
on_error => sub {
$cv->croak("Failed to set password: @_");
}
);
$cv->recv;
Sets password to C<password> for the user identified by C<username>.
Note: C<password> will be automatically enclosed in single quotes.
The required C<on_success> code reference is executed if request was successful,
otherwise executes the required C<on_error> code reference.
=head3 show_users
$cv = AE::cv;
$db->show_users(
on_success => $cv,
on_error => sub {
xt/influxdb-q.t view on Meta::CPAN
use EV;
use AnyEvent;
use JSON;
my $true = JSON::true;
my $false = JSON::false;
my $db = AnyEvent::InfluxDB->new(
server => $ENV{INFLUXDB_SERVER} || 'http://127.0.0.1:8086',
username => 'admin',
password => 'admin',
);
# random data
my @measurements = qw(cpu_load mem_free cpu_temp disk_free);
my @regions = qw(us-east us-west eu-east eu-east);
my @hosts = map { sprintf('server%02d', $_) } 1 .. 10;
my @fields = map { sprintf('field%02d', $_) } 1 .. 10;
my $_15days_ago = time() - int(15 * 24 * 3600);
my $existing_region;
xt/influxdb-q.t view on Meta::CPAN
}
);
ok($cv->recv, "cq per5minutes dropped");
}
{
note "=== create_user ===";
$cv = AE::cv;
$db->create_user(
q => "CREATE USER jdoe WITH PASSWORD 'mypassword'",
on_success => sub { $cv->send("test ok") },
on_error => sub {
$cv->croak("Failed to create user: @_");
}
);
ok($cv->recv, "user created");
}
{
note "=== set_user_password ===";
$cv = AE::cv;
$db->set_user_password(
q => "SET PASSWORD FOR jdoe = 'otherpassword'",
on_success => sub { $cv->send("test ok") },
on_error => sub {
$cv->croak("Failed to set password: @_");
}
);
ok($cv->recv, "password changed");
}
{
note "=== show_users ===";
$cv = AE::cv;
$db->show_users(
on_success => $cv,
on_error => sub {
$cv->croak("Failed to list users: @_");
}
xt/influxdb.t view on Meta::CPAN
use EV;
use AnyEvent;
use JSON;
my $true = JSON::true;
my $false = JSON::false;
my $db = AnyEvent::InfluxDB->new(
server => $ENV{INFLUXDB_SERVER} || 'http://127.0.0.1:8086',
username => 'admin',
password => 'admin',
);
# random data
my @measurements = qw(cpu_load mem_free cpu_temp disk_free);
my @regions = qw(us-east us-west eu-east eu-east);
my @hosts = map { sprintf('server%02d', $_) } 1 .. 10;
my @fields = map { sprintf('field%02d', $_) } 1 .. 10;
my $_15days_ago = time() - int(15 * 24 * 3600);
my $existing_region;
xt/influxdb.t view on Meta::CPAN
);
ok($cv->recv, "cq per5minutes dropped");
}
{
note "=== create_user ===";
$cv = AE::cv;
$db->create_user(
username => 'jdoe',
password => 'mypassword',
on_success => sub { $cv->send("test ok") },
on_error => sub {
$cv->croak("Failed to create user: @_");
}
);
ok($cv->recv, "user created");
}
{
note "=== set_user_password ===";
$cv = AE::cv;
$db->set_user_password(
username => 'jdoe',
password => 'otherpassword',
on_success => sub { $cv->send("test ok") },
on_error => sub {
$cv->croak("Failed to set password: @_");
}
);
ok($cv->recv, "password changed");
}
{
note "=== show_users ===";
$cv = AE::cv;
$db->show_users(
on_success => $cv,
on_error => sub {
$cv->croak("Failed to list users: @_");
}