2

I have a log file which has lines like below.

10.10.205.100 100.10.56.5 - [23/Oct/2012:15:30:01 +0000] "GET /way/?cb=777:Obj.Status&log=signing_in_service&cache=1350334642666 HTTP/1.1" 200 53 "https://www.sample.com/signin?off=undefined&questions=&nouser=&link=%23&country=origin&displayLayer=" "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/534.57.2 (KHTML, like Gecko) Version/5.1.7 Safari/534.57.2" "PD_STATEFUL_d64f218a-fa6a-11e0-b7df-623c1eeb9903=%2Fgateway; PD-ERR=0x132120c8; PD-HOST=sample.com; PD-REFERER=https://www.sample.com/profile%3Fundefined%3D; PD-REFPAGE=signin_user; pSite=; __SIGNIN=signin%23cw%3D400%3Ach%3D321; mmcore.pd=916254201%7CAgAAAAoBQvp/zPBKCBGRP6QBAOZMgV9Kf89IAAsAAAAtzSlGSn/PSAAAAAD/////AEoIAQAAAAAAAQAAAAAA////////////////AAAAAAABRQ%3D%3D; mmcore.srv=cg4.use; mmid=-1116944464%7CAgAAAAr6f8zwSggAAA%3D%3D; mmcore.tst=0.155; PD-SGNPAGE=http%3A%2F%2Fwww.sample.com%2Fcommon%2Fregister%2Feproxy.html; UnicaNIODID=A3nRYVRjMyy-Xv66UJt; survey=1350334587161"

I am trying to get the http status code which is 200 in the above line. This will be placed after the string HTTP/1.1". Version of http (here it is 1.1) may not be common in all the lines.

Based on the status code (if its not 200 and few other status), i have to send an email along with the log file.

4

3 に答える 3

2

The number you have after HTTP/ is the http version number. There are not many versions of http (http 1.0 and http 1.1). So this wil work:

 awk -F"HTTP/1.[0-9]\" " '{print $2}' filename | cut -d' ' -f 1
于 2012-10-26T10:04:49.183 に答える
1

(ログファイル全体ではなく、問題のある行のみをメールで送信することを想定しています。)

ステータスコードの前のスペースの数が常に同じである場合は、を使用して9番目のフィールドを確認できますawk

#!/bin/bash
# find-bad-lines

# Match only statuses that do not start with 2 or 3
# (i.e., 4xx and 5xx)
awk '!match($9, /^[23]/)'

find-bad-lines次に、の出力をにパイプすることによって、各「不良」ラインを処理します。handle-bad-lines

#!/bin/bash
# handle-bad-lines

while read -r line
do
    # This is where you could execute a script for email
    echo "$line"
done

次のようになります。

$ find-bad-lines <logfile.log | handle-bad-lines

明らかに、エラー処理などを追加する必要があります。しかし、これが基本的な考え方です。

于 2012-10-26T10:51:24.067 に答える
1

You can use grep -o 'HTTP/[0-9.]\+" [0-9]\+' to get just the portion from HTTP up to the number. What goes after the space is the number, so just add | cut -f2 -d' '.

于 2012-10-26T10:05:53.897 に答える