linux监听redis服务,linux – redis服务器的监控脚本

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

'

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值