linux – redis服务器的监控脚本
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了linux – redis服务器的监控脚本,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2291字,纯文字阅读大概需要4分钟。
内容图文
![linux – redis服务器的监控脚本](/upload/InfoBanner/zyjiaocheng/895/aac9641889a147be80564461beced4a7.jpg)
我面临着为redis服务器编写脚本的问题.我知道这将是非常正常的脚本,但由于我缺乏知识,我无法做到.
目前我可以在使用以下命令时退出
redis-cli -r -1 -i 300 INFO | grep slave
connected_slaves:4
slave0:ip=70.0.0.170,port=7000,state=online,offset=2425867354,lag=1
slave1:ip=70.0.0.227,port=7000,state=online,offset=2425870831,lag=0
slave2:ip=70.0.0.228,port=7000,state=online,offset=2425871141,lag=0
slave3:ip=70.0.0.171,port=7000,state=online,offset=2428745984,lag=1
我想要一个监控脚本,如果任何奴隶未处于在线状态或滞后超过5,它将发送电子邮件.
解决方法:
这是一个awk程序(在bash脚本中),它解析命令的输出并检测有问题的行.我的awk是生锈的,所以它无疑不优雅,但它有效.
它采用标准输入并仅打印具有您正在寻找的条件的那些线.
我留下了用于在评论之后调试它的打印语句.
为了避免仅为awk程序使用单独的临时或永久文件,整个操作将添加到由单引号括起的awk命令行中,以使其成为一个参数并防止bash扩展它.
要使用它,您可以使用类似的方法将其添加到当前管道的末尾
redis-cli -r -1 -i 300 INFO | grep slave | parse_redis > some-file
如果某个文件不为空,则将其通过电子邮件发送给您自己.
awk代码非常简单,可以很容易地修改它以满足您的需求.
我没有介绍如何从cron等运行它.如果你需要帮助集成它,那么在这个答案中添加注释.
如果redis /你的管道可以发出你的例子中未列出的其他类型的输出,那么你将不得不修改你的管道或这个awk程序来处理它们.
#!/bin/bash
## parse_redis
## parses redis output looking for state and lag problems
## sets awk's field separator to a comma to make things easy
## whole awk program is a single single-quoted string on the awk command line
awk -F ',' '
BEGIN {
max_lag = 5 ## threshold for acceptable lag times
}
##{ print "input is " NR " " $0 }
NR == 1 {next} ## skip first line of input
problem=0 ## flag for problem detected
## detect anything except online
##{ print "field 3 [" $3 "]" }
## If the third field does not contain state=online, then it is a problem
$3 !~ "state=online" {problem = 1}
## Get the value for lag and see if it is too large
## lag is in the 5th field starting at the 5th character
## extract the value from the 5th character to the end
## of the field and turn it into a number
## Probably would work without turning it into a number
{
##{ print "field 5 [" $5 "]" }
lag = strtonum(substr($5, 5))
##{ print "lag [" lag "]" }
if (lag > max_lag) problem = 1
}
##{ print "problem [" problem "]" }
{if (problem == 0) next}
{print}
'
内容总结
以上是互联网集市为您收集整理的linux – redis服务器的监控脚本全部内容,希望文章能够帮你解决linux – redis服务器的监控脚本所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。