redis 簡易監(jiān)控的幾種方法
簡介
針對Redis 實現(xiàn)性能監(jiān)控的幾種方法
一、使用info命令
命令說明
127.0.0.1:6380> info
# Server
redis_version:3.2.11
redis_git_sha1:00000000
redis_git_dirty:0
redis_build_id:41a708998db14d05
redis_mode:standalone
os:Linux 3.0.76-0.11-default x86_64
arch_bits:64
multiplexing_api:epoll
gcc_version:4.3.4
process_id:16942
run_id:e3996796aba6cff723f1e58feb42fa9ea9515b01
tcp_port:6380
uptime_in_seconds:4215152
uptime_in_days:48
hz:10
lru_clock:3786096
executable:/home/iotweb/local/redis/./bin/redis-server
config_file:/home/iotweb/local/redis/redis.conf
# Clients
connected_clients:2
client_longest_output_list:0
client_biggest_input_buf:0
blocked_clients:0
# Memory
used_memory:6392944
used_memory_human:6.10M
used_memory_rss:13623296
used_memory_rss_human:12.99M
used_memory_peak:12523592
used_memory_peak_human:11.94M
total_system_memory:33673551872
total_system_memory_human:31.36G
used_memory_lua:37888
used_memory_lua_human:37.00K
maxmemory:0
maxmemory_human:0B
maxmemory_policy:noeviction
mem_fragmentation_ratio:2.13
mem_allocator:jemalloc-4.0.3
# Persistence
loading:0
rdb_changes_since_last_save:0
rdb_bgsave_in_progress:0
rdb_last_save_time:1510133670
rdb_last_bgsave_status:ok
rdb_last_bgsave_time_sec:0
rdb_current_bgsave_time_sec:-1
aof_enabled:0
aof_rewrite_in_progress:0
aof_rewrite_scheduled:0
aof_last_rewrite_time_sec:-1
aof_current_rewrite_time_sec:-1
aof_last_bgrewrite_status:ok
aof_last_write_status:ok
# Stats
total_connections_received:2013
total_commands_processed:3129128
instantaneous_ops_per_sec:0
total_net_input_bytes:138114786
total_net_output_bytes:3167119897
instantaneous_input_kbps:0.00
instantaneous_output_kbps:0.00
rejected_connections:0
sync_full:0
sync_partial_ok:0
sync_partial_err:0
expired_keys:0
evicted_keys:0
keyspace_hits:1
keyspace_misses:1
pubsub_channels:0
pubsub_patterns:0
latest_fork_usec:1107
migrate_cached_sockets:0
# Replication
role:master
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0
# CPU
used_cpu_sys:3631.63
used_cpu_user:2210.61
used_cpu_sys_children:0.20
used_cpu_user_children:1.47
# Cluster
cluster_enabled:0
# Keyspace
db0:keys=53500,expires=0,avg_ttl=0
指標(biāo)說明
- Server 指通用的服務(wù)器信息,包括版本號、監(jiān)聽端口、進(jìn)程ID等
- Clients 指客戶端信息,包括接入數(shù)量、阻塞數(shù)量等
- Memory 內(nèi)存信息,包括虛擬內(nèi)存、物理內(nèi)存、碎片比例(rss/used)等
- Persistence 持久化信息,包括RDB和AOF的相關(guān)信息
- Stats 統(tǒng)計信息,包括歷史命令數(shù)、鍵數(shù)量、網(wǎng)絡(luò)流量等
- Replication 主從復(fù)制信息,包括slave及backlog相關(guān)信息
- CPU CPU使用相關(guān)信息
- Cluster 集群信息
- Keyspace 邏輯空間信息,包括數(shù)據(jù)庫鍵數(shù)、超時及TTL信息
二、使用redis-stat
項目地址
https://github.com/junegunn/redis-stat
項目說明
redis-stat 是基于Ruby實現(xiàn)的一個簡單易用的 Redis監(jiān)控工具
redis-stat 的實現(xiàn)原理基于INFO(見上)命令,相比MONITOR命令來說其對Redis不會產(chǎn)生任何性能影響。
此外 redis-stat項目也提供了Console及Web界面兩種展現(xiàn)形式。
如何搭建
redis-stat 的使用需要依賴ruby,但Jruby為我們提供了使用Java的打開方式。
- 下載Jar包,點擊這里
- 執(zhí)行啟動腳本
java -jar redis-stat-0.4.14.jar 127.0.0.1:6380 3 --server=63800 > monitor.log &
- 控制臺輸出
Puma 2.3.2 starting...
* Min threads: 0, max threads: 16
* Environment: development
* Listening on tcp://0.0.0.0:63800
== Sinatra/1.3.6 has taken the stage on 63800 for production with backup from Puma
┌────────────────────────┬────────────────┐
│ │ 127.0.0.1:6380 │
├────────────────────────┼────────────────┤
│ redis_version │ 3.2.11 │
│ redis_mode │ standalone │
│ process_id │ 16942 │
│ uptime_in_seconds │ 4216406 │
│ uptime_in_days │ 48 │
│ role │ master │
│ connected_slaves │ 0 │
│ aof_enabled │ 0 │
│ rdb_bgsave_in_progress │ 0 │
│ rdb_last_save_time │ 1510133670 │
└────────────────────────┴────────────────┘
┌────────┬──┬──┬──┬───┬──────┬──────┬─────┬─────┬─────┬─────┬──────┬─────┬─────┬─────┐
time us sy cl bcl mem rss keys cmd/s exp/s evt/s hit%/s hit/s mis/s aofcs
├────────┼──┼──┼──┼───┼──────┼──────┼─────┼─────┼─────┼─────┼──────┼─────┼─────┼─────┤
10:26:30 - - 1 0 6.08MB 13.0MB 53.5k - - - - - - 0B
10:26:33 0 0 1 0 6.08MB 13.0MB 53.5k 0.33 0 0 - 0 0 0B
10:26:36 0 0 1 0 6.08MB 13.0MB 53.5k 0.33 0 0 - 0 0 0B
監(jiān)控效果
使用 http://127.0.0.1:63800/ 地址可以直接訪問Web界面,見下圖

指標(biāo)說明
| 簡寫 | 指標(biāo) | 說明 |
|---|---|---|
| us | used_cpu_user | 用戶空間占用CPU百分比 |
| sy | used_cpu_sys | 內(nèi)核空間占用CPU百分比 |
| cl | connected_clients | 連接客戶端數(shù)量 |
| bcl | blocked_clients | 阻塞客戶端數(shù)量(如BLPOP) |
| mem | used_memory | 使用總內(nèi)存 |
| rss | used_memory_rss | 使用物理內(nèi)存 |
| keys | dbx.keys | key的總數(shù)量 |
| cmd/s | command/s | 每秒執(zhí)行命令數(shù) |
| exp/s | expired_keys/s | 每秒過期key數(shù)量 |
| evt/s | evicted_keys/s | 每秒淘汰key數(shù)量 |
| hit%/s | keyspace_hitratio/s | 每秒命中百分比 |
| hit/s | keyspace_hits/s | 每秒命中數(shù)量 |
| mis/s | keyspace_miss/s | 每秒丟失數(shù)量 |
| aofcs | aof_current_size | AOF日志當(dāng)前大小 |
類似redis-stat 還有redis-live等若干開源項目
三、自研工具
工具簡介
redis-stat提供了一種簡易的方式實現(xiàn)對Redis實例的監(jiān)控,但多數(shù)場景下可能需要進(jìn)行定制,比如鑒權(quán)方式、統(tǒng)計指標(biāo)、告警等等
為實現(xiàn)更靈活的控制,可利用類似的方式對INFO信息進(jìn)行解析,以實現(xiàn)自有Redis實例的統(tǒng)一監(jiān)控。

代碼樣例
- 調(diào)用info 命令
public String getInfo() {
Jedis jedis = get();
try {
String infoContent = jedis.info();
return infoContent;
} finally {
if (jedis != null) {
jedisPool.returnResource(jedis);
}
}
}
- 解析結(jié)果
for (String line : lines) {
if (line.isEmpty()) {
continue;
}
if (line.startsWith("#")) {
part = line.replace("#", "").trim();
continue;
}
int index = line.indexOf(':');
if (index >= 0) {
infoMap.put(part + "." + line.substring(0, index), line.substring(index + 1));
}
}
- 數(shù)據(jù)轉(zhuǎn)換處理
Map<String, String> infoMap = parseInfo(infoContent);
stat.setUsed_cpu_sys(getIntValue(infoMap, "CPU.used_cpu_sys"));
stat.setUsed_cpu_user(getIntValue(infoMap, "CPU.used_cpu_user"));
stat.setBlocked_clients(getIntValue(infoMap, "Clients.blocked_clients"));
stat.setConnected_clients(getIntValue(infoMap, "Clients.connected_clients"));
stat.setUsed_memory(getIntValue(infoMap, "Memory.used_memory"));
stat.setUsed_memory_rss(getIntValue(infoMap, "Memory.used_memory_rss"));
//
int cmd = getIntValue(infoMap, "Stats.total_commands_processed");
int exp = getIntValue(infoMap, "Stats.expired_keys");
int evt = getIntValue(infoMap, "Stats.evicted_keys");
int hit = getIntValue(infoMap, "Stats.keyspace_hits");
int mis = getIntValue(infoMap, "Stats.keyspace_misses");
long lastTs = stat.getTimestamp();
long thisTs = System.currentTimeMillis();
實現(xiàn)效果
| 實例名稱 | 用戶CPU | 系統(tǒng)CPU | 總連接數(shù) | 阻塞連接 | 使用內(nèi)存 | 物理內(nèi)存 | 鍵數(shù)量 | CMD速率 | EXP速率 | EVT速率 | HIT速率 | MIS速率 | 時間 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| appdb0/100.112.56.105:26529 | 2 | 2 | 3 | 0 | 1.0M | 2.0M | 12 | 0 | 0 | 0 | 0 | 0 | 2018/2/5 18:27 |
| appdb1/100.112.56.105:26520 | 2 | 3 | 4 | 0 | 1.0M | 2.0M | 8 | 16 | 0 | 0 | 4 | 0 | 2018/2/5 18:27 |
| appdb2/100.112.56.105:26523 | 3 | 5 | 3 | 0 | 1.0M | 2.0M | 19 | 52 | 0 | 0 | 5 | 0 | 2018/2/5 18:27 |
作者: 美碼師(zale)
出處: http://www.rzrgm.cn/littleatp/, 如果喜歡我的文章,請關(guān)注我的公眾號
本文版權(quán)歸作者和博客園共有,歡迎轉(zhuǎn)載,但未經(jīng)作者同意必須保留此段聲明,且在文章頁面明顯位置給出 原文鏈接 如有問題, 可留言咨詢.
浙公網(wǎng)安備 33010602011771號