6
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

ログ(単位時間当たり)簡易集計スクリプト

Last updated at Posted at 2015-09-16

postfix のログ等において
単位時間当たりの特定のメッセージを集計するスクリプト例

 "status=sent"  postfix が送信/転送したメール数/秒を集計した例


count.sh
#!/bin/bash
count=0
before=""
keyword="status=sent"

tail -f /var/log/maillog | grep --line-buffered "$keyword"|
while read line;do
  now=$(echo $line|cut -f2 -d' ')
  if [ "$before" = "$now" ];then
    count=$[count+1]
    echo -ne "$now $count\r"
  else
    if [ "$before" ] ;then
      echo -ne "$before $count\n"
    fi
    echo -ne "$now 1\r"
    count=1
    before=$now
  fi
done

以下のように 件数/秒 での集計できる。

[user@hogehoge ~]$ sh ./count.sh
16:40:37 1
16:40:37 1
16:41:19 1
16:41:20 2
16:41:21 2
16:41:22 2
16:41:23 2
16:41:24 1
16:41:25 2
16:41:26 2
16:41:27 2
16:41:28 1
16:41:29 2

6
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?