Coder Social home page Coder Social logo

influxdb-php's Introduction

The v1 client libraries for InfluxDB were typically developed and maintained by community members. They have all now been succeeded by v2 client libraries. They are being archived it in favor of the v2 client library. See #171.

If there are still users of this v1 client library, and they or somebody else are willing to keep them updated with security fixes at a minimum please reach out on the Community Forums or InfluxData Slack.

influxdb-php

InfluxDB client library for PHP

Build Status Code Climate Test Coverage

Note: This library is for use with InfluxDB 1.x. For connecting to InfluxDB 2.x instances, please use the influxdb-client-php client.

Overview

A easy to use library for using InfluxDB with PHP. Maintained by @thecodeassassin, @gianarb.

The influxdb-php library was created to have php port of the python influxdb client. This way there will be a common abstraction library between different programming languages.

Installation

Installation can be done with composer:

$ composer require influxdb/influxdb-php

NOTE for PHP 5.3 and PHP 5.4 users

If you use either PHP 5.3 and PHP 5.4, the 0.1.x release is still supported (bug fixes and new release fixes). The 0.1.x branch will work on PHP 5.3 and PHP 5.4 but doesn't contain all the features that the 1.0.0 release has such as UDP support.

Getting started

Initialize a new client object:

$client = new InfluxDB\Client($host, $port);

This will create a new client object which you can use to read and write points to InfluxDB.

It's also possible to create a client from a DSN (Data Source Name):

// directly get the database object
$database = InfluxDB\Client::fromDSN(sprintf('influxdb://user:pass@%s:%s/%s', $host, $port, $dbname));

// get the client to retrieve other databases
$client = $database->getClient();

Important: don't forget to urlencode() the password (and username for that matter) when using a DSN, especially if it contains non-alphanumeric characters. Not doing so might cause exceptions to be thrown.

Reading data

To fetch records from InfluxDB you can do a query directly on a database:

// fetch the database
$database = $client->selectDB('influx_test_db');

// executing a query will yield a resultset object
$result = $database->query('select * from test_metric LIMIT 5');

// get the points from the resultset yields an array
$points = $result->getPoints();

It's also possible to use the QueryBuilder object. This is a class that simplifies the process of building queries.

// retrieve points with the query builder
$result = $database->getQueryBuilder()
	->select('cpucount')
	->from('test_metric')
	->limit(2)
	->offset(2)
	->getResultSet()
	->getPoints();

// get the query from the QueryBuilder
$query = $database->getQueryBuilder()
	->select('cpucount')
	->from('test_metric')
	->where(["region = 'us-west'"])
	->getQuery();

Make sure that you enter single quotes when doing a where query on strings; otherwise InfluxDB will return an empty result.

You can get the last executed query from the client:

// use the getLastQuery() method
$lastQuery = $client->getLastQuery();

// or access the static variable directly:
$lastQuery = Client::lastQuery;

Reading data using a timeout

In production if you are querying InfluxDB to generate a response to a web or API request, you may want to set a specific timeout for InfluxDB calls rather than the default of letting them run indefinitely.

// Fetch the database using a 5 second time out
$database = InfluxDB\Client::fromDSN(sprintf('influxdb://user:pass@%s:%s/%s', $host, $port, $dbname), 5);

Writing data

Writing data is done by providing an array of points to the writePoints method on a database:

// create an array of points
$points = array(
	new Point(
		'test_metric', // name of the measurement
		0.64, // the measurement value
		['host' => 'server01', 'region' => 'us-west'], // optional tags
		['cpucount' => 10], // optional additional fields
		1435255849 // Time precision has to be set to seconds!
	),
    new Point(
    	'test_metric', // name of the measurement
		0.84, // the measurement value
		['host' => 'server01', 'region' => 'us-west'], // optional tags
		['cpucount' => 10], // optional additional fields
		1435255849 // Time precision has to be set to seconds!
	)
);

// we are writing unix timestamps, which have a second precision
$result = $database->writePoints($points, Database::PRECISION_SECONDS);

It's possible to add multiple fields when writing measurements to InfluxDB. The point class allows one to easily write data in batches to influxDB.

The name of a measurement and the value are mandatory. Additional fields, tags and a timestamp are optional. InfluxDB takes the current time as the default timestamp.

You can also write multiple fields to a measurement without specifying a value:

$points = [
	new Point(
		'instance', // the name of the measurement
		null, // measurement value
		['host' => 'server01', 'region' => 'us-west'], // measurement tags
		['cpucount' => 10, 'free' => 1], // measurement fields
		exec('date +%s%N') // timestamp in nanoseconds on Linux ONLY
	),
	new Point(
		'instance', // the name of the measurement
		null, // measurement value
		['host' => 'server01', 'region' => 'us-west'], // measurement tags
		['cpucount' => 10, 'free' => 2], // measurement fields
		exec('date +%s%N') // timestamp in nanoseconds on Linux ONLY
	)
];

Writing data using udp

First, set your InfluxDB host to support incoming UDP sockets:

[udp]
  enabled = true
  bind-address = ":4444"
  database = "test_db"

Then, configure the UDP driver in the client:

// set the UDP driver in the client
$client->setDriver(new \InfluxDB\Driver\UDP($client->getHost(), 4444));

$points = [
	new Point(
		'test_metric',
		0.84,
		['host' => 'server01', 'region' => 'us-west'],
		['cpucount' => 10],
		exec('date +%s%N') // this will produce a nanosecond timestamp on Linux ONLY
	)
];

// now just write your points like you normally would
$result = $database->writePoints($points);

Or simply use a DSN (Data Source Name) to send metrics using UDP:

// get a database object using a DSN (Data Source Name)
$database = \InfluxDB\Client::fromDSN('udp+influxdb://username:pass@localhost:4444/test123');

// write your points
$result = $database->writePoints($points);

Note: It is import to note that precision will be ignored when you use UDP. You should always use nanosecond precision when writing data to InfluxDB using UDP.

Timestamp precision

It's important to provide the correct precision when adding a timestamp to a Point object. This is because if you specify a timestamp in seconds and the default (nanosecond) precision is set; the entered timestamp will be invalid.

// Points will require a nanosecond precision (this is default as per influxdb standard)
$newPoints = $database->writePoints($points);

// Points will require second precision
$newPoints = $database->writePoints($points, Database::PRECISION_SECONDS);

// Points will require microsecond precision
$newPoints = $database->writePoints($points, Database::PRECISION_MICROSECONDS);

Please note that exec('date + %s%N') does NOT work under MacOS; you can use PHP's microtime to get a timestamp with microsecond precision, like such:

list($usec, $sec) = explode(' ', microtime());
$timestamp = sprintf('%d%06d', $sec, $usec*1000000);

Creating databases

When creating a database a default retention policy is added. This retention policy does not have a duration so the data will be flushed with the memory.

This library makes it easy to provide a retention policy when creating a database:

// create the client
$client = new \InfluxDB\Client($host, $port, '', '');

// select the database
$database = $client->selectDB('influx_test_db');

// create the database with a retention policy
$result = $database->create(new RetentionPolicy('test', '5d', 1, true));

// check if a database exists then create it if it doesn't
$database = $client->selectDB('test_db');

if (!$database->exists()) {
	$database->create(new RetentionPolicy('test', '1d', 2, true));
}

You can also alter retention policies:

$database->alterRetentionPolicy(new RetentionPolicy('test', '2d', 5, true));

and list them:

$result = $database->listRetentionPolicies();

You can add more retention policies to a database:

$result = $database->createRetentionPolicy(new RetentionPolicy('test2', '30d', 1, true));

Client functions

Some functions are too general for a database. So these are available in the client:

// list users
$result = $client->listUsers();

// list databases
$result = $client->listDatabases();

Admin functionality

You can use the client's $client->admin functionality to administer InfluxDB via the API.

// add a new user without privileges
$client->admin->createUser('testuser123', 'testpassword');

// add a new user with ALL cluster-wide privileges
$client->admin->createUser('admin_user', 'password', \InfluxDB\Client\Admin::PRIVILEGE_ALL);

// drop user testuser123
$client->admin->dropUser('testuser123');

List all the users:

// show a list of all users
$results = $client->admin->showUsers();

// show users returns a ResultSet object
$users = $results->getPoints();

Granting and revoking privileges

Granting permissions can be done on both the database level and cluster-wide. To grant a user specific privileges on a database, provide a database object or a database name.

// grant permissions using a database object
$database = $client->selectDB('test_db');
$client->admin->grant(\InfluxDB\Client\Admin::PRIVILEGE_READ, 'testuser123', $database);

// give user testuser123 read privileges on database test_db
$client->admin->grant(\InfluxDB\Client\Admin::PRIVILEGE_READ, 'testuser123', 'test_db');

// revoke user testuser123's read privileges on database test_db
$client->admin->revoke(\InfluxDB\Client\Admin::PRIVILEGE_READ, 'testuser123', 'test_db');

// grant a user cluster-wide privileges
$client->admin->grant(\InfluxDB\Client\Admin::PRIVILEGE_READ, 'testuser123');

// Revoke an admin's cluster-wide privileges
$client->admin->revoke(\InfluxDB\Client\Admin::PRIVILEGE_ALL, 'admin_user');

Todo

  • More unit tests
  • Increase documentation (wiki?)
  • Add more features to the query builder
  • Add validation to RetentionPolicy

Changelog

1.15.0

  • Added cURL driver support #122 (thanks @aldas)
  • Improved query error message #129 (thanks @andreasanta)

1.14.8

  • Merged #122

1.14.7

  • Added offset in QueryBuilder (thanks @lifekent and @BentCoder)

1.14.6

  • dependencies update (#97), by @aldas
  • Adding timeout information. (#103), by @NickBusey
  • Add ability to specify connect_timeout for guzzle (#105), by @brycefranzen

1.14.5

  • Update key concepts link to point to the proper place.
  • Replace costly array_merge calls with foreach + array operator
  • Add getter method for verifySSL
  • Support for Symfony 4

1.14.3

  • Deprecate IF NOT EXISTS clause in database creation

1.14.2

  • Fix Notice when calling InfluxDB\Client::fromDSN without username or password
  • fixed Guzzle client timeout is float
  • Fix annotation
  • Remove unused property
  • Fixed misspelling
  • Fixed tag with Boolean/Null value trigger parse error

1.4.1

  • Fixed bug: Escape field values as per line protocol.

1.4.0

  • Updating Influx Database with support for writing direct payloads, thanks @virgofx

1.3.1

  • Added ability to write data to a specific retention policy, thanks @virgofx !

1.3.0

  • Added quoting of dbname in queries
  • Added orderBy to query builder
  • Fixed wrong orderby tests
  • Travis container-infra and php 7

1.2.2

  • Fixed issue with listUsers() method
  • Added more unit tests
  • Added getColumns method to \InfluxDB\ResultSet

1.2.0

  • Added support for 32 bit systems
  • Added setters/getters for Point fields

1.1.3

  • Added support for symfony3

1.1.2

  • Fixed issue with authentication when writing data

1.1.1

  • Added support for 0.9.4
  • Added if not exists support to database->create()
  • Added getLastQuery method

1.1.0

  • Added support for 0.9.3 rc2
  • Changed the way we handle the datatypes of values
  • Changed list retention policies to reflect the changes in 0.9.3

1.0.1

  • Added support for authentication in the guzzle driver
  • Added admin functionality

1.0.0

  • -BREAKING CHANGE- Dropped support for PHP 5.3 and PHP 5.4
  • Allowing for custom drivers
  • UDP support

0.1.2

  • Added exists method to Database class
  • Added time precision to database class

0.1.1

  • Merged repository to influxdb/influxdb-php
  • Added unit test for createRetentionPolicy
  • -BREAKING CHANGE- changed $client->db to $client->selectDB

influxdb-php's People

Contributors

aldas avatar andig avatar andreyvital avatar beckettsean avatar clwells avatar curry684 avatar danibrutal avatar dependabot[bot] avatar enumag avatar eugenegp avatar gianarb avatar igusev avatar jcrombez avatar jdstrand avatar leonboot avatar lifekent avatar mermetbt avatar nickbusey avatar nyholm avatar pauldix avatar pdelre avatar pprkut avatar russorat avatar sc0rp10 avatar sergeyklay avatar skazi0 avatar stefanotorresi avatar thecodeassassin avatar tuupola avatar zorac avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

influxdb-php's Issues

0.1.x Release doesn't support HTTP Auth

It's a pretty simple fix - adding this line to the constructor:

$this->httpClient->setDefaultOption('auth', array($this->username, $this->password));

However - I couldn't see how to do a PR to a correct branch for the 0.1.x releases.

For anyone who finds this issue as well, I got around it by just extending the base class:

class FixedInfluxDBClient extends Client {
  public function __construct($host,$port = 8086,$username = '',$password = '',$ssl = false,$verifySSL = true,$timeout = 0) {
    parent::__construct($host,$port,$username,$password,$ssl,$verifySSL,$timeout);
    $this->httpClient->setDefaultOption('auth', array($this->username, $this->password));
  }
}

An interesting starting point?

Hi! Thanks for your job at InfluxDB, it is a nice tool!

We use it in ours applications written in PHP.
I'm here to underline ours influxdb-php-sdk and to submit it to influxdb community.
This SDK now support UDP and TPC (Guzzle) protocols but exist standard interface to write your implementation. It's fully tested and very simple to use it and extendable.

  1. Use composer to manage dependency
composer require corley/influxdb-sdk
  1. Create your client
$options = new Options();
$adapter = new UdpAdapter($options);

$client = new Client();
$client->setAdapter($adapter);
  1. Mark your points
$client->mark("app.search", [
    "key" => "this is my search"
]);

It's very integrable with dependency injection containers like SymfonyDiC or any other framework.

See README.md

What do you think about it?

Manage connect exception

I simulated an InfluxDB server crash to see how my app work.

I got an exception directly from Guzzle library:

image

Guzzle is used by your library but not by the user. The end use should rely to your exception only IMHO.

If think it would be better to have you own generic exception for that.

What do you think?

[Enhancement] Allow time precision setting when creating database object

I'm writing an API which contains a lot of queries using this library. As I need to set the precision for every call to query, it increases the verbosity of my code.

It would be beneficial to be able to set the precision once, with the option of overriding the setting if precision is set on a specific call to query.

Auto update on packagist

Hello @thecodeassassin do you have permission to add the packagist's token in order to sync in automatic our update on packagist?

@virgofx thanks for your ping

Thanks a lot
PS at the moment the 1.4.0 is not up to date on packagist

Exception on GuzzleHttp fopen on Debian 8.0 AMD64 and InfluxDB 0.12.2

I'm having issue not able to create a database object where a exception is thrown:

os: Debian 8.4 AMD64 jessie
php-cli (from debian repo): PHP 5.6.20-0+deb8u1 (cli) (built: Apr 27 2016 11:26:05)
influxdb (from influxdata repo): 0.12.2

The snippet (the demo as the README.md):

<?php
        require_once "vendor/autoload.php";

        use InfluxDB\Client;
        use InfluxDB\Point;
        use InfluxDB\Database;

        $host = "localhost";
        $port = 8086;
        $user = "endurance";
        $pass = "endurance";
        $dbname = "endurance";

        $client = new InfluxDB\Client($host, $port);
        $db = $client->selectDB($dbname);

        // create an array of points
        $points = array(
                new Point(
                        'test_metric', // name of the measurement
                        0.64, // the measurement value
                        ['host' => 'server01', 'region' => 'us-west'], // optional tags
                        ['cpucount' => 10], // optional additional fields
                        1435255849 // Time precision has to be set to seconds!
                ),
                new Point(
                'test_metric', // name of the measurement
                        0.84, // the measurement value
                        ['host' => 'server01', 'region' => 'us-west'], // optional tags
                        ['cpucount' => 10], // optional additional fields
                        1435255849 // Time precision has to be set to seconds!
                )
        );

        // we are writing unix timestamps, which have a second precision
        $result = $db->writePoints($points, Database::PRECISION_SECONDS);
?>

The error:

PHP Fatal error:  Uncaught exception 'InfluxDB\Exception' with message 'Error creating resource: [message] fopen(http://localhost:8086/write?db=endurance&precision=s): failed to open stream: HTTP request failed!
[file] /vendor/guzzlehttp/guzzle/src/Handler/StreamHandler.php
[line] 287' in vendor/influxdb/influxdb-php/src/InfluxDB/Database.php:144
Stack trace:
#0 writetest.php(36): InfluxDB\Database->writePoints(Array, 's')
#1 {main}
  thrown in vendor/influxdb/influxdb-php/src/InfluxDB/Database.php on line 144

I actually see something flowing on the wire, while still the exception is thrown....:

$ sudo tcpflow -i lo -c  'port 8086'
tcpflow: listening on lo
::1.45219-::1.08086: POST /write?db=endurance&precision=s HTTP/1.1

::1.45219-::1.08086: Content-Length: 151
User-Agent: GuzzleHttp/6.2.0 PHP/5.6.20-0+deb8u1
Host: localhost:8086
Connection: close
Content-Type:
::1.45219-::1.08086:

::1.45219-::1.08086:

::1.45219-::1.08086: test_metric,host=server01,region=us-west cpucount=10i,value=0.64 1435255849
test_metric,host=server01,region=us-west cpucount=10i,value=0.84 1435255849
::1.08086-::1.45219: HTTP/1.1 204 No Content
Request-Id: be4c3e00-0dfa-11e6-aeba-000000000000
X-Influxdb-Version: 0.12.2
Date: Fri, 29 Apr 2016 11:08:46 GMT
Connection: close

Maybe someone can test this also on a vanilla debian, as we digged deep into the GuzzleHttp lib and had no clue...

Select Retention Policy for Writes

I didn't yet find a way to select a retention policy when writing points (at least in 0.1 branch, as I'm using PHP 5.3 on RHEL6).
Possible solution: Add rp parameter to HTTP request.
I'll probably create a PR that adds this functionality via an additional (default=null) parameter to Client::write

Adding new points in intuitive manner

Seems more intuitive to me how https://github.com/corley/influxdb-php-sdk is adding new points:

$client->mark([
    "tags" => [
        "dc" => "eu-west-1",
    ],
    "points" => [
        [
            "measurement" => "instance",
            "fields" => [
                "cpu" => 18.12,
                "free" => 712423,
            ],
        ],
    ]
]);

Compared to what we have in the README example here:

    $points = array(
        new Point(
            'test_metric',
            0.64,
            array('host' => 'server01', 'region' => 'us-west'),
            array('cpucount' => 10),
            1435255849
        )
    );

    $newPoints = $database->writePoints($points, Database::PRECISION_SECONDS);

Can we do the same on this repo? It is relatively hard for someone to understand InfluxDB from your example while example at the top makes it much easier. I, myself, don't even know how to rewrite example at the to work with this library.

writing multiple measurements with differents specified timestamp

I try to write multiple measurements, each with a specified time (not the time from the moment when it's inserted) but I feel the signature doesn't allow this.
prepareMessageSection() in src/Adapter/WriterTrait.php only accepts one "time" key in array, so all inserted measurements will have the same timestamp

any way to insert multiple measurments with their own timestamp ?
it's possible querying directly (with cUrl for example)

Change `exec('date + %s%N')` to `time()` in README.md

I was just using this api and tried exec('date + %s%N') as described in the README. This did not work for me, then I looked further, I saw that its also possible to do:

list($usec, $sec) = explode(' ', microtime());
$timestamp = sprintf('%d%06d', $sec, $usec*1000000);

But AFAIK this is the same was time(). (Please correct me if I am wrong)

Would you mind if I open a PR to change this?

Safe query parameters

Hi there,

I've been looking around for a safe way to inject variables into queries using $database->query(...) but haven't come across anything (and looking through the code, it doesn't appear that this has been implemented. This is a little worrisome considering SQL injects can cause all sorts of nasty attacks and are very easily preventable.

Is there any way we could get something like the following in order to prevent injection attacks?

$database->query("SELECT * FROM measurement WHERE something=? AND other=?", [$something, $other]);

Let me know if I'm missing anything/this is already implemented and thanks a bunch for creating Influx!

Support group by *

Hello,
Currently, Influxdb-php not support the arguments 'group by *'
Here is a sample output with and without group by:
Exemple of request :
$QueryGTD = $database_gtd->query('select * from '.$mesurement_gtd.' order by time desc LIMIT 1'); $QSeriesGtd = $database_gtd->query('select * from '.$mesurement_gtd.' group by * order by time desc LIMIT 1');

var_dump($QueryGTD)
object(InfluxDB \ ResultSet) # 155(1) { ["parsedResults" : protected] = > array(1) { ["results"] = > array(1) { [0] = > array(1) { ["series"] = > array(1) { [0] = > array(3) { ["name"] = > string(3)"gtd" ["columns"] = > array(3) { [0] = > string(4)"time"[1] = > string(8)"platform"[2] = > string(5)"value" } ["values"] = > array(1) { [0] = > array(3) { [0] = > string(20)"2016-07-05T07:48:47Z" [1] = > string(9)"OpenStack" [2] = > int(100) } } } } } } } }

var_dump($QSeriesGtd)
object(InfluxDB \ ResultSet) # 153(1) { ["parsedResults" : protected] = > array(1) { ["results"] = > array(1) { [0] = > array(1) { ["series"] = > array(1) { [0] = > array(4) { ["name"] = > string(3)"gtd" ["tags"] = > array(1) { ["platform"] = > string(9)"OpenStack" } ["columns"] = > array(2) { [0] = > string(4)"time"[1] = > string(5)"value" } ["values"] = > array(1) { [0] = > array(2) { [0] = > string(20)"2016-07-05T07:48:47Z" [1] = > int(100) } } } } } } } }

Paced releases

Would it be possible to deploy regular updates in a similar way with influxdb or telegraf. That'd make us become more confident with the library. It's a shame we do not get enough incentives to switch from the wonderful https://github.com/corley/influxdb-php-sdk to this library, which is closer to the core influxdb project.

Return of listUsers()

Problem

The return of list listUsers() is name of the columns instread of a list of users.

Actual behavior:

Array( 0 => user , 1 => admin)

Expected behavior:

Array( 0 => array( admin => FALSE, user => test_dev), 1 => array( admin => FALSE, user => test_user))

In attachment the patch to fix the problem (IMHO).

fix-listUsers.txt

No access to Point class fields

Hi there, thanks for robust library.

I'm facing problem that I have no way to access data stored into Point instance. All fields of Point are private and have no getters. I need that because my code is built around creating array of Point instances in one place, and in other place I need to update some items of array with another Point instances.

Could fields of Point be at least protected or have getters? Having them protected would perfectly satisfy me, I'd inherit from it and add getters myself.

Doing a group by removes the targeted field

Concrete case: I want to get the last point of each disk partition. So I will group the results by path.

With the following query:

$qb = $this->database->getQueryBuilder()
    ->select('*')
    ->from('disk')
    ->groupBy('path')
    ->limit(1)
    ->orderBy('time', 'DESC')
    ->where(["host = '".$host."'"]);

$result = $qb->getResultSet()->getPoints();

dump($result);

I got:

array:5 [โ–ผ
  0 => array:10 [โ–ผ
    "time" => "2016-06-02T16:01:40Z"
    "free" => 16423927808
    "fstype" => "aufs"
    "host" => "server-01"
    "inodes_free" => 3072403
    "inodes_total" => 5619712
    "inodes_used" => 2547309
    "total" => 90449113088
    "used" => 69407010816
    "used_percent" => 80.864792962421
  ]
  1 => array:10 [โ–ผ
    "time" => "2016-06-02T16:01:40Z"
    "free" => 16423927808
    "fstype" => "ext4"
    "host" => "server-01"
    "inodes_free" => 3072403
    "inodes_total" => 5619712
    "inodes_used" => 2547309
    "total" => 90449113088
    "used" => 69407010816
    "used_percent" => 80.864792962421
  ]
  2 => array:10 [โ–ผ
    "time" => "2016-06-02T16:01:40Z"
    "free" => 16423927808
    "fstype" => "ext4"
    "host" => "server-01"
    "inodes_free" => 3072403
    "inodes_total" => 5619712
    "inodes_used" => 2547309
    "total" => 90449113088
    "used" => 69407010816
    "used_percent" => 80.864792962421
  ]
  3 => array:10 [โ–ผ
    "time" => "2016-06-02T16:01:40Z"
    "free" => 16423927808
    "fstype" => "ext4"
    "host" => "server-01"
    "inodes_free" => 3072403
    "inodes_total" => 5619712
    "inodes_used" => 2547309
    "total" => 90449113088
    "used" => 69407010816
    "used_percent" => 80.864792962421
  ]
  4 => array:10 [โ–ผ
    "time" => "2016-06-02T16:01:40Z"
    "free" => 16423927808
    "fstype" => "ext4"
    "host" => "server-01"
    "inodes_free" => 3072403
    "inodes_total" => 5619712
    "inodes_used" => 2547309
    "total" => 90449113088
    "used" => 69407010816
    "used_percent" => 80.864792962421
  ]
]

And the path field is missing.

So for me, the result is completely useless because I don't know on which part each point is attached.

I think there is two ways to fix it:

  • The quick and BC way: Simply restore the path field to the result.
  • The eloquent but BC break way: replace numeric key of the array by the groupBy field value. This could be done on next major.

Active development

Dear influxDB,

My team and at leaseweb i are creating a library for php that is based on the python library and should work the same way. Do you want us to create the library independently or can we have push rights to this repo?

Our repo (work in progress): https://github.com/LeaseWeb/influxdb-php

Failed to show database when calling exists()

When calling exists() to check a DB, PHP will throw fatal error:

PHP Fatal error: Uncaught exception 'RuntimeException' with message 'Error creating resource: [message] fopen(http://127.0.0.1:8086/query?q=SHOW+DATABASES): failed to open stream: HTTP request failed!
[file] /project/lib/vendor/guzzlehttp/guzzle/src/Handler/StreamHandler.php
[line] 282' in /project/lib/vendor/guzzlehttp/guzzle/src/Handler/StreamHandler.php:222
Stack trace:
#0 /project/lib/vendor/guzzlehttp/guzzle/src/Handler/StreamHandler.php(286): GuzzleHttp\Handler\StreamHandler->createResource(Object(Closure))
#1 /project/lib/vendor/guzzlehttp/guzzle/src/Handler/StreamHandler.php(52): GuzzleHttp\Handler\StreamHandler->createStream(Object(GuzzleHttp\Psr7\Request), Array)
#2 /project/lib/vendor/guzzlehttp/guzzle/src/PrepareBodyMiddleware.php(42): GuzzleHttp\Handler\StreamHandler->__invoke(Object(GuzzleHttp\Psr7\Request), Array)
#3 /project/lib/vendor/guzzlehttp/guzzle/src/Middleware.php(30): GuzzleHttp\PrepareBodyMiddleware in /project/lib/vendor/guzzlehttp/guzzle/src/Exception/RequestException.php on line 51


I can manually use CURL or wget to successfully retrieve:
http://127.0.0.1:8086/query?q=SHOW+DATABASES
but not from influxdb-php's exists().

It seems the error related with guzzle 6.
Is there any one has similar issue like this?

Support for 0.9.5

Recently upgraded to 0.9.5, mostly for its tsm1 engine that saves space. Are there breaking changes that need to be addressed in this library?

UDP/IP support

InfluxDB supports UDP/IP and I believe that using UDP is much lighter, for both the server and the client. Are you going to implement support for UDP? Tx.

why very slow to write data use udp ???

use InfluxDB\Point;

// directly get the database object
$database = InfluxDB\Client::fromDSN(sprintf('influxdb://user:pass@%s:%s/%s', '127.0.0.1', 8086, 'test_db'));

// get the client to retrieve other databases
$client = $database->getClient();

// set the UDP driver in the client
$client->setDriver(new \InfluxDB\Driver\UDP($client->getHost(), 4444));

for($i=0;$i<1000000;$i++){
$points = [
new Point(
'test_metric',
0.84,
['host' => 'server01', 'region' => 'us-west'],
['cpucount' => 10],
exec('date +%s%N') // this will produce a nanosecond timestamp in Linux operating systems
)
];

$result = $database->writePoints($points);
}

results:
select count(value) from test_metric where time > now() - 1m group by time(1s);
time count
2015-08-19T01:00:51Z 25
2015-08-19T01:00:52Z 23
2015-08-19T01:00:53Z 22
2015-08-19T01:00:54Z 20
2015-08-19T01:00:55Z 23
2015-08-19T01:00:56Z 22
2015-08-19T01:00:57Z 23
2015-08-19T01:00:58Z 25
2015-08-19T01:00:59Z 24
2015-08-19T01:01:00Z 28
2015-08-19T01:01:01Z 28
2015-08-19T01:01:02Z 29
2015-08-19T01:01:03Z 29
2015-08-19T01:01:04Z 23
2015-08-19T01:01:05Z 30
2015-08-19T01:01:06Z 28
2015-08-19T01:01:07Z 29
2015-08-19T01:01:08Z 28
2015-08-19T01:01:09Z 22
2015-08-19T01:01:10Z 26
2015-08-19T01:01:11Z 25
2015-08-19T01:01:12Z 24
2015-08-19T01:01:13Z 24
2015-08-19T01:01:14Z 22
2015-08-19T01:01:15Z 24
2015-08-19T01:01:16Z 24
2015-08-19T01:01:17Z 24

I'm very silent

authentication failed while calling Database::writePoints()

In src/InfluxDB/Database.php:

if (!empty($this->username) && !empty($this->password)) {
        $parameters += ['auth' => [$this->username, $this->password]];
}

it checks username and password, but the two value is read from the Database class, but it seems that two values are defined in the Client class. so i'm always get a false result.

is this a bug ?

Better exception class naming

All exception classes of this library are named Exception.

If we import one of them, the code looks like:

        try {
            $stats = $this->get('influx.wrapper')->getLastServerStat($server->getCompleteName());
        } catch (Exception $e) {

        }

Hard to see at the first move the exception is talking about query, database or whatever.

Having QueryException for example would be a good start.

What do you think?

why is 'symfony/event-dispatcher' required dependency?

Why is 'symfony/event-dispatcher' marked as required dependency (https://github.com/influxdata/influxdb-php/blob/master/composer.json#L31)?

So I did little test - I will remove 'symfony/event-dispatcher' from dependencies and do composer install.

These are packages that are installed:

php5.6 ../composer.phar install --no-dev
Loading composer repositories with package information
Installing dependencies from lock file
Package operations: 4 installs, 0 updates, 0 removals
  - Installing psr/http-message (1.0.1): Loading from cache
  - Installing guzzlehttp/psr7 (1.4.2): Loading from cache
  - Installing guzzlehttp/promises (v1.3.1): Loading from cache
  - Installing guzzlehttp/guzzle (6.3.0): Loading from cache

So another test with removing symfony from dev dependencies and replacing their phpunit-bridge with real phpunit my composer.json dependencies will look like

    "require": {
        "php": "^5.5 || ^7.0",
        "guzzlehttp/guzzle": "^6.0"
    },
    "require-dev": {
        "phpunit/phpunit": "^5.7"
    },

and this will get installed:

php5.6 ../composer.phar install
Loading composer repositories with package information
Installing dependencies (including require-dev) from lock file
Package operations: 29 installs, 0 updates, 0 removals
  - Installing psr/http-message (1.0.1): Loading from cache
  - Installing guzzlehttp/psr7 (1.4.2): Loading from cache
  - Installing guzzlehttp/promises (v1.3.1): Loading from cache
  - Installing guzzlehttp/guzzle (6.3.0): Loading from cache
  - Installing webmozart/assert (1.2.0): Loading from cache
  - Installing phpdocumentor/reflection-common (1.0.1): Loading from cache
  - Installing phpdocumentor/type-resolver (0.4.0): Loading from cache
  - Installing phpdocumentor/reflection-docblock (3.3.2): Loading from cache
  - Installing phpunit/php-token-stream (1.4.12): Loading from cache
  - Installing symfony/yaml (v3.4.2): Loading from cache
  - Installing sebastian/version (2.0.1): Loading from cache
  - Installing sebastian/resource-operations (1.0.0): Loading from cache
  - Installing sebastian/recursion-context (2.0.0): Loading from cache
  - Installing sebastian/object-enumerator (2.0.1): Loading from cache
  - Installing sebastian/global-state (1.1.1): Loading from cache
  - Installing sebastian/exporter (2.0.0): Loading from cache
  - Installing sebastian/environment (2.0.0): Loading from cache
  - Installing sebastian/diff (1.4.3): Loading from cache
  - Installing sebastian/comparator (1.2.4): Loading from cache
  - Installing phpunit/php-text-template (1.2.1): Loading from cache
  - Installing doctrine/instantiator (1.0.5): Loading from cache
  - Installing phpunit/phpunit-mock-objects (3.4.4): Loading from cache
  - Installing phpunit/php-timer (1.0.9): Loading from cache
  - Installing phpunit/php-file-iterator (1.4.5): Loading from cache
  - Installing sebastian/code-unit-reverse-lookup (1.0.1): Loading from cache
  - Installing phpunit/php-code-coverage (4.0.8): Loading from cache
  - Installing phpspec/prophecy (1.7.3): Loading from cache
  - Installing myclabs/deep-copy (1.7.0): Loading from cache
  - Installing phpunit/phpunit (5.7.26): Loading from cache
guzzlehttp/guzzle suggests installing psr/log (Required for using the Log middleware)
symfony/yaml suggests installing symfony/console (For validating YAML files using the lint command)
sebastian/global-state suggests installing ext-uopz (*)
phpunit/phpunit-mock-objects suggests installing ext-soap (*)
phpunit/phpunit suggests installing phpunit/php-invoker (~1.1)
Generating autoload files

so only 'symfony/yaml (v3.4.2)' is required through dependency in somewhere in phpunit

Maybe we could remove sympony deps and use phpunit. Looking at .travis.yml (https://github.com/influxdata/influxdb-php/blob/master/.travis.yml) it is little bit silly as it is more about testing sympfony than testing influxdb library. I could do PR for it

ps. I am aware that phpunit 5.7 does not support php5.5 officially.

Grant Privilege, with get?

object(InfluxDB\ResultSet)#33 (2) {
["parsedResults":protected]=>
array(1) {
["results"]=>
array(1) {
[0]=>
array(2) {
["statement_id"]=>
int(0)
["messages"]=>
array(1) {
[0]=>
array(2) {
["level"]=>
string(7) "warning"
["text"]=>
string(120) "deprecated use of 'GRANT ALL PRIVILEGES ON tbh_vps TO tbh_vps' in a read only context, please use a POST request instead"
}
}
}
}
}
["rawResults":protected]=>
string(196) "{"results":[{"statement_id":0,"messages":[{"level":"warning","text":"deprecated use of 'GRANT ALL PRIVILEGES ON tbh_vps TO tbh_vps' in a read only context, please use a POST request instead"}]}]}
"
}

I think it's self explanatory.

example of reliable connection

Hi I'm looking for an example of how to wrap the client in a try catch to detect if the database has gone away, and to attempt to reconnect if so. My php app is a daemon and right now it simply stops executing with no error if influxdb is restarted, e.g. for a simple config file update (I wish it had SIGHUP) which is obviously less than ideal, thanks.

this is why???

include('influxdb-php-sdk1/autoload.php');

/*
$host = '127.0.0.1';
$port = 8086;
$dbname = 'ddd';
$client = new InfluxDB\Client($host, $port);

fetch the database
$database = $client->selectDB('ddd');

// executing a query will yield a resultset object
$result = $database->query('select * from log LIMIT 5');

var_dump($result);
*/

// directly get the database object
$database = InfluxDB\Client::fromDSN(sprintf('influxdb://user:pass@%s:%s/%s', '127.0.0.1', 8086, 'test_db'));

// get the client to retrieve other databases

$client = $database->getClient();

// set the UDP driver in the client
$client->setDriver(new \InfluxDB\Driver\UDP($client->getHost(), 4444));

$points = [
new Point(
'test_metric',
0.84,
['host' => 'server01', 'region' => 'us-west'],
['cpucount' => 10],
exec('date +%s%N') // this will produce a nanosecond timestamp in Linux operating systems
)
];

// now just write your points like you normally would
$result = $database->writePoints($points);

results:

Fatal error: Class 'Point' not found in /mnt/hgfs/windows-webserver/study/influxdb/t4.php on line 47

Float value in point is sent localized

This results in an exception like:

HTTP Code 400 {"error":"unable to parse points"} (Class: InfluxDB\Exception)

Fix in InfluxDB\Point:

`/**
* @param array $fields
*/
public function setFields($fields)
{
foreach ($fields as &$field) {
if (is_integer($field)) {
$field = sprintf('%di', $field);
} elseif (is_string($field)) {
$field = $this->escapeFieldValue($field);
} elseif (is_float($field) || is_double($field)) {
$field = sprintf('%F', $field);
} elseif (is_bool($field)) {
$field = ($field ? "true" : "false");
}
}

    $this->fields = $fields;
}`

Offset in InfluxDB\Query\Builder

Hi,

Is there a way to use offset in Builder? I am planning use it for pagination purposes.

Also what is the simples way to get the total value? SELECT * FROM table returns many stuff rather than single scalar value such as 23. Just to replace $result->getPoints()[0]['count'] which looks ugly.

Thanks

Data is NOT getting written to DB - HTTP 400/ bad timestamp?

I have a simple client that should write/read data- yet the result is always empty, be it Admin interface or client. The code is entirely from the readme:

$client = new InfluxDB\Client('127.0.0.1', 8086);

// fetch the database
$database = $client->selectDB('test');

// executing a query will yield a resultset object
$result = $database->query('select * from test_metric LIMIT 5');

// get the points from the resultset yields an array
$points = $result->getPoints();
print_r($points);

// create an array of points
$points = array(
    new Point(
        'test_metric', // name of the measurement
        0.64, // the measurement value
        ['host' => 'server01', 'region' => 'us-west'], // optional tags
        ['cpucount' => 10], // optional additional fields
        1435255849 // Time precision has to be set to seconds!
    ),
    new Point(
        'test_metric', // name of the measurement
        0.84, // the measurement value
        ['host' => 'server01', 'region' => 'us-west'], // optional tags
        ['cpucount' => 10], // optional additional fields
        1435255849 // Time precision has to be set to seconds!
    )
);

// we are writing unix timestamps, which have a second precision
$result = $database->writePoints($points, Database::PRECISION_SECONDS);
print_r($result);


// executing a query will yield a resultset object
$result = $database->query('SHOW MEASUREMENTS')->getPoints();
print_r($result);

This happens against Windows server, current download.

php client creates invalid line protocol?

Summary

  1. With the php client create a point with a blank tag value e.g. boolean false
  2. Send points to influx

Expected behaviour
The data should be ingested successfully.

Actual behaviour
The error "unable to parse" is thrown

Potentially useful info
The following is an example of a var_dump of one such point:

array (
  0 => 
  InfluxDB\Point::__set_state(array(
     'measurement' => 'actionlog',
     'tags' => 
    array (
      'myint' => '370018',
      'mybool' => '',
    ),
     'fields' => 
    array (
      'exitcode' => '0i',
    ),
     'timestamp' => '1469216277',
  )),
)

Converting the boolean value to string using strval(mybool) didn't help I had to manually convert with turnary e.g.

'mybool' => (mybool) ? 'true' : 'false',

Support unix socket

This is a feature request for this configuration offered by InfluxDB:

  unix-socket-enabled = true # enable http service over unix domain socket
  bind-socket = "/var/run/influxdb.sock"

Database name on UDP protocol

Is it me or the databse name doesn't need to be selected to send data through UDP protocol, since it's configured on the influxDB conf file.
We shoud be able to send data directely from client, no need of database instance, right ?

I can't use the retentionPolicy

i created a database and created six retentionPolicy on this database.

image

but,when i worte the data into it.Only the default policy can success. Other request return 204,but nothing in the database.

i tried to edit the config file,but it see normal.
image

The default policy is POLICY_ONE_MONTH
so i send this request

 curl -i -XPOST 'http://localhost:8086/write?db=haiwaiuni-stat&precision=u&rp=POLICY_ONE_WEEK' --data-binary 'cpu_load_short,host=server01,region=us-west value=0.63 1515414280000001'

the result is

HTTP/1.1 204 No Content
Content-Type: application/json
Request-Id: 4c01452a-f473-11e7-8020-000000000000
X-Influxdb-Build: OSS
X-Influxdb-Version: 1.4.2
X-Request-Id: 4c01452a-f473-11e7-8020-000000000000
Date: Mon, 08 Jan 2018 12:56:09 GMT

nothing
image

and i change it to:

curl -i -XPOST 'http://localhost:8086/write?db=haiwaiuni-stat&precision=u&rp=POLICY_ONE_MONTH' --data-binary 'cpu_load_short,host=server01,region=us-west value=0.63 1515414280000001'

the result is

HTTP/1.1 204 No Content
Content-Type: application/json
Request-Id: b53571f3-f473-11e7-8025-000000000000
X-Influxdb-Build: OSS
X-Influxdb-Version: 1.4.2
X-Request-Id: b53571f3-f473-11e7-8025-000000000000
Date: Mon, 08 Jan 2018 12:59:05 GMT

The data is already in there.
image

i dont't know how it works.

Anyone can help me?

Support Symfony 3.0

Symfony 3.0 will be released in the shortly (roadmap says November, 2015), but influxdb/influxdb-php requires symfony/event-dispatcher: 2.* that conflicted with symfony 3.0.
Do you can update dependency version to 2.|3.?

Safely handle inserting object field values

Problem

Hi, I was trying to insert a uuid into a record, and it caused a 400 error because the finished record looked like this

event,foo=bar id=a605d377-47bd-4d9a-8b6e-c9ed66bed255

The error occurs because the uuid isn't quoted:

event,foo=bar id="a605d377-47bd-4d9a-8b6e-c9ed66bed255"

This is because the uuid was a Uuid object, not a string (which I didn't realize at the time).

Solution

This function should stringify objects before processing them.

if (is_object($field) {
    $field = $field->toString();
}

The object will eventually be stringified when inserted into the line record, so may as well do it early so it can be quoted if needed.

Async request

I can't use UDP, is there any way to write points asynchronously?

Writing data with timestamp not working

From your example:

    // create an array of points
    $points = array(
        new Point(
            'test_metric',
            0.64,
            array('host' => 'server01', 'region' => 'us-west'),
            array('cpucount' => 10),
            1435255849
        ),
        new Point(
            'test_metric',
            0.84,
            array('host' => 'server01', 'region' => 'us-west'),
            array('cpucount' => 10),
            1435255850
        )
    );

    $newPoints = $database->writePoints($points);

with InfluxDB 0.9.1 timestamp is added as 1970-01-01T00:00:01.435255849Z

Same wrong database entry if I do:

        new Point(
            'test_metric',
            0.84,
            array('host' => 'server01', 'region' => 'us-west'),
            array('cpucount' => 10),
            time()
        )

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.