Example #1
0
 /**
  * {@inheritDoc}
  */
 public function flush()
 {
     foreach ($this->data as $data) {
         $this->client->mark($data[0], array('value' => $data[1]));
     }
     $this->data = array();
 }
Example #2
0
 /**
  * {@inheritdoc}
  */
 public function flush()
 {
     foreach ($this->data as $data) {
         $this->client->mark(array('points' => array(array('measurement' => $data[0], 'fields' => array('value' => $data[1]))), 'tags' => $this->tags));
     }
     $this->data = array();
 }
 /**
  * @param string $host
  * @param int    $httpPort
  * @param int    $udpPort
  * @param string $user
  * @param string $password
  * @param bool   $udp
  *
  * @return Client
  */
 private function createClient(string $host, int $httpPort, int $udpPort, string $user, string $password, bool $udp = false) : Client
 {
     $client = new Client($host, $httpPort, $user, $password);
     if ($udp) {
         $client->setDriver(new UDP($client->getHost(), $udpPort));
     }
     return $client;
 }
 public function testWritePointsInASingleCall()
 {
     $point1 = new Point('cpu_load_short', 0.64, array('host' => 'server01', 'region' => 'us-west'), array('cpucount' => 10), 1435222310);
     $point2 = new Point('cpu_load_short', 0.84);
     $payloadExpected = "{$point1}\n{$point2}";
     $this->mockClient->expects($this->once())->method('write')->with($this->equalTo($this->db->getName()), $this->equalTo($payloadExpected))->will($this->returnValue(true));
     $this->db->writePoints(array($point1, $point2));
 }
 /**
  * @expectedException \RuntimeException
  */
 public function testConnectBrokenDatabase()
 {
     $database = $this->getMock('InfluxDB\\Database', [], ['dbname', $this->influxDb]);
     $requestInterface = $this->getMock('Psr\\Http\\Message\\RequestInterface');
     $exception = new \GuzzleHttp\Exception\ConnectException('error', $requestInterface);
     $database->method('exists')->willThrowException($exception);
     $this->influxDb->method('selectDB')->will($this->returnValue($database));
     $this->client->connectDatabase();
 }
Example #6
0
 /**
  */
 public function testGuzzleQuery()
 {
     $client = new Client('localhost', 8086);
     $query = "some-bad-query";
     $bodyResponse = file_get_contents(dirname(__FILE__) . '/result.example.json');
     $httpMockClient = self::buildHttpMockClient($bodyResponse);
     $client->setDriver(new Guzzle($httpMockClient));
     /** @var \InfluxDB\ResultSet $result */
     $result = $client->query(null, $query);
     $this->assertInstanceOf('\\InfluxDB\\ResultSet', $result);
 }
Example #7
0
 public function setUp()
 {
     $this->mockClient = $this->getMockBuilder('\\InfluxDB\\Client')->disableOriginalConstructor()->getMock();
     $this->resultData = file_get_contents(dirname(__FILE__) . '/result.example.json');
     $this->mockClient->expects($this->any())->method('getBaseURI')->will($this->returnValue($this->equalTo('http://localhost:8086')));
     $this->mockClient->expects($this->any())->method('query')->will($this->returnValue(new ResultSet($this->resultData)));
     $httpMockClient = new Guzzle($this->buildHttpMockClient(''));
     // make sure the client has a valid driver
     $this->mockClient->expects($this->any())->method('getDriver')->will($this->returnValue($httpMockClient));
     $this->database = new Database('influx_test_db', $this->mockClient);
 }
 public function __construct(array $params, $username, $password, array $driverOptions = array())
 {
     // directly get the database object
     $dsn = 'influxdb://';
     if ($username) {
         $dsn .= sprintf("%s:%s@", $username, $password);
     }
     if ($params["host"]) {
         $dsn .= $params["host"];
     } else {
         $dsn .= 'localhost';
     }
     if ($params["port"]) {
         $dsn .= ':' . $params["port"];
     } else {
         $dsn .= ':8086';
     }
     if ($params["dbname"]) {
         $dsn .= '/' . $params["dbname"];
     } else {
         $dsn .= '/';
     }
     $this->database = Client::fromDSN($dsn);
     $this->client = $this->database->getClient();
 }
Example #9
0
 public function getConnect()
 {
     if (!$this->connection) {
         $this->connection = Client::fromDSN(sprintf('influxdb://%s:%s@%s:%s/%s', $this->user, $this->password, $this->host, $this->port, $this->db));
     }
     return $this->connection;
 }
 public function setUp()
 {
     $options = new Options();
     $options->setHost("localhost");
     $options->setPort(8086);
     $options->setUsername("root");
     $options->setPassword("root");
     $options->setDatabase("tcp.test");
     $client = new Client(new GuzzleAdapter(new HttpClient(), $options));
     $client->createDatabase("tcp.test");
     $client->createDatabase("udp.test");
     $this->httpClient = $client;
     $opts = new Options();
     $opts->setPort(4444);
     $client = new Client(new UdpAdapter($opts));
     $this->udpClient = $client;
 }
Example #11
0
 public function testFactoryMethod()
 {
     $client = $this->getClient('test', 'test', true);
     $staticClient = \InfluxDB\Client::fromDSN('https+influxdb://test:test@localhost:8086/');
     $this->assertEquals($client, $staticClient);
     $db = $client->selectDB('testdb');
     $staticDB = \InfluxDB\Client::fromDSN('https+influxdb://test:test@localhost:8086/testdb');
     $this->assertEquals($db, $staticDB);
 }
 public function testIntegrationResponseHandler()
 {
     $options = $this->getOptions();
     $options->setDatabase("mydb");
     $client = $this->getClient();
     $client->createDatabase("mydb");
     $client->mark(["time" => "2015-09-10T23:20:35Z", "points" => [["measurement" => "cpu", "fields" => ["value" => "OK", "hello" => 2]], ["measurement" => "mem", "fields" => ["value" => "KO", "hello" => 4]]]]);
     $stack = new HandlerStack();
     $stack->setHandler(new CurlHandler());
     $stack->push(\InfluxDB\Handler\message_handler());
     // Push the response handler
     $http = new HttpClient(['handler' => $stack]);
     $options = new Http\Options();
     $options->setDatabase("mydb");
     $reader = new Http\Reader($http, $options);
     $writer = new Http\Writer($http, $options);
     $client = new Client($reader, $writer);
     $response = $client->query("SELECT * FROM cpu,mem");
     $this->assertEquals(["cpu" => [["value" => "OK", "hello" => 2, "time" => "2015-09-10T23:20:35Z"]], "mem" => [["value" => "KO", "hello" => 4, "time" => "2015-09-10T23:20:35Z"]]], $response);
 }
Example #13
0
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $this->setHelperSet($this->getApplication()->getHelperSet());
     $db = $input->getOption("db");
     $query = $input->getOption("execute");
     $port = $input->getOption("port");
     $host = $input->getOption("host");
     $user = $input->getOption("user");
     $password = $input->getOption("password");
     $verbose = $input->getOption("verbose");
     $c = new Client(sprintf("http://%s:%s", $host, $port), $user, $password, $db);
     if ($verbose) {
         $c->setDebug(true);
     }
     $dialog = $this->getHelperSet()->get('dialog');
     try {
         ///$query = $dialog->ask($output, sprintf('influx: %s> ', $db));
         $begin = microtime(true);
         $result = $c->query($query);
         $end = microtime(true);
         if (empty($result)) {
             $output->writeln(sprintf("Empty set (%f sec)", round($end - $begin, 4)));
             return;
         }
         $total = 0;
         foreach ($result as $chunk) {
             $keys = $chunk->getColumns();
             $table = $this->getHelperSet()->get('table');
             $table->setHeaders($keys);
             $table->setRows($chunk->getPoints());
             $table->render($output);
             $total += count($chunk);
         }
         $output->writeln(sprintf("%d rows in set (%f sec)", $total, round($end - $begin, 4)));
     } catch (\Exception $e) {
         $output->writeln($e->getMessage());
     }
 }
Example #14
0
function influxdb_connect()
{
    global $config;
    $influxdb_cred = '';
    if (!empty($config['influxdb']['username']) && !empty($config['influxdb']['password'])) {
        $influxdb_cred = $config['influxdb']['username'] . ':' . $config['influxdb']['password'] . '@';
        d_echo('Using authentication for InfluxDB');
    }
    $influxdb_url = $influxdb_cred . $config['influxdb']['host'] . ':' . $config['influxdb']['port'] . '/' . $config['influxdb']['db'];
    d_echo($config['influxdb']['transport'] . " transport being used");
    if ($config['influxdb']['transport'] == 'http') {
        $influxdb_conn = 'influxdb';
    } elseif ($config['influxdb']['transport'] == 'udp') {
        $influxdb_conn = 'udp+influxdb';
    } else {
        echo 'InfluxDB support enabled but no valid transport details provided';
        return false;
    }
    $db = \InfluxDB\Client::fromDSN($influxdb_conn . '://' . $influxdb_url);
    return $db;
}
 /**
  * @param array $torrent
  * @param string $fieldName
  * @param string $transmissionHost
  * @param int $lastDays
  * @return int
  */
 public function getTorrentSum(array $torrent, $fieldName, $transmissionHost = '', $lastDays = 0)
 {
     $where = [];
     if (isset($torrent[Torrent\Get::NAME])) {
         $where[] = "torrent_name = '" . $torrent[Torrent\Get::NAME] . "'";
     }
     if ($transmissionHost) {
         $where[] = "host = '" . $transmissionHost . "'";
     }
     if ($lastDays) {
         $fromTimestamp = strtotime('-' . $lastDays . ' days');
         $fromDate = date('c', $fromTimestamp);
         $where[] = "time >= '{$fromDate}'";
     }
     $results = $this->getDatabase()->getQueryBuilder()->from('uploaded')->select("sum({$fieldName}) as {$fieldName}")->where($where)->getResultSet()->getPoints();
     $this->log('debug', $this->influxDb->getLastQuery());
     if (!empty($results)) {
         return $results[0][$fieldName];
     }
     return 0;
 }
Example #16
0
 /**
  * @param string          $type
  * @param string          $privilege
  * @param string          $username
  * @param Database|string $database
  *
  * @throws \InfluxDB\Exception
  * @return \InfluxDB\ResultSet
  */
 private function executePrivilege($type, $privilege, $username, $database = null)
 {
     if (!in_array($privilege, [self::PRIVILEGE_READ, self::PRIVILEGE_WRITE, self::PRIVILEGE_ALL])) {
         throw new Exception($privilege . ' is not a valid privileges, allowed privileges: READ, WRITE, ALL');
     }
     if ($privilege != self::PRIVILEGE_ALL && !$database) {
         throw new Exception('Only grant ALL cluster-wide privileges are allowed');
     }
     $database = $database instanceof Database ? $database->getName() : (string) $database;
     $query = "{$type} {$privilege}";
     if ($database) {
         $query .= sprintf(' ON %s ', $database);
     } else {
         $query .= " PRIVILEGES ";
     }
     if ($username && $type == 'GRANT') {
         $query .= "TO {$username}";
     } elseif ($username && $type == 'REVOKE') {
         $query .= "FROM {$username}";
     }
     return $this->client->query(null, $query);
 }
Example #17
0
 /**
  * @return bool
  */
 public function exists()
 {
     $databases = $this->client->listDatabases();
     return in_array($this->name, $databases);
 }
Example #18
0
 /**
  * Test that we handle socket problems correctly in the UDP
  * adapter, and that they don't inturrupt the user's application.
  *
  * @group udp
  */
 public function testReplicateIssue27()
 {
     $options = new \InfluxDB\Options();
     // Configure options
     $options->setHost('172.16.1.182');
     $options->setPort(4444);
     $options->setDatabase('...');
     $options->setUsername('root');
     $options->setPassword('root');
     $httpAdapter = new \InfluxDB\Adapter\UdpAdapter($options);
     $client = new \InfluxDB\Client($httpAdapter);
     $client->mark("udp.test", ["mark" => "element"]);
 }
Example #19
0
 /**
  * @group udp
  */
 public function testWriteUDPPackagesToInvalidHostname()
 {
     $options = new Options();
     $options->setHost("www.test-invalid.this-is-not-a-tld");
     $options->setUsername("nothing");
     $options->setPassword("nothing");
     $options->setPort(15984);
     $adapter = new UdpAdapter($options);
     $object = new Client($adapter);
     $object->mark("udp.test", ["mark" => "element"]);
 }
Example #20
0
 /**
  * @expectedException BadMethodCallException
  */
 public function testNeedQueryableInterfaceDuringQuery()
 {
     $client = new Client(new \stdClass());
     $client->query("OK", []);
 }