[완료]Apache 로그의 error code 별 페이지값을 뽑으려고 합니다.

help@osori.org의 이미지

우선 제가 간단히 작성한 스크립트는 이렇습니다.

#!/bin/bash
 
LOGHOME=/var/log/httpd
LOGFILE=/var/log/httpd/access_log
ERRORCODE5=`cat $LOGHOME/access_log | grep 404 | awk '{print $7}' | sort -u`
 
echo "Access Log Check now"
echo ""
echo "################### This is ERROR CODE 404 ##################"
echo ""
 
for LIST in $ERRORCODE5
 
do
        echo "error code 404 list is -> $LIST"
        echo -n "error total is -> "
        cat $LOGFILE | grep 404 | grep $LIST | wc -l
        echo ""
done
 
echo "#############################################################"
echo ""

이 스크립트의 결과값은 이렇습니다.

[root@Asianux2 ~/bin]# sh error_check 
Access Log Check now
 
################### This is ERROR CODE 404 ##################
 
error code 404 list is -> /ckm.jsp
error total is -> 12
 
error code 404 list is -> /ckm2222.jsp
error total is -> 10
 
error code 404 list is -> /favicon.ico
error total is -> 3
 
error code 404 list is -> /index
error total is -> 4
 
error code 404 list is -> /index.html
error total is -> 3
 
error code 404 list is -> /test
error total is -> 3
 
error code 404 list is -> /test.jsp
error total is -> 1
 
error code 404 list is -> /test/
error total is -> 1
 
#############################################################
 
[root@Asianux2 ~/bin]# 

하지만 이 스크립트에는 치명적인 문제점이 있습니다.

이를 테면 다음과 같이 access_log 의 7번째 필드는 로케이션 값이고 9번째 필드는 에러코드 값입니다.

192.168.122.1 - - [17/Aug/2007:02:13:03 +0900] "GET /ckm.jsp HTTP/1.1" 404 286 "-" "Mozilla/5.0 (Windows; U; Windows NT 5.1; ko; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6"

이중 9번째 필드에 404 값이 있는 파일만을 추출해야 하는데...

현재의 스크립트로서는 전체 열에서 404를 찾게 됩니다.

아무리 생각해도 답이 않나오네요.. 도와주세요 ㅠㅠ

원하는 로직은 아파치 로그의 9번째 필드 즉 에어코드 필드에서 404값이 있는 행만을 추출하여 그 행의 7번째 필드인 로케이션을 추출하고 그 로케이션값을 가진 행들중 동일한 404값을 가진 행들의 전체 카운트를 다시 뽑아내는 겁니다.

awk 와 wc -l , sort , xargs 등을 이용하면 어떻게 될것 같은데.. 도저히 감이 안오네요..

keizie의 이미지

grep 404 | awk '{print $7}'


awk '$9 = "404" {print $7}'

정도로 고쳐보세요.
help@osori.org의 이미지

이를 테면 저 방법대로 했을 경우

[root@Asianux2 /var/log/httpd]# cat access_log | awk '$9 = "404" {print $7}' | sort -u
/
/ckm.jsp
/ckm2222.jsp
/favicon.ico
/icons/apache_pb2.gif
/icons/powered_by_ax.png
/index
/index.html
/test
/test.jsp
/test/
[root@Asianux2 /var/log/httpd]#
[root@Asianux2 /var/log/httpd]#
[root@Asianux2 /var/log/httpd]#
[root@Asianux2 /var/log/httpd]# cat access_log | grep "powered_by_ax.png" | grep "404"
[root@Asianux2 /var/log/httpd]#
[root@Asianux2 /var/log/httpd]#

보는것과 같이 값이 틀리게 나옵니다.

help@osori.org의 이미지

이를 테면 저 방법대로 했을 경우

[root@Asianux2 /var/log/httpd]# cat access_log | awk '$9 = "404" {print $7}' | sort -u
/
/ckm.jsp
/ckm2222.jsp
/favicon.ico
/icons/apache_pb2.gif
/icons/powered_by_ax.png
/index
/index.html
/test
/test.jsp
/test/
[root@Asianux2 /var/log/httpd]#
[root@Asianux2 /var/log/httpd]#
[root@Asianux2 /var/log/httpd]#
[root@Asianux2 /var/log/httpd]# cat access_log | grep "powered_by_ax.png" | grep "404"
[root@Asianux2 /var/log/httpd]#
[root@Asianux2 /var/log/httpd]#

보는것과 같이 값이 틀리게 나옵니다.

help@osori.org의 이미지

= 과 " 가 아니라

$9 ~ /404/ 로 하면 되네요. ㅎㅎ

man 페이지중 내용 - 첫 번째 필드가 good와 일치하는 레코드에 대해 세 번째 필드를 먼저 출력하고 두 번째 필드를 나중에 출력하고 싶을 때 $1 ~ /good/ { print $3 , $2 }

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.