Memcached collect client ip address
在 ElastiCache Memcached 中如何去取得 client ip address?
因為使用者無法直接登入 ElastiCache Memcached 節點中,直接使用 netstat 命令,來分析該節點實例連線的狀況。
$ netstat -nat | grep "ESTABLISHED" |awk '{print $5}'|sort|uniq -c|sort -nr|head -20
但您可以透過連進特定節點 memcachd 服務後,使用 “stats conns” 命令來取得相關資訊。
$ telnet xxx.usw2.cache.amazonaws.com 11211
stats conns
STAT 11:addr tcp:0.0.0.0:11211
STAT 11:state conn_listening
STAT 11:secs_since_last_cmd 4866
STAT 12:addr tcp6:[::]:11211
STAT 12:state conn_listening
STAT 12:secs_since_last_cmd 4866
STAT 13:UDP UDP
STAT 13:addr udp:0.0.0.0:11211
STAT 13:state conn_read
STAT 13:secs_since_last_cmd 4866
STAT 14:UDP UDP
STAT 14:addr udp6:[::]:11211
STAT 14:state conn_read
STAT 14:secs_since_last_cmd 4866
STAT 15:addr tcp:127.0.0.1:35590
STAT 15:listen_addr tcp:127.0.0.1:11211
STAT 15:state conn_read
STAT 15:secs_since_last_cmd 5
STAT 16:addr tcp:10.0.200.161:46652
STAT 16:listen_addr tcp:10.0.100.229:11211
STAT 16:state conn_parse_cmd
STAT 16:secs_since_last_cmd 0
END
https://github.com/memcached/memcached/blob/master/doc/protocol.txt
Connection statistics
— — — — — — — — — — -
The “stats” command with the argument of “conns” returns information
about currently active connections and about sockets that are listening
for new connections. The data is returned in the format:
補充資訊 (For Redis):
在 Redis 上面,可以使用命令 “client list” 來取得當下,有那些前端連線有連接到該節點,用於判斷是那一前端連接在操作那一些命令。以下是範例.
https://redis.io/commands/client-list
redis > client list
id=141285 addr=172.31.15.23:47206 fd=11 name= age=0 idle=0 flags=N db=0 sub=0 psub=0 multi=-1 qbuf=26 qbuf-free=32742 obl=0 oll=0 omem=0 events=r cmd=client
另外,您也可以透過 “info client” 命令,來取得相關 client 端連線的資訊。以下是範例,提供您參考。
redis > info client
# Clients
connected_clients:2
cluster_connections:0
maxclients:10000
client_recent_max_input_buffer:32
client_recent_max_output_buffer:0
blocked_clients:0
tracking_clients:0
clients_in_timeout_table:0