mirror of
https://github.com/stylersnico/librenms.git
synced 2026-07-28 16:04:35 +02:00
Changed method of sending UDP data to stream, fixed driver logic, added DSN method for creating a UDP session
This commit is contained in:
@@ -30,7 +30,7 @@ $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:
|
||||
It's also possible to create a client from a DSN (Data Source Name):
|
||||
|
||||
```php
|
||||
|
||||
@@ -91,14 +91,14 @@ Writing data is done by providing an array of points to the writePoints method o
|
||||
new Point(
|
||||
'test_metric',
|
||||
0.64,
|
||||
array('host' => 'server01', 'region' => 'us-west'),
|
||||
array('cpucount' => 10),
|
||||
['host' => 'server01', 'region' => 'us-west'],
|
||||
['cpucount' => 10],
|
||||
1435255849 // Time precision has to be set to seconds!
|
||||
),
|
||||
new Point(
|
||||
'test_metric',
|
||||
0.84,
|
||||
array('host' => 'server01', 'region' => 'us-west'),
|
||||
['host' => 'server01', 'region' => 'us-west'],
|
||||
array('cpucount' => 10),
|
||||
1435255850 // Time precision has to be set to seconds!
|
||||
)
|
||||
@@ -129,13 +129,38 @@ First, set your InfluxDB host to support incoming UDP sockets:
|
||||
Then, configure the UDP driver in the client:
|
||||
|
||||
```php
|
||||
// set the udp driver
|
||||
|
||||
// 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, Database::PRECISION_SECONDS);
|
||||
$result = $database->writePoints($points);
|
||||
```
|
||||
|
||||
Or simply use a DSN (Data Source Name) to send metrics using UDP:
|
||||
|
||||
```php
|
||||
|
||||
// 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
|
||||
|
||||
+16
-8
@@ -6,6 +6,7 @@ use InfluxDB\Client\Exception as ClientException;
|
||||
use InfluxDB\Driver\DriverInterface;
|
||||
use InfluxDB\Driver\Guzzle;
|
||||
use InfluxDB\Driver\QueryDriverInterface;
|
||||
use InfluxDB\Driver\UDP;
|
||||
|
||||
/**
|
||||
* Class Client
|
||||
@@ -165,10 +166,8 @@ class Client
|
||||
]);
|
||||
|
||||
try {
|
||||
// send the data
|
||||
$driver->send();
|
||||
|
||||
return $driver->getResultSet();
|
||||
// perform the query and return the resultset
|
||||
return $driver->query();
|
||||
|
||||
} catch (\Exception $e) {
|
||||
throw new Exception(sprintf('Query has failed, exception: %s', $e->getMessage()));
|
||||
@@ -200,12 +199,16 @@ class Client
|
||||
|
||||
/**
|
||||
* Build the client from a dsn
|
||||
* Example: https+influxdb://username:pass@localhost:8086/databasename', timeout=5
|
||||
* Examples:
|
||||
*
|
||||
* https+influxdb://username:pass@localhost:8086/databasename
|
||||
* udp+influxdb://username:pass@localhost:4444/databasename
|
||||
*
|
||||
* @param string $dsn
|
||||
* @param int $timeout
|
||||
* @param bool $verifySSL
|
||||
* @return Client|Database
|
||||
*
|
||||
*@return Client|Database
|
||||
* @throws ClientException
|
||||
*/
|
||||
public static function fromDSN($dsn, $timeout = 0, $verifySSL = false)
|
||||
@@ -217,12 +220,12 @@ class Client
|
||||
$scheme = $schemeInfo[0];
|
||||
|
||||
if (isset($schemeInfo[1])) {
|
||||
$modifier = $schemeInfo[0];
|
||||
$modifier = strtolower($schemeInfo[0]);
|
||||
$scheme = $schemeInfo[1];
|
||||
}
|
||||
|
||||
if ($scheme != 'influxdb') {
|
||||
throw new ClientException(sprintf('%s is not a valid scheme', $scheme));
|
||||
throw new ClientException($scheme . ' is not a valid scheme');
|
||||
}
|
||||
|
||||
$ssl = $modifier === 'https' ? true : false;
|
||||
@@ -238,6 +241,11 @@ class Client
|
||||
$timeout
|
||||
);
|
||||
|
||||
// set the UDP driver when the DSN specifies UDP
|
||||
if ($modifier == 'udp') {
|
||||
$client->setDriver(new UDP($connParams['host'], $connParams['port']));
|
||||
}
|
||||
|
||||
return ($dbName ? $client->selectDB($dbName) : $client);
|
||||
}
|
||||
|
||||
|
||||
@@ -133,7 +133,7 @@ class Database
|
||||
]);
|
||||
|
||||
// send the points to influxDB
|
||||
$driver->send(implode(PHP_EOL, $payload));
|
||||
$driver->write(implode(PHP_EOL, $payload));
|
||||
|
||||
return $driver->isSuccess();
|
||||
|
||||
|
||||
@@ -38,7 +38,7 @@ interface DriverInterface
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function send($data = null);
|
||||
public function write($data = null);
|
||||
|
||||
/**
|
||||
* Should return if sending the data was successful
|
||||
|
||||
@@ -10,6 +10,11 @@ use Guzzle\Http\Message\Response;
|
||||
use GuzzleHttp\Psr7\Request;
|
||||
use InfluxDB\ResultSet;
|
||||
|
||||
/**
|
||||
* Class Guzzle
|
||||
*
|
||||
* @package InfluxDB\Driver
|
||||
*/
|
||||
class Guzzle implements DriverInterface, QueryDriverInterface
|
||||
{
|
||||
|
||||
@@ -70,11 +75,22 @@ class Guzzle implements DriverInterface, QueryDriverInterface
|
||||
* @throws Exception
|
||||
* @return mixed
|
||||
*/
|
||||
public function send($data = null)
|
||||
public function write($data = null)
|
||||
{
|
||||
$requestObject = new Request($this->parameters['method'], $this->parameters['url'], [], $data);
|
||||
$this->response = $this->httpClient->post($this->parameters['url'], ['body' => $data]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return ResultSet
|
||||
*/
|
||||
public function query()
|
||||
{
|
||||
$response = $this->httpClient->get($this->parameters['url']);
|
||||
|
||||
$raw = (string) $response->getBody();
|
||||
|
||||
return new ResultSet($raw);
|
||||
|
||||
$this->response = $this->httpClient->send($requestObject);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -86,14 +102,4 @@ class Guzzle implements DriverInterface, QueryDriverInterface
|
||||
{
|
||||
return in_array($this->response->getStatusCode(), ['200', '204']);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return ResultSet
|
||||
*/
|
||||
public function getResultSet()
|
||||
{
|
||||
$raw = (string) $this->response->getBody(true);
|
||||
|
||||
return new ResultSet($raw);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,5 +18,5 @@ interface QueryDriverInterface
|
||||
/**
|
||||
* @return ResultSet
|
||||
*/
|
||||
public function getResultSet();
|
||||
public function query();
|
||||
}
|
||||
@@ -35,13 +35,7 @@ class UDP implements DriverInterface
|
||||
$this->config['host'] = $host;
|
||||
$this->config['port'] = $port;
|
||||
|
||||
// create a socket connection to check if InfluxDB is alive
|
||||
$socket = fsockopen('udp://' .$host, $port);
|
||||
if (!$socket) {
|
||||
throw new Exception('There is no InfluxDB UDP service running on port '. $port);
|
||||
}
|
||||
|
||||
fclose($socket);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -71,11 +65,15 @@ class UDP implements DriverInterface
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function send($data = null)
|
||||
public function write($data = null)
|
||||
{
|
||||
$socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
|
||||
socket_sendto($socket, $data, strlen($data), 0, $this->config['host'], $this->config['port']);
|
||||
socket_close($socket);
|
||||
|
||||
$host = sprintf('udp://%s:%d', $this->config['host'], $this->config['port']);
|
||||
|
||||
// stream the data using UDP and suppress any errors
|
||||
$stream = @stream_socket_client($host);
|
||||
@stream_socket_sendto($stream, $data);
|
||||
@fclose($stream);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -54,7 +54,7 @@ class ClientTest extends \PHPUnit_Framework_TestCase
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Guzzle\Http\Client
|
||||
* @return GuzzleClient
|
||||
*/
|
||||
public static function buildHttpMockClient($body)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user