Skip to content

Commit

Permalink
Merge pull request #46 from exonet/add-statistics
Browse files Browse the repository at this point in the history
Get PowerDNS statistics
  • Loading branch information
trizz authored Oct 30, 2020
2 parents b81ed43 + 1f508b5 commit 6befb71
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 2 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ All notable changes to `powerdns-php` will be documented in this file.
Updates should follow the [Keep a CHANGELOG](http://keepachangelog.com/) principles.

## Unreleased
[Compare v2.4.0 - Unreleased](https://github.com/exonet/powerdns-php/compare/v2.4.0...develop)
[Compare v2.5.0 - Unreleased](https://github.com/exonet/powerdns-php/compare/v2.5.0...develop)

## [v2.5.0](https://github.com/exonet/powerdns-php/releases/tag/v2.5.0) - 2020-10-30
### Added
- Get PowerDNS statistics.

## [v2.4.0](https://github.com/exonet/powerdns-php/releases/tag/v2.4.0) - 2020-08-04
### Added
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
}
},
"config": {
"sort-packages": true
"sort-packages": true,
"platform-check": false
}
}
27 changes: 27 additions & 0 deletions src/Powerdns.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,33 @@ public function cryptokeys(string $canonicalDomain): Cryptokey
return new Cryptokey($this->connector, $canonicalDomain);
}

/**
* Query PowerDNS internal statistics.
* The ring statistics are disabled by default to speedup the request and reduce the response size.
*
* The $statistics and $includeRings parameters are supported in PowerDNS 4.3 and newer.
* On older PowerDNS instances these parameters are ignored.
*
* @param null|string $statistic Optional name of a specific statistic to get.
* @param bool $includeRings Include ring statistics or not.
*
* @return array An array with statistics.
*/
public function statistics($statistic = null, $includeRings = false): array
{
// Convert $includeRings param to string.
$includeRings = $includeRings ? 'true' : 'false';

$endpoint = 'statistics?includerings='.$includeRings;

// Request a specific statistic.
if ($statistic) {
$endpoint .= '&statistic='.$statistic;
}

return $this->connector->get($endpoint);
}

/**
* Get the PowerDNS server version.
*
Expand Down
34 changes: 34 additions & 0 deletions tests/PowerdnsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,40 @@ public function testConfigViaMethods(): void
$this->assertSame('test-key', $config['apiKey']);
}

public function testStatistics(): void
{
$connector = Mockery::mock(Connector::class);
$connector->shouldReceive('get')->withArgs(['statistics?includerings=false'])->once()->andReturn($example = [
[
'name' => 'corrupt-packets',
'type' => 'StatisticItem',
'value' => 0,
],
]);

$powerDns = new Powerdns(null, null, null, null, $connector);
$stats = $powerDns->statistics();

self::assertEquals($example, $stats);
}

public function testStatisticsWithParams(): void
{
$connector = Mockery::mock(Connector::class);
$connector->shouldReceive('get')->withArgs(['statistics?includerings=true&statistic=corrupt-packets'])->once()->andReturn($example = [
[
'name' => 'corrupt-packets',
'type' => 'StatisticItem',
'value' => 0,
],
]);

$powerDns = new Powerdns(null, null, null, null, $connector);
$stats = $powerDns->statistics('corrupt-packets', true);

self::assertEquals($example, $stats);
}

public function testZone(): void
{
$connector = Mockery::mock(Connector::class);
Expand Down

0 comments on commit 6befb71

Please sign in to comment.