2.命令行监控

本文涉及的产品
云数据库 Redis 版,社区版 2GB
推荐场景:
搭建游戏排行榜
简介:
前面可以看到,虽然图形化监控Redis比较美观、直接,但是安装起来比较麻烦。如果只是想简单看一下Redis的负载情况的话,完全可以用它提供的一些命令来完成。
2.1 吞吐量
Redis提供的INFO命令不仅能够查看实时的吞吐量(ops/sec),还能看到一些有用的运行时信息。下面用grep过滤出一些比较重要的实时信息,比如已连接的和在阻塞的客户端、已用内存、拒绝连接、实时的tps和数据流量等:
  1. [root@vm redis-3.0.3]# src/redis-cli -h 127.0.0.1 info | grep -e "connected_clients" -e "blocked_clients" -e "used_memory_human" -e "used_memory_peak_human" -e "rejected_connections" -e "evicted_keys" -e "instantaneous"

  2. connected_clients:1
  3. blocked_clients:0
  4. used_memory_human:799.66K
  5. used_memory_peak_human:852.35K
  6. instantaneous_ops_per_sec:0
  7. instantaneous_input_kbps:0.00
  8. instantaneous_output_kbps:0.00
  9. rejected_connections:0
  10. evicted_keys:0
复制代码

2.2 延迟 2.2.1 客户端PING
从客户端可以监控Redis的延迟,利用Redis提供的PING命令,不断PING服务端,记录服务端响应PONG的时间。下面开两个终端,一个监控延迟,一个监视服务端收到的命令:
  1. [root@vm redis-3.0.3]# src/redis-cli --latency -h 127.0.0.1
  2. min: 0, max: 1, avg: 0.08

  3. [root@vm redis-3.0.3]# src/redis-cli -h 127.0.0.1
  4. 127.0.0.1:6379> monitor
  5. OK
  6. 1439361594.867170 [0 127.0.0.1:59737] "PING"
  7. 1439361594.877413 [0 127.0.0.1:59737] "PING"
  8. 1439361594.887643 [0 127.0.0.1:59737] "PING"
  9. 1439361594.897858 [0 127.0.0.1:59737] "PING"
  10. 1439361594.908063 [0 127.0.0.1:59737] "PING"
  11. 1439361594.918277 [0 127.0.0.1:59737] "PING"
  12. 1439361594.928469 [0 127.0.0.1:59737] "PING"
  13. 1439361594.938693 [0 127.0.0.1:59737] "PING"
  14. 1439361594.948899 [0 127.0.0.1:59737] "PING"
  15. 1439361594.959110 [0 127.0.0.1:59737] "PING"
复制代码

如果我们故意用DEBUG命令制造延迟,就能看到一些输出上的变化:
  1. [root@vm redis-3.0.3]# src/redis-cli --latency -h 127.0.0.1
  2. min: 0, max: 1995, avg: 1.60 (2361 samples)

  3. [root@vm redis-3.0.3]# src/redis-cli -h 127.0.0.1
  4. 127.0.0.1:6379> debug sleep 1
  5. OK
  6. (1.00s)
  7. 127.0.0.1:6379> debug sleep .15
  8. OK
  9. 127.0.0.1:6379> debug sleep .5
  10. OK
  11. (0.50s)
  12. 127.0.0.1:6379> debug sleep 2
  13. OK
  14. (2.00s)
复制代码

2.2.2 服务端内部机制
服务端内部的延迟监控稍微麻烦一些,因为延迟记录的默认阈值是0。尽管空间和时间耗费很小,Redis为了高性能还是默认关闭了它。所以首先我们要开启它,设置一个合理的阈值,例如下面命令中设置的100ms:
  1. 127.0.0.1:6379> CONFIG SET latency-monitor-threshold 100
  2. OK
复制代码

因为Redis执行命令非常快,所以我们用DEBUG命令人为制造一些慢执行命令:
  1. 127.0.0.1:6379> debug sleep 2
  2. OK
  3. (2.00s)
  4. 127.0.0.1:6379> debug sleep .15
  5. OK
  6. 127.0.0.1:6379> debug sleep .5
  7. OK
复制代码

下面就用LATENCY的各种子命令来查看延迟记录:

LATEST:四列分别表示事件名、最近延迟的Unix时间戳、最近的延迟、最大延迟。
HISTORY:延迟的时间序列。可用来产生图形化显示或报表。
GRAPH:以图形化的方式显示。最下面以竖行显示的是指延迟在多久以前发生。
RESET:清除延迟记录。
  1. 127.0.0.1:6379> latency latest
  2. 1) 1) "command"
  3.    2) (integer) 1439358778
  4.    3) (integer) 500
  5.    4) (integer) 2000

  6. 127.0.0.1:6379> latency history command
  7. 1) 1) (integer) 1439358773
  8.    2) (integer) 2000
  9. 2) 1) (integer) 1439358776
  10.    2) (integer) 150
  11. 3) 1) (integer) 1439358778
  12.    2) (integer) 500

  13. 127.0.0.1:6379> latency graph command
  14. command - high 2000 ms, low 150 ms (all time high 2000 ms)
  15. --------------------------------------------------------------------------------
  16. #  
  17. |  
  18. |  
  19. |_#

  20. 666
  21. mmm
复制代码

在执行一条DEBUG命令会发现GRAPH图的变化,多出一条新的柱状线,下面的时间2s就是指延迟刚发生两秒钟:
  1. 127.0.0.1:6379> debug sleep 1.5
  2. OK
  3. (1.50s)
  4. 127.0.0.1:6379> latency graph command
  5. command - high 2000 ms, low 150 ms (all time high 2000 ms)
  6. --------------------------------------------------------------------------------
  7. #   
  8. |  #
  9. |  |
  10. |_#|

  11. 2222
  12. 333s
  13. mmm
复制代码

还有一个有趣的子命令DOCTOR,它能列出一些指导建议,例如开启慢日志进一步追查问题原因,查看是否有大对象被踢出或过期,以及操作系统的配置建议等。
  1. 127.0.0.1:6379> latency doctor
  2. Dave, I have observed latency spikes in this Redis instance. You don't mind talking about it, do you Dave?

  3. 1. command: 3 latency spikes (average 883ms, mean deviation 744ms, period 210.00 sec). Worst all time event 2000ms.

  4. I have a few advices for you:

  5. - Check your Slow Log to understand what are the commands you are running which are too slow to execute. Please check http://redis.io/commands/slowlog for more information.
  6. - Deleting, expiring or evicting (because of maxmemory policy) large objects is a blocking operation. If you have very large objects that are often deleted, expired, or evicted, try to fragment those objects into multiple smaller objects.
  7. - I detected a non zero amount of anonymous huge pages used by your process. This creates very serious latency events in different conditions, especially when Redis is persisting on disk. To disable THP support use the command 'echo never > /sys/kernel/mm/transparent_hugepage/enabled', make sure to also add it into /etc/rc.local so that the command will be executed again after a reboot. Note that even if you have already disabled THP, you still need to restart the Redis process to get rid of the huge pages already created.
复制代码

2.2.3 度量延迟Baseline
延迟中的一部分是来自环境的,比如操作系统内核、虚拟化环境等等。Redis提供了让我们度量这一部分延迟基线(Baseline)的方法:
  1. [root@vm redis-3.0.3]# src/redis-cli --intrinsic-latency 100 -h 127.0.0.1
  2. Max latency so far: 2 microseconds.
  3. Max latency so far: 3 microseconds.
  4. Max latency so far: 26 microseconds.
  5. Max latency so far: 37 microseconds.
  6. Max latency so far: 1179 microseconds.
  7. Max latency so far: 1623 microseconds.
  8. Max latency so far: 1795 microseconds.
  9. Max latency so far: 2142 microseconds.

  10. 35818026 total runs (avg latency: 2.7919 microseconds / 27918.90 nanoseconds per run).
  11. Worst run took 767x longer than the average latency.
复制代码

–intrinsic-latency后面是测试的时长(秒),一般100秒足够了。





本文作者:geelou
本文来自云栖社区合作伙伴rediscn,了解相关信息可以关注redis.cn网站。
相关实践学习
基于Redis实现在线游戏积分排行榜
本场景将介绍如何基于Redis数据库实现在线游戏中的游戏玩家积分排行榜功能。
云数据库 Redis 版使用教程
云数据库Redis版是兼容Redis协议标准的、提供持久化的内存数据库服务,基于高可靠双机热备架构及可无缝扩展的集群架构,满足高读写性能场景及容量需弹性变配的业务需求。 产品详情:https://www.aliyun.com/product/kvstore     ------------------------------------------------------------------------- 阿里云数据库体验:数据库上云实战 开发者云会免费提供一台带自建MySQL的源数据库 ECS 实例和一台目标数据库 RDS实例。跟着指引,您可以一步步实现将ECS自建数据库迁移到目标数据库RDS。 点击下方链接,领取免费ECS&RDS资源,30分钟完成数据库上云实战!https://developer.aliyun.com/adc/scenario/51eefbd1894e42f6bb9acacadd3f9121?spm=a2c6h.13788135.J_3257954370.9.4ba85f24utseFl
目录
相关文章
|
4月前
|
缓存 运维 监控
shell监控系统状态和资源使用命令
shell监控系统状态和资源使用命令
64 2
|
8月前
|
JSON 监控 Shell
用shell脚本如何获取grafana上的监控数据
用shell脚本如何获取grafana上的监控数据
165 2
|
6月前
|
机器学习/深度学习 运维 Shell
运维(22)- shell开机自启动脚本
运维(22)- shell开机自启动脚本
60 1
|
监控 Shell
使用 Shell 脚本监控主机,实现主机性能监控
使用 Shell 脚本监控主机,实现主机性能监控
440 0
使用 Shell 脚本监控主机,实现主机性能监控
|
前端开发 JavaScript Python
用命令行运行脚本并生成报告
用命令行运行脚本并生成报告
149 0
|
存储 缓存 Shell
通过Shell脚本实现批量Linux服务器巡检
通过shell与expect脚本实现Linux服务器批量巡检
2015 0
|
Shell 应用服务中间件 网络安全