LoginSignup
7
8

More than 5 years have passed since last update.

PHPのOpcacheの利用状況をモニタリング

Last updated at Posted at 2015-08-02

最近某社のシステムで動いてる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)である。

7
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
8