mirror of
https://github.com/librenms/librenms.git
synced 2024-10-07 16:52:45 +00:00
Use prepared default options
In order to use user prepared options the message is alse merged with all user preapared options.
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
namespace InfluxDB\Adapter;
|
||||
|
||||
use InfluxDB\Options;
|
||||
|
||||
abstract class AdapterAbstract implements AdapterInterface
|
||||
{
|
||||
private $options;
|
||||
|
||||
/**
|
||||
* @param Options $options
|
||||
*/
|
||||
public function __construct(Options $options)
|
||||
{
|
||||
$this->options = $options;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Options
|
||||
*/
|
||||
public function getOptions()
|
||||
{
|
||||
return $this->options;
|
||||
}
|
||||
|
||||
protected function getMessageDefaults()
|
||||
{
|
||||
return [
|
||||
"database" => $this->getOptions()->getDatabase(),
|
||||
];
|
||||
}
|
||||
|
||||
abstract public function send(array $message);
|
||||
}
|
||||
@@ -11,5 +11,5 @@ interface AdapterInterface
|
||||
* @param mixed $message
|
||||
* @param string|boolean $timePrecision
|
||||
*/
|
||||
public function send($message);
|
||||
public function send(array $message);
|
||||
}
|
||||
|
||||
@@ -10,77 +10,46 @@ use InfluxDB\Options;
|
||||
*
|
||||
* @deprecated
|
||||
*/
|
||||
class GuzzleAdapter implements AdapterInterface, QueryableInterface
|
||||
class GuzzleAdapter extends AdapterAbstract implements QueryableInterface
|
||||
{
|
||||
/**
|
||||
* @var GuzzleHttp\Client
|
||||
*/
|
||||
private $httpClient;
|
||||
|
||||
/**
|
||||
* @var \InfluxDB\Options
|
||||
*/
|
||||
private $options;
|
||||
|
||||
/**
|
||||
* @param Client $httpClient
|
||||
* @param Options $options
|
||||
*/
|
||||
public function __construct(Client $httpClient, Options $options)
|
||||
{
|
||||
parent::__construct($options);
|
||||
|
||||
$this->httpClient = $httpClient;
|
||||
$this->options = $options;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Options
|
||||
*/
|
||||
public function getOptions()
|
||||
public function send(array $message)
|
||||
{
|
||||
return $this->options;
|
||||
}
|
||||
$message = array_replace_recursive($this->getMessageDefaults(), $message);
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function send($message, $timePrecision = false)
|
||||
{
|
||||
$httpMessage = [
|
||||
"auth" => [$this->options->getUsername(), $this->options->getPassword()],
|
||||
"auth" => [$this->getOptions()->getUsername(), $this->getOptions()->getPassword()],
|
||||
"body" => json_encode($message)
|
||||
];
|
||||
|
||||
if ($timePrecision) {
|
||||
$httpMessage["query"]["time_precision"] = $timePrecision;
|
||||
}
|
||||
|
||||
$endpoint = $this->options->getHttpSeriesEndpoint();
|
||||
$endpoint = $this->getOptions()->getHttpSeriesEndpoint();
|
||||
return $this->httpClient->post($endpoint, $httpMessage);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function query($query, $timePrecision = false)
|
||||
public function query($query)
|
||||
{
|
||||
$options = [
|
||||
"auth" => [$this->options->getUsername(), $this->options->getPassword()],
|
||||
"auth" => [$this->getOptions()->getUsername(), $this->getOptions()->getPassword()],
|
||||
'query' => [
|
||||
"q" => $query,
|
||||
"db" => $this->getOptions()->getDatabase(),
|
||||
]
|
||||
];
|
||||
|
||||
if ($timePrecision) {
|
||||
$options["query"]["time_precision"] = $timePrecision;
|
||||
}
|
||||
|
||||
return $this->get($options);
|
||||
}
|
||||
|
||||
private function get(array $httpMessage)
|
||||
{
|
||||
$endpoint = $this->options->getHttpQueryEndpoint();
|
||||
$endpoint = $this->getOptions()->getHttpQueryEndpoint();
|
||||
return $this->httpClient->get($endpoint, $httpMessage)->json();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,38 +3,15 @@ namespace InfluxDB\Adapter;
|
||||
|
||||
use InfluxDB\Options;
|
||||
|
||||
/**
|
||||
* Clent adapter to call InfluxDb by UDP protocol
|
||||
* @link http://influxdb.com/docs/v0.6/api/reading_and_writing_data.html#writing-data-through-json-+-udp
|
||||
*/
|
||||
class UdpAdapter implements AdapterInterface
|
||||
final class UdpAdapter extends AdapterAbstract
|
||||
{
|
||||
private $options;
|
||||
|
||||
/**
|
||||
* @param Options $options
|
||||
*/
|
||||
public function __construct(Options $options)
|
||||
public function send(array $message)
|
||||
{
|
||||
$this->options = $options;
|
||||
}
|
||||
$message = array_replace_recursive($this->getMessageDefaults(), $message);
|
||||
|
||||
/**
|
||||
* @return Options
|
||||
*/
|
||||
public function getOptions()
|
||||
{
|
||||
return $this->options;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function send($message)
|
||||
{
|
||||
$message = json_encode($message);
|
||||
$socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
|
||||
socket_sendto($socket, $message, strlen($message), 0, $this->options->getHost(), $this->options->getPort());
|
||||
socket_sendto($socket, $message, strlen($message), 0, $this->getOptions()->getHost(), $this->getOptions()->getPort());
|
||||
socket_close($socket);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,7 +29,6 @@ class Client
|
||||
if (!is_array($name)) {
|
||||
$data =[];
|
||||
|
||||
$data["database"] = $this->getAdapter()->getOptions()->getDatabase();
|
||||
$data['points'][0]['name'] = $name;
|
||||
$data['points'][0]['fields'] = $values;
|
||||
}
|
||||
|
||||
@@ -53,7 +53,7 @@ class ClientTest extends \PHPUnit_Framework_TestCase
|
||||
*/
|
||||
public function testGuzzleHttpApiWorksCorrectly()
|
||||
{
|
||||
$t = $this->object->mark("tcp.test", ["mark" => "element"]);
|
||||
$this->object->mark("tcp.test", ["mark" => "element"]);
|
||||
|
||||
sleep(1);
|
||||
|
||||
@@ -103,7 +103,6 @@ class ClientTest extends \PHPUnit_Framework_TestCase
|
||||
public function testWriteDirectMessages()
|
||||
{
|
||||
$this->object->mark([
|
||||
"database" => "tcp.test",
|
||||
"tags" => [
|
||||
"dc" => "eu-west-1",
|
||||
],
|
||||
@@ -160,6 +159,47 @@ class ClientTest extends \PHPUnit_Framework_TestCase
|
||||
$this->assertEquals("element", $body["results"][0]["series"][0]["values"][0][1]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @group udp
|
||||
*/
|
||||
public function testOverrideDatabaseNameViaMessage()
|
||||
{
|
||||
$rawOptions = $this->rawOptions;
|
||||
$options = new Options();
|
||||
$options->setHost($rawOptions["udp"]["host"]);
|
||||
$options->setUsername($rawOptions["udp"]["username"]);
|
||||
$options->setPassword($rawOptions["udp"]["password"]);
|
||||
$options->setPort($rawOptions["udp"]["port"]);
|
||||
$options->setDatabase("a-wrong-database");
|
||||
|
||||
$adapter = new UdpAdapter($options);
|
||||
$object = new Client();
|
||||
$object->setAdapter($adapter);
|
||||
|
||||
$object->mark([
|
||||
"database" => "{$rawOptions["udp"]["database"]}",
|
||||
"points" => [
|
||||
[
|
||||
"name" => "vm-serie",
|
||||
"fields" => [
|
||||
"cpu" => 18.12,
|
||||
"free" => 712423,
|
||||
],
|
||||
],
|
||||
]
|
||||
]);
|
||||
|
||||
sleep(1);
|
||||
|
||||
$this->options->setDatabase($rawOptions["udp"]["database"]);
|
||||
$body = $this->object->query("select * from \"vm-serie\"");
|
||||
|
||||
$this->assertCount(1, $body["results"][0]["series"][0]["values"]);
|
||||
$this->assertEquals("cpu", $body["results"][0]["series"][0]["columns"][1]);
|
||||
$this->assertEquals(18.12, $body["results"][0]["series"][0]["values"][0][1]);
|
||||
$this->assertEquals(712423, $body["results"][0]["series"][0]["values"][0][2]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @group udp
|
||||
*/
|
||||
@@ -178,7 +218,6 @@ class ClientTest extends \PHPUnit_Framework_TestCase
|
||||
$object->setAdapter($adapter);
|
||||
|
||||
$object->mark([
|
||||
"database" => "udp.test",
|
||||
"points" => [
|
||||
[
|
||||
"name" => "vm-serie",
|
||||
@@ -220,7 +259,6 @@ class ClientTest extends \PHPUnit_Framework_TestCase
|
||||
$object->setAdapter($adapter);
|
||||
|
||||
$object->mark([
|
||||
"database" => "udp.test",
|
||||
"tags" => [
|
||||
"region" => "eu",
|
||||
],
|
||||
|
||||
Reference in New Issue
Block a user