天天看點

如何監控MemCached的狀态

使用MemCached以後,肯定希望知道cache的效果,對于MemCached的一些運作狀态進行監控是必要的。MemCached也提供了stats接口輸出一些資訊,最簡單的方式,就是telnet上去輸入stats檢視:

telnet 127.0.0.1 11211
Trying 127.0.0.1 ...
Connected to memcache_test_host (127.0.0.1 ).
Escape character is '^]'.
stats
STAT pid 7186
STAT uptime 1695
STAT time 1238401344
STAT version 1.2.6
STAT pointer_size 64
STAT rusage_user 0.003999
STAT rusage_system 0.002999
STAT curr_items 1
STAT total_items 54
STAT bytes 135
STAT curr_connections 2
STAT total_connections 111
STAT connection_structures 4
STAT cmd_get 3
STAT cmd_set 54
STAT get_hits 0
STAT get_misses 3
STAT evictions 0
STAT bytes_read 5957
STAT bytes_written 50914
STAT limit_maxbytes 2147483648
STAT threads 1
END
      

這種方式相當的不友善,是以網上就有各種不同用戶端接口寫的工具,比如用perl寫的這個memcache-tool:

./memcached_tool
Usage: memcached-tool  [mode]

       memcached-tool 10.0.0.5:11211 display    # shows slabs
       memcached-tool 10.0.0.5:11211            # same.  (default is display)
       memcached-tool 10.0.0.5:11211 stats      # shows general stats
       memcached-tool 10.0.0.5:11211 move 7 9   # takes 1MB slab from class #7
                                                # to class #9.

You can only move slabs around once memory is totally allocated, and only
once the target class is full.  (So you can't move from #6 to #9 and #7
to #9 at the same itme, since you'd have to wait for #9 to fill from
the first reassigned page)

$ ./memcached_tool 127.0.0.1:11211 stats
#127.0.0.1:11211 Field       Value
                   bytes         135
              bytes_read        5964
           bytes_written       51394
                 cmd_get           3
                 cmd_set          54
   connection_structures           4
        curr_connections           3
              curr_items           1
               evictions           0
                get_hits           0
              get_misses           3
          limit_maxbytes  2147483648
                     pid        7186
            pointer_size          64
           rusage_system    0.002999
             rusage_user    0.003999
                 threads           1
                    time  1238401521
       total_connections         112
             total_items          54
                  uptime        1872
                 version       1.2.6
      

指令行的方式,在批處理調用的時候比較友善。但是在展現方面還是web方式更加直覺有效,是以就有了php寫的memcache.php,是的,用一次就知道這是我想要的。