一、Redis监视器:监视器

### --- 监视器

~~~ Redis客户端通过执行MONITOR命令可以将自己变为一个监视器,
~~~ 实时地接受并打印出服务器当前处理的命令请求的相关信息。
~~~ 此时,当其他客户端向服务器发送一条命令请求时,服务器除了会处理这条命令请求之外,
~~~ 还会将这条命令请求的信息发送给所有监视器。

|NO.Z.00031|——————————|^^  配置 ^^|——|Hadoop&Redis.V07|——|Redis.v07|监视器|_redis

### --- Redis客户端1

127.0.0.1:6379> monitor
OK
1631795768.802450 [0 127.0.0.1:43976] "COMMAND"
1631795777.655054 [0 127.0.0.1:43976] "set" "name:10" "zhaoyun"
1631795782.508276 [0 127.0.0.1:43976] "get" "name:10"
### --- Redis客户端2

[root@linux123 bin]# ./redis-cli
127.0.0.1:6379> set name:10 zhaoyun
OK
127.0.0.1:6379> get name:10
"zhaoyun"

二、实现监视器

### --- 监视器
~~~ redisServer 维护一个monitors 的链表,记录自己的监视器,
~~~ 每次收到MONITOR 命令之后,将客户端追加到链表尾。

void monitorCommand(redisClient *c) {
/* ignore MONITOR if already slave or in monitor mode */
if (c->flags & REDIS_SLAVE) return;
c->flags |= (REDIS_SLAVE|REDIS_MONITOR);
listAddNodeTail(server.monitors,c);
addReply(c,shared.ok); // 回复OK
}
### --- 向监视器发送命令信息

~~~ 利用call函数实现向监视器发送命令
### --- call 主要调用了replicationFeedMonitors ,
~~~ 这个函数的作用就是将命令打包为协议,发送给监视器。

// call() 函数是执行命令的核心函数,这里只看监视器部分
/*src/redis.c/call*/
/* Call() is the core of Redis execution of a command */
void call(redisClient *c, int flags) {
long long dirty, start = ustime(), duration;
int client_old_flags = c->flags;
/* Sent the command to clients in MONITOR mode, only if the commands are
* not generated from reading an AOF. */
if (listLength(server.monitors) &&
!server.loading &&
!(c->cmd->flags & REDIS_CMD_SKIP_MONITOR))
{
replicationFeedMonitors(c,server.monitors,c->db->id,c->argv,c->argc);
}
......
}

三、Redis监控平台

### --- grafana、prometheus以及redis_exporter。

~~~ # Grafana
~~~ 是一个开箱即用的可视化工具,具有功能齐全的度量仪表盘和图形编辑器,
~~~ 有灵活丰富的图形化选项,可以混合多种风格,支持多个数据源特点。
~~~ # Prometheus
~~~ 是一个开源的服务监控系统,
~~~ 它通过HTTP协议从远程的机器收集数据并存储在本地的时序数据库上。
~~~ # redis_exporter
~~~ 为Prometheus提供了redis指标的导出,配合Prometheus以及grafana进行可视化及监控。

|NO.Z.00031|——————————|^^  配置 ^^|——|Hadoop&Redis.V07|——|Redis.v07|监视器|_服务器_02


Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart

                                                                                                                                                   ——W.S.Landor