Redisサーバー監視スクリプト

Redisサーバー監視スクリプト

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つ以上遅れている場合は、電子メールを送信する監視スクリプトが必要です。

ベストアンサー1

これはawk、コマンドの出力を解析し、その中で問題のある行を検出するプログラム(bashスクリプト)です。私awkは錆びていてエレガントではありませんが、うまくいきます。

標準入力を受け取り、探している基準に一致する行のみを印刷します。

デバッグに使用した印刷ステートメントをコメントアウトしてからそのままにしました。

プログラムに別々の一時ファイルまたは永続ファイルを使用することを避けるために、ファイル全体をawkコマンドawkラインに追加し、一重引用符で囲んで引数にし、bashが拡張するのを防ぎます。

これを使用するには、現在のパイプラインの末尾に追加します。

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

空でない場合はsome-fileメールを送信します。

awkコードは非常にシンプルで、必要に応じて簡単に修正できます。

cronなどで実行する方法は扱っていません。統合に役立つ場合は、この回答にコメントを追加してください。

redis/ yourパイプラインが例にリストされていない他の種類の出力をエクスポートできる場合は、パイプラインまたはこのプログラムを変更して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}
'

おすすめ記事