PHP code example of yproximite / influxdb-bundle

1. Go to this page and download the library: Download yproximite/influxdb-bundle library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

yproximite / influxdb-bundle example snippets


// app/AppKernel.php

public function registerBundles()
{
    $bundles = array(
        // ...
        new Yproximite\InfluxDbBundle\InfluxDbBundle(),
    );

    // ...

    return $bundles
}

$httpDatabase = $this->get('yproximite_influx_db.connection.http'); // Default HTTP connection
$udpDatabase = $this->get('yproximite_influx_db.connection.udp');   // Default UDP connection

// Same as before.
$httpDatabase = $this->get('yproximite_influx_db.connection.default.http');
$udpDatabase = $this->get('yproximite_influx_db.connection.default.udp');

$database = $this->get('yproximite_influx_db.connection_registry')->getDefaultHttpConnection();
$database = $this->get('yproximite_influx_db.connection_registry')->getDefaultUdpConnection();

// Same as before.
$database = $this->get('yproximite_influx_db.connection_registry')->getHttpConnection('default');
$database = $this->get('yproximite_influx_db.connection_registry')->getUdpConnection('default');

$time = new \DateTime();

$points = [new Point(
    'test_metric', // name of the measurement
    0.64, // the measurement value
    ['host' => 'server01', 'region' => 'italy'], // optional tags
    ['cpucount' => rand(1,100), 'memory' => memory_get_usage(true)], // optional additional fields
    $time->getTimestamp()
)];

// UDP
$container
    ->get('event_dispatcher')
    ->dispatch(InfluxDbEvent::NAME, new UdpEvent($points, Database::PRECISION_SECONDS));

// HTTP
$container
    ->get('event_dispatcher')
    ->dispatch(InfluxDbEvent::NAME, new HttpEvent($points, Database::PRECISION_SECONDS));

// Deferred Events
// Collect your measurements during the request and make only one write to influxdb.
// Deferred events are catched and "stored". Than on the kernel.terminate event one write per
// event type and precision will be fired.

// UDP
$container
    ->get('event_dispatcher')
    ->dispatch(InfluxDbEvent::NAME, new DeferredUdpEvent($points, Database::PRECISION_SECONDS));

// HTTP
$container
    ->get('event_dispatcher')
    ->dispatch(InfluxDbEvent::NAME, new DeferredHttpEvent($points, Database::PRECISION_SECONDS));

// UDP
$container
    ->get('event_dispatcher')
    ->dispatch(InfluxDbEvent::NAME, new UdpEvent($points, Database::PRECISION_SECONDS, 'other_connection'));

// HTTP
$container
    ->get('event_dispatcher')
    ->dispatch(InfluxDbEvent::NAME, new HttpEvent($points, Database::PRECISION_SECONDS, 'other_connection'));

$form
    ->add('measurement', MeasurementType::class, [
        'connection' => 'default' // Optional: The connection you want to use.
    ])
    ->add('fields', FieldKeyType::class, [
        'measurement' => 'cpu', // The concerned measurement.
        'multiple' => true, // Parent type is ChoiceType. You can use parent option like multiple.
    ])
    ->add('tags', TagKeyType::class, [
        'measurement' => 'cpu',
        'exclude_host' => false, // True by default. Excludes the 'host' choice value.
        'multiple' => true,
    ])
    ->add('tag_value', TagValueType::class, [
        'measurement' => 'disk',
        'tag_key' => 'fstype', // The related tag key.
    ])
;

 make start  # will start the needed containers and put you inside the php-cli container