mirror of
https://github.com/stylersnico/librenms.git
synced 2026-07-27 16:08:55 +02:00
@@ -1,7 +1,15 @@
|
||||
<?php
|
||||
namespace InfluxDB\Adapter;
|
||||
|
||||
/**
|
||||
* Every InfluxDB adapter implements this interface
|
||||
*/
|
||||
interface AdapterInterface
|
||||
{
|
||||
/**
|
||||
* Send series into database
|
||||
* @param mixed $message
|
||||
* @param string|boolean $timePrecision
|
||||
*/
|
||||
public function send($message, $timePrecision = false);
|
||||
}
|
||||
|
||||
@@ -4,22 +4,43 @@ namespace InfluxDB\Adapter;
|
||||
use GuzzleHttp\Client;
|
||||
use InfluxDB\Options;
|
||||
|
||||
/**
|
||||
* Integration of Guzzle HttpClient how InfluxDB client adapter
|
||||
* @link http://guzzle.readthedocs.org/en/latest/
|
||||
*/
|
||||
class GuzzleAdapter implements AdapterInterface, QueryableInterface
|
||||
{
|
||||
/**
|
||||
* @var GuzzleHttp\Client
|
||||
*/
|
||||
private $httpClient;
|
||||
|
||||
/**
|
||||
* @var \InfluxDB\Options
|
||||
*/
|
||||
private $options;
|
||||
|
||||
/**
|
||||
* @param Client $httpClient
|
||||
* @param Options $options
|
||||
*/
|
||||
public function __construct(Client $httpClient, Options $options)
|
||||
{
|
||||
$this->httpClient = $httpClient;
|
||||
$this->options = $options;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Options
|
||||
*/
|
||||
public function getOptions()
|
||||
{
|
||||
return $this->options;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function send($message, $timePrecision = false)
|
||||
{
|
||||
$httpMessage = [
|
||||
@@ -35,6 +56,9 @@ class GuzzleAdapter implements AdapterInterface, QueryableInterface
|
||||
return $this->httpClient->post($endpoint, $httpMessage);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function query($query, $timePrecision = false)
|
||||
{
|
||||
$options = [
|
||||
@@ -53,6 +77,9 @@ class GuzzleAdapter implements AdapterInterface, QueryableInterface
|
||||
return $this->httpClient->get($endpoint, $options)->json();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function getDatabases()
|
||||
{
|
||||
$options = [
|
||||
@@ -64,6 +91,9 @@ class GuzzleAdapter implements AdapterInterface, QueryableInterface
|
||||
return $this->httpClient->get($endpoint, $options)->json();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function createDatabase($name)
|
||||
{
|
||||
$httpMessage = [
|
||||
@@ -75,6 +105,9 @@ class GuzzleAdapter implements AdapterInterface, QueryableInterface
|
||||
return $this->httpClient->post($endpoint, $httpMessage)->json();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function deleteDatabase($name)
|
||||
{
|
||||
$httpMessage = [
|
||||
|
||||
@@ -1,10 +1,34 @@
|
||||
<?php
|
||||
namespace InfluxDB\Adapter;
|
||||
|
||||
/**
|
||||
* The Adapter implement this interface if it supports database query
|
||||
*/
|
||||
interface QueryableInterface
|
||||
{
|
||||
/**
|
||||
* Make query into database
|
||||
* @param string $query
|
||||
* @param string|bool $timePrecision
|
||||
*/
|
||||
public function query($query, $timePrecision = false);
|
||||
|
||||
/**
|
||||
* Return database
|
||||
*/
|
||||
public function getDatabases();
|
||||
|
||||
/**
|
||||
* Create database
|
||||
* @param string $name
|
||||
* @return array
|
||||
*/
|
||||
public function createDatabase($name);
|
||||
|
||||
/**
|
||||
* Delete database by database
|
||||
* @param string $name
|
||||
* @return array
|
||||
*/
|
||||
public function deleteDatabase($name);
|
||||
}
|
||||
|
||||
@@ -3,20 +3,33 @@ 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
|
||||
{
|
||||
private $options;
|
||||
|
||||
/**
|
||||
* @param Options $options
|
||||
*/
|
||||
public function __construct(Options $options)
|
||||
{
|
||||
$this->options = $options;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return Options
|
||||
*/
|
||||
public function getOptions()
|
||||
{
|
||||
return $this->options;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function send($message, $timePrecision = false)
|
||||
{
|
||||
$message = json_encode($message);
|
||||
|
||||
@@ -5,33 +5,68 @@ namespace InfluxDB;
|
||||
use InfluxDb\Adapter\QueryableInterface;
|
||||
use InfluxDB\Filter\FilterInterface;
|
||||
|
||||
/**
|
||||
* Client to manage request at InfluxDB
|
||||
*/
|
||||
class Client
|
||||
{
|
||||
/**
|
||||
* @var \InfluxDB\Adapter\AdapterInterface
|
||||
*/
|
||||
private $adapter;
|
||||
|
||||
/**
|
||||
* @var \InfluxDB\Filter\FilterInterface
|
||||
*/
|
||||
private $filter;
|
||||
|
||||
/**
|
||||
* Set filter
|
||||
* @param Filter\FilterInterface $filter
|
||||
* @return Client
|
||||
*/
|
||||
public function setFilter(Filter\FilterInterface $filter)
|
||||
{
|
||||
$this->filter = $filter;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get filter
|
||||
* @return Filter\FilterInterface
|
||||
*/
|
||||
public function getFilter()
|
||||
{
|
||||
return $this->filter;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set InfluxDB adapter
|
||||
* @param Adapter\AdapterInterface
|
||||
* @return Client
|
||||
*/
|
||||
public function setAdapter(Adapter\AdapterInterface $adapter)
|
||||
{
|
||||
$this->adapter = $adapter;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get adapter
|
||||
* @return Adapter\AdapterInterface
|
||||
*/
|
||||
public function getAdapter()
|
||||
{
|
||||
return $this->adapter;
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert point into series
|
||||
* @param string $name
|
||||
* @param array $value
|
||||
* @param bool|string $timePrecision
|
||||
* @return mixed
|
||||
*/
|
||||
public function mark($name, array $values, $timePrecision = false)
|
||||
{
|
||||
$data =[];
|
||||
@@ -45,6 +80,12 @@ class Client
|
||||
return $this->getAdapter()->send([$data], $timePrecision);
|
||||
}
|
||||
|
||||
/**
|
||||
* Make a query into database
|
||||
* @param string $query
|
||||
* @param bool|string $timePrecision
|
||||
* @return array
|
||||
*/
|
||||
public function query($query, $timePrecision = false)
|
||||
{
|
||||
if (!($this->getAdapter() instanceOf QueryableInterface)) {
|
||||
@@ -62,6 +103,10 @@ class Client
|
||||
return $return;
|
||||
}
|
||||
|
||||
/**
|
||||
* List of databases
|
||||
* @return array
|
||||
*/
|
||||
public function getDatabases()
|
||||
{
|
||||
if (!($this->getAdapter() instanceOf QueryableInterface)) {
|
||||
@@ -70,6 +115,10 @@ class Client
|
||||
return $this->getAdapter()->getDatabases();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create database by name
|
||||
* @param string $name
|
||||
*/
|
||||
public function createDatabase($name)
|
||||
{
|
||||
if (!($this->getAdapter() instanceOf QueryableInterface)) {
|
||||
@@ -78,6 +127,10 @@ class Client
|
||||
return $this->getAdapter()->createDatabase($name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete database by name
|
||||
* @param string $name
|
||||
*/
|
||||
public function deleteDatabase($name)
|
||||
{
|
||||
if (!($this->getAdapter() instanceOf QueryableInterface)) {
|
||||
@@ -86,6 +139,11 @@ class Client
|
||||
return $this->getAdapter()->deleteDatabase($name);
|
||||
}
|
||||
|
||||
/**
|
||||
* List of time precision choose
|
||||
* @param string $timePrecision
|
||||
* @return bool|string
|
||||
*/
|
||||
private function clearTimePrecision($timePrecision)
|
||||
{
|
||||
switch ($timePrecision) {
|
||||
|
||||
@@ -4,8 +4,18 @@ namespace InfluxDB;
|
||||
use Zend\Stdlib\Hydrator\ClassMethods;
|
||||
use GuzzleHttp\Client as GuzzleClient;
|
||||
|
||||
/**
|
||||
* Create your static client
|
||||
*/
|
||||
abstract class ClientFactory
|
||||
{
|
||||
/**
|
||||
* Create new client
|
||||
* @param array $options
|
||||
* @return Client
|
||||
* @throws InvalidArgumentException If not exist adapter name
|
||||
* or not find adapter
|
||||
*/
|
||||
public static function create(array $options)
|
||||
{
|
||||
$defaultOptions = [
|
||||
|
||||
@@ -2,8 +2,14 @@
|
||||
|
||||
namespace InfluxDB\Filter;
|
||||
|
||||
/**
|
||||
* This filter manipulates response
|
||||
*/
|
||||
class ColumnsPointsFilter implements FilterInterface
|
||||
{
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function filter($metrics)
|
||||
{
|
||||
$response = [];
|
||||
|
||||
@@ -1,7 +1,14 @@
|
||||
<?php
|
||||
namespace InfluxDB\Filter;
|
||||
|
||||
/**
|
||||
* Every filter implement this interface
|
||||
*/
|
||||
interface FilterInterface
|
||||
{
|
||||
/**
|
||||
* Filter metrics
|
||||
* @param mixed $anything
|
||||
*/
|
||||
public function filter($anything);
|
||||
}
|
||||
|
||||
@@ -2,16 +2,44 @@
|
||||
|
||||
namespace InfluxDB;
|
||||
|
||||
/**
|
||||
* Manage in the best way InfluxDB Client Configuration
|
||||
*/
|
||||
class Options
|
||||
{
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $host;
|
||||
|
||||
/**
|
||||
* @var string|int
|
||||
*/
|
||||
private $port;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $username;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $password;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $protocol;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $database;
|
||||
|
||||
/**
|
||||
* Set default options
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->host = "localhost";
|
||||
@@ -21,17 +49,27 @@ class Options
|
||||
$this->setProtocol("http");
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getProtocol()
|
||||
{
|
||||
return $this->protocol;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $protocol
|
||||
* @return Options
|
||||
*/
|
||||
public function setProtocol($protocol)
|
||||
{
|
||||
$this->protocol = $protocol;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getHost()
|
||||
{
|
||||
return $this->host;
|
||||
@@ -43,50 +81,82 @@ class Options
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string|int
|
||||
*/
|
||||
public function getPort()
|
||||
{
|
||||
return $this->port;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string|int $port
|
||||
* @return Options
|
||||
*/
|
||||
public function setPort($port)
|
||||
{
|
||||
$this->port = $port;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getUsername()
|
||||
{
|
||||
return $this->username;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $usarname
|
||||
* @return Options
|
||||
*/
|
||||
public function setUsername($username)
|
||||
{
|
||||
$this->username = $username;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getPassword()
|
||||
{
|
||||
return $this->password;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $password
|
||||
* @return Options
|
||||
*/
|
||||
public function setPassword($password)
|
||||
{
|
||||
$this->password = $password;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function getDatabase()
|
||||
{
|
||||
return $this->database;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $database
|
||||
* @return Options
|
||||
*/
|
||||
public function setDatabase($database)
|
||||
{
|
||||
$this->database = $database;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build http series edpoint
|
||||
* @return string
|
||||
*/
|
||||
public function getHttpSeriesEndpoint()
|
||||
{
|
||||
return sprintf(
|
||||
@@ -98,6 +168,11 @@ class Options
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Build http database endpoint by name
|
||||
* @param string $name
|
||||
* @return string
|
||||
*/
|
||||
public function getHttpDatabaseEndpoint($name = false)
|
||||
{
|
||||
$url = sprintf(
|
||||
|
||||
Reference in New Issue
Block a user