diff --git a/CHANGELOG.md b/CHANGELOG.md index a63696b..1e15823 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/composer.json b/composer.json index 206bd56..a18989d 100755 --- a/composer.json +++ b/composer.json @@ -39,6 +39,7 @@ } }, "config": { - "sort-packages": true + "sort-packages": true, + "platform-check": false } } diff --git a/src/Powerdns.php b/src/Powerdns.php index 95f12d3..9401a19 100644 --- a/src/Powerdns.php +++ b/src/Powerdns.php @@ -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. * diff --git a/tests/PowerdnsTest.php b/tests/PowerdnsTest.php index cd5078d..55fed56 100644 --- a/tests/PowerdnsTest.php +++ b/tests/PowerdnsTest.php @@ -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);