我们在监控Redis的时候依赖服务的当前的运行状态,Redis相对简易,需要我们自己到INFO里面去抓数据

下面我们通过info命令获取下

1
# redis -h -p -a info > /tmp/redisinfo.txt

加工处理

1
# $if [ "`cat /tmp/redisinfo.txt |grep role |gawk -F ":" '{print $2}'`" == "master" ]; then echo 'okok'; fi

我们发现,以上语法没问题,分段执行也没有问题,结果异常

我们观察了下redisinfo.txt的文件格式

1
2
3
4
# file /tmp/redisinfo.txt
redisinfo.txt: ASCII text, with CRLF line terminators
这就不对了,为什么还有CRLF

我们再来看看redisinfo.txt的内容,很多vim由于版本低,所以看不到

1
2
3
4
5
6
7
# cat -v /tmp/redisinfo.txt
...
# Replication^M
role:master^M
connected_slaves:2^M
...

终结符问题

官方说明

字符串行输出格式
块输出编码

这里需要我们将文件编码转换下

本来想着info命令是否有类似mysql的into outfile的terminated by ‘\r\n’,目前没有找到,那只能从系统层面解决了

1
2
# dos2unix /tmp/redisinfo.txt
dos2unix: converting file /tmp/redisinfo.txt to UNIX format ...
1
2
# file /tmp/redisinfo.txt
/tmp/redisinfo.txt: ASCII text
1
2
3
4
5
6
7
# cat -v /tmp/redisinfo.txt
...
# Replication
role:master
connected_slaves:2
...

再来看下加工处理情况

1
2
3
# $if [ "`cat /tmp/redisinfo.txt |grep role |gawk -F ":" '{print $2}'`" == "master" ]; then echo 'okok'; fi
# okok