apacheのプロセス数をウォッチするシェル

テストやリリース後の初期流動期間等にApacheのプロセス数をウォッチしたいときに使用するシェルスクリプト。毎回必要になると書いてバカらしいのでメモしておこう。

#!/bin/bash
LANG=C
tput clear
maxcnt=0
currentdt=`date '+%F %T'`
array=()

for i in {0..19} ; do
    array[$i]="N/A"
done

maxcntline="Max Prefork Count $maxcnt at $currentdt"
while true;do
    tput clear
    currcnt=`ps -ef | grep httpd | grep -v grep | wc -l`
    currentdt=`date "+%F %T"`
    currentline="$currentdt HTTP Prefork Count=$currcnt"
    if [ $maxcnt -lt $currcnt ];then
        maxcnt=$currcnt;maxcntline="Max Prefork Count $maxcnt at $currentdt"
    fi
    loopcnt=0
    for ((i=20; i > $loopcnt; i--));do
       array[$i]=${array[$i-1]}
    done
    array[0]=$currentline
    echo "-----------------------------------"
    echo $maxcntline
    echo "-----------------------------------"
    for j in "${array[@]}";do
       echo "${j}"
    done
    sleep 1s
done
実行するとこんな感じ。一番上にウォッチ中のプロセス数最大値を表示して下にCURRENT20秒のプロセス数を表示。 1秒間隔で無限ループです。CTRL-Cで停止。
-----------------------------------
Max Prefork Count 74 at 2019-03-22 01:39:12
-----------------------------------
2019-03-22 01:39:15 HTTP Prefork Count=72
2019-03-22 01:39:14 HTTP Prefork Count=73
2019-03-22 01:39:13 HTTP Prefork Count=74
2019-03-22 01:39:12 HTTP Prefork Count=74
2019-03-22 01:39:11 HTTP Prefork Count=42
2019-03-22 01:39:10 HTTP Prefork Count=26
2019-03-22 01:39:09 HTTP Prefork Count=18
2019-03-22 01:39:08 HTTP Prefork Count=14
2019-03-22 01:39:07 HTTP Prefork Count=12
2019-03-22 01:39:06 HTTP Prefork Count=11
2019-03-22 01:39:05 HTTP Prefork Count=11
2019-03-22 01:39:04 HTTP Prefork Count=11
2019-03-22 01:39:03 HTTP Prefork Count=11
2019-03-22 01:39:02 HTTP Prefork Count=11
2019-03-22 01:39:01 HTTP Prefork Count=11
2019-03-22 01:39:00 HTTP Prefork Count=11
2019-03-22 01:38:59 HTTP Prefork Count=11
2019-03-22 01:38:58 HTTP Prefork Count=11
2019-03-22 01:38:57 HTTP Prefork Count=11
2019-03-22 01:38:56 HTTP Prefork Count=11
2019-03-22 01:38:55 HTTP Prefork Count=11
 
ファイルにリダイレクトしておけば、あとから見直しもできるか。 MetricBeatで色々な情報をウォッチしておきたいんだけど、入れて「いい」「悪い」とかあって面倒くさいからこんなのでお茶をにごしておこう。 いじょ。