AnyEvent-InfluxDB

 view release on metacpan or  search on metacpan

README  view on Meta::CPAN

NAME
    AnyEvent::InfluxDB - An asynchronous library for InfluxDB time-series
    database

VERSION
    version 1.0.2.0

SYNOPSIS
        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,
            );

            $hdl->push_read(
                line => sub {
                    my (undef, $line) = @_;

                    # Disk\t/=382MB;15264;15269;; /var=218MB;9443;9448
                    my ($measurement, $perfstring) = split(/\t/, $line);

                    my @perfdata
                        = Monitoring::Plugin::Performance->parse_perfstring($perfstring);

                    $db->write(
                        database => 'mydb',
                        data => [
                            map {
                                +{
                                    measurement => $measurement,
                                    tags => {
                                        label => $_->label,
                                    },
                                    fields => {
                                        value => $_->value,
                                        uom => '"'. $_->uom .'"',
                                    },
                                }
                            } @perfdata
                        ],
                        on_success => sub { print "$line written\n"; },
                        on_error => sub { print "$line error: @_\n"; },
                    );

                    $hdl->on_drain(
                        sub {
                            $hdl->fh->close;
                            undef $hdl;
                        }
                    );
                },
            );
        };

        EV::run;

DESCRIPTION
    Asynchronous client library for InfluxDB time-series database
    <https://influxdb.com>.

    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 => {
                verify => 1,
                verify_peername => 'https',
                ca_file => '/path/to/cacert.pem',
            }
        );

    As an debugging aid the "on_request" code reference may also be
    provided. It will be executed before each request with the method name,
    url and POST data if set.

        my $db = AnyEvent::InfluxDB->new(
            ...
            on_request => sub {
                my ($method, $url, $post_data) = @_;
                print "$method $url\n";
                print "$post_data\n" if $post_data;
            }
        );

  ping
        $cv = AE::cv;
        $db->ping(
            wait_for_leader => 2,

            on_success => $cv,
            on_error => sub {
                $cv->croak("Failed to ping cluster leader: @_");
            }
        );
        my $version = $cv->recv;

    Checks the leader of the cluster to ensure that the leader is available
    and ready. The optional parameter "wait_for_leader" specifies the number
    of seconds to wait before returning a response.

    The required "on_success" code reference is executed if request was
    successful with the value of "X-Influxdb-Version" response header as
    argument, otherwise executes the required "on_error" code reference with
    the value of "Reason" response header as argument.

  Managing Data
   write
        $cv = AE::cv;
        $db->write(
            database => 'mydb',
            precision => 's',
            rp => 'last_day',
            consistency => 'quorum',

            data => [

README  view on Meta::CPAN

            on_success => $cv,
            on_error => sub {
                $cv->croak("Failed to list tag values: @_");
            }
        );
        my $tag_values = $cv->recv;
        for my $measurement ( sort keys %{ $tag_values } ) {
            print "Measurement: $measurement\n";
            for my $tag_key ( sort keys %{ $tag_values->{$measurement} } ) {
                print "  Tag key: $tag_key\n";
                print "   * $_\n" for @{ $tag_values->{$measurement}->{$tag_key} };
            }
        }

    Returns a hash reference with measurements as keys and their unique tag
    values as values from database "database" and optional measurement
    "measurement" from a single tag key "key" or a list of tag keys "keys"
    with number of results limited to "limit" with offset "offset".

    The required "on_success" code reference is executed if request was
    successful, otherwise executes the required "on_error" code reference.

   show_field_keys
        $cv = AE::cv;
        $db->show_field_keys(
            database => 'mydb',

            # raw query
            q => "SHOW FIELD KEYS FROM cpu_load",

            # or query created from arguments
            measurement => 'cpu_load',

            # callbacks
            on_success => $cv,
            on_error => sub {
                $cv->croak("Failed to list field keys: @_");
            }
        );
        my $field_keys = $cv->recv;
        for my $measurement ( sort keys %{ $field_keys } ) {
            print "Measurement: $measurement\n";
            for my $field ( @{ $field_keys->{$measurement} } ) {
                print "  Key:  $field->{key}\n";
                print "  Type: $field->{type}\n";
            }
        }

    Returns a hash reference with measurements as keys and their field keys
    names and type as values from database "database" and optional
    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: @_");
            }
        );
        my @users = $cv->recv;
        for my $u ( @users ) {
            print "Name: $u->{user}\n";
            print "Admin?: $u->{admin}\n";
        }

    Returns a list of hash references with keys "user" and "admin" for each
    defined user.

    The required "on_success" code reference is executed if request was
    successful, otherwise executes the required "on_error" code reference.

   grant_privileges
        $cv = AE::cv;
        $db->grant_privileges(
            # raw query
            q => "GRANT ALL ON mydb TO jdoe",

            # or query created from arguments
            username => 'jdoe',

            # privileges at single database
            database => 'mydb',
            access => 'ALL',

            # or to grant cluster administration privileges
            all_privileges => 1,

            # callbacks
            on_success => $cv,
            on_error => sub {
                $cv->croak("Failed to grant privileges: @_");
            }
        );
        $cv->recv;

    Grants to user "username" access "access" on database "database". If
    flag "all_privileges" is set it grants cluster administration privileges
    instead.

    The required "on_success" code reference is executed if request was
    successful, otherwise executes the required "on_error" code reference.

   show_grants
        $cv = AE::cv;
        $db->show_grants(
            # raw query
            q => "SHOW GRANTS FOR jdoe",



( run in 1.012 second using v1.01-cache-2.11-cpan-75ffa21a3d4 )