AnyEvent-InfluxDB

 view release on metacpan or  search on metacpan

README  view on Meta::CPAN

9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
use EV;
 
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,
    );

README  view on Meta::CPAN

75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
    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 => {

README  view on Meta::CPAN

827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
  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 "https://metacpan.org/pod/username">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

6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
$AnyEvent::InfluxDB::VERSION = '1.0.2.0';
use URI;
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

51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
    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

1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
 
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

1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
            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

1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
use EV;
 
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

1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
=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

1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
    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

2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
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

9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
use EV;
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

236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
        }
    );
    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

9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
use EV;
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

251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
    );
    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: @_");
        }



( run in 0.276 second using v1.01-cache-2.11-cpan-3cd7ad12f66 )