最近某社のシステムで動いてるPHPを5.3から5.6に切り替えた関係でOpcacheのモニタリング用スクリプトを書いた。2年前に別の会社で5.3から5.5に移行した際に似たようなことをした記憶があるが最早忘却の彼方である。歳は取りたくない。
opcache-status.php
<?php
$conf = opcache_get_configuration();
$status = opcache_get_status();
$statictics = $status['opcache_statistics'];
$configurations = array(
'memory_max' => $conf['directives']['opcache.memory_consumption'],
'interned_strings_max' => $conf['directives']['opcache.interned_strings_buffer'],
'max_file_size' => $conf['directives']['opcache.max_file_size'],
'max_accelerated_files' => $conf['directives']['opcache.max_accelerated_files'],
);
$statictics = array(
'num_cached_scripts' => $statictics['num_cached_scripts'],
'num_cached_keys' => $statictics['num_cached_keys'],
'max_cached_keys' => $statictics['max_cached_keys'],
'opcache_hit_rate' => $statictics['opcache_hit_rate'],
);
$result = array(
'config' => $configurations,
'is_cache_full' => $status['cache_full'] ? 1 : 0,
'memory_usage' => $status['memory_usage'],
'interned_strings_usage' => $status['interned_strings_usage'],
'statictics' => $statictics,
);
echo json_encode($result);
あとはこれのハンドラを設定して、
opcache-status.conf
<VirtualHost _default_:80>
DocumentRoot "/var/www/opcache"
<Location /opcache-status.php>
Order deny,allow
Deny from all
Allow from 127.0.0.1
</Location>
</VirtualHost>
curl
とかでアクセスすれば良い感じにJSONを返してくれる。jq
と組み合わせるとなお便利。
$ curl -s "http://127.0.0.1/opcache-status.php"
ちなみに最近はPHP書いてない。GoとLuaを書いている。なお、一番多いのはYAML(Ansibleのplaybook)である。