这是一个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}
'