I facing problem to write a script for redis server . I know it will very normal script ,but due to my lack of knowledge I can't able to .

Currently I can getting out put when using below command

redis-cli -r -1 -i 300 INFO | grep slave

I want a monitoring script , if any slave is not in online state or lag is more than 5 it will send email .


Here's an awk program (in a bash script) which parses the output of your commands and detects lines with problems in them. My awk is rusty, so it is undoubtedly not elegant, but it works.

It takes standard input and prints only those lines with the conditions you're looking for.

I left in the print statements I used to debug it after commenting them out.

To avoid using a separate temporary or permanent file just for the awk program, the whole thing is added on the awk command line surrounded by single quotes to make it one argument and to keep bash from expanding it.

To use it, you would add it to the end of your current pipeline with something like

redis-cli -r -1 -i 300 INFO | grep slave | parse_redis > some-file

If some-file is not empty, then send it in an email to yourself.

The awk code is pretty simple making it easy to modify it to suit your needs.

I didn't cover how to run this from cron, etc.. If you need help integrating it, then add a comment to this answer.

If redis/your pipe can issue other types of output not listed in your example, then you'll have to modify your pipe or this awk program to handle them.

## 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 ',' '

  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}


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.