memcachedの各種ステータス表示ツール memcached_stat.php
PHPでCLIで簡単にmemcashedの各種ステータス表示するツール作った
スタブ毎のキーとそのヒット率の調査のために作ってみたんだがそれなりに使えそうなので公開してみる
とりあえず、ソースはこんな感じ。
<?php function usage() { echo "Usage: memcached_stat.php [ option ]\n"; echo "option:\n"; echo "\t dump # dump key and status\n"; echo "\t get <key> # show key value\n"; echo "\t status # show status\n"; echo "\t slabs # show slabs status\n"; echo "\t items # show items status\n"; echo "\t sizes # show items sizes\n"; echo "\t settings # show settings\n"; echo "\t maps # maps is enabled\n"; echo "\t malloc # malloc is enabled\n"; echo "\t reset # reset is enabled\n"; echo "\t cachedump <slab> <limit> # show slabs key and status\n"; } // リモートホストに接続したい場合はこのあたりを書き換える $memcache = new Memcache; $memcache->connect('127.0.0.1', 11211) or die ('Could not connect'); for ($i=1; $i < $argc; $i++) { switch($argv[$i]) { case 'get': $i++; $key = $argv[$i]; if(isset($argv[$i])) { $value = var_export(unserialize($memcache->get($key)), true); echo 'key='.$key."\n"; echo 'value='.$value."\n"; exit; } else { //error usage(); exit; } break; case 'status': $items = $memcache->getStats(); echo var_export($items, true)."\n"; exit; case 'reset': case 'malloc': case 'maps': case 'slabs': case 'sizes': case 'items': case 'settings': $items = $memcache->getStats($argv[$i]); echo var_export($items, true)."\n"; exit; case 'cachedump': if (isset($argv[$i+1])) { $slab = $argv[$i+1]; } else { $slab = 1; } if (isset($argv[$i+2])) { $number = $argv[$i+2]; } else { $number = 100; } $items = $memcache->getStats($argv[$i++], $slab, $number); echo var_export($items, true)."\n"; exit; case 'dump': break; default: usage(); exit(1); } } $items = $memcache->getStats('items'); if (!empty($items['items'])) { foreach ($items['items'] as $slab => $entry) { $number = $entry['number'] * 2; $dump = $memcache->getStats('cachedump', $slab, $number); foreach ($dump as $key => $value){ // 除外フィルタ(必要ならここに書く) /* if (strpos($key, 'XXXXXX') === 0) { continue; } else if (strpos($key, 'YYYYYY') === 0) { continue; } */ echo date('Y-m-d H:i:s', $value[1]).' '.$slab.' '.$value[0].' '.$key."\n"; } } }
使い方と結果は有用そうなのだけ書くとこんな感じ。
1.全てのキーをダンプする
フォーマットはキャッシュの期限、スラグ、サイズ(バイト)、キー の順
コマンド
php memcached_stat.php or php memcached_stat.php dump
結果
2010-08-31 09:14:27 1 11 F9A2 2010-08-31 09:14:27 1 11 F9C6 2010-08-31 10:07:58 2 18 E6EA 2010-08-31 10:07:58 2 18 E6E8 2010-08-31 10:07:58 2 18 E6E7 2010-08-31 10:07:58 2 18 E6EB 〜略〜 2010-08-20 21:38:02 21 8041 96E68B 2010-08-20 22:11:11 22 9115 90E6E1 2010-08-20 21:17:08 23 10240 9AA6EB 2010-08-20 22:11:05 24 13906 9BB6B4 2010-08-20 17:14:54 25 19629 96C69D
2.指定したキーの内容をダンプする
コマンド
php memcached_stat.php get z88UqKr
結果
key=z88UqKr value=array ( 0 => array ( 'user' => '4dcthuynjiok', 'entry_date' => '2010-06-15T16:05:24+09:00', 'modify_date' => '2010-06-15T16:05:24+09:00', ), 1 => array ( 'user' => 'ihninios78y', 'entry_date' => '2010-06-15T18:05:24+09:00', 'modify_date' => '2010-06-15T18:05:24+09:00', ), )
3.memcachedのステータスを表示
コマンド
php memcached_stat.php status
結果
array ( 'pid' => '17167', 'uptime' => '1731923', 'time' => '1282310744', 'version' => '1.4.5', 'pointer_size' => '32', 'rusage_user' => '44.225276', 'rusage_system' => '1073.168853', 'curr_connections' => '5', 'total_connections' => '44683', 'connection_structures' => '15', 'cmd_get' => '3343', 'cmd_set' => '1863', 'cmd_flush' => '0', 'get_hits' => '327', 'get_misses' => '1196', 'delete_misses' => '330', 'delete_hits' => '54719', 'incr_misses' => '0', 'incr_hits' => '0', 'decr_misses' => '0', 'decr_hits' => '0', 'cas_misses' => '0', 'cas_hits' => '0', 'cas_badval' => '0', 'auth_cmds' => '0', 'auth_errors' => '0', 'bytes_read' => '5862', 'bytes_written' => '6641', 'limit_maxbytes' => '408864', 'accepting_conns' => '1', 'listen_disabled_num' => '0', 'threads' => '4', 'conn_yields' => '0', 'bytes' => '4681041', 'curr_items' => '4940', 'total_items' => '2831863', 'evictions' => '0', 'reclaimed' => '101627', )
4.スラグごとの統計情報を表示
コマンド
php memcached_stat.php slabs
結果
array ( 1 => array ( 'chunk_size' => '80', 'chunks_per_page' => '13107', 'total_pages' => '1', 'total_chunks' => '13107', 'used_chunks' => '24', 'free_chunks' => '0', 'free_chunks_end' => '13083', 'mem_requested' => '1920', 'get_hits' => '207', 'cmd_set' => '5', 'delete_hits' => '0', 'incr_hits' => '0', 'decr_hits' => '0', 'cas_hits' => '0', 'cas_badval' => '0', ), 2 => array ( 'chunk_size' => '104', 'chunks_per_page' => '10082', 'total_pages' => '1', 'total_chunks' => '10082', 'used_chunks' => '922', 'free_chunks' => '30', 'free_chunks_end' => '9130', 'mem_requested' => '80945', 'get_hits' => '198651', 'cmd_set' => '1569', 'delete_hits' => '448', 'incr_hits' => '0', 'decr_hits' => '0', 'cas_hits' => '0', 'cas_badval' => '0', ), 〜略〜 42 => array ( 'chunk_size' => '1048576', 'chunks_per_page' => '1', 'total_pages' => '2', 'total_chunks' => '2', 'used_chunks' => '1', 'free_chunks' => '1', 'free_chunks_end' => '0', 'mem_requested' => '835998', 'get_hits' => '144', 'cmd_set' => '315', 'delete_hits' => '0', 'incr_hits' => '0', 'decr_hits' => '0', 'cas_hits' => '0', 'cas_badval' => '0', ), 'active_slabs' => '39', 'total_malloced' => '67681640', )
5.スラグごとのアイテムステータスを表示
コマンド
php memcached_stat.php items
結果
array ( 'items' => array ( 1 => array ( 'number' => '24', 'age' => '131593', 'evicted' => '0', 'evicted_nonzero' => '0', 'evicted_time' => '0', 'outofmemory' => '0', 'tailrepairs' => '0', 'reclaimed' => '0', ), 2 => array ( 'number' => '922', 'age' => '147211', 'evicted' => '0', 'evicted_nonzero' => '0', 'evicted_time' => '0', 'outofmemory' => '0', 'tailrepairs' => '0', 'reclaimed' => '8', ), 〜略〜 42 => array ( 'number' => '1', 'age' => '1706796', 'evicted' => '0', 'evicted_nonzero' => '0', 'evicted_time' => '0', 'outofmemory' => '0', 'tailrepairs' => '0', 'reclaimed' => '21', ), ), )
6.サイズ毎のキーの数を表示
コマンド
php memcached_stat.php sizes
結果
array ( 96 => '813', 128 => '186', 160 => '15', 192 => '38', 〜略〜 245984 => '1', 552608 => '1', 593152 => '1', 842304 => '1', )
7.現在のmemcachedの設定値を表示
コマンド
php memcached_stat.php settings
結果
array ( 'maxbytes' => '108864', 'maxconns' => '1024', 'tcpport' => '11211', 'udpport' => '11211', 'inter' => '127.0.0.1', 'verbosity' => '1', 'oldest' => '0', 'evictions' => 'on', 'domain_socket' => 'NULL', 'umask' => '700', 'growth_factor' => '1.25', 'chunk_size' => '48', 'num_threads' => '4', 'stat_key_prefix' => ':', 'detail_enabled' => 'no', 'reqs_per_event' => '20', 'cas_enabled' => 'yes', 'tcp_backlog' => '1024', 'binding_protocol' => 'auto-negotiate', 'auth_enabled_sasl' => 'no', 'item_size_max' => '48576', )
8.現在の各統計情報のリセット
コマンド
php memcached_stat.php reset
結果
true //成功の場合
9.指定したスラブのキーを100個までもしくはlimit個表示する
コマンド
php memcached_stat.php cachedump <slab> <limit> php memcached_stat.php cachedump 1 5
結果
array ( 'F8EE' => //キー array ( 0 => '11', //サイズ(バイト) 1 => '1283331661', //UNIXタイムでキャッシュの期限を意味する ), 'F8E6' => array ( 0 => '11', 1 => '1284712170', ), 'F6B7' => array ( 0 => '11', 1 => '1284537409', ), 'F99F' => array ( 0 => '11', 1 => '1284529037', ), 'F8A3' => array ( 0 => '11', 1 => '1284529037', ), )
その他は省略