add example for USG traffic statistics (#3)

* add example for USG traffic statistics

* fix bps
This commit is contained in:
Sebastian Nohn 2017-11-26 23:42:12 +01:00 committed by Joris van de Sande
parent e9a769d943
commit d54f15be2a
2 changed files with 50 additions and 0 deletions

View File

@ -0,0 +1,39 @@
<?php
require __DIR__ . '/../vendor/autoload.php';
$config = require 'config.php';
use JVDS\UnifiApiClient\Client;
use GuzzleHttp\Client as HttpClient;
use GuzzleHttp\Exception\RequestException;
$apiClient = new Client(new HttpClient(['base_uri' => $config['base_uri']]));
try {
// login to the unifi controller API
$apiClient->login($config['username'], $config['password']);
// Fetch statistics for the given site
$responseBody = $apiClient
->health($config['site'])
->getBody()
->getContents();
$data = json_decode($responseBody, true);
echo "Latency: ".$data['data'][2]['latency']." ms\n";
echo "RX : ".round(($data['data'][2]['rx_bytes-r']/1024/1024*8), 2)." Mb/s\n";
echo "TX : ".round(($data['data'][2]['tx_bytes-r']/1024/1024*8), 2)." Mb/s\n";
$apiClient->logout();
} catch (RequestException $e) {
echo $e->getMessage() . PHP_EOL;
echo '----- Request ------' . PHP_EOL;
echo $e->getRequest()->getBody()->getContents();
echo PHP_EOL;
echo '----- Response ------' . PHP_EOL;
echo $e->hasResponse() ? $e->getResponse()->getBody()->getContents() : '- no response -';
echo PHP_EOL;
}

View File

@ -90,6 +90,17 @@ class Client
$this->client->request('get', '/logout', ['allow_redirects' => false] + $this->requestOptions);
}
/**
* @param string $site
*
* @return ResponseInterface
* @throws GuzzleException
*/
public function health($site)
{
return $this->get('/api/s/' . $site . '/stat/health');
}
/**
* @param string $site
*