stevencorona / opcache-json
这个库可以从Zend Opcache中抓取一些关键统计数据并以JSON格式输出,用于监控和警报。也可以输出到statsd存储。
Requires
- php: >=5.5.0
- ext-json: *
- domnikl/statsd: *
This package is not auto-updated.
Last update: 2024-09-24 02:18:48 UTC
README
PHP 5.5非常棒。它速度快,更加现代,并且拥有许多优秀的功能。其中之一就是它默认捆绑了Zend Opcache——再见了APC!太好了!
但这个胜利也带来了一些新的负担。Zend Opcache有大量的配置设置,监控它们并不容易。默认输出有点笨拙,并没有真正考虑到现代DevOps的需求。
为现代DevOps设计
这个库就是为了解决这个问题而设计的。它清理了Zend Opcache的内部统计数据,使其可以轻松地以JSON格式抓取,应用程序可以选择将其公开到内部HTTP端点。Opcache-json还通过statsd公开这些数据,您可以使用它来查看历史缓存数据并观察趋势。
入门指南
最佳安装方式是使用Composer,并将以下内容添加到项目的composer.json
文件中
{ "require": { "stevencorona/opcache-json": "*" } }
基本用法
// By default Statsd output is disabled // $opcache = new Opcache\Status; // Or pass in Statsd config with an array // $opcache = new Opcache\Status(["host" => "localhost", "port" => "8125"]); // Or configure the Statsd connection with a block $opcache = new Opcache\Status(function() { $c = new \Domnikl\Statsd\Connection\UdpSocket("127.0.0.1", "8125"); return new \Domnikl\Statsd\Client($c, "opcache"); }); echo $opcache->status(true);
Statsd输出
Statsd输出是默认的。只需通过传递一个块或数组到Opcache\Status
构造函数来配置您的连接。以下键作为仪表值发送到Statsd
opcache.used_memory:5593056|g
opcache.free_memory:61515808|g
opcache.wasted_memory:0|g
opcache.current_wasted_percentage:0|g
opcache.num_cached_scripts:11|g
opcache.num_cached_keys:13|g
opcache.max_cached_keys:3907|g
opcache.hits:33|g
opcache.start_time:1396011952|g
opcache.last_restart_time:0|g
opcache.oom_restarts:0|g
opcache.hash_restarts:0|g
opcache.manual_restarts:0|g
opcache.misses:11|g
opcache.blacklist_misses:0|g
opcache.blacklist_miss_ratio:0|g
opcache.opcache_hit_rate:75|g
提示:如果您有多个服务器或多个工作环境(提示:我们大多数人都有),您可能希望将服务器主机名和PHP PID添加到键命名空间中。
额外的统计数据
如果您想发送有关Opcache状态的额外统计数据,即以下指标之一
opcache.opcache_enabled|1|g
opcache.cache_full|0|g
opcache.restart_pending|0|g
opcache.restart_in_progress|0|g
您可以通过在调用status()之前调用Opcache\Status->send_extra_stats()方法来实现
$opcache->send_extra_stats(array ( 'opcache_enabled' => true, 'cache_full' => true, ));
如何在生产中运行
显然,但不够优雅的方法可能是将这个库嵌入到您的应用程序中并创建一个端点。可行,但不是那么干净。
这里有一个更好的方法,如果您使用PHP-FPM。首先,您需要安装cgi-fcgi
实用程序(它在ubuntu包apt-get install libfcgi0ldbl
中)。
接下来,将此存储库克隆到服务器上的某个位置。在这个演示中,我们假设它在/tmp/opcache-json
。我还假设您的PHP-FPM池在127.0.0.1:9000
上运行。现在,这里是技巧
SCRIPT_NAME=/example.php \
SCRIPT_FILENAME=/tmp/opcache-json/example.php \
REQUEST_METHOD=GET \
cgi-fcgi -bind -connect 127.0.0.1:9000
这将返回运行中的PHP-FPM池的JSON状态。您可以将它放入cron或其他任务中,并自动监控它,而无需在运行中的应用程序中添加此代码。
JSON输出
JSON输出易于阅读,可以公开为内部HTTP端点,可供人类眼睛或监控系统消费。它非常直接——输出看起来像这样
运行测试
您必须启用opcache.enable_cli
才能运行测试。PHP必须使用Zend Opcache构建。
$ composer install
$ ./vendor/bin/phpunit -d opcache.enable_cli=1 tests/Opcache.php
MIT许可证(MIT)
Copyright (c) 2014 Steve Corona Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.